package com.mortals.xhx.busiz;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.utils.ServletUtils;
import com.mortals.xhx.base.system.idgenerator.service.IdgeneratorService;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.model.DeptPerformStatEntity;
import com.mortals.xhx.module.dept.model.DeptQuery;
import com.mortals.xhx.module.dept.service.DeptPerformStatService;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.hik.face.model.req.img.ImgReq;
import com.mortals.xhx.module.hik.face.service.IHikFaceService;
import com.mortals.xhx.module.perform.model.PerformPerposeDeptStatEntity;
import com.mortals.xhx.module.perform.model.PerformPerposeStaffEntity;
import com.mortals.xhx.module.perform.model.PerformPerposeStaffStatEntity;
import com.mortals.xhx.module.perform.service.PerformPerposeDeptStatService;
import com.mortals.xhx.module.perform.service.PerformPerposeStaffService;
import com.mortals.xhx.module.perform.service.PerformPerposeStaffStatService;
import com.mortals.xhx.module.staff.model.*;
import com.mortals.xhx.module.staff.service.StaffPerformStatService;
import com.mortals.xhx.module.staff.service.StaffPerformSummaryService;
import com.mortals.xhx.module.staff.service.StaffService;
import com.mortals.xhx.module.workman.model.WorkmanEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 设备
 *
 * @author: zxfei
 * @date: 2021/8/24 20:28
 */
@RestController
@Slf4j
@RequestMapping("/mock")
public class MockDataController {

    @Autowired
    private IdgeneratorService idgeneratorService;

    @Autowired
    private ICacheService cacheService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private UserService userService;
    @Autowired
    private IHikFaceService faceService;
    @Autowired
    private UploadService uploadService;
    @Autowired
    private DeptService deptService;

    @Autowired
    private PerformPerposeStaffStatService perposeStaffStatService;
    @Autowired
    private PerformPerposeDeptStatService perposeDeptStatService;
    @Autowired
    private StaffPerformSummaryService summaryService;
    @Autowired
    private StaffPerformStatService staffPerformStatService;
    @Autowired
    private DeptPerformStatService deptPerformStatService;

    /**
     * 生成模拟数据
     * mortals_xhx_perform_perpose_staff_stat
     *
     * @return
     */
    @GetMapping("randomStatPerposeStaffData")
    @UnAuth
    public Rest<Void> randomStatPerposeStaffData() {
        List<StaffEntity> cacheList = staffService.getCacheList();
        //最近两月模拟数据
        DateTime beforeStartDate = DateUtil.offsetDay(new Date(), -60);
        for (int i = 1; i <= 60; i++) {
            DateTime curDate = DateUtil.offsetDay(beforeStartDate, i);

            List<PerformPerposeStaffStatEntity> collect = cacheList.stream().map(item -> {
                PerformPerposeStaffStatEntity statEntity = new PerformPerposeStaffStatEntity();
                statEntity.initAttrValue();
                statEntity.setStaffId(item.getId());
                statEntity.setStaffName(item.getName());
                statEntity.setNumber(item.getWorkNum());

                statEntity.setAbsentRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setAbsenceRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setLateRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setEarlyRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setLeaveRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setTurnoverRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setSleepRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setPlayPhoneRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setExpireWorkRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setNegaReviewRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setYear(DateUtil.year(curDate));
                statEntity.setMonth(DateUtil.month(curDate) + 1);
                statEntity.setDay(DateUtil.dayOfMonth(curDate));


                return statEntity;

            }).collect(Collectors.toList());
            perposeStaffStatService.save(collect);
        }
        return Rest.ok();
    }


