| | |
| | | package com.ycl.platform.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | import com.baomidou.mybatisplus.extension.service.IService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ycl.exception.job.TaskException; |
| | | import com.ycl.platform.base.BaseSelect; |
| | | import com.ycl.platform.domain.entity.CheckTemplate; |
| | | import com.ycl.platform.domain.entity.CheckTemplateRule; |
| | | import com.ycl.platform.domain.entity.YwUnit; |
| | | import com.ycl.platform.domain.vo.RuleItemVO; |
| | | import com.ycl.platform.domain.query.CheckTemplateQuery; |
| | | import com.ycl.platform.mapper.CheckTemplateMapper; |
| | | import com.ycl.platform.mapper.CheckTemplateRuleMapper; |
| | | import com.ycl.platform.service.CheckTemplateService; |
| | | import com.ycl.platform.service.YwUnitService; |
| | | import com.ycl.platform.service.ICheckTemplateRuleService; |
| | | import com.ycl.platform.service.ICheckTemplateService; |
| | | import com.ycl.system.AjaxResult; |
| | | import com.ycl.system.Result; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ycl.platform.domain.form.CheckTemplateForm; |
| | | import com.ycl.platform.domain.vo.CheckTemplateVO; |
| | | import com.ycl.platform.domain.query.CheckTemplateQuery; |
| | | import java.util.List; |
| | | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.ycl.system.page.PageUtil; |
| | | import com.ycl.utils.DateUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import com.ycl.system.domain.SysJob; |
| | | import com.ycl.system.entity.SysDept; |
| | | import com.ycl.system.service.ISysDeptService; |
| | | import com.ycl.system.service.ISysJobService; |
| | | import com.ycl.utils.SecurityUtils; |
| | | import constant.CheckConstants; |
| | | import org.quartz.SchedulerException; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.Assert; |
| | | import java.util.ArrayList; |
| | | import java.util.Objects; |
| | | import org.springframework.util.CollectionUtils; |
| | | import utils.DateUtils; |
| | | import utils.StringUtils; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | /** |
| | | * 考核模板 服务实现类 |
| | | * 考核模板Service业务层处理 |
| | | * |
| | | * @author xp |
| | | * @since 2024-03-06 |
| | | * @author ruoyi |
| | | * @date 2024-04-01 |
| | | */ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class CheckTemplateServiceImpl extends ServiceImpl<CheckTemplateMapper, CheckTemplate> implements CheckTemplateService { |
| | | |
| | | private final CheckTemplateMapper checkTemplateMapper; |
| | | private final CheckTemplateRuleMapper checkTemplateRuleMapper; |
| | | public class CheckTemplateServiceImpl extends ServiceImpl<CheckTemplateMapper, CheckTemplate> implements ICheckTemplateService { |
| | | @Autowired |
| | | private YwUnitService unitService; |
| | | private CheckTemplateMapper checkTemplateMapper; |
| | | @Autowired |
| | | private ICheckTemplateRuleService templateRuleServicee; |
| | | @Autowired |
| | | private CheckTemplateRuleMapper checkTemplateRuleMapper; |
| | | @Autowired |
| | | private ISysJobService jobService; |
| | | |
| | | /** |
| | | * 添加 |
| | | * @param form |
| | | * @return |
| | | * 查询考核模板 |
| | | * |
| | | * @param id 考核模板主键 |
| | | * @return 考核模板 |
| | | */ |
| | | @Override |
| | | public CheckTemplateQuery selectCheckTemplateById(Integer id) { |
| | | CheckTemplate checkTemplate = checkTemplateMapper.selectCheckTemplateById(id); |
| | | CheckTemplateQuery checkTemplateQuery = new CheckTemplateQuery(); |
| | | BeanUtils.copyProperties(checkTemplate, checkTemplateQuery); |
| | | List<Integer> deptIds = JSONArray.parseArray(checkTemplate.getDeptId(), Integer.class); |
| | | checkTemplateQuery.setDeptId(deptIds) |
| | | .setAdjustCoefficient(checkTemplate.getAdjustCoefficient() + "") |
| | | .setAlarmScore(checkTemplate.getAlarmScore() + ""); |
| | | //查询规则权重 |
| | | List<CheckTemplateRule> templateRuleList = checkTemplateRuleMapper.selectListByTemplateId(checkTemplate.getId()); |
| | | List<Map<String, Object>> list = new ArrayList<>(); |
| | | for (CheckTemplateRule checkTemplateRule : templateRuleList) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("ruleId", checkTemplateRule.getCheckRuleId()); |
| | | map.put("weight", checkTemplateRule.getWeight()); |
| | | list.add(map); |
| | | } |
| | | checkTemplateQuery.setRuleFormList(list); |
| | | |
| | | return checkTemplateQuery; |
| | | } |
| | | |
| | | /** |
| | | * 查询考核模板列表 |
| | | * |
| | | * @param checkTemplateDTO 考核模板 |
| | | * @return 考核模板 |
| | | */ |
| | | @Override |
| | | public List<CheckTemplateQuery> selectCheckTemplateList(CheckTemplateQuery checkTemplateDTO) { |
| | | List<CheckTemplate> checkTemplates = checkTemplateMapper.selectCheckTemplateList(checkTemplateDTO); |
| | | List<CheckTemplateQuery> checkTemplateList = new ArrayList<>(); |
| | | //转换部门id为集合,转换alarmScore为string |
| | | for (CheckTemplate template : checkTemplates) { |
| | | List<Integer> deptIds = JSONArray.parseArray(template.getDeptId(), Integer.class); |
| | | CheckTemplateQuery checkTemplateQuery = new CheckTemplateQuery(); |
| | | BeanUtils.copyProperties(template, checkTemplateQuery); |
| | | checkTemplateQuery.setDeptId(deptIds) |
| | | .setAdjustCoefficient(template.getAdjustCoefficient() + ""); |
| | | checkTemplateList.add(checkTemplateQuery); |
| | | } |
| | | |
| | | return checkTemplateList; |
| | | } |
| | | |
| | | /** |
| | | * 新增考核模板 |
| | | * |
| | | * @param checkTemplateDTO 考核模板 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result add(CheckTemplateForm form) { |
| | | CheckTemplate entity = CheckTemplateForm.getEntityByForm(form, null); |
| | | baseMapper.insert(entity); |
| | | form.getRuleFormList().stream().forEach(rule -> { |
| | | CheckTemplateRule checkTemplateRule = new CheckTemplateRule(); |
| | | checkTemplateRule.setCheckTemplateId(entity.getId()); |
| | | checkTemplateRule.setCheckRuleId(rule.getRuleId()); |
| | | checkTemplateRule.setWeight(rule.getWeight()); |
| | | checkTemplateRuleMapper.insert(checkTemplateRule); |
| | | }); |
| | | return Result.ok("添加成功"); |
| | | public int insertCheckTemplate(CheckTemplateQuery checkTemplateDTO) throws SchedulerException, TaskException { |
| | | String username = SecurityUtils.getUsername(); |
| | | /** 插入t_template */ |
| | | CheckTemplate checkTemplate = new CheckTemplate(); |
| | | BeanUtils.copyProperties(checkTemplateDTO, checkTemplate); |
| | | //从小到大排序 |
| | | List<Integer> deptId = checkTemplateDTO.getDeptId(); |
| | | Collections.sort(deptId); |
| | | Date nowDate = DateUtils.getNowDate(); |
| | | checkTemplate.setDeptId(JSONArray.toJSONString(deptId)) |
| | | .setUpdateUserName(username) |
| | | .setCreateUserName(username) |
| | | .setAdjustCoefficient(new BigDecimal(checkTemplateDTO.getAdjustCoefficient() + "")) |
| | | //不填报警分数---->零分---->不报警 |
| | | .setAlarmScore(new BigDecimal(checkTemplateDTO.getAlarmScore() == null ? "" : checkTemplateDTO.getAlarmScore())); |
| | | int i = checkTemplateMapper.insertCheckTemplate(checkTemplate); |
| | | |
| | | //新增定时任务,状态同模板,然后赋值jobId给template |
| | | SysJob sysJob = addJob(checkTemplateDTO.setId(checkTemplate.getId())); |
| | | checkTemplate.setJobId(Integer.parseInt(sysJob.getJobId() + "")); |
| | | checkTemplateMapper.updateCheckTemplate(checkTemplate); |
| | | |
| | | /** t_template_rule新增权重 */ |
| | | insertTemlpateRule(checkTemplateDTO, checkTemplate); |
| | | |
| | | return i; |
| | | } |
| | | |
| | | /** |
| | | * 修改 |
| | | * @param form |
| | | * @return |
| | | * 复制考核模板 |
| | | * |
| | | * @param checkTemplate 考核模板 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result update(CheckTemplateForm form) { |
| | | |
| | | CheckTemplate entity = baseMapper.selectById(form.getId()); |
| | | // 为空抛IllegalArgumentException,做全局异常处理 |
| | | Assert.notNull(entity, "记录不存在"); |
| | | BeanUtils.copyProperties(form, entity); |
| | | baseMapper.updateById(entity); |
| | | |
| | | // 先删除原先的对应关系 |
| | | new LambdaUpdateChainWrapper<>(checkTemplateRuleMapper) |
| | | .eq(CheckTemplateRule::getCheckTemplateId, form.getId()) |
| | | .remove(); |
| | | // 再重新添加 |
| | | form.getRuleFormList().stream().forEach(rule -> { |
| | | CheckTemplateRule checkTemplateRule = new CheckTemplateRule(); |
| | | checkTemplateRule.setCheckTemplateId(entity.getId()); |
| | | checkTemplateRule.setCheckRuleId(rule.getRuleId()); |
| | | checkTemplateRule.setWeight(rule.getWeight()); |
| | | checkTemplateRuleMapper.insert(checkTemplateRule); |
| | | }); |
| | | return Result.ok("修改成功"); |
| | | public int copyCheckTemplate(CheckTemplateQuery checkTemplateDTO) throws SchedulerException, TaskException { |
| | | //插入模板表 |
| | | CheckTemplate checkTemplate = checkTemplateMapper.selectCheckTemplateById(checkTemplateDTO.getId()); |
| | | String username = SecurityUtils.getUsername(); |
| | | checkTemplate.setUpdateUserName(username) |
| | | .setCreateUserName(username) |
| | | .setStatus(CheckConstants.Status_Stop) |
| | | .setId(null); |
| | | int i = checkTemplateMapper.insertCheckTemplate(checkTemplate); |
| | | //插入template_rule表 |
| | | List<CheckTemplateRule> templateRuleList = checkTemplateRuleMapper.selectListByTemplateId(checkTemplateDTO.getId()); |
| | | templateRuleList.forEach(checkTemplateRule -> checkTemplateRule.setCheckTemplateId(checkTemplate.getId()).setId(null)); |
| | | templateRuleServicee.saveBatch(templateRuleList); |
| | | //新增定时任务,状态同模板,然后赋值jobId给template |
| | | SysJob sysJob = addJob(checkTemplateDTO.setId(checkTemplate.getId())); |
| | | checkTemplate.setJobId(Integer.parseInt(sysJob.getJobId() + "")); |
| | | checkTemplateMapper.updateCheckTemplate(checkTemplate); |
| | | return i; |
| | | } |
| | | |
| | | /** |
| | | * 批量删除 |
| | | * @param ids |
| | | * @return |
| | | * 修改考核模板 |
| | | * |
| | | * @param checkTemplate 考核模板 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result remove(List<String> ids) { |
| | | baseMapper.deleteBatchIds(ids); |
| | | new LambdaUpdateChainWrapper<>(checkTemplateRuleMapper) |
| | | .in(CheckTemplateRule::getCheckTemplateId, ids) |
| | | .remove(); |
| | | return Result.ok("删除成功"); |
| | | public AjaxResult updateCheckTemplate(CheckTemplateQuery checkTemplateDTO) throws SchedulerException, TaskException { |
| | | CheckTemplate checkTemplate = new CheckTemplate(); |
| | | BeanUtils.copyProperties(checkTemplateDTO, checkTemplate); |
| | | checkTemplate.setUpdateUserName(SecurityUtils.getUsername()); |
| | | List<Integer> deptId = checkTemplateDTO.getDeptId(); |
| | | Collections.sort(deptId); |
| | | checkTemplate.setDeptId(JSONArray.toJSONString(deptId)) |
| | | .setAdjustCoefficient(new BigDecimal(checkTemplateDTO.getAdjustCoefficient() + "")); |
| | | checkTemplateMapper.updateCheckTemplate(checkTemplate); |
| | | |
| | | /** t_template_rule修改权重 */ |
| | | if (!CollectionUtils.isEmpty(checkTemplateDTO.getRuleFormList())) { |
| | | //先删除原数据 |
| | | checkTemplateRuleMapper.deleteByTemplateId(checkTemplate.getId()); |
| | | //插入新规则数据 |
| | | insertTemlpateRule(checkTemplateDTO, checkTemplate); |
| | | } |
| | | //调整job表里的状态 |
| | | SysJob job = jobService.selectJobById(Long.valueOf(checkTemplateDTO.getJobId())); |
| | | job.setStatus(checkTemplateDTO.getStatus()); |
| | | jobService.updateJob(job); |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | /** |
| | | * id删除 |
| | | * @param id |
| | | * @return |
| | | * 删除考核模板信息 |
| | | * |
| | | * @param id 考核模板主键 |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result removeById(String id) { |
| | | baseMapper.deleteById(id); |
| | | new LambdaUpdateChainWrapper<>(checkTemplateRuleMapper) |
| | | .eq(CheckTemplateRule::getCheckTemplateId, id) |
| | | .remove(); |
| | | return Result.ok("删除成功"); |
| | | public int deleteCheckTemplateById(Integer id) throws SchedulerException { |
| | | CheckTemplate checkTemplate = checkTemplateMapper.selectCheckTemplateById(id); |
| | | //删除定时任务 |
| | | SysJob job = new SysJob(); |
| | | if (checkTemplate.getJobId() != null) { |
| | | job.setJobId(Long.valueOf(checkTemplate.getJobId())); |
| | | job.setJobGroup("CHECK"); |
| | | jobService.deleteJob(job); |
| | | } |
| | | return checkTemplateMapper.deleteCheckTemplateById(id); |
| | | } |
| | | |
| | | /** |
| | | * 分页查询 |
| | | * @param query |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result page(CheckTemplateQuery query) { |
| | | IPage<CheckTemplate> page = new LambdaQueryChainWrapper<>(baseMapper) |
| | | .like(StringUtils.hasText(query.getTemplateName()), CheckTemplate::getTemplateName, query.getTemplateName()) |
| | | .eq(StringUtils.hasText(query.getStatus()), CheckTemplate::getStatus, query.getStatus()) |
| | | .between(Objects.nonNull(query.getStart()) && Objects.nonNull(query.getEnd()), |
| | | CheckTemplate::getCreateTime, |
| | | DateUtils.getDayStart(query.getStart()), |
| | | DateUtils.getDayEnd(query.getEnd())) |
| | | .orderByDesc(CheckTemplate::getCreateTime) |
| | | .page(PageUtil.getPage(query, CheckTemplate.class)); |
| | | public Result pullList() { |
| | | List<CheckTemplate> checkTemplates = checkTemplateMapper.selectCheckTemplateList(new CheckTemplateQuery()); |
| | | |
| | | List<CheckTemplateVO> vos = page.getRecords().stream() |
| | | .map( |
| | | entity -> CheckTemplateVO.getVoByEntity(entity, null) |
| | | // .setUnitName(unitService.getById(entity.getUnitId()).getUnitName()) |
| | | ) |
| | | .collect(Collectors.toList()); |
| | | return Result.ok().data(vos).total(page.getTotal()); |
| | | return Result.ok().data(checkTemplates); |
| | | } |
| | | |
| | | /** |
| | | * 根据id查找 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result detail(String id) { |
| | | |
| | | CheckTemplate entity = baseMapper.selectById(id); |
| | | Assert.notNull(entity, "记录不存在"); |
| | | CheckTemplateVO vo = CheckTemplateVO.getVoByEntity(entity, null); |
| | | |
| | | // 查考核规则 |
| | | List<CheckTemplateRule> ruleList = new LambdaQueryChainWrapper<>(checkTemplateRuleMapper) |
| | | .eq(CheckTemplateRule::getCheckTemplateId, entity.getId()) |
| | | .list(); |
| | | List<RuleItemVO> ruleVOS = ruleList.stream().map(rule -> { |
| | | RuleItemVO ruleItemVO = new RuleItemVO(); |
| | | ruleItemVO.setRuleId(rule.getCheckRuleId()); |
| | | ruleItemVO.setWeight(rule.getWeight()); |
| | | return ruleItemVO; |
| | | }).collect(Collectors.toList()); |
| | | vo.setRuleFormList(ruleVOS); |
| | | |
| | | return Result.ok().data(vo); |
| | | private void insertTemlpateRule(CheckTemplateQuery checkTemplateDTO, CheckTemplate checkTemplate) { |
| | | List<Map<String, Object>> ruleFormList = checkTemplateDTO.getRuleFormList(); |
| | | if (!CollectionUtils.isEmpty(ruleFormList)) { |
| | | List<CheckTemplateRule> templateRuleList = new ArrayList<>(); |
| | | for (Map<String, Object> map : ruleFormList) { |
| | | templateRuleList.add(new CheckTemplateRule() |
| | | .setCheckRuleId((Integer) map.get("ruleId")) |
| | | .setCheckTemplateId(checkTemplate.getId()) |
| | | .setWeight(new BigDecimal(map.get("weight").toString()))); |
| | | } |
| | | //批量插入数据库 |
| | | templateRuleServicee.saveBatch(templateRuleList); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 列表 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Result all() { |
| | | List<CheckTemplate> entities = baseMapper.selectList(null); |
| | | List<BaseSelect> vos = entities.stream() |
| | | .map( |
| | | entity -> { |
| | | BaseSelect baseSelect = new BaseSelect(); |
| | | baseSelect.setId(entity.getId()); |
| | | baseSelect.setValue(entity.getTemplateName()); |
| | | return baseSelect; |
| | | } |
| | | ) |
| | | .collect(Collectors.toList()); |
| | | return Result.ok().data(vos); |
| | | private SysJob addJob(CheckTemplateQuery checkTemplateDTO) throws SchedulerException, TaskException { |
| | | SysJob job = new SysJob(); |
| | | job.setCreateBy(SecurityUtils.getLoginUser().getUsername()); |
| | | //调用方法名 |
| | | job.setInvokeTarget("checkScoreTask.executeTemplate(" + checkTemplateDTO.getId() + ")"); |
| | | job.setConcurrent("1"); |
| | | job.setStatus(checkTemplateDTO.getStatus()); |
| | | job.setJobGroup("CHECK"); |
| | | job.setCronExpression("0 0 6 * * ?"); |
| | | job.setJobName(checkTemplateDTO.getTemplateName()); |
| | | int i = jobService.insertJob(job); |
| | | return job; |
| | | } |
| | | |
| | | // /** |
| | | // * 批量删除考核模板 |
| | | // * |
| | | // * @param ids 需要删除的考核模板主键 |
| | | // * @return 结果 |
| | | // */ |
| | | // @Override |
| | | // public int deleteCheckTemplateByIds(Integer[] ids) { |
| | | // return checkTemplateMapper.deleteCheckTemplateByIds(ids); |
| | | // } |
| | | } |