package com.ruoyi.pay.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.pay.domain.PayRequireAttendanceReport;
import com.ruoyi.system.domain.vo.SysUserImportVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pay.domain.vo.PayRequireAttendanceReportVo;
import com.ruoyi.pay.domain.bo.PayRequireAttendanceReportBo;
import com.ruoyi.pay.service.IPayRequireAttendanceReportService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 应出勤报
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/requireAttendanceReport")
public class PayRequireAttendanceReportController extends BaseController {

    private final IPayRequireAttendanceReportService iPayRequireAttendanceReportService;

    /**
     * 查询应出勤报列表
     */
    @SaCheckPermission("pay:requireAttendanceReport:list")
    @GetMapping("/list")
    public TableDataInfo<PayRequireAttendanceReportVo> list(PayRequireAttendanceReportBo bo, PageQuery pageQuery) {
        return iPayRequireAttendanceReportService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出应出勤报列表
     */
    @SaCheckPermission("pay:requireAttendanceReport:export")
    @Log(title = "应出勤报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayRequireAttendanceReportBo bo, HttpServletResponse response) {
        List<PayRequireAttendanceReportVo> list = iPayRequireAttendanceReportService.queryList(bo);
        ExcelUtil.exportExcel(list, "应出勤报", PayRequireAttendanceReportVo.class, response);
    }
    /**
     * 导入模板
     */
    @Log(title = "应出勤下载模板", businessType = BusinessType.EXPORT)
    @PostMapping("/importTemplate")
    public void importTemplate( HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "应出勤数据", PayRequireAttendanceReportVo.class, response);
    }

    /**
     * 导入应出勤报列表
     */
    @SaCheckPermission("pay:requireAttendanceReport:import")
    @Log(title = "应出勤报", businessType = BusinessType.EXPORT)
    @PostMapping("/import")
    public  R<Void> importData(MultipartFile file, boolean updateSupport, String date,Long tenantId) throws Exception {
        PayRequireAttendanceReportBo report = new PayRequireAttendanceReportBo();
        report.setYear(date.split("-")[0]);
        report.setMonth(date.split("-")[1]);
        List<PayRequireAttendanceReportVo>  occupaReports= iPayRequireAttendanceReportService.queryList(report);
        if(occupaReports!=null&&occupaReports.size()>0){//判断是否有当月应出勤信息
            if(!updateSupport){
                return R.ok("已有当月应出勤信息,如需更新请勾选更新选项");
            }else{//更新则删除之前信息
                List<Long> ids = new ArrayList<>();
                for(PayRequireAttendanceReportVo re:occupaReports){
                    ids.add(re.getId()) ;
                }
                iPayRequireAttendanceReportService.deleteWithValidByIds(ids,true);
            }
        }

        List<PayRequireAttendanceReportVo> requireAttendanceReports = ExcelUtil.importExcel(file.getInputStream(), PayRequireAttendanceReportVo.class);
        String message = iPayRequireAttendanceReportService.importAttendanceReport(requireAttendanceReports, updateSupport, date,tenantId);
        return R.ok(message);

    }



    /**
     * 获取应出勤报详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pay:requireAttendanceReport:query")
    @GetMapping("/{id}")
    public R<PayRequireAttendanceReportVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iPayRequireAttendanceReportService.queryById(id));
    }

    /**
     * 新增应出勤报
     */
    @SaCheckPermission("pay:requireAttendanceReport:add")
    @Log(title = "应出勤报", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PayRequireAttendanceReportBo bo) {
        // 验证是否存在这个人的报告
        PayRequireAttendanceReportBo query = new PayRequireAttendanceReportBo();
        query.setTenantId(bo.getTenantId());
        query.setYear(bo.getYear());
        query.setMonth(bo.getMonth());
        query.setYear(bo.getEmpName());
        List<PayRequireAttendanceReportVo> r = iPayRequireAttendanceReportService.queryList(bo);
        if (r!=null&&r.size()>0)
        {
            return  R.fail("已存在"+bo.getEmpName()+" "+bo.getYear()+bo.getMonth()+"月的信息");
        }
        return toAjax(iPayRequireAttendanceReportService.insertByBo(bo));
    }

    /**
     * 修改应出勤报
     */
    @SaCheckPermission("pay:requireAttendanceReport:edit")
    @Log(title = "应出勤报", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayRequireAttendanceReportBo bo) {
        return toAjax(iPayRequireAttendanceReportService.updateByBo(bo));
    }

    /**
     * 删除应出勤报
     *
     * @param ids 主键串
     */
    @SaCheckPermission("pay:requireAttendanceReport:remove")
    @Log(title = "应出勤报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iPayRequireAttendanceReportService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
