Newer
Older
huludao / src / main / java / com / newfiber / modules / inspection / service / impl / InspectionInfoServiceImpl.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.common.utils.Constant;
import com.newfiber.common.utils.DateUtils;
import com.newfiber.common.utils.PageUtils;
import com.newfiber.modules.inspection.dao.InspectionInfoDao;
import com.newfiber.modules.inspection.dao.InspectionTemplateDao;
import com.newfiber.modules.inspection.entity.EquInfoEntity;
import com.newfiber.modules.inspection.entity.InspectionDetectionEqu;
import com.newfiber.modules.inspection.entity.InspectionInfoEntity;
import com.newfiber.modules.inspection.entity.InspectionTemplateEntity;
import com.newfiber.modules.inspection.entity.InspectionUserEqu;
import com.newfiber.modules.inspection.entity.InspectionUserItem;
import com.newfiber.modules.inspection.entity.ManPumpstation;
import com.newfiber.modules.inspection.enums.EBoolean;
import com.newfiber.modules.inspection.enums.EInspectionInfoStatus;
import com.newfiber.modules.inspection.service.EquInfoService;
import com.newfiber.modules.inspection.service.InspectionDetectionEquService;
import com.newfiber.modules.inspection.service.InspectionInfoService;
import com.newfiber.modules.inspection.service.InspectionTemplateService;
import com.newfiber.modules.inspection.service.InspectionUserEquService;
import com.newfiber.modules.inspection.service.InspectionUserItemService;
import com.newfiber.modules.inspection.service.ManPumpstationService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service("inspectionInfoService")
public class InspectionInfoServiceImpl extends ServiceImpl<InspectionInfoDao, InspectionInfoEntity> implements InspectionInfoService {

    @Autowired
    private InspectionInfoDao inspectionInfoDao;
    @Autowired
    private InspectionTemplateDao inspectionTemplateDao;

    @Resource
    private InspectionDetectionEquService inspectionDetectionEquService;

    @Resource
    private InspectionUserItemService inspectionUserItemService;

    @Resource
    private EquInfoService equInfoService;

    @Resource
    private InspectionUserEquService inspectionUserEquService;

    @Resource
    private InspectionTemplateService inspectionTemplateService;

    @Resource
    private ManPumpstationService manPumpstationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        if (params.get("username").equals(Constant.ADMIN)) {
            params.put("username", "");
        }
        int page = Integer.parseInt(params.get(Constant.PAGE).toString());
        int limit = Integer.parseInt(params.get(Constant.LIMIT).toString());
        PageUtils.paging(params); //分页整理
        /**获取分页数据并整理*/
        List<InspectionInfoEntity> inspectionInfoEntities = inspectionInfoDao.queryPage(params);
        /**获取数据不同的巡检编号*/
        List<String> inspectionNos = inspectionInfoEntities.stream().map(InspectionInfoEntity::getInspectionNo).distinct().collect(Collectors.toList());

        List<Map<String, Object>> resultList = new ArrayList<>();
        for (String inspectionNo : inspectionNos) {

            List<InspectionInfoEntity> collect = inspectionInfoEntities.stream().filter(s -> s.getInspectionNo().equals(inspectionNo)).collect(Collectors.toList());
            Map<String, Object> map = new HashMap<>();
            map.put("username", collect.get(0).getUsername());
            map.put("fullname", collect.get(0).getFullname());
            map.put("time", collect.get(0).getCreateTime());
            map.put("inspectionNo", collect.get(0).getInspectionNo());
            map.put("status", collect.get(0).getStatus());
            map.put("details", collect);

            resultList.add(map);
        }

        int total = inspectionInfoDao.queryTotal(params);

