package com.zcloud.util;

import com.zcloud.entity.PageData;
import com.zcloud.service.api.PreventionApiService;
import com.zcloud.service.bus.CheckRecordService;
import com.zcloud.service.bus.CorpInfoService;
import com.zcloud.service.bus.HiddenService;
import com.zcloud.service.filemanager.MfolderService;
import com.zcloud.service.system.CorpScoreService;
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.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class ExamineScheduled {
	@Autowired
	private CorpInfoService corpInfoService;
	@Autowired
	private MfolderService mfolderService;
	@Autowired
	private PreventionApiService preventionApiService;
	@Autowired
	private CheckRecordService checkRecordService;
	@Autowired
	private HiddenService hiddenService;
	@Autowired
	private CorpScoreService corpScoreService;

	@Scheduled(cron ="0 5 2 * * ?")
	@Transactional
    public void scheduled(){
		try {
			System.out.println( "============企业评分==========");
			List<PageData> corpList = corpInfoService.listAll(null);
			Map<String,PageData> corps = listToMap(corpList);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Date date = new Date();
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			PageData pd = new PageData();
			pd.put("MONTH",sdf.format(cal.getTime()));

			//3体系文件填报情况
			List<PageData> score3List = mfolderService.getExamine(null);
			for (PageData score3pd : score3List) {
				double score3 = Integer.parseInt(score3pd.get("count").toString());
				if (score3 > 0.0) score3 = 10.0;
				if(corps.containsKey(score3pd.getString("CORPINFO_ID")))
					corps.get(score3pd.getString("CORPINFO_ID")).put("SCORE3",score3);
			}

			//4风险台账建立情况
			List<PageData> score4List = preventionApiService.getDeptExamine(null);
			for (PageData score4pd : score4List) {
				double maxScore4 = 10.0;
				double allcount = Integer.parseInt(score4pd.get("dept_count").toString());
				double riskcount = Integer.parseInt(score4pd.get("risk_count").toString());
				if(riskcount == 0.0){
					continue;
				}
				double score4 = Math.round(maxScore4 / allcount * riskcount);
				if(corps.containsKey(score4pd.getString("CORPINFO_ID"))) {
					corps.get(score4pd.getString("CORPINFO_ID")).put("SCORE2", 10.0);  //有建立部门 所以给分
					corps.get(score4pd.getString("CORPINFO_ID")).put("SCORE4", score4);
				}
			}

			//5.检查完成率
			List<PageData> score5List = checkRecordService.getCheckExamine(pd);
			for (PageData score5pd : score5List) {
				if(score5pd.get("SHOULD_COUNT").equals(0)){//没有应检查次数,直接零分
					if(corps.containsKey(score5pd.getString("CORPINFO_ID")))
						corps.get(score5pd.getString("CORPINFO_ID")).put("SCORE5", 0.0);
					continue;
				}

				int should_count = Integer.parseInt(score5pd.get("SHOULD_COUNT").toString());
				int not_count = Integer.parseInt(score5pd.get("NOT_COUNT").toString());
				DecimalFormat df=new DecimalFormat("0.00");//设置保留位数
				if(should_count == 0){
					if(corps.containsKey(score5pd.getString("CORPINFO_ID")))
						corps.get(score5pd.getString("CORPINFO_ID")).put("SCORE5", 0.00);
					continue;
				}
				double not_rate = (double) not_count / should_count;
				double check_rate = 1-not_rate;
				double score5 = Math.round(check_rate * 30);
				if(score5 > 30) score5 = 30;

				if(corps.containsKey(score5pd.getString("CORPINFO_ID")))
					corps.get(score5pd.getString("CORPINFO_ID")).put("SCORE5", score5);
			}

			//6.隐患整改率
			List<PageData> score6List = hiddenService.getRectifyExamine(pd);
			for (PageData score6pd : score6List) {
				int hidden_count = Integer.parseInt(score6pd.get("HIDDEN_COUNT").toString());
				int rectify_count = Integer.parseInt(score6pd.get("RECTIFY_COUNT").toString());
				if(hidden_count == 0){//没有隐患,直接零分
					if(corps.containsKey(score6pd.getString("CORPINFO_ID")))
						corps.get(score6pd.getString("CORPINFO_ID")).put("SCORE6", 0.0);
					continue;
				}
				double hidden_rate = (double)rectify_count / hidden_count;
				double score6 = Math.round(hidden_rate * 30);
				if(score6 > 30) score6 = 30;

				if(corps.containsKey(score6pd.getString("CORPINFO_ID")))
					corps.get(score6pd.getString("CORPINFO_ID")).put("SCORE6", score6);
			}

			//  删除本月之前数据
			corpScoreService.deleteAll(pd);
			//	保存
			List<PageData> scores = new ArrayList<PageData>(corps.values());
			corpScoreService.saveBatch(scores);

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

	private Map<String, PageData> listToMap(List<PageData> corpList) {
		Map<String,PageData> corps = new HashMap<String,PageData>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		for (PageData corp : corpList) {
			PageData score = new PageData();
			score.put("CORP_SCORE_ID",UuidUtil.get32UUID());
			score.put("CORPINFO_ID",corp.get("CORPINFO_ID"));
			score.put("EXAMINE_DATE",sdf.format(cal.getTime()));
			score.put("SCORE1",10.0); //企业信息
			score.put("SCORE2",0.0); //岗位落实
			score.put("SCORE3",0.0); //体系文件
			score.put("SCORE4",0.0);	//风险台账建立情况
			score.put("SCORE5",0.0);  //日常检查完成率
			score.put("SCORE6",0.0);  //隐患整改率
			corps.put(corp.getString("CORPINFO_ID"),score);
		}
		return corps;
	}
}
