Newer
Older
huludao / src / main / java / com / newfiber / modules / inspection / service / impl / EquRepairOrderServiceImpl.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.Constant;
import com.newfiber.common.utils.FilesUtils;
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.EquRepairOrderDao;
import com.newfiber.modules.inspection.dao.EquTechnologicalProcessInfoDao;
import com.newfiber.modules.inspection.entity.*;
import com.newfiber.modules.inspection.service.EquRepairOrderService;
import com.newfiber.modules.inspection.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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


@Service("equRepairOrderService")
public class EquRepairOrderServiceImpl extends ServiceImpl<EquRepairOrderDao, EquRepairOrderEntity> implements EquRepairOrderService {
    @Autowired
    private FilesUtils filesUtils;
    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private EquRepairOrderDao orderDao;
    @Autowired
    private EquInfoDao equInfoDao;
    @Autowired
    private EquTechnologicalProcessInfoDao processInfoDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /**管理员可查看所有的维修工单*/
        QueryWrapper<EquRepairOrderEntity> queryWrapper = new QueryWrapper<EquRepairOrderEntity>();

        String username = (String) params.get("username");

        if (username != null && !username.equals(Constant.ADMIN)) {
            queryWrapper.eq("repair_personnel", params.get("username"));
        }

        String equNo = (String) params.get("equNo");
        queryWrapper.eq(equNo != null, "equ_no", equNo);

        String maintainNo = (String) params.get("maintainNo");
        queryWrapper.eq(maintainNo != null, "maintain_no", maintainNo);

        String repairNo = (String) params.get("repairNo");
        queryWrapper.eq(repairNo != null, "repair_no", repairNo);

        Integer workOrderStatus = (Integer) params.get("workOrderStatus");
        queryWrapper.eq(workOrderStatus != null, "work_order_status", workOrderStatus);

        Integer repairState = (Integer) params.get("repairState");
        queryWrapper.eq(repairState != null, "repair_state", repairState);

        IPage<EquRepairOrderEntity> page = this.page(
                new Query<EquRepairOrderEntity>().getPage(params), queryWrapper);

        List<EquRepairOrderEntity> records = page.getRecords();

        Map<String, Object> map = new HashMap<>();

        for (EquRepairOrderEntity record : records) {
            /**获取维修工单上传的图片*/
            map.put("relation_id", record.getRepairNo());
            record.setFileUploadEntityList(fileUploadService.listByMap(map));

            /**获取维修工单,提交的审批流程信息*/
            List<List<EquTechnologicalProcessInfoEntity>> approvalList = new ArrayList();
            QueryWrapper<EquTechnologicalProcessInfoEntity> wrapper = new QueryWrapper<EquTechnologicalProcessInfoEntity>().orderByDesc("create_time").orderByAsc("order_num");
            wrapper.eq("repair_no", record.getRepairNo());
            List<EquTechnologicalProcessInfoEntity> infoEntities = processInfoDao.selectList(wrapper);
            List<String> approvalNos = infoEntities.stream().map(EquTechnologicalProcessInfoEntity::getApprovalNo).distinct().collect(Collectors.toList());
            for (String approvalNo : approvalNos) {
                List<EquTechnologicalProcessInfoEntity> collect = infoEntities.stream().filter(s -> s.getApprovalNo().equals(approvalNo)).collect(Collectors.toList());
                approvalList.add(collect);
            }
            record.setApprovalList(approvalList);
        }

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void updateState(EquRepairOrderEntity equRepairOrder, MultipartFile[] files) throws IOException {
        if (files != null) {
            /**上传文件并保存*/
            List<FileUploadEntity> fileUploadEntities = filesUtils.filesUpload(equRepairOrder.getRepairNo(), files);
            fileUploadService.saveBatch(fileUploadEntities);
        }

        orderDao.updateById(equRepairOrder);

        /**维修工单完成后,重置设备保养周期*/
        Integer repairState = equRepairOrder.getRepairState();
        if (repairState != null && repairState == 1) {
            EquInfoEntity infoEntity = equInfoDao.selectOne(new QueryWrapper<EquInfoEntity>()
                    .eq("equ_no", equRepairOrder.getEquNo()));

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(infoEntity.getMaintainTime());
            calendar.add(Calendar.DATE, infoEntity.getMaintainCycle());

            Date time = calendar.getTime();
            Date now = new Date();
            if (time.getTime() > now.getTime()) {
                infoEntity.setMaintainTime(calendar.getTime());
            } else {
                calendar.setTime(now);
                calendar.add(Calendar.DATE, infoEntity.getMaintainCycle());
                infoEntity.setMaintainTime(calendar.getTime());
            }
            equInfoDao.updateById(infoEntity);
        }
    }


}