package com.dcsoft.system.kq.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.common.security.utils.SecurityUtils;
import com.dcsoft.system.domain.SysPeopleRecord;
import com.dcsoft.system.kq.domain.KqAdjust;
import com.dcsoft.system.kq.domain.KqGather;
import com.dcsoft.system.kq.domain.KqGroup;
import com.dcsoft.system.kq.domain.KqGroupUser;
import com.dcsoft.system.kq.mapper.KqGroupUserMapper;
import com.dcsoft.system.kq.service.IKqGatherService;
import com.dcsoft.system.kq.service.IKqGroupService;
import com.dcsoft.system.kq.service.IKqGroupUserService;
import com.dcsoft.system.service.ISysPeopleRecordService;
import com.dcsoft.system.uniubi.service.ISysApiService;
import com.dcsoft.system.uniubi.service.ISysSdkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 考勤汇总Controller
 *
 * @author dcsoft
 * @date 2023-06-12
 */
@RestController
@RequestMapping("/gather")
public class KqGatherController extends BaseController
{
    @Autowired
    private IKqGatherService kqGatherService;

    @Autowired
    private IKqGroupService kqGroupService;

    @Autowired
    private IKqGroupUserService kqGroupUserService;

    @Autowired
    private ISysSdkService sdkService;

    @Autowired
    protected ISysPeopleRecordService peopleRecordService;



    /**
     * 查询考勤汇总列表
     */
    @RequiresPermissions("kq:gather:list")
    @GetMapping("/list")
    public TableDataInfo list(KqGather kqGather)
    {
        startPage();
        List<KqGather> list = kqGatherService.selectKqGatherList(kqGather);
        return getDataTable(list);
    }

    /**
     * 导出考勤汇总列表
     */
    @Log(title = "考勤汇总", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KqGather kqGather)
    {
        List<KqGather> list = kqGatherService.selectKqGatherList(kqGather);
        ExcelUtil<KqGather> util = new ExcelUtil<KqGather>(KqGather.class);
        if("0".equals(kqGather.getFlag())){
            for(KqGather gather:list){
                if (StringUtils.isNotEmpty(gather.getLateTime())){
                    gather.setWorkKqTime(null);;
                }
                if (StringUtils.isNotEmpty(gather.getLeaveTime())){
                    gather.setOutKqTime(null);;;
                }
            }
        }
        util.exportExcel(response, list, "考勤汇总数据");
    }

    /**
     * 获取考勤汇总详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(kqGatherService.selectKqGatherById(id));
    }

    /**
     * 新增考勤汇总
     */
    @Log(title = "考勤汇总", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KqGather kqGather)
    {
        return toAjax(kqGatherService.insertKqGather(kqGather));
    }

    /**
     * 修改考勤汇总
     */
    @Log(title = "考勤汇总", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KqGather kqGather)
    {
        return toAjax(kqGatherService.updateKqGather(kqGather));
    }

    /**
     * 删除考勤汇总
     */
    @Log(title = "考勤汇总", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(kqGatherService.deleteKqGatherByIds(ids));
    }


    /**
     * 新增考勤汇总
     */
    @PostMapping("/gather")
    public AjaxResult gather(@RequestBody KqGather kqGather)
    {
        //选择考勤组和考勤汇总的时间然后进行主动生成
        //先删除考勤组和考勤时间的考勤
        kqGatherService.deleteKqGatherByKqGather(kqGather);
        //查询考勤组考勤人员
        KqGroup group=kqGroupService.selectKqGroupByGroupId(kqGather.getGroupId());
        KqGroupUser kqGroupUser=new KqGroupUser();
        kqGroupUser.setGroupId(kqGather.getGroupId());
        List<KqGroupUser> groupUserList= kqGroupUserService.selectKqGroupUserList(kqGroupUser);
        for(KqGroupUser u:groupUserList){
            //查询考勤打卡记录
            SysPeopleRecord peopleRecord=new SysPeopleRecord();
            peopleRecord.setShowDate(kqGather.getKqTime());
            peopleRecord.setPeopleId(u.getUserId()+"");
            List<SysPeopleRecord> prList=peopleRecordService.selectSysPeopleRecordLists(peopleRecord);
            for(SysPeopleRecord p:prList){
                //依据考勤记录将信息放入到考勤结果表中
                p.setGroupId(kqGather.getGroupId());
                sdkService.insertKqxx(p);
            }

        }
        return toAjax(1);
    }

    @Log(title = "日汇总信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<KqGather> util = new ExcelUtil<KqGather>(KqGather.class);
        List<KqGather> userList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = kqGatherService.importkqGather(userList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<KqGather> util = new ExcelUtil<KqGather>(KqGather.class);
        util.importTemplateExcel(response, "日汇总数据");
    }

    /**
     * 新增请假日汇总数据
     */
    @PostMapping("/saveAskForLeave")
    public void saveAskForLeave() throws ParseException {
        kqGatherService.saveAskForLeave();
    }

}
