package com.jpsoft.shinestar.scheduled;

import cn.hutool.core.date.DateUtil;
import com.jpsoft.shinestar.modules.base.entity.CompanyInfo;
import com.jpsoft.shinestar.modules.base.entity.CompanySupervise;
import com.jpsoft.shinestar.modules.base.service.CompanyInfoService;
import com.jpsoft.shinestar.modules.base.service.CompanySuperviseService;
import com.jpsoft.shinestar.modules.base.service.PersonPopedomService;
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.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 墨鱼_mo
 * @date 2021-1-20 14:50
 */
@Component
@Slf4j
@Transactional
public class CompanySuperviseTask {

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private CompanySuperviseService companySuperviseService;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Scheduled(cron = "0 00 00 ? * *")
    public void run() {
        log.warn("公司每日健康监管初始化数据定时任务开始");


        String recordTime = DateUtil.format(new Date(), "yyyy-MM-dd");

        //公司健康监管数据初始化数据生成
        List<CompanyInfo> companyInfoList = companyInfoService.findByUnitType("1");
        if (companyInfoList.size() > 0) {
            for (CompanyInfo companyInfo1 : companyInfoList) {
                CompanySupervise companySupervise0 = companySuperviseService.findByCompanyIdAndRecordDate(companyInfo1.getId(), recordTime);
                if (companySupervise0 == null) {
                    List<CompanyInfo> companyInfoList1 = companyInfoService.findAllCompanyByCode(companyInfo1.getCode()+"%");
                    Integer personNum = personPopedomService.findNumByCompanyList(companyInfoList1);
                    if (personNum >0){
                        CompanySupervise companySupervise = new CompanySupervise();
                        companySupervise.setId(UUID.randomUUID().toString());
                        companySupervise.setCompanyId(companyInfo1.getId());
                        companySupervise.setTotalNum(personNum);
                        companySupervise.setTotalCheckNum(0);
                        companySupervise.setTotalAbnormalNum(0);
                        companySupervise.setAtOutProvinceNum(0);
                        companySupervise.setAtProvinceNum(0);
                        companySupervise.setLeaveNum(0);
                        companySupervise.setTestRate("0");
                        companySupervise.setRecordTime(DateUtil.format(new Date(), "yyyy-MM-dd"));
                        companySupervise.setDelFlag(false);
                        companySupervise.setCreateTime(new Date());
                        CompanySupervise companySupervise1 = companySuperviseService.findByCompanyIdAndRecordDate(companyInfo1.getId(), recordTime);
                        if (companySupervise1 == null){
                            companySuperviseService.insert(companySupervise);
                        }

                    }
                }
            }


        }

        log.warn("公司每日健康监管初始化数据定时任务结束");
    }

}
