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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newfiber.common.utils.PageUtils;
import com.newfiber.common.utils.Query;
import com.newfiber.modules.inspection.dao.EquInfoDao;
import com.newfiber.modules.inspection.dao.EquMaintenanceWorkOrderDao;
import com.newfiber.modules.inspection.dao.EquRepairOrderDao;
import com.newfiber.modules.inspection.entity.EquInfoEntity;
import com.newfiber.modules.inspection.entity.EquMaintenanceApprovalEntity;
import com.newfiber.modules.inspection.entity.EquMaintenanceWorkOrderEntity;
import com.newfiber.modules.inspection.enums.EApprovalStatus;
import com.newfiber.modules.inspection.enums.EApprovalType;
import com.newfiber.modules.inspection.service.EquInfoService;
import com.newfiber.modules.inspection.service.EquMaintenanceApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service("equInfoService")
public class EquInfoServiceImpl extends ServiceImpl<EquInfoDao, EquInfoEntity> implements EquInfoService {

    @Autowired
    private EquInfoDao equInfoDao;
    @Autowired
    private EquMaintenanceWorkOrderDao equMaintenanceWorkOrderDao;
    @Autowired
    private EquMaintenanceApprovalService approvalService;


    @Autowired
    private EquRepairOrderDao equRepairOrderDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String pumpNo = (String) params.get("pumpNo");
        String query = (String) params.get("query");

        IPage<EquInfoEntity> page = this.page(
                new Query<EquInfoEntity>().getPage(params),
                new QueryWrapper<EquInfoEntity>()
                        .eq(pumpNo != null, "pump_no", pumpNo)
                        .like(query != null, "equ_name", query)
        );

        return new PageUtils(page);
    }

    @Override
    public EquInfoEntity getByEquNo(String equNo) {

        return equInfoDao.getByEquNo(equNo);
    }

    @Override
    public List<EquInfoEntity> getByPumpNo(
            String pumpNo) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("pump_no", pumpNo);
        return this.listByMap(params);
    }

    @Override
    public Map<String, Object> getEquRecord(String equNo) {
        /**统计*/
        Map<String, Object> result = new HashMap<String, Object>();

        Calendar calendar = Calendar.getInstance();
        Date now = calendar.getTime();
        calendar.add(Calendar.MONTH, -6);
        Date before = calendar.getTime();
        /**获取近半年设备报修工单的数量*/
        QueryWrapper<EquMaintenanceWorkOrderEntity> queryWrapper = new QueryWrapper<EquMaintenanceWorkOrderEntity>()
                .eq("equ_no", equNo)
                .between("repair_time", before, now);
        queryWrapper.and(Wrapper -> Wrapper
                .eq("work_order_type", EApprovalStatus.No.getKey())
                .or()
                .eq("work_order_type", EApprovalStatus.END.getKey()));

        List<EquMaintenanceWorkOrderEntity> workOrderEntities = equMaintenanceWorkOrderDao.selectList(queryWrapper);
        if (workOrderEntities == null || workOrderEntities.size() == 0) {
            return result;
        }

        List<EquMaintenanceWorkOrderEntity> automatic = workOrderEntities.stream().filter(s ->
                s.getDistributionType().equals(EApprovalType.NEWS.getKey())).collect(Collectors.toList());
        Map<String, Object> automaticMap;
        if (automatic != null && automatic.size() > 0) {
            automaticMap = accumulate(automatic);
        } else {
            automaticMap = accumulate(new ArrayList<>());
        }

        List<EquMaintenanceWorkOrderEntity> manual = workOrderEntities.stream().filter(s ->
                s.getDistributionType().equals(EApprovalType.RETURN.getKey())).collect(Collectors.toList());
        Map<String, Object> manualMap;
        if (manual != null && manual.size() > 0) {
            manualMap = accumulate(manual);
        } else {
            manualMap = accumulate(new ArrayList<>());
        }
        result.put("totalCount", automatic.size() + manual.size());
        result.put("automatic", automaticMap);
        result.put("manual", manualMap);

        return result;
    }

    private Map<String, Object> accumulate(List<EquMaintenanceWorkOrderEntity> automatic) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<EquMaintenanceWorkOrderEntity> failed = automatic.stream().filter(s ->
                s.getWorkOrderType().equals(EApprovalStatus.No.getKey())).collect(Collectors.toList());
        List<EquMaintenanceWorkOrderEntity> adopt = automatic.stream().filter(s ->
                s.getWorkOrderType().equals(EApprovalStatus.END.getKey())).collect(Collectors.toList());
        map.put("total", failed.size() + adopt.size());//总计
        map.put("failed", failed.size());//未通过
        map.put("adopt", adopt.size());//已完成
        List<String> adoptNos = adopt.stream().map(EquMaintenanceWorkOrderEntity::getWorkOrderNo).distinct().collect(Collectors.toList());
        if (adoptNos != null && adoptNos.size() > 0) {
            map.put("detailed", statistics(approvalService.list(
                    new QueryWrapper<EquMaintenanceApprovalEntity>().in("work_order_no", adoptNos))));
        } else {
            map.put("detailed", statistics(new ArrayList<>()));
        }
        return map;
    }

    private Map<String, Object> statistics(List<EquMaintenanceApprovalEntity> approvalEntities) {
        Map<String, Object> result = new HashMap<>();
        int maintain = 0;
        int small = 0;
        int intermediate = 0;
        int large = 0;
        int external = 0;
        for (EquMaintenanceApprovalEntity approvalEntity : approvalEntities) {
            switch (approvalEntity.getThingLevel()) {
                case 0:
                    maintain++;
                    break;
                case 1:
                    small++;
                    break;
                case 2:
                    intermediate++;
                    break;
                case 3:
                    large++;
                    break;
                case 4:
                    external++;
                    break;
            }
        }
        /**维修级别统计*/
        result.put("maintain", maintain);
        result.put("small", small);
        result.put("intermediate", intermediate);
        result.put("large", large);
        result.put("external", external);
        return result;
    }


}