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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newfiber.modules.inspection.dao.InspectionUserItemDao;
import com.newfiber.modules.inspection.entity.EquDetectionItem;
import com.newfiber.modules.inspection.entity.InspectionDetectionEqu;
import com.newfiber.modules.inspection.entity.InspectionInfoEntity;
import com.newfiber.modules.inspection.entity.InspectionUserEqu;
import com.newfiber.modules.inspection.entity.InspectionUserItem;
import com.newfiber.modules.inspection.enums.EBoolean;
import com.newfiber.modules.inspection.request.InspectionStatisRequest;
import com.newfiber.modules.inspection.request.InspectionUserItemCheckBatchReq;
import com.newfiber.modules.inspection.request.InspectionUserItemCheckPumpReq;
import com.newfiber.modules.inspection.request.InspectionUserItemCheckReq;
import com.newfiber.modules.inspection.response.InspectionStatisResponse;
import com.newfiber.modules.inspection.service.EquDetectionItemService;
import com.newfiber.modules.inspection.service.InspectionInfoService;
import com.newfiber.modules.inspection.service.InspectionUserEquService;
import com.newfiber.modules.inspection.service.InspectionUserItemService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* 用户巡检检测项ServiceImpl
*
* @author : xiongk
* @since : 2020-11-06 10:00
*/
@Service
public class InspectionUserItemServiceImpl extends ServiceImpl<InspectionUserItemDao, InspectionUserItem> implements InspectionUserItemService {

    @Resource
    private InspectionInfoService inspectionInfoService;

    @Resource
    private EquDetectionItemService equDetectionItemService;

    @Resource
    private InspectionUserEquService inspectionUserEquService;

    @Override
    public void create(String fullname, String inspectionNo,
            List<InspectionDetectionEqu> inspectionDetectionEquList) {
        List<InspectionUserItem> inspectionUserItemList = new ArrayList<>();
        List<InspectionUserEqu> inspectionUserEquList = new ArrayList<>();

        for(InspectionDetectionEqu inspectionDetectionEqu : inspectionDetectionEquList){

            List<EquDetectionItem> equDetectionItemList = equDetectionItemService.listByEqu(inspectionDetectionEqu.getEquNo());

            for(EquDetectionItem equDetectionItem : equDetectionItemList){
                InspectionUserItem inspectionUserItem = new InspectionUserItem();
                inspectionUserItem.setFullname(fullname);
                inspectionUserItem.setInspectionNo(inspectionNo);
                inspectionUserItem.setPumpNo(inspectionDetectionEqu.getPumpNo());
                inspectionUserItem.setEquNo(inspectionDetectionEqu.getEquNo());
                inspectionUserItem.setEquName(inspectionDetectionEqu.getEquName());
                inspectionUserItem.setItemId(equDetectionItem.getId());
                inspectionUserItem.setItemName(equDetectionItem.getDetectionName());
                inspectionUserItem.setCreateDatetime(new Date());
                inspectionUserItemList.add(inspectionUserItem);
            }

            InspectionUserEqu inspectionUserEqu = new InspectionUserEqu();
            inspectionUserEqu.setFullname(fullname);
            inspectionUserEqu.setInspectionNo(inspectionNo);
            inspectionUserEqu.setPumpNo(inspectionDetectionEqu.getPumpNo());
            inspectionUserEqu.setEquNo(inspectionDetectionEqu.getEquNo());
            inspectionUserEqu.setEquName(inspectionDetectionEqu.getEquName());
            inspectionUserEqu.setCreateDatetime(new Date());
            inspectionUserEquList.add(inspectionUserEqu);
        }

        inspectionUserEquService.saveBatch(inspectionUserEquList);
        this.saveBatch(inspectionUserItemList);
    }

    @Override
    public void check(InspectionUserItemCheckReq request) {
        InspectionUserItem inspectionUserItem = new InspectionUserItem();
        BeanUtils.copyProperties(request, inspectionUserItem);
        inspectionUserItem.setResultDatetime(new Date());
        this.updateById(inspectionUserItem);
    }

    @Override
    public Integer uncheckCount(String inspectionNo) {
        QueryWrapper<InspectionUserItem> queryWrapper = new QueryWrapper<InspectionUserItem>().eq("inspection_no", inspectionNo).isNull("result");
        return this.count(queryWrapper);
    }

    @Override
    public List<InspectionUserItem> list(String inspectionNo) {
        QueryWrapper<InspectionUserItem> queryWrapper = new QueryWrapper<InspectionUserItem>().eq("inspection_no",inspectionNo);
        return this.list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void checkBatch(InspectionUserItemCheckBatchReq request) {
        for(InspectionUserItemCheckReq checkReq : request.getInspectionUserItemCheckReqList()){
            this.check(checkReq);
        }

        // 更新排班表
        endInspection(request.getInspectionNo());
    }

    private void endInspection(String inspectionNo) {
        if(0 == uncheckCount(inspectionNo)){
            InspectionInfoEntity inspectionInfoEntity = inspectionInfoService.getByInspectionNo(inspectionNo);

            inspectionInfoService.endInspection(inspectionInfoEntity.getInspectionNo());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void checkPump(InspectionUserItemCheckPumpReq request) {
        QueryWrapper<InspectionUserItem> queryWrapper = new QueryWrapper<InspectionUserItem>().
                eq("inspection_no", request.getInspectionNo()).
                eq("pump_no", request.getPumpNo()).
                isNull("result");

        List<InspectionUserItem> resultList = new ArrayList<>();
        List<InspectionUserItem> inspectionUserItemList = this.list(queryWrapper);

        for(InspectionUserItem inspectionUserItem : inspectionUserItemList){
            InspectionUserItem result = new InspectionUserItem();
            result.setId(inspectionUserItem.getId());
            result.setResult(EBoolean.YES.getKey());
            result.setResultNote("巡检结果正常");
            result.setResultDatetime(new Date());

            result.setPointx(request.getPointx());
            result.setPointy(request.getPointy());
            result.setIp(request.getIp());
            resultList.add(result);
        }

        this.updateBatchById(resultList);
        endInspection(request.getInspectionNo());
    }

    @Override
    public List<InspectionStatisResponse> inspectionStatis(InspectionStatisRequest request) {
        return this.baseMapper.inspectionStatis(request);
    }

}