package org.springblade.modules.byMonth.task;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.modules.byMonth.entity.ByMonthEntity;
import org.springblade.modules.byMonth.service.IByMonthService;
import org.springblade.modules.byMonth.vo.ByMonthVO;
import org.springblade.modules.gzInfo.service.IInfoService;
import org.springblade.modules.khsdPcs.service.IKhsdPcsService;
import org.springblade.modules.pfInfo.service.PfInfoService;
import org.springblade.modules.pfb.service.IPfbService;
import org.springblade.modules.sign.service.ISignService;
import org.springblade.modules.xpgxb.entity.XpgxbEntity;
import org.springblade.modules.xpgxb.service.IXpgxbService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 考勤 定时任务
 */
@Configuration
@EnableScheduling
@AllArgsConstructor
public class PfqrTask extends BladeController {

	private final IPfbService pfbService;
	private final ISignService signService;
	private final IInfoService gzInfoService;
	private final IXpgxbService xpgxbService;
	private final PfInfoService pfInfoService;
	private final IByMonthService byMonthService;
	private final IKhsdPcsService khsdPcsService;


	/**
	 * 每月最后一天 自动计算所有警员当月评分
	 */
//	@Scheduled(cron = "0 0 0 L 1/1 ? ")
//	@Scheduled(cron = "0/50 * * * * ? ")
	public void autoCalculateScore() {
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM");
		Calendar lastMonth = Calendar.getInstance();
		lastMonth.setTime(now);
		lastMonth.add(Calendar.MONTH, -1); // 上个月
		Calendar thisMonth = Calendar.getInstance();
		thisMonth.setTime(now); // 本月

		ByMonthEntity byMonthEntity;
		Map<String, Object> params = new HashMap<>();
		IPage<XpgxbEntity> pages = xpgxbService.selectXpgxbPage(Condition.getPage(new Query()), params);
		for (XpgxbEntity xpgxbEntity : pages.getRecords()) {
			byMonthEntity = new ByMonthEntity();
			byMonthEntity.setUserId(xpgxbEntity.getUserId());
			byMonthEntity.setDeptId(Long.parseLong(xpgxbEntity.getXppcsdm()));
			byMonthEntity.setSjhm(xpgxbEntity.getXpmjlxdh());
			byMonthEntity.setYf(sd2.format(thisMonth.getTime()));
			// 日常管理 {"党支部活动", "四联四促活动"}
			// 党建工作 = 上月手评 - 本月扣分
			params.put("sjhm", xpgxbEntity.getXpmjlxdh());
			params.put("yf", sd2.format(lastMonth.getTime()));
			params.put("userId", xpgxbEntity.getUserId());
			// 上月各项得分 sjhm yf pfxId
			ByMonthVO byMonthVO = pfbService.getPf(params);
			if (null == byMonthVO) {
				byMonthVO = new ByMonthVO();
			}
			// 党支部活动本月需要参加的次数 sjhm yf pfxId
			params.put("pfxId", "1677929593842249729");
			params.put("yf", sdf.format(thisMonth.getTime()));
			Integer dzbhdKhcs = khsdPcsService.getKhcs(params);
			if (null == dzbhdKhcs) {
				dzbhdKhcs = 0;
			}
			// 党支部活动本月实际参加次数 userId yf pfx
			params.put("pfx", "党支部活动");
			params.put("yf", sdf.format(thisMonth.getTime()));
			int time = gzInfoService.getDzbhdCs(params);
			// 少参加一次减0.5
			String dzbhdBykf = String.valueOf(time >= dzbhdKhcs ? 0 : (-(dzbhdKhcs - time) * 0.5));

			// 四联四促活动本月需要参加的次数 sjhm yf pfxId
			params.put("pfxId", "1677929940627304450");
			params.put("yf", sdf.format(thisMonth.getTime()));
			Integer slscKhcs = khsdPcsService.getKhcs(params);
			if (null == slscKhcs) {
				slscKhcs = 0;
			}
			// 四联四促活动本月实际参加次数 userId yf pfx
			params.put("pfx", "四联四促活动");
			params.put("yf", sdf.format(thisMonth.getTime()));
			int slscTime = gzInfoService.getDzbhdCs(params);
			// 少参加一次减0.5
			String slscBykf = String.valueOf(slscTime >= slscKhcs ? 0 : (-(slscKhcs - slscTime) * 0.5));
			double djgzFs = Double.valueOf(StringUtils.isBlank(byMonthVO.getDjgzFs()) ? "10" : byMonthVO.getDjgzFs()) + Double.valueOf(dzbhdBykf) + Double.valueOf(slscBykf);
			byMonthEntity.setDjgzFs(String.valueOf(djgzFs));

			// 出勤考勤 = 上月手评 - 本月扣分
			params.put("pfxId", "1677929132670136321");
			params.put("yf", sdf.format(lastMonth.getTime()));
			// 旷工次数
			params.put("type", 3);
			params.put("yf", sdf.format(thisMonth.getTime()));
			int kgTime = signService.getKgCs(params);
			// 一日减0.5
			String kgBykf = String.valueOf(-kgTime * 0.5);
			// 请假次数
			params.put("type", 4);
			params.put("yf", sdf.format(thisMonth.getTime()));
			int qjTime = signService.getKgCs(params);
			// 一日减0.2
			String qjBykf = String.valueOf(-qjTime * 0.2);
			double cqkqFs = Double.valueOf(StringUtils.isBlank(byMonthVO.getCqkqFs()) ? "10" : byMonthVO.getCqkqFs()) + Double.valueOf(kgBykf) + Double.valueOf(qjBykf);
			byMonthEntity.setCqkqFs(String.valueOf(cqkqFs));

			// 工作纪律 {"不服从派出所工作安排和管理", "群众有理投诉或被上级部门通报"}
			// 工作纪律上月得分 sjhm yf pfxId
			params.put("pfxId", "1677930693983023105");
			params.put("yf", sdf.format(lastMonth.getTime()));
			// 不服从派出所工作安排和管理 次数
			params.put("pfxId", "1677930781652365313");
			params.put("yf", sdf.format(thisMonth.getTime()));
			int bfcTime = pfInfoService.getDzbhdCs(params);
			// 一次减0.5
			String bfcBykf = String.valueOf(-bfcTime * 0.5);
			// 群众有理投诉或被上级部门通报 次数
			params.put("pfxId", "1677930845296734209");
			params.put("yf", sdf.format(thisMonth.getTime()));
			int qztsTime = pfInfoService.getDzbhdCs(params);
			// 一次减0.5
			String qztsBykf = String.valueOf(-qztsTime * 0.5);
			double gzjlFs = Double.valueOf(StringUtils.isBlank(byMonthVO.getGzjlFs()) ? "10" : byMonthVO.getGzjlFs()) + Double.valueOf(bfcBykf) + Double.valueOf(qztsBykf);
			byMonthEntity.setGzjlFs(String.valueOf(gzjlFs));


			byMonthService.save(byMonthEntity);
		}
	}
}
