zf
2023-08-30 9ca0dc6e7833715b875cc9b6b91d5c497b2bae8b
src/main/java/com/gk/hotwork/Service/ServiceImpl/SafetySelfInspectionImpl.java
@@ -1,14 +1,20 @@
package com.gk.hotwork.Service.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.CompanyStatisticInspectionDO;
import com.gk.hotwork.Domain.Do.SafetySelfInspectionItemQualifiedCountDO;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.Utils.BeanUtils;
import com.gk.hotwork.Domain.Utils.StringUtils;
import com.gk.hotwork.Domain.Vo.UserVo;
import com.gk.hotwork.Domain.dto.resp.SafetySelfInspectionElementRespDTO;
import com.gk.hotwork.Domain.dto.resp.SafetySelfInspectionRespDTO;
import com.gk.hotwork.Mapper.*;
import com.gk.hotwork.Service.SafetySelfInspectionService;
import com.gk.hotwork.Service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -34,32 +40,57 @@
    private SafetySelfInspectionItemDeductionMapper safetySelfInspectionItemDeductionMapper;
    @Autowired
    private SafetyInspectionItemDeductionMapper safetyInspectionItemDeductionMapper;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private ElementManagementService elementManagementService;
    @Autowired
    private SafetyInspectionElementAService safetyInspectionElementAService;
    @Autowired
    private UserService userService;
    @Autowired
    private InspectionExpertGroupService expertGroupService;
    @Autowired
    private InspectionExpertService expertService;
    @Autowired
    private InspectionHiddenDangerService dangerService;
    @Autowired
    private AttachmentInfoService attachmentInfoService;
    /**
     * @Description: 分页
     * @Description: 自查分页
     */
    @Override
    public IPage<SafetySelfInspection> selectPage(Page<SafetySelfInspection> page, Map<String, Object> filter, UserInfo user) {
        Integer type = user.getType();
        if (!type.equals(1) || !type.equals(2)) {
        //企业用户
        if (type.equals(3)) {
            Long companyid = user.getCompanyid();
            filter.put("companyid",companyid);
            filter.put("checkedCompanyId",companyid);
        }
        //监管用户
        if (type.equals(2)) {
            //获取企业信息
            filter.put("province",user.getProvince());
            filter.put("city",user.getCity());
            filter.put("area",user.getCounty());
        }
        filter.put("flag",(byte)2);
        IPage<SafetySelfInspection> res = safetySelfInspectionMapper.selectPages(page, filter);
        List<SafetySelfInspection> 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());
                    }
                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());