    /**
     * 生成模拟数据
     * mortals_xhx_perform_perpose_dept_stat
     *
     * @return
     */
    @GetMapping("randomStatPerposeDeptData")
    @UnAuth
    public Rest<Void> randomStatPerposeDeptData() {

        List<DeptEntity> deptEntityList = deptService.find(new DeptQuery());

        //最近两月模拟数据
        DateTime beforeStartDate = DateUtil.offsetDay(new Date(), -60);
        for (int i = 1; i <= 60; i++) {
            DateTime curDate = DateUtil.offsetDay(beforeStartDate, i);

            List<PerformPerposeDeptStatEntity> collect = deptEntityList.stream().map(item -> {
                PerformPerposeDeptStatEntity statEntity = new PerformPerposeDeptStatEntity();
                statEntity.initAttrValue();
                statEntity.setDeptId(item.getId());
                statEntity.setDeptName(item.getDeptName());

                statEntity.setAbsentRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setAbsenceRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setLateRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setEarlyRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setLeaveRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setTurnoverRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setSleepRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setPlayPhoneRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setExpireWorkRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setNegaReviewRate(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
                statEntity.setYear(DateUtil.year(curDate));
                statEntity.setMonth(DateUtil.month(curDate) + 1);
                statEntity.setDay(DateUtil.dayOfMonth(curDate));
                return statEntity;
            }).collect(Collectors.toList());
            perposeDeptStatService.save(collect);
        }
        return Rest.ok();
    }


    /**
     * 生成模拟数据
     * mortals_xhx_staff_perform_summary
     *
     * @return
     */
    @GetMapping("randomStatStaffSummaryData")
    @UnAuth
    public Rest<Void> randomStatStaffSummaryData() {
        List<StaffEntity> cacheList = staffService.getCacheList();
        //最近两月模拟数据
        DateTime beforeStartDate = DateUtil.offsetMonth(new Date(), -3);
        for (int i = 1; i <= 3; i++) {
            DateTime curDate = DateUtil.offsetMonth(beforeStartDate, i);

            List<StaffPerformSummaryEntity> collect = cacheList.stream().map(item -> {

                StaffPerformSummaryEntity statEntity = new StaffPerformSummaryEntity();
                statEntity.initAttrValue();
                statEntity.setStaffId(item.getId());
                statEntity.setStaffName(item.getName());
                statEntity.setWorkNum(item.getWorkNum());
                statEntity.setDeptId(item.getDeptId());
                statEntity.setDeptName(item.getDeptName());
                BigDecimal attendScore = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal(15)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal reviewScore = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal(15)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal complainScore = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal(15)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal goworkScore = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal(15)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal effectScore = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal(15)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal otherScore = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal(15)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal errorScore = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal(15)).setScale(1, BigDecimal.ROUND_CEILING);

                BigDecimal totalScore = attendScore.add(reviewScore).add(complainScore).add(goworkScore).add(effectScore).add(otherScore).add(errorScore);

                statEntity.setAttendScore(attendScore);
                statEntity.setReviewScore(reviewScore);
                statEntity.setComplainScore(complainScore);
                statEntity.setGoworkScore(goworkScore);
                statEntity.setEffectScore(effectScore);
                statEntity.setOtherScore(otherScore);
                statEntity.setErrorScore(errorScore);
                statEntity.setTotalScore(totalScore);

                statEntity.setYear(DateUtil.year(curDate));
                statEntity.setMonth(DateUtil.month(curDate) + 1);


                return statEntity;

            }).collect(Collectors.toList());
            summaryService.save(collect);
        }
        return Rest.ok();
    }


