Newer
Older
huludao / src / main / java / com / newfiber / modules / inspection / service / impl / InspectionDetectionEquServiceImpl.java
package com.newfiber.modules.inspection.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newfiber.common.exception.RRException;
import com.newfiber.modules.inspection.dao.InspectionDetectionEquDao;
import com.newfiber.modules.inspection.entity.EquDetectionItem;
import com.newfiber.modules.inspection.entity.EquInfoEntity;
import com.newfiber.modules.inspection.entity.InspectionDetectionEqu;
import com.newfiber.modules.inspection.entity.InspectionTemplateEntity;
import com.newfiber.modules.inspection.request.InspectionDetectionEquSaveReq;
import com.newfiber.modules.inspection.request.InspectionDetectionEquUpdateReq;
import com.newfiber.modules.inspection.service.EquDetectionItemService;
import com.newfiber.modules.inspection.service.EquInfoService;
import com.newfiber.modules.inspection.service.InspectionDetectionEquService;
import com.newfiber.modules.inspection.service.InspectionTemplateService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* 巡检模板检测项ServiceImpl
*
* @author : xiongk
* @since : 2020-11-06 09:33
*/
@Service
public class InspectionDetectionEquServiceImpl extends ServiceImpl<InspectionDetectionEquDao, InspectionDetectionEqu> implements
        InspectionDetectionEquService {

    @Resource
    private EquInfoService equInfoService;

    @Resource
    private InspectionTemplateService inspectionTemplateService;

    @Resource
    private EquDetectionItemService equDetectionItemService;

    @Override
    public void create(InspectionDetectionEquSaveReq request) {
        doCreate(request.getTemplateNo(), request.getEquNo(), request.getItemIdList());
    }

    @Override
    public void doCreate(String templateNo, String equNo, List<Integer> itemIdList) {
        EquInfoEntity equInfoEntity = equInfoService.getByEquNo(equNo);
        if(null == equInfoEntity){
            throw new RRException(String.format("该设备不存在:{%s}", equNo));
        }

        InspectionTemplateEntity inspectionTemplateEntity = inspectionTemplateService.queryByNo(
                templateNo);
        if(null == inspectionTemplateEntity){
            throw new RRException(String.format("该模板不存在:{%s}", templateNo));
        }

        List<InspectionDetectionEqu> inspectionDetectionEquList = new ArrayList<>();
        for(Integer itemId : itemIdList){
            EquDetectionItem equDetectionItem = equDetectionItemService.getById(itemId);
            if(null == equDetectionItem){
                throw new RRException(String.format("该检测项不存在:{%s}", itemId));
            }

            InspectionDetectionEqu inspectionDetectionEqu = new InspectionDetectionEqu();
            inspectionDetectionEqu.setTemplateNo(inspectionTemplateEntity.getTemplateNo());
            inspectionDetectionEqu.setTemplateName(inspectionTemplateEntity.getTemplateName());
            inspectionDetectionEqu.setEquNo(equInfoEntity.getEquNo());
            inspectionDetectionEqu.setEquName(equInfoEntity.getEquName());
            inspectionDetectionEqu.setCreateDatetime(new Date());
            inspectionDetectionEquList.add(inspectionDetectionEqu);
        }

        this.saveBatch(inspectionDetectionEquList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(InspectionDetectionEquUpdateReq request) {
        Map<String, Object> param = new HashMap<>();
        param.put("template_no", request.getTemplateNo());
        this.removeByMap(param);

        List<InspectionDetectionEqu> inspectionDetectionEquList = new ArrayList<>();
        InspectionTemplateEntity inspectionTemplateEntity = inspectionTemplateService.queryByNo(
                request.getTemplateNo());

        for(String equNo : request.getEquNoList()){
            EquInfoEntity equInfoEntity = equInfoService.getByEquNo(equNo);

            InspectionDetectionEqu inspectionDetectionEqu = new InspectionDetectionEqu();
            inspectionDetectionEqu.setTemplateNo(inspectionTemplateEntity.getTemplateNo());
            inspectionDetectionEqu.setTemplateName(inspectionTemplateEntity.getTemplateName());
            inspectionDetectionEqu.setPumpNo(equInfoEntity.getPumpNo());
            inspectionDetectionEqu.setEquNo(equInfoEntity.getEquNo());
            inspectionDetectionEqu.setEquName(equInfoEntity.getEquName());
            inspectionDetectionEqu.setCreateDatetime(new Date());
            inspectionDetectionEquList.add(inspectionDetectionEqu);
        }

        this.saveBatch(inspectionDetectionEquList);
    }

    @Override
    public void equItemNullCheck(String templateNo) {
        InspectionDetectionEqu condition = new InspectionDetectionEqu();
        condition.setTemplateNo(templateNo);
        List<InspectionDetectionEqu> inspectionDetectionEquList = baseMapper.selectWithItemCount(condition);
        inspectionDetectionEquList = inspectionDetectionEquList.stream().filter(t->t.getItemCount() == 0).collect(
                Collectors.toList());

        if(CollectionUtils.isNotEmpty(inspectionDetectionEquList)){
            throw new RRException(String.format("操作失败,请添加设备%s的检测项", inspectionDetectionEquList.get(0).getEquName()));
        }

    }

    @Override
    public List<InspectionDetectionEqu> queryByTemplateNo(String templateNo) {
        Map<String, Object> param = new HashMap<>();
        param.put("template_no", templateNo);
        return this.listByMap(param);
    }

}