package com.whyc.service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.whyc.constant.WorkflowEnum; import com.whyc.constant.WorkflowProcessEnum; import com.whyc.constant.WorkflowTypeEnum; import com.whyc.dto.Response; import com.whyc.mapper.WorkflowMainMapper; import com.whyc.pojo.db_user.User; import com.whyc.pojo.web_site.WorkflowLink; import com.whyc.pojo.web_site.WorkflowMain; import com.whyc.util.ActionUtil; import com.whyc.util.CommonUtil; import com.whyc.util.ThreadLocalUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import javax.servlet.ServletContext; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; @Service public class WorkflowMainService { @Resource private WorkflowMainMapper mapper; @Autowired(required = false) private WorkflowLinkService linkService; /** * 分派单号: * */ public String getNextOrderId(String typeName) { ServletContext application = ActionUtil.getApplication(); List orderIdList = (List) application.getAttribute("orderIdList"); if(orderIdList == null){ orderIdList = new LinkedList<>(); } String nextSequence = ""; QueryWrapper wrapper = Wrappers.query(); String orderId = typeName+"-"; String ymd = new SimpleDateFormat("yyyyMMdd").format(new Date()); orderId = orderId+ymd+"-"; //先查询是否缓存中是否存在前缀相同的单号,有的话存起来 List sequenceList = new LinkedList<>(); sequenceList.add(0); for (String item : orderIdList) { if(item.startsWith(orderId)){ String sequence = item.split("-")[3]; sequenceList.add(Integer.parseInt(sequence)); } } wrapper.likeRight("order_id",orderId).orderByDesc("order_id").last(" limit 1"); WorkflowMain workflowMain = mapper.selectOne(wrapper); if(workflowMain != null){ String sequence = workflowMain.getOrderId().split("-")[3]; sequenceList.add(Integer.parseInt(sequence)); } Integer maxSequence = sequenceList.stream().max(Comparator.comparing(Integer::intValue)).get(); nextSequence = String.format("%05d", maxSequence+1); String nextOrderId = orderId + nextSequence; //加入缓存中 orderIdList.add(nextOrderId); application.setAttribute("orderIdList",orderIdList); return nextOrderId; } public void add(WorkflowMain main) { mapper.insert(main); } public void addBatch(List workflowMainList) { mapper.insertBatchSomeColumn(workflowMainList); } public WorkflowMain getBaseInfo(Integer mainId) { //根据mainId查询是哪种类型 QueryWrapper query = Wrappers.query(); query.eq("id",mainId).last(" limit 1"); WorkflowMain workflowMain = mapper.selectOne(query); Integer type = workflowMain.getType(); if(type ==1){ //List tempList = tempService.getListByMainId(mainId); //workflowMain.setTempList(tempList); } List linkList = linkService.getWorkflowInfo(mainId); workflowMain.setLinkList(linkList); return workflowMain; } public List getAssignReply(Integer mainId) { QueryWrapper wrapper = Wrappers.query(); //wrapper.eq("main_id",mainId) //return linkMapper. return null; } public void updateStatus(WorkflowMain main) { mapper.updateById(main); } /** * * @param userId * @param type * @see WorkflowEnum * * @return */ public Response> getOwnStatistics(int userId, int type) { Map statistics = new HashMap<>(); statistics.put(1,0); statistics.put(2,0); statistics.put(3,0); QueryWrapper query = Wrappers.query(); query.eq("create_user_id",userId).eq("type",type); List mains = mapper.selectList(query); Map> statusListMap = mains.stream().collect(Collectors.groupingBy(WorkflowMain::getStatus)); Set statusSet = statusListMap.keySet(); for (Integer status : statusSet) { statistics.put(status,statusListMap.get(status).size()); } return new Response>().set(1,statistics); } /** * 二次核容和故障隐患的统计 * * 增加了全部 key=0 * @param type ={0,2,3} 0代表二次核容和故障隐患一起 * @see WorkflowEnum * * @return */ public Response> getOwnStatistics2(int userId, int type) { Map statistics = new HashMap<>(); statistics.put(0,0); statistics.put(1,0); statistics.put(2,0); statistics.put(3,0); QueryWrapper query = Wrappers.query(); query.eq("create_user_id", userId); if(type == 0){ query.in("type",2,3); }else { query.eq("type", type); } List mains = mapper.selectList(query); Map> statusListMap = mains.stream().collect(Collectors.groupingBy(WorkflowMain::getStatus)); Set statusSet = statusListMap.keySet(); int sum = 0; for (Integer status : statusSet) { int size = statusListMap.get(status).size(); statistics.put(status, size); sum+=size; } statistics.put(0,sum); return new Response>().set(1,statistics); } public Response> ownListPage(int userId, int type, int status, int pageNum, int pageSize) { PageHelper.startPage(pageNum,pageSize); List mains = getOwnListByUserAndType(userId,type,status); if(type == 1) { for (WorkflowMain main : mains) { Integer id = main.getId(); //List tempList = tempService.getListByMainId(id); //main.setTempList(tempList); } } PageInfo pageInfo = new PageInfo<>(mains); return new Response>().set(1,pageInfo); } private List getOwnListByUserAndType(int userId, int type, int status) { QueryWrapper query = Wrappers.query(); if(status == 0){ query.eq("create_user_id",userId).eq("type",type).orderByDesc("id"); }else { query.eq("create_user_id", userId).eq("type", type).eq("status", status).orderByDesc("id"); } return mapper.selectList(query); } public Response> getReceivedStatistics(int type, User user) { Map statisticsMap = linkService.getReceivedStatistics(type,user); return new Response>().set(1,statisticsMap); } public Response> getReceivedListPage(int type, int status, User user, int pageNum, int pageSize) { PageHelper.startPage(pageNum,pageSize); List mains = getReceivedListByUserAndType(user,type,status); if (type == 1) { for (WorkflowMain main : mains) { Integer id = main.getId(); //List tempList = tempService.getListByMainId(id); //main.setTempList(tempList); } } PageInfo pageInfo = new PageInfo<>(mains); return new Response>().set(1, pageInfo); } private List getReceivedListByUserAndType(User user, int type, int status) { return mapper.getReceivedListByUserAndType(user, type, status); } public WorkflowMain getById(Integer mainId) { return mapper.selectById(mainId); } //查询表单编号 public int getMaxId() { Integer id = mapper.getMaxId(); if (id == null) { id = 1; } else { id = id + 1; } return id; } /** * 通用提交方法,提交给角色-角色层 * 如果related_id不为空,则需要特殊处理. 证明会影响到关联单据 */ @Transactional public Response submit(WorkflowMain main){ //1.提交到单据审批流程 //如果存在关联单据id,首先校验提交的数量 Date now = new Date(); WorkflowMain mainRelated = getById(main.getRelatedId()); if(main.getRelatedId() != null) { if (main.getQuantity() > mainRelated.getQuantity()) { return new Response().setII(1, "入库数量不能大于维修申请关联单据的数量"); } } //主表插入 //初始化相关字段 WorkflowTypeEnum typeEnum = WorkflowTypeEnum.getByType(main.getType()); String name = typeEnum.getName(); String namingPrefix = typeEnum.getNamingPrefix(); String orderId = getNextOrderId(namingPrefix); //String title = mainTypeCN+"审批单-"+ DateUtil.YYYY_MM_DD_HH_MM_SS.format(now); String title = name + ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM_DD_HH_MM_SS, now); Integer mainStatus = WorkflowEnum.MAIN_STATUS_DEALING.getValue(); User user = CommonUtil.getUser(); WorkflowProcessEnum process = getProcessLevel(main.getType(),user.getRole()); main.setOrderId(orderId); main.setTitle(title); main.setCreateUserId(user.getId()); main.setCreateTime(now); main.setBeginTime(now); main.setStatus(mainStatus); main.setProcessStage(process.getStage()); add(main); //内存中去除已插入数据库的单号 ServletContext application = ActionUtil.getApplication(); List orderIdList = (List) application.getAttribute("orderIdList"); //校验是否去除 boolean remove = orderIdList.remove(orderId); if(!remove){ System.err.println("没有去除掉!!!!!!!!!!!!!"); } application.setAttribute("orderIdList",orderIdList); //节点表插入 WorkflowLink link = new WorkflowLink(); link.setMainId(main.getId()); link.setParentId(0); link.setProcessStage(process.getStage()); link.setProcessName(process.getName()); link.setCreateTime(now); link.setDealRoleId(getNextDealRoleId(process.getName())); link.setDealType(WorkflowEnum.TYPE_DELIVER.getValue()); link.setDealDesc(main.getTaskDesc()); link.setStatus(WorkflowEnum.LINK_STATUS_TAKING.getValue()); linkService.add(link); //如果存在关联单据id,处理关联单据及自动生成新单据 checkRelatedAndDone(main, now, mainRelated, user); return new Response().setII(1,"提交完成"); } private void checkRelatedAndDone(WorkflowMain main, Date now, WorkflowMain mainRelated, User user) { if(main.getRelatedId() != null){ //判断是申请的入库,还是报废. if (main.getQuantity() < mainRelated.getQuantity()){ //说明需要自动生成入库或者报废单据 //需要自动生成报废的申请 WorkflowMain mainAuto = new WorkflowMain(); WorkflowTypeEnum typeEnumAuto; Integer statusAuto = WorkflowEnum.MAIN_STATUS_DEALING.getValue(); WorkflowProcessEnum processAuto = WorkflowProcessEnum.PROCESS_MANAGER1SIGN; mainAuto.setCreateUserId(user.getId()); mainAuto.setCreateTime(now); mainAuto.setBeginTime(now); mainAuto.setQuantity(mainRelated.getQuantity()- main.getQuantity()); mainAuto.setProcessStage(processAuto.getStage()); mainAuto.setStatus(statusAuto); if(main.getType() == WorkflowTypeEnum.DEVICE_IN.getType().intValue()) { //申请的入库 mainAuto.setTaskDesc("提交了维修后的入库申请,系统自动生成剩余数量的报废申请"); typeEnumAuto = WorkflowTypeEnum.DEVICE_SCRAP; }else { //申请的报废 mainAuto.setTaskDesc("提交了维修后的报废申请,系统自动生成剩余数量的入库申请"); typeEnumAuto = WorkflowTypeEnum.DEVICE_IN; } mainAuto.setOrderId(getNextOrderId(typeEnumAuto.getNamingPrefix())); String titleAuto = typeEnumAuto.getName() + ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM_DD_HH_MM_SS, now); mainAuto.setTitle(titleAuto); mainAuto.setType(typeEnumAuto.getType()); add(mainAuto); //自动节点生成 WorkflowLink linkAuto = new WorkflowLink(); linkAuto.setParentId(0); linkAuto.setProcessStage(processAuto.getStage()); linkAuto.setProcessName(processAuto.getName()); linkAuto.setCreateTime(now); linkAuto.setDealRoleId(getNextDealRoleId(processAuto.getName())); linkAuto.setDealType(WorkflowEnum.TYPE_DELIVER.getValue()); linkAuto.setStatus(WorkflowEnum.LINK_STATUS_TAKING.getValue()); linkAuto.setMainId(mainAuto.getId()); linkAuto.setDealDesc(mainAuto.getTaskDesc()); linkService.add(linkAuto); } //关闭关联单据的状态 WorkflowMain relatedMain = new WorkflowMain(); relatedMain.setId(main.getRelatedId()); relatedMain.setStatus(WorkflowEnum.MAIN_STATUS_END_DEALT.getValue()); relatedMain.setEndTime(now); if(main.getType() == WorkflowTypeEnum.DEVICE_IN.getType().intValue()) { relatedMain.setEndReason("用户提交维修申请后的入库申请,维修流程结束"); }else{ relatedMain.setEndReason("用户提交维修申请后的报废申请,维修流程结束"); } updateStatus(relatedMain); } } /** * TODO 每次新增流程时,需要持续维护 * 如果后续存在会签,则需要追加一个多type类型的同名方法 * @param processLevelName 流程阶段名 * @return 流程处理的下一个角色 */ private Integer getNextDealRoleId(String processLevelName) { WorkflowProcessEnum processEnum = WorkflowProcessEnum.getByName(processLevelName); switch (processEnum){ //角色:1普通用户,2管理层,3领导层,4运维班组 case PROCESS_MANAGER1SIGN: return 2; default: return 2; } } /** * TODO 每次新增流程时,需要持续维护 * @param type 流程的类型 * @param roleId 现在的角色 * @return 流程处理的下一个角色 */ private WorkflowProcessEnum getProcessLevel(Integer type,Integer roleId) { WorkflowTypeEnum typeEnum = WorkflowTypeEnum.getByType(type); String name = typeEnum.getName(); //角色:1普通用户,2管理层,3领导层,4运维班组 switch (name){ //审批顺序为 普通用户->管理员 ->普通用户(进行完结) case "设备维修申请单": case "设备入库申请单": case "设备报废申请单": case "设备出库申请单": if (roleId == 1) { return WorkflowProcessEnum.PROCESS_MANAGER1SIGN; } default: return WorkflowProcessEnum.PROCESS_SPECIALIST; } } public void updateProcessLevel(String processLevel, Integer mainId) { UpdateWrapper update = Wrappers.update(); update.set("process_level",processLevel).eq("id",mainId); mapper.update((WorkflowMain) ActionUtil.objeNull,update); } }