package com.whyc.service;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.whyc.constant.WorkflowEnum;
|
import com.whyc.dto.Response;
|
import com.whyc.mapper.*;
|
import com.whyc.pojo.db_user.User;
|
import com.whyc.pojo.web_site.*;
|
import com.whyc.util.CommonUtil;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.context.annotation.Lazy;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
@Service
|
public class WorkflowLinkService {
|
|
@Resource
|
private WorkflowLinkMapper mapper;
|
|
@Autowired
|
@Lazy
|
private WorkflowMainService mainService;
|
|
@Autowired
|
private WorkflowDeviceService deviceService;
|
|
@Autowired
|
private DeviceSpareService deviceSpareService;
|
@Autowired
|
private DeviceScrapService deviceScrapService;
|
|
|
public void addBatch(List<WorkflowLink> links) {
|
mapper.insertBatchSomeColumn(links);
|
}
|
|
public List<WorkflowLink> getWorkflowInfo(Integer mainId) {
|
QueryWrapper<WorkflowLink> wrapper = Wrappers.query();
|
wrapper.eq("main_id", mainId);
|
return mapper.selectList(wrapper);
|
}
|
|
@Transactional
|
public Response updateLink(WorkflowLink link) {
|
//获取当前节点的记录
|
WorkflowLink linkInDB = mapper.selectById(link.getId());
|
User user = CommonUtil.getUser();
|
Date now = new Date();
|
//根据id.查询关联的主表
|
WorkflowMain mainInDB = mainService.getById(linkInDB.getMainId());
|
//查看申请流程类型
|
switch (mainInDB.getType()) {
|
case 1: { //设备维修申请
|
//查看主表的状态
|
//因为是单个链路节点,所以不需要判断是不是待处理,肯定只有1个节点. 审批通过或者驳回
|
//if(main.getStatus() == WorkflowEnum.MAIN_STATUS_DEALING.getValue().intValue()){ //待处理,下一步是审批通过,待处理 或者驳回
|
if(link.getStatus() == WorkflowEnum.LINK_STATUS_PASS.getValue().intValue()){
|
mainInDB.setStatus(WorkflowEnum.MAIN_STATUS_WAIT_FOR_DEALING.getValue());
|
mainService.updateById(mainInDB);
|
}else if(link.getStatus() == WorkflowEnum.LINK_STATUS_REJECT.getValue().intValue()){
|
mainInDB.setStatus(WorkflowEnum.MAIN_STATUS_END_REJECT.getValue());
|
mainInDB.setEndReason(link.getDealRejectReason());
|
mainInDB.setEndTime(now);
|
mainService.updateById(mainInDB);
|
link.setDealAndClose(1);
|
}
|
}break;
|
//设备入库申请 TODO 入库影响库存
|
case 2:
|
//设备报废申请
|
case 3:{
|
if(link.getStatus() == WorkflowEnum.LINK_STATUS_PASS.getValue().intValue()){
|
mainInDB.setStatus(WorkflowEnum.MAIN_STATUS_END_PASS.getValue());
|
mainInDB.setEndTime(now);
|
mainInDB.setEndReason(link.getDealReason());
|
mainService.updateById(mainInDB);
|
|
List<WorkflowDevice> deviceListInDB = deviceService.getByMainId(mainInDB.getId());
|
//入库
|
if(mainInDB.getType() == 2){
|
List<DeviceSpare> spareList = new ArrayList<>();
|
deviceListInDB.forEach(device -> {
|
DeviceSpare spare = new DeviceSpare();
|
/*spare.setName(device.getName());
|
spare.setModel(device.getModel());
|
spare.setVersion(device.getVersion());
|
spare.setQuantity(device.getQuantity());
|
spare.setBrand(device.getBrand());
|
spare.setType(device.getType());
|
spare.setSupplier(device.getSupplier());*/
|
BeanUtils.copyProperties(device,spare);
|
|
spareList.add(spare);
|
});
|
deviceSpareService.addOrUpdate(spareList);
|
}else{ //进入报废库
|
List<DeviceScrap> scrapList = new ArrayList<>();
|
deviceListInDB.forEach(device -> {
|
DeviceScrap deviceScrap = new DeviceScrap();
|
BeanUtils.copyProperties(device,deviceScrap);
|
deviceScrap.setApplyUserId(mainInDB.getCreateUserId());
|
deviceScrap.setApplyUserName(mainInDB.getCreateUserName());
|
deviceScrap.setCreateTime(now);
|
deviceScrap.setMainId(mainInDB.getId());
|
scrapList.add(deviceScrap);
|
});
|
deviceScrapService.add(scrapList);
|
|
}
|
}else if(link.getStatus() == WorkflowEnum.LINK_STATUS_REJECT.getValue().intValue()){
|
mainInDB.setStatus(WorkflowEnum.MAIN_STATUS_END_REJECT.getValue());
|
mainInDB.setEndReason(link.getDealRejectReason());
|
mainInDB.setEndTime(now);
|
//检查是否有关联工单.
|
// 如果有关联工单,关联工单状态重置为完结待处理,完成时间重置为空
|
if(mainInDB.getRelatedId() != null){
|
Integer relatedId = mainInDB.getRelatedId();
|
mainService.resetRepairStatus(relatedId);
|
//关联工单的设备附属表未处理数量也要回退,等于主表设备数量
|
List<WorkflowDevice> deviceRelatedListInDB = deviceService.getByMainId(mainInDB.getRelatedId());
|
List<WorkflowDevice> deviceListInDB = deviceService.getByMainId(mainInDB.getId());
|
for (int i = 0; i < deviceListInDB.size(); i++) {
|
WorkflowDevice deviceInDB = deviceListInDB.get(i);
|
for (int j = 0; j < deviceRelatedListInDB.size(); j++) {
|
WorkflowDevice deviceRelatedInDB = deviceRelatedListInDB.get(j);
|
if (deviceInDB.getName().equals(deviceRelatedInDB.getName())
|
&& deviceInDB.getModel().equals(deviceRelatedInDB.getModel())
|
&& deviceInDB.getVersion().equals(deviceRelatedInDB.getVersion())
|
&& deviceInDB.getBrand().equals(deviceRelatedInDB.getBrand())
|
&& deviceInDB.getType().equals(deviceRelatedInDB.getType())
|
&& deviceInDB.getSupplier().equals(deviceRelatedInDB.getSupplier())
|
) {
|
deviceRelatedInDB.setQuantityUnprocessed(deviceInDB.getQuantity());
|
//关联工单当前设备附属的未处理数量回退完成,下一个
|
break;
|
}
|
}
|
}
|
deviceService.updateQuantityUnprocessedBatch(deviceRelatedListInDB);
|
}
|
mainService.updateById(mainInDB);
|
link.setDealAndClose(1);
|
}
|
}break;
|
case 4:{ //TODO 出库申请
|
if(link.getStatus() == WorkflowEnum.LINK_STATUS_PASS.getValue().intValue()){
|
mainInDB.setStatus(WorkflowEnum.MAIN_STATUS_WAIT_FOR_DEALING.getValue());
|
mainService.updateById(mainInDB);
|
}else if(link.getStatus() == WorkflowEnum.LINK_STATUS_REJECT.getValue().intValue()){
|
mainInDB.setStatus(WorkflowEnum.MAIN_STATUS_END_REJECT.getValue());
|
mainInDB.setEndReason(link.getDealRejectReason());
|
mainInDB.setEndTime(now);
|
//检查是否有关联工单.如果有关联工单,关联工单状态重置为完结待处理,完成时间重置为空
|
if(mainInDB.getRelatedId() != null){
|
Integer relatedId = mainInDB.getRelatedId();
|
//mainService.resetRepairStatus(relatedId,mainInDB.getQuantity());
|
}
|
mainService.updateById(mainInDB);
|
link.setDealAndClose(1);
|
}
|
}break;
|
default:
|
break;
|
|
}
|
|
link.setId(linkInDB.getId());
|
link.setDealUserId(user.getId());
|
link.setDealTime(now);
|
|
mapper.updateById(link);
|
|
return new Response().setII(1,"更新完成");
|
}
|
|
/**
|
*
|
* @param mainId 特定的单据
|
* @param processLevel 特定的过程
|
* @return 特定单据,特定过程,未完成的会签的数量
|
*/
|
private int getLeftAssignNum(Integer mainId, String processLevel) {
|
QueryWrapper<WorkflowLink> query = Wrappers.query();
|
query.select("count(*) as id").eq("main_id",mainId).eq("process_level",processLevel).ne("status",WorkflowEnum.LINK_STATUS_PASS.getValue());
|
return mapper.selectOne(query).getId();
|
}
|
|
/**
|
* 更新节点,主节点不完结
|
* @param link 参数id,status,dealReason或者dealRejectReason
|
*/
|
private void updateLinkField(WorkflowLink link) {
|
Date now = new Date();
|
link.setDealTime(now);
|
mapper.updateById(link);
|
}
|
|
|
public Map<Integer, Integer> getReceivedStatistics(int type, User user) {
|
Map<Integer,Integer> statistics = new HashMap<>();
|
statistics.put(1,0);
|
statistics.put(6,0);
|
statistics.put(58,0);
|
List<WorkflowLink> links = mapper.getReceivedList(type,user);
|
Map<Integer, List<WorkflowLink>> receivedListMap = links.stream().collect(Collectors.groupingBy(WorkflowLink::getStatus));
|
Set<Integer> statusSet = receivedListMap.keySet();
|
for (Integer status : statusSet) {
|
if (status == 5 || status == 8) {
|
statistics.put(58, statistics.get(58) + receivedListMap.get(status).size());
|
} else {
|
statistics.put(status, receivedListMap.get(status).size());
|
}
|
}
|
return statistics;
|
}
|
|
/**
|
*
|
* 二次核容和隐患故障的统计
|
*
|
* 增加了全部 0
|
* 0:全部
|
* 1:审批中
|
* 2:通过
|
* 3:驳回
|
*
|
* status含义:1-待接单,6-待审核,58-已审核
|
* @param type
|
* @param user
|
* @return
|
*/
|
public Response getReceivedStatistics2(int type, User user) {
|
Map<Integer,Integer> statistics = new HashMap<>();
|
statistics.put(0,0);
|
statistics.put(1,0);
|
statistics.put(2,0);
|
statistics.put(3,0);
|
List<WorkflowLink> links = mapper.getReceivedList2(type,user);
|
Map<Integer, List<WorkflowLink>> receivedListMap = links.stream().collect(Collectors.groupingBy(WorkflowLink::getStatus));
|
Set<Integer> statusSet = receivedListMap.keySet();
|
int sum = 0;
|
for (Integer status : statusSet) {
|
int size = receivedListMap.get(status).size();
|
if (status == 1 || status == 6) { //审批中
|
statistics.put(1, statistics.get(1) + size);
|
} else if(status == 5){ //通过
|
statistics.put(2, size);
|
}else if(status == 8){ //驳回
|
statistics.put(3,size);
|
}
|
sum+=size;
|
}
|
statistics.put(0,sum);
|
return new Response().set(1,statistics);
|
}
|
|
|
public void add(WorkflowLink link) {
|
mapper.insert(link);
|
}
|
|
|
}
|