    /**
     * 生成模拟数据
     * mortals_xhx_staff_perform_stat
     *
     * @return
     */
    @GetMapping("randomStaffPerformStatData")
    @UnAuth
    public Rest<Void> randomStaffPerformStatData() {
        List<StaffEntity> cacheList = staffService.getCacheList();
        //最近两月模拟数据
        DateTime beforeStartDate = DateUtil.offsetDay(new Date(), -60);
        for (int i = 1; i <= 60; i++) {
            DateTime curDate = DateUtil.offsetDay(beforeStartDate, i);

            List<StaffPerformStatEntity> collect = cacheList.stream().map(item -> {
                StaffPerformStatEntity statEntity = new StaffPerformStatEntity();
                statEntity.initAttrValue();
                statEntity.setStaffId(item.getId());
                statEntity.setStaffName(item.getName());

                BigDecimal attendScoreAdd = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal reviewScoreAdd = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal complainScoreAdd = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal goworkScoreAdd = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal effectScoreAdd = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal otherScoreAdd = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).setScale(1, BigDecimal.ROUND_CEILING);

                BigDecimal attendScoreSub = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal reviewScoreSub = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal complainScoreSub = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal goworkScoreSub = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal effectScoreSub = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal otherScoreSub = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).setScale(1, BigDecimal.ROUND_CEILING);

                statEntity.setAttendScoreAdd(attendScoreAdd);
                statEntity.setAttendScoreSub(attendScoreSub);
                statEntity.setReviewScoreAdd(reviewScoreAdd);
                statEntity.setReviewScoreSub(reviewScoreSub);
                statEntity.setComplainScoreAdd(complainScoreAdd);
                statEntity.setComplainScoreSub(complainScoreSub);
                statEntity.setGoworkScoreAdd(goworkScoreAdd);
                statEntity.setGoworkScoreSub(goworkScoreSub);
                statEntity.setEffectScoreAdd(effectScoreAdd);
                statEntity.setEffectScoreSub(effectScoreSub);
                statEntity.setOtherScoreAdd(otherScoreAdd);
                statEntity.setOtherScoreSub(otherScoreSub);

                BigDecimal totalScoreAdd = attendScoreAdd.add(reviewScoreAdd).add(complainScoreAdd)
                        .add(goworkScoreAdd).add(effectScoreAdd).add(otherScoreAdd);
                BigDecimal totalScoreSub = attendScoreSub.add(reviewScoreSub).add(complainScoreSub)
                        .add(goworkScoreSub).add(effectScoreSub).add(otherScoreSub);

                statEntity.setTotalAddScore(totalScoreAdd);
                statEntity.setTotalSubScore(totalScoreSub);

                BigDecimal subtract = statEntity.getTotalAddScore().subtract(statEntity.getTotalSubScore());
                statEntity.setTotalScore(subtract.compareTo(BigDecimal.ZERO) > 0 ? subtract : BigDecimal.ZERO);
                statEntity.setYear(DateUtil.year(curDate));
                statEntity.setMonth(DateUtil.month(curDate) + 1);
                statEntity.setDay(DateUtil.dayOfMonth(curDate));

                return statEntity;

            }).collect(Collectors.toList());
            staffPerformStatService.save(collect);
        }
        return Rest.ok();
    }


    /**
     * 生成模拟数据
     * mortals_xhx_dept_perform_stat
     *
     * @return
     */
    @GetMapping("randomDeptPerformStatData")
    @UnAuth
    public Rest<Void> randomDeptPerformStatData() {

        List<DeptEntity> deptEntityList = deptService.find(new DeptQuery());

        //最近两月模拟数据
        DateTime beforeStartDate = DateUtil.offsetDay(new Date(), -60);
        for (int i = 1; i <= 60; i++) {
            DateTime curDate = DateUtil.offsetDay(beforeStartDate, i);

            List<DeptPerformStatEntity> collect = deptEntityList.stream().map(item -> {
                DeptPerformStatEntity statEntity = new DeptPerformStatEntity();
                statEntity.initAttrValue();
                statEntity.setDeptId(item.getId());
                statEntity.setDeptName(item.getDeptName());
                int personNum = staffService.count(new StaffQuery().deptId(item.getId()), null);

                BigDecimal attendScoreAdd = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).multiply(new BigDecimal(personNum)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal reviewScoreAdd = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).multiply(new BigDecimal(personNum)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal complainScoreAdd = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).multiply(new BigDecimal(personNum)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal goworkScoreAdd = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).multiply(new BigDecimal(personNum)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal effectScoreAdd = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).multiply(new BigDecimal(personNum)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal otherScoreAdd = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).multiply(new BigDecimal(personNum)).setScale(1, BigDecimal.ROUND_CEILING);

                BigDecimal attendScoreSub = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).multiply(new BigDecimal(personNum)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal reviewScoreSub = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).multiply(new BigDecimal(personNum)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal complainScoreSub = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).multiply(new BigDecimal(personNum)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal goworkScoreSub = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).multiply(new BigDecimal(personNum)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal effectScoreSub = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).multiply(new BigDecimal(personNum)).setScale(1, BigDecimal.ROUND_CEILING);
                BigDecimal otherScoreSub = RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("5")).multiply(new BigDecimal(personNum)).setScale(1, BigDecimal.ROUND_CEILING);

                statEntity.setAttendScoreAdd(attendScoreAdd);
                statEntity.setAttendScoreSub(attendScoreSub);
                statEntity.setReviewScoreAdd(reviewScoreAdd);
                statEntity.setReviewScoreSub(reviewScoreSub);
                statEntity.setComplainScoreAdd(complainScoreAdd);
                statEntity.setComplainScoreSub(complainScoreSub);
                statEntity.setGoworkScoreAdd(goworkScoreAdd);
                statEntity.setGoworkScoreSub(goworkScoreSub);
                statEntity.setEffectScoreAdd(effectScoreAdd);
                statEntity.setEffectScoreSub(effectScoreSub);
                statEntity.setOtherScoreAdd(otherScoreAdd);
                statEntity.setOtherScoreSub(otherScoreSub);

                BigDecimal totalScoreAdd = attendScoreAdd.add(reviewScoreAdd).add(complainScoreAdd)
                        .add(goworkScoreAdd).add(effectScoreAdd).add(otherScoreAdd);
                BigDecimal totalScoreSub = attendScoreSub.add(reviewScoreSub).add(complainScoreSub)
                        .add(goworkScoreSub).add(effectScoreSub).add(otherScoreSub);

                statEntity.setTotalAddScore(totalScoreAdd);
                statEntity.setTotalSubScore(totalScoreSub);

                BigDecimal subtract = statEntity.getTotalAddScore().subtract(statEntity.getTotalSubScore());
                statEntity.setTotalScore(subtract.compareTo(BigDecimal.ZERO) > 0 ? subtract : BigDecimal.ZERO);



                if (personNum > 0) {
                    BigDecimal aver = statEntity.getTotalScore().divide(new BigDecimal(personNum), 2, BigDecimal.ROUND_CEILING);
                    statEntity.setAverageScore(aver);
                } else {
                    statEntity.setAverageScore(BigDecimal.ZERO);
                }

                statEntity.setYear(DateUtil.year(curDate));
                statEntity.setMonth(DateUtil.month(curDate) + 1);
                statEntity.setDay(DateUtil.dayOfMonth(curDate));
                return statEntity;
            }).collect(Collectors.toList());
            deptPerformStatService.save(collect);
        }
        return Rest.ok();
    }


    public static void main(String[] args) {
        System.out.println(RandomUtil.randomBigDecimal(BigDecimal.ZERO, new BigDecimal("1")).setScale(2, BigDecimal.ROUND_CEILING));
    }


}
