package com.ycl.task; import com.ycl.platform.domain.entity.*; import com.ycl.platform.mapper.CalculateRecordMapper; import com.ycl.platform.service.CalculateRecordService; import com.ycl.platform.service.IContractResultService; import com.ycl.platform.service.IContractScoreService; import com.ycl.platform.service.ITContractService; import com.ycl.utils.DateUtils; import com.ycl.utils.bean.BeanUtils; import enumeration.general.CalculateReportStatusEnum; import enumeration.general.PublishType; import lombok.Data; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.List; /** * @author ghl * @since 2024/4/28 下午 4:14 */ @Data @Component("contractResultTask") public class ContractResultTask { private final ITContractService contractService; private final IContractScoreService contractScoreService; private final IContractResultService checkResultService; private final CalculateRecordMapper calculateRecordMapper; private String startTime; private String endTime; /** * 每个月1号执行,统计考核结果 */ @Transactional public void contractResultTask() { // 查询有效期内的合同 List list = contractService.selectUsingContract(); getLastMonth(); for (TContract tContract : list) { // 上个月扣分记录 List contractScore = contractScoreService.getDateRangeScoreByContractId(tContract.getId(), startTime, endTime); // 新增考核结果 //获取上个月时间 Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, -1); ContractResult contractResult = getCheckResult(tContract, contractScore); contractResult.setWhichMonth(calendar.get(Calendar.MONTH)+1); contractResult.setWhichYear(calendar.get(Calendar.YEAR)); checkResultService.save(contractResult); // // 新增考核结果记录 // List contractResultRecord = getContractResultRecord(contractScore, contractResult); // checkResultService.saveBatchRecord(contractResultRecord); // 新增核算明细记录 CalculateRecord calculateRecord = new CalculateRecord(); //改为直接去查 // calculateRecord.setScore(contractResult.getScore()); calculateRecord.setContractId(Integer.parseInt(tContract.getId()+"")); calculateRecord.setCreateTime(new Date()); calculateRecord.setUpdateTime(new Date()); calculateRecord.setStatus(PublishType.UNPUBLISHED.getCode()); calculateRecord.setWhichYear(calendar.get(Calendar.YEAR)); calculateRecord.setWhichMonth(calendar.get(Calendar.MONTH)+1); calculateRecordMapper.insert(calculateRecord); } } /** * 保存考核记录 * * @param contractScore 扣分 * @param contractResult 考核结果 * @return 考核记录 */ private List getContractResultRecord(List contractScore, ContractResult contractResult) { return contractScore.stream().map( item -> { ContractResultRecord record = new ContractResultRecord(); BeanUtils.copyProperties(item, record); record.setResultId(contractResult.getId()); record.setCreateTime(DateUtils.getNowDate()); return record; } ).toList(); } /** * 计算考核结果 * * @param tContract 合同 * @param contractScore 扣分 * @return 考核结果 */ private ContractResult getCheckResult(TContract tContract, List contractScore) { ContractResult contractResult = new ContractResult(); contractResult.setUnitId(tContract.getUnitId()); contractResult.setContractId(tContract.getId()); contractResult.setPublish(0); contractResult.setCheckTime(DateUtils.getNowDate()); //改为直接去查 // contractResult.setScore(contractScore.isEmpty() ? new BigDecimal("100") : new BigDecimal("100").subtract(contractScore.stream().map(ContractScore::getScore).reduce(BigDecimal::add).get())); return contractResult; } /** * 获取上个月的第一天和最后一天 */ public void getLastMonth() { LocalDate currentDate = LocalDate.now().minusMonths(1); LocalDate firstDayOfMonth = currentDate.withDayOfMonth(1); String firstDay = firstDayOfMonth.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); LocalDate lastDayOfMonth = currentDate.withDayOfMonth(currentDate.lengthOfMonth()); String lastDay = lastDayOfMonth.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); this.startTime = firstDay; this.endTime = lastDay; } }