package com.ruoyi.web.controller.week;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ISysOperLogService;
import com.ruoyi.system.service.ISysUnitsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Shawn
 */

@RestController
@RequestMapping("/week")
public class SysWeekController extends BaseController {

    @Autowired
    private ISysUnitsService unitsService;

    @Autowired
    private SysWeekMapper weekMapper;

    @Autowired
    private SysDailyMapper dailyMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private B1Mapper b1Mapper;

    @Autowired
    private B2Mapper b2Mapper;

    @Autowired
    private B3Mapper b3Mapper;

    @Autowired
    private B4Mapper b4Mapper;

    @Autowired
    private B6Mapper b6Mapper;

    @Autowired
    private B7Mapper b7Mapper;

    @Autowired
    private B8Mapper b8Mapper;

    @Autowired
    private B9Mapper b9Mapper;

    @Autowired
    private B10Mapper b10Mapper;

    @Autowired
    private B11Mapper b11Mapper;

    @Autowired
    private B12Mapper b12Mapper;

    @Autowired
    private B13Mapper b13Mapper;

    @Autowired
    private B14Mapper b14Mapper;

    @Autowired
    private B15Mapper b15Mapper;

    @GetMapping("/getDaily/{projectId}/{weekNum}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    @Transactional
    public AjaxResult getStaff(@PathVariable String projectId, @PathVariable String weekNum, @PathVariable String from, @PathVariable String to) {
        Set<String> staffList = new HashSet<>();
        List<DailyReportStatus> dailyReportStatuses = new ArrayList<>();
        for (String s : SysWeekController.getWeekDateByFromAndTo(from, to)) {
            dailyReportStatuses.addAll(dailyMapper.selectDailyReportStatusList(projectId, s, s.replace("-", "").substring(0, 6)));
        }
        for (DailyReportStatus dailyReportStatus : dailyReportStatuses) {
            staffList.add(dailyReportStatus.getUserId());
        }
        LoginUser loginUser = getLoginUser();
        String userId = loginUser.getUser().getId();
        String weekReportStatus = weekMapper.searchExamineFlag(userId, projectId, weekNum);
        AjaxResult success = AjaxResult.success();
        success.put("staffList", staffList);
        success.put("dailyReportList", dailyReportStatuses);
        success.put("weekReportStatus", weekReportStatus);
        return success;
    }

    public static List<String> getDayBetween(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }


    @GetMapping("/getReAuditDailys/{userId}/{projectId}/{weekNum}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    @Transactional
    public AjaxResult reAuditSelectDaily(@PathVariable String userId, @PathVariable String projectId, @PathVariable String weekNum, @PathVariable String from, @PathVariable String to) {
        Set<String> staffList = new HashSet<>();
        List<DailyReportStatus> dailyReportStatuses = new ArrayList<>();
        for (String s : SysWeekController.getWeekDateByFromAndTo(from, to)) {
            dailyReportStatuses.addAll(dailyMapper.selectDailyReportStatusList(projectId, s, s.replace("-", "").substring(0, 6)));
        }
        for (DailyReportStatus dailyReportStatus : dailyReportStatuses) {
            staffList.add(dailyReportStatus.getUserId());
        }
        String weekReportStatus = weekMapper.searchExamineFlag(userId, projectId, weekNum);
        SysUser sysUser = userMapper.selectUserById(userId);
        if (sysUser == null) {
            weekReportStatus = "0";
        }
        AjaxResult success = AjaxResult.success();
        success.put("staffList", staffList);
        success.put("dailyReportList", dailyReportStatuses);
        success.put("weekReportStatus", weekReportStatus);
        return success;
    }

    @GetMapping("/getWeekInfo")
    public AjaxResult getWeekInfo() {
        String date = DateUtils.getDate();
        SysWeekInfo sysWeekInfo = weekMapper.selectWeekInfoByDate(date);
        return AjaxResult.success(sysWeekInfo);
    }


    @GetMapping("/getWeekInfo2")
    public AjaxResult getWeekInfo2() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        SysWeekInfo sysWeekInfo = weekMapper.selectWeekInfoByDate(yesterday);
        return AjaxResult.success(sysWeekInfo);
    }

    @GetMapping("/getAllWeekInfo")
    public AjaxResult getAllWeekInfo() {
        List<SysWeekInfo> sysWeekInfos = weekMapper.selectAllWeekInfo();
        TreeSet<WeekNumAndFromAndTo> weekNumAndFromAndTos = new TreeSet<>();
        for (SysWeekInfo sysWeekInfo : sysWeekInfos) {
            WeekNumAndFromAndTo weekNumAndFromAndTo = new WeekNumAndFromAndTo();
            weekNumAndFromAndTo.setWeekNum(sysWeekInfo.getWeekNum());
            weekNumAndFromAndTo.setFrom(sysWeekInfo.getFrom());
            weekNumAndFromAndTo.setTo(sysWeekInfo.getTo());
            weekNumAndFromAndTos.add(weekNumAndFromAndTo);
        }
        return AjaxResult.success(weekNumAndFromAndTos);
    }

    @GetMapping("/getB1/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B1> getB1(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B1> b1s = new ArrayList<>();
        b1s = b1Mapper.selectB1ListByProjectIDAndFromTo(projectId, from, to);
        return b1s;
    }

    @GetMapping("/getreauditB1/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B1> getReauditB1(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B1 b1 = new B1();
        b1.setSurveyPerson(userId);
        b1.setProjectOwnerUnit(projectId);
        b1.setCurrentTime(dateAt);
        List<B1> b1s = new ArrayList<>();
        b1s = b1Mapper.selectB1ListByInfo(b1);
        return b1s;
    }

    @GetMapping("/getB2/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B2> getB2(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B2> b2s = new ArrayList<>();
        b2s = b2Mapper.selectB2ListByProjectIDAndFromTo(projectId, from, to);
        return b2s;
    }

    @GetMapping("/getreauditB2/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B2> getreauditB2(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B2 b2 = new B2();
        b2.setXinTuPerson(userId);
        b2.setProjectOwnerUnit(projectId);
        b2.setCurrentTime(dateAt);
        List<B2> b2s;
        b2s = b2Mapper.selectListByInfo(b2);
        return b2s;
    }

    @GetMapping("/getB3/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B3> getB3(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B3> b3s = new ArrayList<>();
        b3s = b3Mapper.selectB3ListByProjectIDAndFromTo(projectId, from, to);
        return b3s;
    }

    @GetMapping("/getreauditB3/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B3> getreauditB3(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B3 b3 = new B3();
        b3.setSurveyPerson(userId);
        b3.setProjectOwnerUnit(projectId);
        b3.setCurrentTime(dateAt);
        List<B3> b3s;
        b3s = b3Mapper.selectListByInfo(b3);
        return b3s;
    }

    @GetMapping("/getB4/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B4> getB4(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B4> b4s = new ArrayList<>();
        b4s = b4Mapper.selectB4ListByProjectIDAndFromTo(projectId, from, to);
        return b4s;
    }

    @GetMapping("/getreauditB4/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B4> getreauditB4(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B4 b4 = new B4();
        b4.setNewPerson(userId);
        b4.setProjectOwnerUnit(projectId);
        b4.setCurrentTime(dateAt);
        List<B4> b4s;
        b4s = b4Mapper.selectListByInfo(b4);
        return b4s;
    }

    @GetMapping("/getB6/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B6> getB6(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B6> b6s = new ArrayList<>();
        b6s = b6Mapper.selectB6ListByProjectIDAndFromTo(projectId, from, to);
        return b6s;
    }

    @GetMapping("/getreauditB6/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B6> getreauditB6(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B6 b6 = new B6();
        b6.setName(userId);
        b6.setProjectOwnerUnit(projectId);
        b6.setCurrentTime(dateAt);
        List<B6> b6s;
        b6s = b6Mapper.selectListByInfo(b6);
        return b6s;
    }

    @GetMapping("/getB7/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B7> getB7(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B7> b7s = new ArrayList<>();
        b7s = b7Mapper.selectB7ListByProjectIDAndFromTo(projectId, from, to);
        return b7s;
    }

    @GetMapping("/getreauditB7/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B7> getreauditB7(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B7 b7 = new B7();
        b7.setSurveyPerson(userId);
        b7.setProjectOwnerUnit(projectId);
        b7.setCurrentTime(dateAt);
        List<B7> b7s;
        b7s = b7Mapper.selectListByInfo(b7);
        return b7s;
    }

    @GetMapping("/getB8/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B8> getB8(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B8> b8s = new ArrayList<>();
        b8s = b8Mapper.selectB8ListByProjectIDAndFromTo(projectId, from, to);
        return b8s;
    }

    @GetMapping("/getreauditB8/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B8> getreauditB8(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B8 b8 = new B8();
        b8.setNewPerson(userId);
        b8.setProjectOwnerUnit(projectId);
        b8.setCurrentTime(dateAt);
        List<B8> b8s;
        b8s = b8Mapper.selectListByInfo(b8);
        return b8s;
    }

    @GetMapping("/getB9/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B9> getB9(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B9> b9s = new ArrayList<>();
        b9s = b9Mapper.selectB9ListByProjectIDAndFromTo(projectId, from, to);
        return b9s;
    }

    @GetMapping("/getreauditB9/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B9> getreauditB9(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B9 b9 = new B9();
        b9.setNewPerson(userId);
        b9.setProjectOwnerUnit(projectId);
        b9.setCurrentTime(dateAt);
        List<B9> b9s;
        b9s = b9Mapper.selectListByInfo(b9);
        return b9s;
    }

    @GetMapping("/getB10/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B10> getB10(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B10> b10s = new ArrayList<>();
        b10s = b10Mapper.selectB10ListByProjectIDAndFromTo(projectId, from, to);
        return b10s;
    }

    @GetMapping("/getreauditB10/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B10> getreauditB10(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B10 b10 = new B10();
        b10.setSurveyPerson(userId);
        b10.setProjectOwnerUnit(projectId);
        b10.setCurrentTime(dateAt);
        List<B10> b10s;
        b10s = b10Mapper.selectListByInfo(b10);
        return b10s;
    }

    @GetMapping("/getB11/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B11> getB11(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B11> b11s = new ArrayList<>();
        b11s = b11Mapper.selectB11ListByProjectIDAndFromTo(projectId, from, to);
        return b11s;
    }

    @GetMapping("/getreauditB11/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B11> getreauditB11(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B11 b11 = new B11();
        b11.setNewPerson(userId);
        b11.setProjectOwnerUnit(projectId);
        b11.setCurrentTime(dateAt);
        List<B11> b11s;
        b11s = b11Mapper.selectListByInfo(b11);
        return b11s;
    }

    @GetMapping("/getB12/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B12> getB12(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B12> b12s = new ArrayList<>();
        b12s = b12Mapper.selectB12ListByProjectIDAndFromTo(projectId, from, to);
        return b12s;
    }

    @GetMapping("/getreauditB12/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B12> getreauditB12(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B12 b12 = new B12();
        b12.setSurveyPerson(userId);
        b12.setProjectOwnerUnit(projectId);
        b12.setCurrentTime(dateAt);
        List<B12> b12s;
        b12s = b12Mapper.selectListByInfo(b12);
        return b12s;
    }

    @GetMapping("/getB13/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B13> getB13(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B13> b13s = new ArrayList<>();
        b13s = b13Mapper.selectB13ListByProjectIDAndFromTo(projectId, from, to);
        return b13s;
    }

    @GetMapping("/getreauditB13/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B13> getreauditB13(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B13 b13 = new B13();
        b13.setNewPerson(userId);
        b13.setProjectOwnerUnit(projectId);
        b13.setCurrentTime(dateAt);
        List<B13> b13s;
        b13s = b13Mapper.selectListByInfo(b13);
        return b13s;
    }

    @GetMapping("/getB14/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B14> getB14(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B14> b14s = new ArrayList<>();
        b14s = b14Mapper.selectB14ListByProjectIDAndFromTo(projectId, from, to);
        return b14s;
    }

    @GetMapping("/getreauditB14/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B14> getreauditB14(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B14 b14 = new B14();
        b14.setSurveyPerson(userId);
        b14.setProjectOwnerUnit(projectId);
        b14.setSurveyPlotDay(dateAt);
        List<B14> b14s;
        b14s = b14Mapper.selectListByInfo(b14);
        return b14s;
    }

    @GetMapping("/getB15/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    public List<B15> getB15(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) {
        List<B15> b15s = new ArrayList<>();
        b15s = b15Mapper.selectB15ListByProjectIDAndFromTo(projectId, from, to);
        return b15s;
    }

    @GetMapping("/getreauditB15/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    public List<B15> getreauditB15(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt) {
        B15 b15 = new B15();
        b15.setClaimPaymentPerson(userId);
        b15.setProjectOwnerUnit(projectId);
        b15.setExpenseDay(dateAt);
        List<B15> b15s;
        b15s = b15Mapper.selectListByInfo(b15);
        return b15s;
    }

    @GetMapping("/selectDaily/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:select')")
    public List<DailyReportStatus> weekSelect(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) throws ParseException {
        List<String> monthList = new ArrayList<>();
        List<DailyReportStatus> Dailys = new ArrayList<>();
        for (String s : SysWeekController.getMonthBetween(from, to)) {
            Dailys.addAll(weekMapper.selectWeekNumByFromTo(projectId, from, to, s.replace("-", "")));
        }
        return Dailys;
    }

    @GetMapping("/delWeek/{userId}/{weekNum}/{region}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('week:error:del')")
    @Transactional
    public AjaxResult delWeek(@PathVariable String userId, @PathVariable String weekNum, @PathVariable String region, @PathVariable String from, @PathVariable String to) {
        int i = weekMapper.delWeekReportStatus(userId, region, weekNum);
        if (i != 1) {
            return AjaxResult.error();
        }

        List<String> weekDateByFromAndTo = getWeekDateByFromAndTo(from, to);
        for (String s : weekDateByFromAndTo) {
            dailyMapper.delWeekPpdateDailyReportStatus(region, s, s.split("-")[0] + s.split("-")[1]);
        }

        b1Mapper.weekdelB1(region, from, to);
        b2Mapper.weekdelB2(region, from, to);
        b3Mapper.weekdelB3(region, from, to);
        b4Mapper.weekdelB4(region, from, to);
        b6Mapper.weekdelB6(region, from, to);
        b7Mapper.weekdelB7(region, from, to);
        b8Mapper.weekdelB8(region, from, to);
        b9Mapper.weekdelB9(region, from, to);
        b10Mapper.weekdelB10(region, from, to);
        b11Mapper.weekdelB11(region, from, to);
        b12Mapper.weekdelB12(region, from, to);
        b13Mapper.weekdelB13(region, from, to);
        b14Mapper.weekdelB14(region, from, to);
        b15Mapper.weekdelB15(region, from, to);
        return AjaxResult.success();
    }

    public static List<String> getWeekDateByFromAndTo(String from, String to) {
        Date start = DateUtils.parseDate(from);
        Date end = DateUtils.parseDate(to);
        List<String> list = new ArrayList<String>();
        long s = start.getTime();
        long e = end.getTime();
        long oneDay = 1000 * 60 * 60 * 24L;
        while (s <= e) {
            start = new Date(s);
            list.add(new SimpleDateFormat("yyyy-MM-dd").format(start));
            s += oneDay;
        }
        return list;
    }


    /**
     * @param week    data:dailtReportList
     * @param weekNum 周号201101
     * @param region  日期区间
     * @return AjaxResult.error()之类的
     * 这个接口执行耗时,在分钟级别，前端很可能提示超时错误，因此，改造为异步接口
     * by Paul
     * 2022-11-15
     */
    @PostMapping("/examineAll/{weekNum}/{region}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
    @Transactional
    public AjaxResult weekExamine(@RequestBody String week, @PathVariable String weekNum, @PathVariable String region) {
        LoginUser loginUser = getLoginUser();
        int i = weekMapper.examinePending(loginUser.getUserId(), region, weekNum);

        if (i != 1) {
            return AjaxResult.error();
        }

        AsyncManager.me().execute5000(this.asyncReauditExamine(week,loginUser.getUserId(), weekNum, region));

        return AjaxResult.success("审核任务已提交，请1分钟后，查询结果！");
    }

    /**
     * @param week    data:dailtReportList
     * @param weekNum 周号201101
     * @param region  日期区间
     * @param userId  单元负责人
     * @return AjaxResult.error()之类的
     * 这个接口执行耗时,在分钟级别，前端很可能提示超时错误，因此，改造为异步接口
     * by Paul
     * 2022-11-15
     */
    @PostMapping("/reauditExamineAll/{userId}/{weekNum}/{region}")
    @PreAuthorize("@ss.hasPermi('week:error:reaudit')")
    @Transactional
    public AjaxResult weekReauditExamine(@RequestBody String week, @PathVariable String userId, @PathVariable String weekNum, @PathVariable String region) {

        int i = weekMapper.examinePending(userId, region, weekNum);
        if (i != 1) {
            return AjaxResult.error();
        }

        AsyncManager.me().execute5000(this.asyncReauditExamine(week, userId, weekNum, region));
        return AjaxResult.success("审核任务已提交，请1分钟后，查询结果！");
    }

    /**
     * 异步写入  审核结果 + 补审核结果
     *
     * @param
     * @return 任务task
     * by Paul
     * 2022-11-15
     */
    public TimerTask asyncReauditExamine(String week, String userId, String weekNum, String region) {
        return new TimerTask() {
            @Override
            public void run() {

                List<DailyReportStatus> list = new ArrayList(JSONArray.parseArray(week, DailyReportStatus.class));
//        System.out.println(list.toString());
                for (DailyReportStatus dailyReportStatus : list) {
                    if (!"2".equals(dailyReportStatus.getDailyReportStatus())) {
                        dailyMapper.updateDailyReportStatus(dailyReportStatus.getUserId(), dailyReportStatus.getProjectId(), dailyReportStatus.getDateAt(), dailyReportStatus.getDateAt().substring(0, 7).replace("-", ""));
                        B1 b1 = new B1(dailyReportStatus.getDateAt(), dailyReportStatus.getUserId(), dailyReportStatus.getProjectId());
                        b1Mapper.examineB1(b1);

                        B2 b2 = new B2(dailyReportStatus.getDateAt(), dailyReportStatus.getUserId(), dailyReportStatus.getProjectId());
                        b2Mapper.examineB2(b2);

                        B3 b3 = new B3(dailyReportStatus.getDateAt(), dailyReportStatus.getUserId(), dailyReportStatus.getProjectId());
                        b3Mapper.examineB3(b3);

                        B4 b4 = new B4(dailyReportStatus.getDateAt(), dailyReportStatus.getUserId(), dailyReportStatus.getProjectId());
                        b4Mapper.examineB4(b4);

                        B6 b6 = new B6(dailyReportStatus.getDateAt(), dailyReportStatus.getUserId(), dailyReportStatus.getProjectId());
                        b6Mapper.examineB6(b6);

                        B7 b7 = new B7(dailyReportStatus.getDateAt(), dailyReportStatus.getUserId(), dailyReportStatus.getProjectId());
                        b7Mapper.examineB7(b7);

                        B8 b8 = new B8(dailyReportStatus.getDateAt(), dailyReportStatus.getUserId(), dailyReportStatus.getProjectId());
                        b8Mapper.examineB8(b8);

                        B9 b9 = new B9(dailyReportStatus.getDateAt(), dailyReportStatus.getUserId(), dailyReportStatus.getProjectId());
                        b9Mapper.examineB9(b9);

                        B10 b10 = new B10(dailyReportStatus.getDateAt(), dailyReportStatus.getUserId(), dailyReportStatus.getProjectId());
                        b10Mapper.examineB10(b10);

                        B11 b11 = new B11(dailyReportStatus.getDateAt(), dailyReportStatus.getUserId(), dailyReportStatus.getProjectId());
                        b11Mapper.examineB11(b11);

                        B12 b12 = new B12(dailyReportStatus.getDateAt(), dailyReportStatus.getUserId(), dailyReportStatus.getProjectId());
                        b12Mapper.examineB12(b12);

                        B13 b13 = new B13(dailyReportStatus.getDateAt(), dailyReportStatus.getUserId(), dailyReportStatus.getProjectId());
                        b13Mapper.examineB13(b13);

                        B14 b14 = new B14(dailyReportStatus.getDateAt(), dailyReportStatus.getUserId(), dailyReportStatus.getProjectId());
                        b14Mapper.examineB14(b14);

                        B15 b15 = new B15(dailyReportStatus.getDateAt(), dailyReportStatus.getProjectId(), dailyReportStatus.getUserId());
                        b15Mapper.examineB15(b15);
                    }
                }
                int i = weekMapper.examineByUpdate(userId, region, weekNum);
            }
        };
    }

//    @PostMapping("/examineByFilter/{weekNum}")
//    @PreAuthorize("@ss.hasPermi('week:weekreport:examine')")
//    public AjaxResult examineByFilter(@RequestBody String week,@PathVariable String weekNum){
//        System.out.println(week);
//        System.out.println(weekNum);
//        return AjaxResult.success();
//    }

    public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }

    @GetMapping("/export/{projectId}/{dateRegion}")
    @PreAuthorize("@ss.hasPermi('week:weekreport:select')")
    public AjaxResult getExport(@PathVariable String projectId, @PathVariable String dateRegion) {
//        List<DailyReportStatus> list = JSONArray.parseArray(weekInfo, DailyReportStatus.class);
//        List<String> betweenDate = DateUtils.getBetweenDate(dateRegion.split(",")[0], dateRegion.split(",")[1]);
        weekSum sum = new weekSum();
        List<B1> b1sum = new ArrayList<>();
        List<B2> b2sum = new ArrayList<>();
        List<B3> b3sum = new ArrayList<>();
        List<B4> b4sum = new ArrayList<>();
        List<B6> b6sum = new ArrayList<>();
        List<B7> b7sum = new ArrayList<>();
        List<B8> b8sum = new ArrayList<>();
        List<B9> b9sum = new ArrayList<>();
        List<B10> b10sum = new ArrayList<>();
        List<B11> b11sum = new ArrayList<>();
        List<B12> b12sum = new ArrayList<>();
        List<B13> b13sum = new ArrayList<>();
        List<B14> b14sum = new ArrayList<>();
        List<B15> b15sum = new ArrayList<>();

        List<B1> b1s = b1Mapper.selectB1ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b1s.size() != 0) {
            b1sum.addAll(b1s);
        }


        List<B2> b2s = b2Mapper.selectB2ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b2s.size() != 0) {
            b2sum.addAll(b2s);
        }

