package com.dcsoft.system.consume.controller;


import com.dcsoft.common.core.text.Convert;
import com.dcsoft.common.core.utils.StringUtils;
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.ConsumeGroup;
import com.dcsoft.system.consume.domain.ConsumeGroupRecord;
import com.dcsoft.system.consume.domain.ConsumeGroupUser;
import com.dcsoft.system.consume.domain.ConsumeRechargeRecord;
import com.dcsoft.system.consume.service.IConsumeGroupRecordService;
import com.dcsoft.system.consume.service.IConsumeGroupService;
import com.dcsoft.system.consume.service.IConsumeGroupUserService;
import com.dcsoft.system.consume.service.IConsumeRechargeRecordService;
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.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 消费组Controller
 *
 * @author dcsoft
 * @date 2023-01-05
 */
@RestController
@RequestMapping("/consumeGroup")
public class ConsumeGroupController extends BaseController
{
    @Autowired
    private IConsumeGroupService consumeGroupService;


    @Autowired
    private IConsumeGroupUserService consumeGroupUserService;

    @Autowired
    private ISysPeopleService sysPeopleService;

    @Autowired
    private IConsumeGroupRecordService consumeGroupRecordService;


    @Autowired
    private IConsumeRechargeRecordService consumeRechargeRecordService;

    /**
     * 查询消费组列表
     */
    @RequiresPermissions("consume:group:list")
    @GetMapping("/list")
    public TableDataInfo list(ConsumeGroup consumeGroup)
    {
        startPage();
        List<ConsumeGroup> list = consumeGroupService.selectConsumeGroupList(consumeGroup);
        return getDataTable(list);
    }

    /**
     * 查询消费组列表
     */
    @GetMapping("/tree")
    public TableDataInfo tree(ConsumeGroup consumeGroup)
    {
        List<ConsumeGroup> list = consumeGroupService.selectConsumeGroupList(consumeGroup);
        return getDataTable(list);
    }

    /**
     * 导出消费组列表
     */
    @RequiresPermissions("consume:group:export")
    @Log(title = "消费组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ConsumeGroup consumeGroup)
    {
        List<ConsumeGroup> list = consumeGroupService.selectConsumeGroupList(consumeGroup);
        ExcelUtil<ConsumeGroup> util = new ExcelUtil<ConsumeGroup>(ConsumeGroup.class);
        util.exportExcel(response, list, "消费组数据");
    }

    /**
     * 获取消费组详细信息
     */
    @RequiresPermissions("consume:group:query")
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId)
    {
        return success(consumeGroupService.selectConsumeGroupByGroupId(groupId));
    }

    /**
     * 新增消费组
     */
    @RequiresPermissions("consume:group:add")
    @Log(title = "消费组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConsumeGroup consumeGroup)
    {
        return toAjax(consumeGroupService.insertConsumeGroup(consumeGroup));
    }

    /**
     * 修改消费组
     */
    @RequiresPermissions("consume:group:edit")
    @Log(title = "消费组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConsumeGroup consumeGroup)
    {
        return toAjax(consumeGroupService.updateConsumeGroup(consumeGroup));
    }

    /**
     * 删除消费组
     */
    @RequiresPermissions("consume:group:remove")
    @Log(title = "消费组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds)
    {
        return toAjax(consumeGroupService.deleteConsumeGroupByGroupIds(groupIds));
    }

    @PostMapping("/createConsumeGroup")
    public void createConsumeGroup(HttpServletResponse response) throws IOException
    {
        //生成分组消费数据
        List<ConsumeGroup> list = consumeGroupService.selectConsumeGroupList(new ConsumeGroup());
        for(ConsumeGroup consumeGroup: list){
            ConsumeGroupUser consumeGroupUser=new ConsumeGroupUser();
            consumeGroupUser.setGroupId(consumeGroup.getGroupId());
            List<ConsumeGroupUser> userList=consumeGroupUserService.selectConsumeGroupUserList(consumeGroupUser);
            for(ConsumeGroupUser u:userList){
                //充值到账号
                SysPeople people=sysPeopleService.selectSysPeopleById(u.getUserId());
                people.setBtje(consumeGroup.getMoney()+people.getBtje());
                sysPeopleService.updateSysPeople(people);
                //进入分组餐补
                ConsumeGroupRecord consumeGroupRecord=new ConsumeGroupRecord();
                consumeGroupRecord.setAccountId(people.getId());
                consumeGroupRecord.setAccountName(people.getName());
                consumeGroupRecord.setBranchId(people.getBranchId());
                consumeGroupRecord.setBranchName(people.getBranchName());
                consumeGroupRecord.setEmpId(people.getGh());
                consumeGroupRecord.setAllMoney(consumeGroup.getMoney());
                consumeGroupRecord.setSubsidyTime(new Date());
                consumeGroupRecord.setPosition(people.getPosition());
                consumeGroupRecordService.insertConsumeGroupRecord(consumeGroupRecord);
                //下发补贴
                //补贴记录
                ConsumeRechargeRecord crr=new ConsumeRechargeRecord();
                crr.setConsumeMoney(consumeGroup.getMoney()+"");
                crr.setAccountId(people.getId()+"");
                crr.setEmpId(people.getGh());
                crr.setOrderType("2");//补贴
                crr.setRechargeWay("0");//平台
                crr.setRechargeType("0");
                crr.setCreateBy(SecurityUtils.getUsername());
                consumeRechargeRecordService.insertConsumeRechargeRecord(crr);

            }

        }

    }
}
