package com.dcsoft.system.consume.controller;

import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.web.page.TableDataInfo;
import com.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.common.security.annotation.RequiresPermissions;
import com.dcsoft.common.security.utils.SecurityUtils;
import com.dcsoft.system.consume.domain.ConsumeRechargeRecord;
import com.dcsoft.system.consume.domain.ConsumeSubsidyRecord;
import com.dcsoft.system.consume.service.IConsumeRechargeRecordService;
import com.dcsoft.system.consume.service.IConsumeSubsidyRecordService;
import com.dcsoft.system.domain.SysPeople;
import com.dcsoft.system.service.ISysPeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 餐补明细Controller
 *
 * @author nichun
 * @date 2023-10-11
 */
@RestController
@RequestMapping("/subsidyRecord")
public class ConsumeSubsidyRecordController extends BaseController
{
    @Autowired
    private IConsumeSubsidyRecordService consumeSubsidyRecordService;

    @Autowired
    private IConsumeRechargeRecordService consumeRechargeRecordService;

    @Autowired
    private ISysPeopleService sysPeopleService;



    /**
     * 查询餐补明细列表
     */
    @RequiresPermissions("consume:subsidyRecord:list")
    @GetMapping("/list")
    public TableDataInfo list(ConsumeSubsidyRecord consumeSubsidyRecord)
    {
        startPage();
        List<ConsumeSubsidyRecord> list = consumeSubsidyRecordService.selectConsumeSubsidyRecordList(consumeSubsidyRecord);
        return getDataTable(list);
    }