        List<B3> b3s = b3Mapper.selectB3ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b3s.size() != 0) {
            b3sum.addAll(b3s);
        }

        List<B4> b4s = b4Mapper.selectB4ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b4s.size() != 0) {
            b4sum.addAll(b4s);
        }

        List<B6> b6s = b6Mapper.selectB6ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b6s.size() != 0) {
            b6sum.addAll(b6s);
        }


        List<B7> b7s = b7Mapper.selectB7ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b7s.size() != 0) {
            b7sum.addAll(b7s);
        }


        List<B8> b8s = b8Mapper.selectB8ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b8s.size() != 0) {
            b8sum.addAll(b8s);
        }


        List<B9> b9s = b9Mapper.selectB9ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b9s.size() != 0) {
            b9sum.addAll(b9s);
        }

        List<B10> b10s = b10Mapper.selectB10ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b10s.size() != 0) {
            b10sum.addAll(b10s);
        }


        List<B11> b11s = b11Mapper.selectB11ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b11s.size() != 0) {
            b11sum.addAll(b11s);
        }


        List<B12> b12s = b12Mapper.selectB12ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b12s.size() != 0) {
            b12sum.addAll(b12s);
        }


        List<B13> b13s = b13Mapper.selectB13ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b13s.size() != 0) {
            b13sum.addAll(b13s);
        }


        List<B14> b14s = b14Mapper.selectB14ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b14s.size() != 0) {
            b14sum.addAll(b14s);
        }


        List<B15> b15s = b15Mapper.selectB15ListByProjectIDAndFromTo(projectId, dateRegion.split(",")[0], dateRegion.split(",")[1]);
        if (b15s.size() != 0) {
            b15sum.addAll(b15s);
        }


        sum.setB1s(b1sum);
        sum.setB2s(b2sum);
        sum.setB3s(b3sum);
        sum.setB4s(b4sum);
        sum.setB6s(b6sum);
        sum.setB7s(b7sum);
        sum.setB8s(b8sum);
        sum.setB9s(b9sum);
        sum.setB10s(b10sum);
        sum.setB11s(b11sum);
        sum.setB12s(b12sum);
        sum.setB13s(b13sum);
        sum.setB14s(b14sum);
        sum.setB15s(b15sum);
        return AjaxResult.success(sum);
    }
}