@@ -68,25 +99,93 @@
        return res;
    }
    /**
     * @Description: 监管检查分页
     */
    @Override
    public IPage<SafetySelfInspection> selectSupervisePage(Page<SafetySelfInspection> page, Map<String, Object> filter, UserInfo user) {
        Integer type = user.getType();
        //普通用户
        if (type.equals(3)) {
            Long companyid = user.getCompanyid();
            filter.put("checkedCompanyId",companyid);
        }
        //监管用户
        if (type.equals(2)) {
            //获取企业信息
            filter.put("province",user.getProvince());
            filter.put("city",user.getCity());
            filter.put("area",user.getCounty());
        }
        filter.put("flag",(byte)1);
        IPage<SafetySelfInspection> res = safetySelfInspectionMapper.selectPages(page, filter);
        List<SafetySelfInspection> records = res.getRecords();
        if (CollectionUtils.isNotEmpty(records)){
            DecimalFormat df = new DecimalFormat("0.00%");
            records = records.stream().map((safetySelfInspection) -> {
                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;
    }
    //累计过出不合格扣分
    private Integer unqualifiedItemDelScore(List<SafetySelfInspectionItem> itemList){
        int delScore = 0;
        //过滤出不合格
        List<SafetySelfInspectionItem> unqualifiedItemList = itemList
                .stream()
                .filter(item -> item.getSafetyInspectionItemResult().equals(1))
                .collect(Collectors.toList());
        for (SafetySelfInspectionItem unqualifiedItem : unqualifiedItemList){
            List<SafetySelfInspectionItemDeduction> itemDeductionList = safetySelfInspectionItemDeductionMapper
                    .selectList(new LambdaQueryWrapper<SafetySelfInspectionItemDeduction>()
                            .eq(SafetySelfInspectionItemDeduction::getSafetySelfInspectionItemId, unqualifiedItem.getId())
                            .eq(SafetySelfInspectionItemDeduction::getValidFlag,1));
            for (SafetySelfInspectionItemDeduction itemDeduction : itemDeductionList) {
                delScore += itemDeduction.getPoint();
            }
        }
        return delScore;
    }
    /**
     * @Description: 新增
     */
    @Override
    @Transactional
    public void addOne(SafetySelfInspection param, UserInfo user) {
        requiredVerification(param);
        Date date = new Date();
        String username = user.getRealname();
        param.setValidFlag(Boolean.TRUE);
        param.setFlag((byte) 2);
        param.setUpdateBy(username);
        param.setCreateBy(username);
        param.setUpdateTime(date);
        param.setCreateTime(date);
        param.setCheckUnit(user.getCompany());
        this.save(param);
        if (CollectionUtils.isNotEmpty(param.getItemList())) {
            for (SafetySelfInspectionItem safetySelfInspectionItem : param.getItemList()) {
                //默认合格
                safetySelfInspectionItem.setSafetyInspectionItemResult(2);
                safetySelfInspectionItem.setValidFlag(Boolean.TRUE);
                safetySelfInspectionItem.setUpdateBy(username);
                safetySelfInspectionItem.setCreateBy(username);
@@ -94,6 +193,94 @@
                safetySelfInspectionItem.setCreateTime(date);
                safetySelfInspectionItem.setSafetySelfInspectionId(param.getId());
                safetySelfInspectionItemMapper.insert(safetySelfInspectionItem);
            }
        }
        if(CollectionUtils.isNotEmpty(param.getElementAList())){
            for (SafetyInspectionElementA inspectionElementA : param.getElementAList()) {
                SafetyInspectionElementA safetyInspectionElementA = new SafetyInspectionElementA();
                safetyInspectionElementA.setSafetyInspectionId(param.getId());
                safetyInspectionElementA.setElementA(inspectionElementA.getElementA());
                safetyInspectionElementAService.save(safetyInspectionElementA);
            }
        }
    }
    /**
     * 监管检查-新增
     * @param param
     * @param user
     */
    @Transactional
    @Override
    public void addSupervise(SafetySelfInspection param, UserInfo user) {
        requiredSuperviseVerification(param);
        Date date = new Date();
        String username = user.getRealname();
        param.setValidFlag(Boolean.TRUE);
        param.setFlag((byte) 1);
        param.setInspector(user.getId());
        param.setUpdateBy(username);
        param.setCreateBy(username);
        param.setUpdateTime(date);
        param.setCreateTime(date);
        param.setCheckUnit(user.getCompany());
        this.save(param);
        //生成专家组记录
        InspectionExpertGroup inspectionExpertGroup = new InspectionExpertGroup();
        inspectionExpertGroup.setInspectionName(param.getInspectionName());
        inspectionExpertGroup.setInspectionTime(param.getInspectionTime());
        inspectionExpertGroup.setCheckedCompanyId(param.getCheckedCompanyId());
        inspectionExpertGroup.setCheckedCompanyName(param.getCheckedCompanyName());
        inspectionExpertGroup.setCheckUnit(user.getCompany());
        inspectionExpertGroup.setSelfInspectionId(param.getId());
        inspectionExpertGroup.setValidFlag(true);
        inspectionExpertGroup.setCreateTime(new Date());
        List<InspectionExpert> collect = param.getExpertList().stream().filter(inspectionExpert -> inspectionExpert.getIsLeader() == true).collect(Collectors.toList());
        if (collect.size() > 0) {
            UserVo userVo = userService.selectUserVoById(collect.get(0).getExpertId());
            inspectionExpertGroup.setExpertLeader(userVo.getRealname());
        }
        expertGroupService.save(inspectionExpertGroup);
        if (CollectionUtils.isNotEmpty(param.getItemList())) {
            for (SafetySelfInspectionItem safetySelfInspectionItem : param.getItemList()) {
                //默认合格
                safetySelfInspectionItem.setSafetyInspectionItemResult(2);
                safetySelfInspectionItem.setValidFlag(Boolean.TRUE);
                safetySelfInspectionItem.setUpdateBy(username);
                safetySelfInspectionItem.setCreateBy(username);
                safetySelfInspectionItem.setUpdateTime(date);
                safetySelfInspectionItem.setCreateTime(date);
                safetySelfInspectionItem.setSafetySelfInspectionId(param.getId());
                safetySelfInspectionItemMapper.insert(safetySelfInspectionItem);
            }
        }
        if(CollectionUtils.isNotEmpty(param.getElementAList())){
            for (SafetyInspectionElementA inspectionElementA : param.getElementAList()) {
                SafetyInspectionElementA safetyInspectionElementA = new SafetyInspectionElementA();
                safetyInspectionElementA.setSafetyInspectionId(param.getId());
                safetyInspectionElementA.setElementA(inspectionElementA.getElementA());
                safetyInspectionElementAService.save(safetyInspectionElementA);
            }
        }
        if(CollectionUtils.isNotEmpty(param.getExpertList())){
            for (InspectionExpert inspectionExpert : param.getExpertList()) {
                UserVo userVo = userService.selectUserVoById(inspectionExpert.getExpertId());
                inspectionExpert.setCompanyName(userVo.getCompany());
                inspectionExpert.setIdcard(userVo.getIdcard());
                inspectionExpert.setName(userVo.getRealname());
                inspectionExpert.setPhone(userVo.getUsername());
                inspectionExpert.setType(userVo.getType());
                inspectionExpert.setProfessionalLevel(user.getProfessionalLevel());
                inspectionExpert.setSpecialityName(userVo.getSpecialityName());
                inspectionExpert.setSelfInspectionId(param.getId());
                inspectionExpert.setExpertGropId(inspectionExpertGroup.getId());
                expertService.save(inspectionExpert);
            }
        }
    }
@@ -160,6 +347,7 @@
    /**
     * @Description: 删除
     */
    @Transactional
    @Override
    public void delOne(Long id, UserInfo user) {
        selectVerification(id);
@@ -170,9 +358,15 @@
        delOne.setUpdateBy(user.getRealname());
        delOne.setValidFlag(Boolean.FALSE);
        this.updateById(delOne);
        InspectionExpertGroup expertGroup = expertGroupService.getBySelfInspectionId(id);
        if(expertGroup != null){
            expertGroup.setValidFlag(false);
            expertGroupService.updateById(expertGroup);
        }
    }
    @Override
  /*  @Override
    public SafetySelfInspection infoOne(Long id,String unqualified, UserInfo user) {
        SafetySelfInspection safetySelfInspection=selectVerification(id);
        //组装检查项
@@ -209,12 +403,172 @@
            safetySelfInspection.setItemList(new ArrayList<>());
        }
        return safetySelfInspection;
    }*/
    @Override
    public SafetySelfInspectionRespDTO infoOne(Long id, String unqualified, UserInfo user) {
        SafetySelfInspection safetySelfInspection = selectVerification(id);
        SafetySelfInspectionRespDTO safetySelfInspectionRespDTO = new SafetySelfInspectionRespDTO();
        BeanUtils.copyProperties(safetySelfInspection,safetySelfInspectionRespDTO);
        //获取所有检查项
        List<SafetySelfInspectionItem> itemList= safetySelfInspectionItemMapper.getDetailBySafetySelfInspectionId(id,unqualified);
        //获取要素树
        List<ElementTree> elementTreeList = elementManagementService.getMenuType1Tree();
        //获取附件
        List<AttachmentInfo> byBusinessId = attachmentInfoService.findByBusinessId(id);
        safetySelfInspectionRespDTO.setAttachmentList(byBusinessId);
        List<SafetySelfInspectionElementRespDTO> arespDTOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(elementTreeList)) {
            for (ElementTree elementA : elementTreeList) {
                List<SafetySelfInspectionItem> selectElementList = itemList
                        .stream()
                        .filter(item -> item.getElementA() != null &&
                        item.getElementA().equals(elementA.getValue()))
                        .collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(selectElementList)) {
                    SafetySelfInspectionElementRespDTO elementARespDTO = new SafetySelfInspectionElementRespDTO();
                    elementARespDTO.setElementId(elementA.getValue());
                    elementARespDTO.setElementName(elementA.getLabel());
                    elementARespDTO.setType(elementA.getType());
                    List<SafetySelfInspectionElementRespDTO> brespDTOList = new ArrayList<>();
                    //二级要素
                    List<ElementTree> elementBList = elementA.getChildren();
                    if(CollectionUtils.isNotEmpty(elementBList)){
                        for (ElementTree stree : elementBList) {
                            SafetySelfInspectionElementRespDTO elementBRespDTO = new SafetySelfInspectionElementRespDTO();
                            elementBRespDTO.setElementId(stree.getValue());
                            elementBRespDTO.setElementName(stree.getLabel());
                            elementBRespDTO.setType(stree.getType());
                            elementBRespDTO.setPid(stree.getPid());
                            List<SafetySelfInspectionElementRespDTO> cRespDTOList = new ArrayList<>();
                            //三级要素
                            List<ElementTree> elementCList = stree.getChildren();
                            if(CollectionUtils.isEmpty(elementCList)){
                                //扣分
                                int delScore = 0;
                                int totalScore = stree.getPoint();
                                //无三级要素过滤出二级要素检查内容
                                List<SafetySelfInspectionItem> sItemList = itemList
                                        .stream()
                                        .filter(item -> item.getElementC() == null
                                                && item.getElementB() != null
                                                && item.getElementB().equals(stree.getValue()))
                                        .collect(Collectors.toList());
                                //填充检查项
                                elementBRespDTO.setItemList(assemblyDeduction(sItemList));
                                //判断是否有否决项
                                List<SafetySelfInspectionItem> rejectList = sItemList
                                        .stream()
                                        .filter(item -> item.getSafetyInspectionItemResult().equals(0))
                                        .collect(Collectors.toList());
                                if(rejectList.size() > 0){
                                    delScore = stree.getPoint();
                                }else {
                                    //计算出不合格扣分
                                    delScore += unqualifiedItemDelScore(sItemList);
                                }
                                //总分
                                elementBRespDTO.setTotalScore(stree.getPoint());
                                //得分
                                elementBRespDTO.setScore(totalScore-delScore);
                            }else {
                                for(ElementTree ttree : elementCList){
                                    SafetySelfInspectionElementRespDTO elementCRespDTO = new SafetySelfInspectionElementRespDTO();
                                    elementCRespDTO.setElementId(ttree.getValue());
                                    elementCRespDTO.setElementName(ttree.getLabel());
                                    elementCRespDTO.setType(ttree.getType());
                                    elementCRespDTO.setPid(ttree.getPid());
                                    //扣分
                                    int delScore = 0;
                                    int totalScore = ttree.getPoint();
                                    //过滤该三级要素检查项目
                                    List<SafetySelfInspectionItem> tItemList = itemList
                                            .stream()
                                            .filter(item -> item.getElementC() != null && item.getElementC().equals(ttree.getValue()))
                                            .collect(Collectors.toList());
                                    //填充检查项
                                    elementCRespDTO.setItemList(assemblyDeduction(tItemList));
                                    //判断是否有被否决
                                    List<SafetySelfInspectionItem> rejectList = tItemList
                                            .stream()
                                            .filter(item -> item.getSafetyInspectionItemResult().equals(0))
                                            .collect(Collectors.toList());
                                    if(rejectList.size() > 0){
                                        delScore = ttree.getPoint();
                                    }else {
                                        //计算出不合格扣分
                                        delScore = unqualifiedItemDelScore(tItemList);
                                    }
                                    //总分
                                    elementCRespDTO.setTotalScore(ttree.getPoint());
                                    //得分
                                    elementCRespDTO.setScore(totalScore-delScore);
                                    cRespDTOList.add(elementCRespDTO);
                                }
                            }
                            elementBRespDTO.setChildren(cRespDTOList);
                            brespDTOList.add(elementBRespDTO);
                        }
                    }
                    elementARespDTO.setChildren(brespDTOList);
                    arespDTOList.add(elementARespDTO);
                }
            }
        }
        safetySelfInspectionRespDTO.setElementList(arespDTOList);
        return safetySelfInspectionRespDTO;
    }
    //组装扣分项
    private List<SafetySelfInspectionItem> assemblyDeduction( List<SafetySelfInspectionItem> itemList){
        for (SafetySelfInspectionItem safetySelfInspectionItem : itemList){
            if (safetySelfInspectionItem.getSafetyInspectionItemResult().equals(2)){
                //根据检查项id获取原始扣分项
                List<SafetyInspectionItemDeduction> safetyInspectionItemDeductionList = safetyInspectionItemDeductionMapper.getBySafetyInspectionItemId(safetySelfInspectionItem.getSafetyInspectionItemId());
                if (CollectionUtils.isNotEmpty(safetyInspectionItemDeductionList)){
                    List<SafetySelfInspectionItemDeduction> 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<SafetySelfInspectionItemDeduction> selfDeductionList = safetySelfInspectionItemDeductionMapper.getBySafetySelfInspectionItemId(safetySelfInspectionItem.getId());
                if (CollectionUtils.isNotEmpty(selfDeductionList)){
                    safetySelfInspectionItem.setSelfDeductionList(selfDeductionList);
                }
            }
        }
        return itemList;
    }
    @Override
    public SafetySelfInspectionItem itemInfoOne(Long id, UserInfo user){
        SafetySelfInspectionItem safetySelfInspectionItem = safetySelfInspectionItemMapper.getDetailById(id);
        List<SafetySelfInspectionItemDeduction> selfDeductionList = safetySelfInspectionItemDeductionMapper.getBySafetySelfInspectionItemId(safetySelfInspectionItem.getId());
        List<InspectionHiddenDanger> inspectionHiddenDangerList = dangerService.getBySafetySelfInspectionItemId(id);
        List<AttachmentInfo> byBusinessId = attachmentInfoService.findByBusinessId(id);
        if (CollectionUtils.isNotEmpty(selfDeductionList)){
            safetySelfInspectionItem.setSelfDeductionList(selfDeductionList);
        }else{
@@ -234,11 +588,28 @@
                safetySelfInspectionItem.setSelfDeductionList(new ArrayList<>());
            }
        }
        for (InspectionHiddenDanger inspectionHiddenDanger : inspectionHiddenDangerList) {
            if(StringUtils.isNotBlank(inspectionHiddenDanger.getUrl())){
                List<Long> attachmentIds = new ArrayList<>();
                String[] split = inspectionHiddenDanger.getUrl().split(",");
                for (String s : split) {
                    attachmentIds.add(Long.valueOf(s));
                }
                List<AttachmentInfo> dangerAttachList = attachmentInfoService.findByIds(attachmentIds);
                inspectionHiddenDanger.setAttachmentList(dangerAttachList);
            }
        }
        safetySelfInspectionItem.setAttachmentList(byBusinessId);
        safetySelfInspectionItem.setDangerList(inspectionHiddenDangerList);
        return safetySelfInspectionItem;
    }
    @Transactional
    @Override
    public void modItemInfo(SafetySelfInspectionItem param, UserInfo user) {
        if (param.getSafetyInspectionItemResult()==0 && StringUtils.isBlank(param.getSafetyInspectionItemResultDesc())){
            throw new BusinessException("否决说明必填!");
        }
@@ -247,9 +618,22 @@
        param.setUpdateTime(date);
        param.setUpdateBy(username);
        safetySelfInspectionItemMapper.updateById(param);
        //附件
        List<AttachmentInfo> attachmentInfoList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(param.getAttachmentList())){
            for (AttachmentInfo attachmentInfo : param.getAttachmentList()) {
                attachmentInfo.setBusinessId(param.getId());
                attachmentInfoList.add(attachmentInfo);
            }
        }
        if (param.getSafetyInspectionItemResult()==0 || param.getSafetyInspectionItemResult() == 2){
            //否决 合格--删除扣分记录
            safetySelfInspectionItemDeductionMapper.delBySafetySelfInspectionItemId(param.getId(),username,date);
            //删除隐患
            dangerService.delByInspectionItemId(param.getId(),user);
            //删除附件
            attachmentInfoService.deleteByBusinessId(param.getId());
        }else{
            //扣分
            List<SafetySelfInspectionItemDeduction> list = safetySelfInspectionItemDeductionMapper.getBySafetySelfInspectionItemId(param.getId());
@@ -273,8 +657,71 @@
                    safetySelfInspectionItemDeductionMapper.updateById(safetySelfInspectionItemDeduction);
                }
            }
            //隐患
            if(CollectionUtils.isNotEmpty(param.getDangerList())){
                List<InspectionHiddenDanger> oldDangerList = dangerService.getBySafetySelfInspectionItemId(param.getId());
                //过滤出要删除数据
                List<InspectionHiddenDanger> deleteOldDangerList = oldDangerList.stream().parallel().filter(a ->
                                param.getDangerList().stream().noneMatch(b ->
                                        a.getId().equals(b.getId())))
                        .collect(Collectors.toList());
                //删除
                if (CollectionUtils.isNotEmpty(deleteOldDangerList)) {
                    List<Long> idList = deleteOldDangerList.stream().map(InspectionHiddenDanger::getId).collect(Collectors.toList());
                    dangerService.delByIds(idList);
                }
                for (InspectionHiddenDanger inspectionHiddenDanger : param.getDangerList()) {
                    StringBuffer stringBuffer = new StringBuffer();
                    //附件
                    if(CollectionUtils.isNotEmpty(inspectionHiddenDanger.getAttachmentList())){
                        for (AttachmentInfo attachmentInfo : inspectionHiddenDanger.getAttachmentList()) {
                            stringBuffer.append(attachmentInfo.getId().toString()).append(",");
                        }
                        stringBuffer = stringBuffer.deleteCharAt(stringBuffer.length()-1);
                    }
                    if(inspectionHiddenDanger.getId() == null){
                        inspectionHiddenDanger.setCreateBy(user.getRealname());
                        inspectionHiddenDanger.setCreateTime(new Date());
                        inspectionHiddenDanger.setUpdateBy(user.getRealname());
                        inspectionHiddenDanger.setUpdateTime(new Date());
                        inspectionHiddenDanger.setStatus((byte)-1);
                        inspectionHiddenDanger.setValidFlag(true);
                        inspectionHiddenDanger.setSelfInspectionId(param.getSafetySelfInspectionId());
                        inspectionHiddenDanger.setSelfInspectionItemId(param.getId());
                        inspectionHiddenDanger.setUrl(stringBuffer.toString());
                        dangerService.save(inspectionHiddenDanger);
                        //附件
                        if(StringUtils.isNotBlank(inspectionHiddenDanger.getUrl())){
                            for (AttachmentInfo attachmentInfo : inspectionHiddenDanger.getAttachmentList()) {
                                attachmentInfo.setBusinessId(inspectionHiddenDanger.getId());
                                attachmentInfoList.add(attachmentInfo);
                            }
                        }
                    }else {
                        inspectionHiddenDanger.setUpdateBy(user.getRealname());
                        inspectionHiddenDanger.setUpdateTime(new Date());
                        inspectionHiddenDanger.setUrl(stringBuffer.toString());
                        dangerService.updateById(inspectionHiddenDanger);
                        //附件
                        if(CollectionUtils.isNotEmpty(inspectionHiddenDanger.getAttachmentList())){
                            for (AttachmentInfo attachmentInfo : inspectionHiddenDanger.getAttachmentList()) {
                                attachmentInfo.setBusinessId(inspectionHiddenDanger.getId());
                                attachmentInfoList.add(attachmentInfo);
                            }
                        }
                    }
                }
            }
        }
        //附件信息更新
        if(CollectionUtils.isNotEmpty(attachmentInfoList)){
            attachmentInfoService.updateBusinessIdBatch(attachmentInfoList);
        }
    }
    @Override
    public void finish(Long id, UserInfo user) {
@@ -284,9 +731,67 @@
        safetySelfInspection.setStatus(2);
        safetySelfInspection.setUpdateTime(date);
        safetySelfInspection.setUpdateBy(username);
        safetySelfInspection.setInspectionEndTime(new Date());
        safetySelfInspectionMapper.updateById(safetySelfInspection);
    }
    @Override
    public void addItemExpert(List<SafetySelfInspectionItem> itemList, UserInfo user) {
        itemList.forEach(item -> {
            item.setUpdateBy(user.getRealname());
            item.setUpdateTime(new Date());
        });
        safetySelfInspectionItemMapper.updateBatch(itemList);
    }
    @Override
    public void addCheckAttachment(List<AttachmentInfo> attachmentList, UserInfo user) {
        requiredAttachmentVerification(attachmentList);
        for (AttachmentInfo attachmentInfo : attachmentList) {
            attachmentInfo.setUpdateTime(new Date());
            attachmentInfo.setUpdateUid(user.getId());
            attachmentInfo.setUpdateUname(user.getRealname());
        }
        attachmentInfoService.updateBusinessIdBatch(attachmentList);
    }
    @Override
    public Date selectLastTimeByCompanyId(Long companyId,Date startTime,Date endTime) {
        SafetySelfInspection inspection = safetySelfInspectionMapper.selectOne(new LambdaQueryWrapper<SafetySelfInspection>()
                 .eq(SafetySelfInspection::getValidFlag,1)
                .eq(SafetySelfInspection::getFlag, 2)
                .eq(SafetySelfInspection::getCheckedCompanyId, companyId)
                .ge(startTime != null,SafetySelfInspection::getInspectionTime,startTime)
                .le(endTime != null,SafetySelfInspection::getInspectionTime,endTime)
                .orderByDesc(SafetySelfInspection::getInspectionTime)
                .last("limit 1"));
        if(inspection != null){
            return inspection.getInspectionTime();
        }
       return null;
    }
    @Override
    public CompanyStatisticInspectionDO companyStatisticInspection(Map<String, Object> params) {
        return safetySelfInspectionMapper.companyStatisticInspection(params);
    }
    private void requiredAttachmentVerification(List<AttachmentInfo> attachmentList) {
        if(CollectionUtils.isEmpty(attachmentList)){
            throw new BusinessException("附件信息为空");
        }
        for (AttachmentInfo attachmentInfo : attachmentList) {
            if(attachmentInfo.getId() == null){
                throw new BusinessException("附件主键信息不能为空");
            }
            if(attachmentInfo.getBusinessId() == null){
                throw new BusinessException("附件信息关联业务id不能为空");
            }
        }
    }
    /**
     * 查询验证
@@ -313,5 +818,37 @@
        if (param.getCheckedCompanyId() == null){
            throw new BusinessException("请选择被检查公司");
        }
        if(CollectionUtils.isEmpty(param.getElementAList())){
            throw new BusinessException("请选择A检查要素");
        }
        for(SafetyInspectionElementA elementA : param.getElementAList()){
            if(elementA.getElementA() == null){
                throw new BusinessException("请选择A检查要素");
            }
        }
    }
    public void requiredSuperviseVerification(SafetySelfInspection param) {
        if (StringUtils.isBlank(param.getInspectionName())) throw new BusinessException("请填写排查清单名称");
        if (param.getInspectionTime() == null) throw new BusinessException("请选择排查时间");
        if (CollectionUtils.isEmpty(param.getExpertList())) throw new BusinessException("请选择检查专家");
        for(InspectionExpert inspectionExpert : param.getExpertList()){
            if(inspectionExpert.getExpertId() == null){
                throw new BusinessException("请选择检查专家");
            }
            if(StringUtils.isBlank(inspectionExpert.getName())){
                throw new BusinessException("请选择检查专家名字");
            }
            if(inspectionExpert.getIsLeader() == null){
                throw new BusinessException("请选择组长或者组员");
            }
        }
        if (CollectionUtils.isEmpty(param.getItemList())) {
            throw new BusinessException("请选择检查项");
        }
        if (param.getCheckedCompanyId() == null){
            throw new BusinessException("请选择被检查公司");
        }
    }
}