    /**
     * 导出餐补明细列表
     */
    @RequiresPermissions("consume:subsidyRecord:export")
    @Log(title = "餐补明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ConsumeSubsidyRecord consumeSubsidyRecord)
    {
        List<ConsumeSubsidyRecord> list = consumeSubsidyRecordService.selectConsumeSubsidyRecordList(consumeSubsidyRecord);
        ExcelUtil<ConsumeSubsidyRecord> util = new ExcelUtil<ConsumeSubsidyRecord>(ConsumeSubsidyRecord.class);
        String[] tableNames = consumeSubsidyRecord.getTableNames();
        util.excludeFields=tableNames;
        util.exportExcel(response, list, "餐补明细数据");
    }



    /**
     * 获取餐补明细详细信息
     */
    @RequiresPermissions("consume:subsidyRecord:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(consumeSubsidyRecordService.selectConsumeSubsidyRecordById(id));
    }

    /**
     * 新增餐补明细
     */
    @RequiresPermissions("consume:subsidyRecord:add")
    @Log(title = "餐补明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConsumeSubsidyRecord consumeSubsidyRecord)
    {
        return toAjax(consumeSubsidyRecordService.insertConsumeSubsidyRecord(consumeSubsidyRecord));
    }

    /**
     * 修改餐补明细
     */
    @RequiresPermissions("consume:subsidyRecord:edit")
    @Log(title = "餐补明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConsumeSubsidyRecord consumeSubsidyRecord)
    {
        return toAjax(consumeSubsidyRecordService.updateConsumeSubsidyRecord(consumeSubsidyRecord));
    }

    /**
     * 删除餐补明细
     */
    @RequiresPermissions("consume:subsidyRecord:remove")
    @Log(title = "餐补明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(consumeSubsidyRecordService.deleteConsumeSubsidyRecordByIds(ids));
    }

    /**
     * 新增餐补明细
     */
    @Log(title = "餐补明细", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public AjaxResult create(@RequestBody ConsumeSubsidyRecord consumeSubsidyRecord)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if(consumeSubsidyRecord.getSubsidyTime()==null){
            try {
                Calendar cale = Calendar.getInstance();
                cale.setTime(format.parse(format.format(new Date())));
                cale.set(Calendar.DAY_OF_MONTH, cale.getActualMinimum(Calendar.DAY_OF_MONTH));
                consumeSubsidyRecord.setSubsidyTime(cale.getTime());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        consumeSubsidyRecord.setTabFlag("ycz");
        List<ConsumeSubsidyRecord> list=consumeSubsidyRecordService.selectConsumeSubsidyRecordList(consumeSubsidyRecord);
        if(list.size()>0){
            return AjaxResult.error("已存在"+format.format(consumeSubsidyRecord.getSubsidyTime())+"补贴明细，请先删除再生成");
        }
        return toAjax(consumeSubsidyRecordService.createConsumeSubsidy(consumeSubsidyRecord));
    }

    /**
     * 新增餐补明细
     */
    @Log(title = "餐补明细", businessType = BusinessType.INSERT)
    @PostMapping("/createJob")
    public AjaxResult createJob(HttpServletResponse response)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        ConsumeSubsidyRecord consumeSubsidyRecord=new ConsumeSubsidyRecord();
        Calendar cale = Calendar.getInstance();
        try {
            cale.setTime(format.parse(format.format(new Date())));
            cale.set(Calendar.DAY_OF_MONTH, cale.getActualMinimum(Calendar.DAY_OF_MONTH));
            consumeSubsidyRecord.setSubsidyTime(cale.getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        consumeSubsidyRecord.setTabFlag("ycz");
        List<ConsumeSubsidyRecord> list=consumeSubsidyRecordService.selectConsumeSubsidyRecordList(consumeSubsidyRecord);
        if(list.size()>0){
            return AjaxResult.error("已存在"+format.format(consumeSubsidyRecord.getSubsidyTime())+"补贴明细，请先删除再生成");
        }
        return toAjax(consumeSubsidyRecordService.createConsumeSubsidy(consumeSubsidyRecord));
    }

    /**
     * 补贴餐补明细
     */
    @Log(title = "餐补补贴", businessType = BusinessType.UPDATE)
    @PostMapping("/issue/{ids}")
    public AjaxResult issue(@PathVariable Long[] ids)
    {
        //餐补补助下发到补贴明细表中
        for(Long id:ids){
            ConsumeSubsidyRecord consumeSubsidyRecord=consumeSubsidyRecordService.selectConsumeSubsidyRecordById(id);
            if("1".equals(consumeSubsidyRecord.getIsSh())){
                //补贴人员
                SysPeople people=sysPeopleService.selectSysPeopleById(consumeSubsidyRecord.getAccountId());
                people.setBtje(consumeSubsidyRecord.getAllMoney()+people.getBtje());
                sysPeopleService.updateSysPeople(people);
                //补贴记录
                ConsumeRechargeRecord crr=new ConsumeRechargeRecord();
                crr.setConsumeMoney(consumeSubsidyRecord.getAllMoney()+"");
                crr.setAccountId(consumeSubsidyRecord.getAccountId()+"");
                crr.setEmpId(consumeSubsidyRecord.getEmpId());
                crr.setOrderType("2");//补贴
                crr.setRechargeWay("0");//平台
                crr.setRechargeType("0");
                crr.setCreateBy(SecurityUtils.getUsername());
                consumeRechargeRecordService.insertConsumeRechargeRecord(crr);
            }
        }
        return toAjax(consumeSubsidyRecordService.issueConsumeSubsidyRecordByIds(ids));
    }

    /**
     * 补贴餐补明细
     */
    @Log(title = "餐补补贴", businessType = BusinessType.UPDATE)
    @PostMapping("/examine/{ids}")
    public AjaxResult examine(@PathVariable Long[] ids)
    {
        return toAjax(consumeSubsidyRecordService.examineConsumeSubsidyRecordByIds(ids));
    }


    @Log(title = "餐补批量补贴", businessType = BusinessType.UPDATE)
    @PostMapping("/issueAll")
    public AjaxResult issueAll(ConsumeSubsidyRecord consumeSubsidyRecord)
    {
        //餐补补助下发到补贴明细表中
        List<ConsumeSubsidyRecord> list = consumeSubsidyRecordService.selectConsumeSubsidyRecordList(consumeSubsidyRecord);
        for(ConsumeSubsidyRecord record:list){
            if("1".equals(record.getIsSh())){
                //补贴人员
                SysPeople people=sysPeopleService.selectSysPeopleById(record.getAccountId());
                people.setBtje(record.getAllMoney()+people.getBtje());
                sysPeopleService.updateSysPeople(people);
                //补贴记录
                ConsumeRechargeRecord crr=new ConsumeRechargeRecord();
                crr.setConsumeMoney(record.getAllMoney()+"");
                crr.setAccountId(record.getAccountId()+"");
                crr.setEmpId(record.getEmpId());
                crr.setOrderType("2");//补贴
                crr.setRechargeWay("0");//平台
                crr.setRechargeType("0");
                crr.setCreateBy(SecurityUtils.getUsername());
                consumeRechargeRecordService.insertConsumeRechargeRecord(crr);
                record.setDelFlag(1L);
                consumeSubsidyRecordService.updateConsumeSubsidyRecord(record);
            }
        }
        return toAjax(list.size());
    }

    @Log(title = "餐补批量审核", businessType = BusinessType.UPDATE)
    @PostMapping("/examineAll")
    public AjaxResult examineAll(ConsumeSubsidyRecord consumeSubsidyRecord)
    {
        List<ConsumeSubsidyRecord> list = consumeSubsidyRecordService.selectConsumeSubsidyRecordList(consumeSubsidyRecord);
        for(ConsumeSubsidyRecord record:list){
                record.setIsSh("1");
                consumeSubsidyRecordService.updateConsumeSubsidyRecord(record);
        }
        return toAjax(list.size());
    }
}