        return new PageUtils(resultList, total, limit, page);
    }

    @Override
    @Transactional
    public void updateInfo(InspectionInfoEntity inspectionInfo) {
        inspectionInfoDao.updateById(inspectionInfo);

        QueryWrapper<InspectionInfoEntity> queryWrapper = new QueryWrapper<InspectionInfoEntity>()
                .eq("inspection_no", inspectionInfo.getInspectionNo());

        List<InspectionInfoEntity> entityList = inspectionInfoDao.selectList(queryWrapper);
        List<InspectionInfoEntity> collect = entityList.stream().filter(s -> s.getState().equals(0)).collect(Collectors.toList());
        if (collect.size() == 0) {
            for (InspectionInfoEntity entity : entityList) {
                entity.setStatus(1);
            }
            this.updateBatchById(entityList);
        }
    }

    @Override
    public void startInspection(String inspectionNo) {
        InspectionInfoEntity inspectionInfoEntity = new InspectionInfoEntity();
        inspectionInfoEntity.setInspectionNo(inspectionNo);
        inspectionInfoEntity.setStatus(EInspectionInfoStatus.DOING.getKey());
        inspectionInfoDao.updateStatusByNo(inspectionInfoEntity);
    }

    @Override
    public void endInspection(String inspectionNo) {
        InspectionInfoEntity inspectionInfoEntity = new InspectionInfoEntity();
        inspectionInfoEntity.setInspectionNo(inspectionNo);
        inspectionInfoEntity.setStatus(EInspectionInfoStatus.DONE.getKey());
        inspectionInfoDao.updateStatusByNo(inspectionInfoEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveInspectioninfo(Map<String, Object> params) {
        String uuid = UUID.randomUUID().toString().replace("-", "");

        /**获取对应的巡检模版*/
        List<InspectionTemplateEntity> entities = inspectionTemplateDao.queryList(params.get("templateNo").toString());

        //检测是否存在检测项为空的设备
        inspectionDetectionEquService.equItemNullCheck(params.get("templateNo").toString());

        /**到期下派巡检工单*/
        List<InspectionInfoEntity> inspectionInfoEntities = new ArrayList<>();
        for (InspectionTemplateEntity entity : entities) {
            InspectionInfoEntity inspectionInfo = new InspectionInfoEntity();
            inspectionInfo.setInspectionNo(uuid);
            inspectionInfo.setUsername(params.get("username").toString());
            inspectionInfo.setFullname(params.get("fullname").toString());
            inspectionInfo.setPointx(entity.getPointx());
            inspectionInfo.setPointy(entity.getPointy());
            inspectionInfo.setDeviation(entity.getDeviation());

            inspectionInfo.setStartDatetime(DateUtils.stringToDate(params.get("startDatetime").toString(), DateUtils.DATE_TIME_PATTERN));
            inspectionInfo.setEndDatetime(DateUtils.stringToDate(params.get("endDatetime").toString(), DateUtils.DATE_TIME_PATTERN));

            inspectionInfoEntities.add(inspectionInfo);
        }
        this.saveBatch(inspectionInfoEntities);

        if(CollectionUtils.isNotEmpty(entities)){
            List<InspectionDetectionEqu> inspectionDetectionEquList = inspectionDetectionEquService
                    .queryByTemplateNo(entities.get(0).getTemplateNo());
            inspectionUserItemService.create(params.get("fullname").toString(), uuid,
                    inspectionDetectionEquList);
        }

        inspectionTemplateService.increaseRelatedUserCount(params.get("templateNo").toString(), inspectionInfoEntities.size());
    }

    @Override
    public InspectionInfoEntity getByInspectionNo(String inspectionNo) {
        InspectionInfoEntity inspectionInfoEntity = null;
        QueryWrapper<InspectionInfoEntity> queryWrapper = new QueryWrapper<InspectionInfoEntity>()
                .eq("inspection_no", inspectionNo);
        List<InspectionInfoEntity> entityList = inspectionInfoDao.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(entityList)){
            inspectionInfoEntity = entityList.get(0);
        }
        return inspectionInfoEntity;
    }

    @Override
    public List<ManPumpstation> queryData(String inspectionNo) {
        List<InspectionUserEqu> inspectionUserEquList = inspectionUserEquService.listByInspectionNo(
                inspectionNo);
        List<InspectionUserItem> inspectionUserItemList = inspectionUserItemService.list(
                inspectionNo);
        List<ManPumpstation> pumpInfoEntityList = new ArrayList<>();

        if(CollectionUtils.isNotEmpty(inspectionUserEquList)){
            List<String> pumpNoList = inspectionUserEquList.stream().map(
                    InspectionUserEqu::getPumpNo).distinct().collect( Collectors.toList());

            for(String pumpNo : pumpNoList){
                List<String> equNoList = inspectionUserEquList.stream().filter(t->t.getPumpNo().equals(pumpNo)).map(
                        InspectionUserEqu::getEquNo).distinct().collect( Collectors.toList());
                List<EquInfoEntity> equInfoEntities = new ArrayList<>();

                ManPumpstation pumpInfoEntity = manPumpstationService.getByNo(pumpNo);
                pumpInfoEntity.setInspectionInfoEntity(getByInspectionNo(inspectionNo));

                if(CollectionUtils.isEmpty(inspectionUserItemList.stream().filter(t->t.getPumpNo().equals(pumpNo) && StringUtils
                        .isBlank(t.getResult()))
                        .collect(Collectors.toList()))){
                    pumpInfoEntity.setInspectionDoneFlag(EBoolean.YES.getKey());
                }else{
                    pumpInfoEntity.setInspectionDoneFlag(EBoolean.NO.getKey());
                }

                for(String equNo : equNoList){
                    EquInfoEntity equInfoEntity = equInfoService.getByEquNo(equNo);
                    if(null == equInfoEntity){
                        continue;
                    }

                    InspectionUserEqu inspectionUserEqu = inspectionUserEquList.stream().filter(t->t.getEquNo().equals(equNo)).collect(Collectors.toList()).get(0);
                    List<InspectionUserItem> subInspectionUserItemList = inspectionUserItemList.stream().filter(t->t.getPumpNo().equals(pumpNo)).
                            filter(t->t.getEquNo().equals(equInfoEntity.getEquNo())).collect(Collectors.toList());
                    equInfoEntity.setInspectionUserItemList(subInspectionUserItemList);
                    equInfoEntity.setInspectionScanFlag(inspectionUserEqu.getScanFlag());
                    equInfoEntity.setInspectionUserEquId(inspectionUserEqu.getId());

                    equInfoEntities.add(equInfoEntity);
                }

                pumpInfoEntity.setEquInfoEntityList(equInfoEntities);
                pumpInfoEntityList.add(pumpInfoEntity);
            }

        }
        return pumpInfoEntityList;
    }
}