New file |
| | |
| | | package com.ycl.task; |
| | | |
| | | import com.ycl.platform.domain.entity.CheckResult; |
| | | import com.ycl.platform.domain.entity.ContractResultRecord; |
| | | import com.ycl.platform.domain.entity.ContractScore; |
| | | import com.ycl.platform.domain.entity.TContract; |
| | | import com.ycl.platform.service.ICheckResultService; |
| | | import com.ycl.platform.service.IContractScoreService; |
| | | import com.ycl.platform.service.ITContractService; |
| | | import com.ycl.utils.DateUtils; |
| | | import com.ycl.utils.bean.BeanUtils; |
| | | import lombok.Data; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | 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.List; |
| | | |
| | | /** |
| | | * @author ghl |
| | | * @since 2024/4/28 下午 4:14 |
| | | */ |
| | | |
| | | @Component |
| | | @Data |
| | | public class ContractResultTask { |
| | | |
| | | private final ITContractService contractService; |
| | | private final IContractScoreService contractScoreService; |
| | | private final ICheckResultService checkResultService; |
| | | |
| | | private String startTime; |
| | | private String endTime; |
| | | |
| | | /** |
| | | * 每个月1号执行,统计考核结果 |
| | | */ |
| | | @Scheduled(cron = "0 0 0 1 * ?") |
| | | @Transactional |
| | | // @PostConstruct //启动时执行一次 |
| | | public void contractResultTask() { |
| | | // 查询有效期内的合同 |
| | | List<TContract> list = contractService.selectUsingContract(); |
| | | getLastMonth(); |
| | | for (TContract tContract : list) { |
| | | // 上个月扣分记录 |
| | | List<ContractScore> contractScore = contractScoreService.getDateRangeScoreByContractId(tContract.getId(), startTime, endTime); |
| | | // 新增考核结果 |
| | | CheckResult checkResult = getCheckResult(tContract, contractScore); |
| | | checkResultService.save(checkResult); |
| | | // 新增考核结果记录 |
| | | List<ContractResultRecord> contractResultRecord = getContractResultRecord(contractScore, checkResult); |
| | | checkResultService.saveBatchRecord(contractResultRecord); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 保存考核记录 |
| | | * |
| | | * @param contractScore 扣分 |
| | | * @param checkResult 考核结果 |
| | | * @return 考核记录 |
| | | */ |
| | | private List<ContractResultRecord> getContractResultRecord(List<ContractScore> contractScore, CheckResult checkResult) { |
| | | return contractScore.stream().map( |
| | | item -> { |
| | | ContractResultRecord record = new ContractResultRecord(); |
| | | BeanUtils.copyProperties(item, record); |
| | | record.setResultId(checkResult.getId()); |
| | | record.setCreateTime(DateUtils.getNowDate()); |
| | | return record; |
| | | } |
| | | ).toList(); |
| | | } |
| | | |
| | | /** |
| | | * 计算考核结果 |
| | | * |
| | | * @param tContract 合同 |
| | | * @param contractScore 扣分 |
| | | * @return 考核结果 |
| | | */ |
| | | private CheckResult getCheckResult(TContract tContract, List<ContractScore> contractScore) { |
| | | CheckResult checkResult = new CheckResult(); |
| | | checkResult.setUnitId(tContract.getUnitId()); |
| | | checkResult.setContractId(tContract.getId()); |
| | | checkResult.setPublish(0); |
| | | checkResult.setDeleted("0"); |
| | | checkResult.setCheckTime(DateUtils.getNowDate()); |
| | | checkResult.setScore(contractScore.isEmpty() ? new BigDecimal("100") : new BigDecimal("100").subtract(contractScore.stream().map(ContractScore::getScore).reduce(BigDecimal::add).get())); |
| | | return checkResult; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取上个月的第一天和最后一天 |
| | | */ |
| | | 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; |
| | | } |
| | | |
| | | } |