package com.gk.hotwork.Service.ServiceImpl; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.gk.hotwork.Domain.*; import com.gk.hotwork.Domain.Do.SafetySelfInspectionItemQualifiedCountDO; import com.gk.hotwork.Domain.Exception.BusinessException; import com.gk.hotwork.Domain.Utils.StringUtils; import com.gk.hotwork.Mapper.*; import com.gk.hotwork.Service.SafetySelfInspectionService; import org.apache.commons.collections4.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @Service("SafetySelfInspectionService") @Transactional public class SafetySelfInspectionImpl extends ServiceImpl implements SafetySelfInspectionService { @Autowired private SafetySelfInspectionMapper safetySelfInspectionMapper; @Autowired private SafetySelfInspectionItemMapper safetySelfInspectionItemMapper; @Autowired private SafetySelfInspectionItemDeductionMapper safetySelfInspectionItemDeductionMapper; @Autowired private SafetyInspectionItemDeductionMapper safetyInspectionItemDeductionMapper; /** * @Description: 分页 */ @Override public IPage selectPage(Page page, Map filter, UserInfo user) { Integer type = user.getType(); if (!type.equals(1) || !type.equals(2)) { Long companyid = user.getCompanyid(); filter.put("companyid",companyid); } IPage res = safetySelfInspectionMapper.selectPages(page, filter); List records = res.getRecords(); if (CollectionUtils.isNotEmpty(records)){ DecimalFormat df = new DecimalFormat("0.00%"); records = records.stream().map((safetySelfInspection) -> { if (safetySelfInspection.getStatus() == 2) { Long id = safetySelfInspection.getId(); SafetySelfInspectionItemQualifiedCountDO countDO = safetySelfInspectionItemMapper.countQualifiedDataById(id); safetySelfInspection.setUnqualifiedItem(countDO.getItemSum() - countDO.getQualifiedItem()); if (countDO != null && countDO.getItemSum() != 0 && countDO.getItemSum() != null) { BigDecimal rate = new BigDecimal(countDO.getQualifiedItem()) .divide(new BigDecimal(countDO.getItemSum()), 4, BigDecimal.ROUND_HALF_UP); String qualifiedRate = df.format(rate); safetySelfInspection.setQualifiedRate(qualifiedRate); safetySelfInspection.setItemSum(countDO.getItemSum()); } } return safetySelfInspection; }).collect(Collectors.toList()); res.setRecords(records); } return res; } /** * @Description: 新增 */ @Override public void addOne(SafetySelfInspection param, UserInfo user) { requiredVerification(param); Date date = new Date(); String username = user.getRealname(); param.setValidFlag(Boolean.TRUE); param.setUpdateBy(username); param.setCreateBy(username); param.setUpdateTime(date); param.setCreateTime(date); this.save(param); if (CollectionUtils.isNotEmpty(param.getItemList())) { for (SafetySelfInspectionItem safetySelfInspectionItem : param.getItemList()) { safetySelfInspectionItem.setValidFlag(Boolean.TRUE); safetySelfInspectionItem.setUpdateBy(username); safetySelfInspectionItem.setCreateBy(username); safetySelfInspectionItem.setUpdateTime(date); safetySelfInspectionItem.setCreateTime(date); safetySelfInspectionItem.setSafetySelfInspectionId(param.getId()); safetySelfInspectionItemMapper.insert(safetySelfInspectionItem); } } } /** * @Description: 修改 */ @Override public void modOne(SafetySelfInspection param, UserInfo user) { selectVerification(param.getId()); requiredVerification(param); //更新主表 Date date = new Date(); String username = user.getRealname(); param.setUpdateTime(date); param.setUpdateBy(username); this.updateById(param); //更新检查项表 //1.新增新的元素 //2.删除旧的不存在的元素 List oldFileList = safetySelfInspectionItemMapper.getBySafetySelfInspectionId(param.getId()); List newFileList = param.getItemList(); List oldIdList = new ArrayList<>(); List newIdList = new ArrayList<>(); for (SafetySelfInspectionItem oldSafetySelfInspectionItem :oldFileList ){ oldIdList.add(oldSafetySelfInspectionItem.getId()); } for (SafetySelfInspectionItem newSafetySelfInspectionItem :newFileList ){ if (newSafetySelfInspectionItem.getId()==null){ //1.添加新增的元素 newSafetySelfInspectionItem.setValidFlag(Boolean.TRUE); newSafetySelfInspectionItem.setUpdateBy(username); newSafetySelfInspectionItem.setCreateBy(username); newSafetySelfInspectionItem.setUpdateTime(date); newSafetySelfInspectionItem.setCreateTime(date); newSafetySelfInspectionItem.setSafetySelfInspectionId(param.getId()); safetySelfInspectionItemMapper.insert(newSafetySelfInspectionItem); }else{ newIdList.add(newSafetySelfInspectionItem.getId()); } } //2.删除不存在的元素 List diffList = getDif(oldIdList,newIdList); if (CollectionUtils.isNotEmpty(diffList)){ for (Long id : diffList){ safetySelfInspectionItemMapper.deleteById(id,username,date); } } } public List getDif(List oldIdList , List newIdList ){ ArrayList dif = new ArrayList<>(); //查找出oldIdList表中不包含newIdList的元素 for (Long id : oldIdList) { if (!(newIdList.contains(id))) { dif.add(id); } } return dif; } /** * @Description: 删除 */ @Override public void delOne(Long id, UserInfo user) { selectVerification(id); SafetySelfInspection delOne = new SafetySelfInspection(); delOne.setId(id); delOne.setUpdateTime(new Date()); delOne.setUpdateBy(user.getRealname()); delOne.setValidFlag(Boolean.FALSE); this.updateById(delOne); } @Override public SafetySelfInspection infoOne(Long id,String unqualified, UserInfo user) { SafetySelfInspection safetySelfInspection=selectVerification(id); //组装检查项 List itemList= safetySelfInspectionItemMapper.getDetailBySafetySelfInspectionId(id,unqualified); if(CollectionUtils.isNotEmpty(itemList)){ //组装扣分项 for (SafetySelfInspectionItem safetySelfInspectionItem : itemList){ if (safetySelfInspectionItem.getSafetyInspectionItemResult()==null){ //根据检查项id获取原始扣分项 List safetyInspectionItemDeductionList = safetyInspectionItemDeductionMapper.getBySafetyInspectionItemId(safetySelfInspectionItem.getSafetyInspectionItemId()); if (CollectionUtils.isNotEmpty(safetyInspectionItemDeductionList)){ List oldSafetySelfInspectionItemDeductionList=new ArrayList<>(); for (SafetyInspectionItemDeduction safetyInspectionItemDeduction : safetyInspectionItemDeductionList){ SafetySelfInspectionItemDeduction safetySelfInspectionItemDeduction = new SafetySelfInspectionItemDeduction(); safetySelfInspectionItemDeduction.setSafetyInspectionItemDeductionId(safetyInspectionItemDeduction.getId()); safetySelfInspectionItemDeduction.setName(safetyInspectionItemDeduction.getName()); safetySelfInspectionItemDeduction.setPoint(0); oldSafetySelfInspectionItemDeductionList.add(safetySelfInspectionItemDeduction); } safetySelfInspectionItem.setSelfDeductionList(oldSafetySelfInspectionItemDeductionList); }else{ safetySelfInspectionItem.setSelfDeductionList(new ArrayList<>()); } }else if (safetySelfInspectionItem.getSafetyInspectionItemResult().equals(1)){ //根据自查清单记录的检查项id获取其扣分项 List selfDeductionList = safetySelfInspectionItemDeductionMapper.getBySafetySelfInspectionItemId(safetySelfInspectionItem.getId()); if (CollectionUtils.isNotEmpty(selfDeductionList)){ safetySelfInspectionItem.setSelfDeductionList(selfDeductionList); } } } safetySelfInspection.setItemList(itemList); }else{ safetySelfInspection.setItemList(new ArrayList<>()); } return safetySelfInspection; } @Override public SafetySelfInspectionItem itemInfoOne(Long id, UserInfo user){ SafetySelfInspectionItem safetySelfInspectionItem = safetySelfInspectionItemMapper.getDetailById(id); List selfDeductionList = safetySelfInspectionItemDeductionMapper.getBySafetySelfInspectionItemId(safetySelfInspectionItem.getId()); if (CollectionUtils.isNotEmpty(selfDeductionList)){ safetySelfInspectionItem.setSelfDeductionList(selfDeductionList); }else{ //根据自查清单检查项里的检查项表去找原有的扣分项 List safetyInspectionItemDeductionList = safetyInspectionItemDeductionMapper.getBySafetyInspectionItemId(safetySelfInspectionItem.getSafetyInspectionItemId()); if (CollectionUtils.isNotEmpty(safetyInspectionItemDeductionList)){ List oldSafetySelfInspectionItemDeductionList=new ArrayList<>(); for (SafetyInspectionItemDeduction safetyInspectionItemDeduction : safetyInspectionItemDeductionList){ SafetySelfInspectionItemDeduction safetySelfInspectionItemDeduction = new SafetySelfInspectionItemDeduction(); safetySelfInspectionItemDeduction.setSafetyInspectionItemDeductionId(safetyInspectionItemDeduction.getId()); safetySelfInspectionItemDeduction.setName(safetyInspectionItemDeduction.getName()); safetySelfInspectionItemDeduction.setPoint(0); oldSafetySelfInspectionItemDeductionList.add(safetySelfInspectionItemDeduction); } safetySelfInspectionItem.setSelfDeductionList(oldSafetySelfInspectionItemDeductionList); }else{ safetySelfInspectionItem.setSelfDeductionList(new ArrayList<>()); } } return safetySelfInspectionItem; } @Override public void modItemInfo(SafetySelfInspectionItem param, UserInfo user) { if (param.getSafetyInspectionItemResult()==0 && StringUtils.isBlank(param.getSafetyInspectionItemResultDesc())){ throw new BusinessException("否决说明必填!"); } Date date = new Date(); String username = user.getRealname(); param.setUpdateTime(date); param.setUpdateBy(username); safetySelfInspectionItemMapper.updateById(param); if (param.getSafetyInspectionItemResult()==0 || param.getSafetyInspectionItemResult() == 2){ //否决 合格--删除扣分记录 safetySelfInspectionItemDeductionMapper.delBySafetySelfInspectionItemId(param.getId(),username,date); }else{ //扣分 List list = safetySelfInspectionItemDeductionMapper.getBySafetySelfInspectionItemId(param.getId()); if (CollectionUtils.isEmpty(list)){ //新增扣分记录 for (SafetySelfInspectionItemDeduction safetySelfInspectionItemDeduction : param.getSelfDeductionList()){ safetySelfInspectionItemDeduction.setValidFlag(Boolean.TRUE); safetySelfInspectionItemDeduction.setUpdateBy(username); safetySelfInspectionItemDeduction.setCreateBy(username); safetySelfInspectionItemDeduction.setUpdateTime(date); safetySelfInspectionItemDeduction.setCreateTime(date); safetySelfInspectionItemDeduction.setSafetySelfInspectionItemId(param.getId()); safetySelfInspectionItemDeduction.setSafetyInspectionItemDeductionId(safetySelfInspectionItemDeduction.getSafetyInspectionItemDeductionId()); safetySelfInspectionItemDeductionMapper.insert(safetySelfInspectionItemDeduction); } }else{ //更新扣分记录 for (SafetySelfInspectionItemDeduction safetySelfInspectionItemDeduction : param.getSelfDeductionList()){ safetySelfInspectionItemDeduction.setUpdateBy(username); safetySelfInspectionItemDeduction.setUpdateTime(date); safetySelfInspectionItemDeductionMapper.updateById(safetySelfInspectionItemDeduction); } } } } @Override public void finish(Long id, UserInfo user) { SafetySelfInspection safetySelfInspection=selectVerification(id); Date date = new Date(); String username = user.getRealname(); safetySelfInspection.setStatus(2); safetySelfInspection.setUpdateTime(date); safetySelfInspection.setUpdateBy(username); safetySelfInspectionMapper.updateById(safetySelfInspection); } /** * 查询验证 * 验证对象存在 */ public SafetySelfInspection selectVerification(Long id) { if (id == null) throw new BusinessException("id传参不能为空"); SafetySelfInspection SafetySelfInspection = this.getById(id); if (SafetySelfInspection == null) throw new BusinessException("找不到对应实体"); return SafetySelfInspection; } /** * 操作验证 * 验证必填项 */ public void requiredVerification(SafetySelfInspection param) { if (StringUtils.isBlank(param.getInspectionName())) throw new BusinessException("请填写自查清单名称"); if (param.getInspectionTime() == null) throw new BusinessException("请选择自查时间"); if (param.getInspector() == null) throw new BusinessException("请选择自查人员"); if (CollectionUtils.isEmpty(param.getItemList())) { throw new BusinessException("请选择检查项"); } if (param.getCheckedCompanyId() == null){ throw new BusinessException("请选择被检查公司"); } } }