package com.jpsoft.shinestar.scheduled;

import cn.hutool.core.date.DateUtil;
import com.jpsoft.shinestar.modules.base.entity.CheckNumCompany;
import com.jpsoft.shinestar.modules.base.entity.CompanyInfo;
import com.jpsoft.shinestar.modules.base.entity.CompanySupervise;
import com.jpsoft.shinestar.modules.base.entity.TotalCheckCompany;
import com.jpsoft.shinestar.modules.base.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 墨鱼_mo
 * @date 2020-3-28 10:28
 */
@Component
@Slf4j
@Transactional
public class CheckPersonTask {

    @Autowired
    private CheckNumCompanyService checkNumCompanyService;

    @Autowired
    private TotalCheckCompanyService totalCheckCompanyService;

    @Autowired
    private PersonDeviceFilterLogService personDeviceFilterLogService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Scheduled(cron = "0 59 23 ? * *")
    public  void run() {
        log.warn("公司每日过人记录定时任务开始");

        Date date = new Date();
        Date startTime = DateUtil.beginOfDay(date);
        Date endTime = DateUtil.endOfDay(date);
        String recordTime = DateUtil.format(new Date(),"yyyy-MM-dd");

        //查询当天有记录的公司
        List<CheckNumCompany> checkNumCompanyList = checkNumCompanyService.findByRecordTime(recordTime);
        //获取所有公司统计表的数据
        List<TotalCheckCompany> totalCheckCompanyList = totalCheckCompanyService.findAll();
        List<String> totalCheckCompanyIds = new ArrayList<>();

        //将公司统计表id数据存放在totalCheckCompanyIds
        for (TotalCheckCompany totalCheckCompany: totalCheckCompanyList){
            totalCheckCompanyIds.add(totalCheckCompany.getCompanyId());
        }

        //当日有记录的公司的id集合初始化
        List<String> checkNumCompanyIds = new ArrayList<>();

        //当日有数据时要进行判断正常，异常，过人记录
        if (checkNumCompanyList.size()>0){
            for (CheckNumCompany checkNumCompany: checkNumCompanyList){
                //查询公司信息
                CompanyInfo companyInfo = companyInfoService.get(checkNumCompany.getCompanyId());
                //将有记录的公司id保存到checkNumCompanyIds
                checkNumCompanyIds.add(companyInfo.getId());
                //将有记录的公司存在list中以方便后面接口使用
                List<CompanyInfo> list = new ArrayList<>();
                list.add(companyInfo);
                //按照公司id查询公司无异常人数
                Integer normal = personDeviceFilterLogService.getDayNormalNumByCompanyId(startTime,endTime,checkNumCompany.getCompanyId());
                //按照公司list查询公司异常人数
                Integer abNormal = personDeviceFilterLogService.getDayAbnormalNumByCompanyList(startTime,endTime,list);
                //将正常异常数据保存
                checkNumCompany.setNormalNum(normal==null?0:normal.longValue());
                checkNumCompany.setAbnormalNum(abNormal == null?0:abNormal.longValue());
                checkNumCompanyService.update(checkNumCompany);

                //查询出此公司的统计数据
                TotalCheckCompany totalCheckCompany = totalCheckCompanyService.findByCompanyId(companyInfo.getId());
                //如果不存在则新建一条
                if (totalCheckCompany == null){
                    TotalCheckCompany totalCheckCompany1 = new TotalCheckCompany();
                    totalCheckCompany1.setCompanyId(companyInfo.getId());
                    //如果有异常则连续正常日期清零，并记录发生异常的日期
                    if (checkNumCompany.getAbnormalNum()>0){
                        totalCheckCompany1.setNormalDay(0L);
                        totalCheckCompany1.setAbnormalTime(DateUtil.beginOfDay(new Date()));
                    }else {
                        totalCheckCompany1.setNormalDay(1L);
                    }
                    totalCheckCompany1.setTotalCheckNum(checkNumCompany.getCheckNum());
                    totalCheckCompany1.setTotalAbnormalNum(checkNumCompany.getAbnormalNum());
                    totalCheckCompany1.setDelFlag(false);
                    totalCheckCompany1.setCreateTime(new Date());
                    totalCheckCompanyService.insert(totalCheckCompany1);

                }else {
                    //当公司记录存在并有异常时要更新异常日期
                    if (checkNumCompany.getAbnormalNum()>0){
                        totalCheckCompany.setNormalDay(0L);
                        totalCheckCompany.setAbnormalTime(DateUtil.beginOfDay(new Date()));
                    }else {
                        Long normalDay = totalCheckCompany.getNormalDay();
                        totalCheckCompany.setNormalDay(normalDay+1L);
                    }
                    Long totalCheckNum = totalCheckCompany.getTotalCheckNum();
                    totalCheckCompany.setTotalCheckNum(totalCheckNum+checkNumCompany.getCheckNum());
                    Long totalAbnormalNum = totalCheckCompany.getTotalAbnormalNum();
                    totalCheckCompany.setTotalAbnormalNum(totalAbnormalNum+checkNumCompany.getAbnormalNum());
                    totalCheckCompany.setUpdateTime(date);
                    totalCheckCompanyService.update(totalCheckCompany);

                }
            }

            //将已经有记录的公司移除，无记录的公司默认正常天数加1
            boolean success =totalCheckCompanyIds.removeAll(checkNumCompanyIds);

            if (success){
                for (String companyId : totalCheckCompanyIds){
                    TotalCheckCompany totalCheckCompany = totalCheckCompanyService.findByCompanyId(companyId);
                    Long normalDay = totalCheckCompany.getNormalDay();
                    totalCheckCompany.setNormalDay(normalDay+1L);
                    totalCheckCompany.setUpdateTime(new Date());
                    totalCheckCompanyService.update(totalCheckCompany);
                }
            }
         //无记录时所有公司正常日期加1，异常时间不更新
        }else {
            for (TotalCheckCompany totalCheckCompany : totalCheckCompanyList){
                Long normalDay = totalCheckCompany.getNormalDay();
                totalCheckCompany.setNormalDay(normalDay+1L);
                totalCheckCompany.setUpdateTime(new Date());
                totalCheckCompanyService.update(totalCheckCompany);
            }
        }
        log.warn("公司每日过人记录定时任务结束");
    }
}
