package com.iamberry.rst.controllers.approval; import com.auth0.jwt.internal.org.apache.commons.io.FileUtils; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.iamberry.rst.core.approval.*; import com.iamberry.rst.core.page.PageRequest; import com.iamberry.rst.core.page.PagedResult; import com.iamberry.rst.core.sys.Admin; import com.iamberry.rst.faces.approval.ApplyPickService; import com.iamberry.rst.faces.approval.ApprovalOrderService; import com.iamberry.rst.faces.approval.ApprovalProductionService; import com.iamberry.rst.faces.approval.ApprovalTaskService; import com.iamberry.rst.faces.sys.SysService; import com.iamberry.wechat.core.entity.WechatUtils; import com.iamberry.wechat.file.FileType; import com.iamberry.wechat.tools.ResponseJson; import com.iamberry.wechat.tools.ResultInfo; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.activiti.bpmn.model.*; import org.activiti.bpmn.model.Process; import org.activiti.engine.RepositoryService; import org.activiti.engine.repository.Deployment; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.multipart.commons.CommonsMultipartFile; import sun.misc.BASE64Decoder; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import java.io.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** * Created by Administrator on 2018/10/9. */ @Controller /*@RequestMapping("/approval")*/ @RequestMapping("/wechat/approval") public class ApprovalController { @Autowired private ApprovalOrderService approvalOrderService; @Autowired private ApprovalTaskService approvalTaskService; @Autowired private SysService sysService; @Autowired private ApplyPickService applyPickService; @Autowired private ApprovalProductionService approvalProductionService; @Autowired private RepositoryService repositoryService; @SuppressWarnings("restriction") private BASE64Decoder decoder = new BASE64Decoder(); /** * 新增审批信息 * @param request * @param approvalProduction * @param productionItems * @return */ @ResponseBody @RequestMapping(value = "/addApproval") public ResponseJson addApproval(HttpServletRequest request, ApprovalProduction approvalProduction, String productionItems,String productionContracts) throws ParseException { ResponseJson rj =new ResponseJson(200, "添加成功", 200); if (approvalProduction == null || productionItems == null) { return ResponseJson.getFAILURE(); } //获取openid,根据openid查询当前登录人信息 String openId = WechatUtils.getUserBySession(request).getUserOpenid(); /*String openId = "oEy-_s5C7LwmLGTpEGfJoecfsYZc";*/ //获取审批任务id Map taskinfo = approvalTaskService.salesmanSubmitApply(1); String deliveryDate = request.getParameter("deliveryDate"); String inspectionTime = request.getParameter("inspectionDate"); if(deliveryDate == null ){ return ResponseJson.getFAILURE(); } if(inspectionTime == null ){ return ResponseJson.getFAILURE(); } SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); approvalProduction.setProductionDeliveryDate(format.parse(deliveryDate)); approvalProduction.setInspectionTime(format.parse(inspectionTime)); //封装生产审批产品项 List listApprovalProductionItem = new ArrayList(); JSONArray jsonProductionItems = JSONArray.fromObject(productionItems); for (int i = 0; i < jsonProductionItems.size(); i++) { JSONObject orderItem = jsonProductionItems.getJSONObject(i); //添加产品项内容 ApprovalProductionItem approvalProductionItem = new ApprovalProductionItem(); approvalProductionItem.setItemBrandName(orderItem.getString("itemBrandName")); approvalProductionItem.setItemProductName(orderItem.getString("itemProductName")); approvalProductionItem.setItemColorName(orderItem.getString("itemColorName")); approvalProductionItem.setItemProductNum(orderItem.getInt("itemProductNum")); approvalProductionItem.setItemProductModel(orderItem.getString("itemProductModel")); approvalProductionItem.setItemProductUnit(orderItem.getString("itemProductUnit")); approvalProductionItem.setItemProductBoxNumber(orderItem.getInt("itemProductBoxNumber")); approvalProductionItem.setItemShouldStorage(orderItem.getInt("itemProductNum")); approvalProductionItem.setItemActualStorage(0); approvalProductionItem.setItemLackNumber(orderItem.getInt("itemProductNum")); approvalProductionItem.setItemCanpickNumber(0); if (orderItem.containsKey("itemExternalModel")) { approvalProductionItem.setItemExternalModel(orderItem.getString("itemExternalModel")); } listApprovalProductionItem.add(approvalProductionItem); } //封装生产审批合同 List listProductionContract = new ArrayList(); if(productionContracts != null){ JSONArray jsonproductionContracts = JSONArray.fromObject(productionContracts); for (int i = 0; i < jsonproductionContracts.size(); i++) { JSONObject contract = jsonproductionContracts.getJSONObject(i); ApprovalProductionContract contractInfo = new ApprovalProductionContract(); contractInfo.setContractUrl(contract.getString("contractUrl")); contractInfo.setContractType(contract.getString("contractType")); listProductionContract.add(contractInfo); } } boolean returnValue = approvalOrderService.insertApprovalProduction(approvalProduction,listApprovalProductionItem,listProductionContract,taskinfo,openId); if(!returnValue){ return ResponseJson.getFAILURE(); } return rj; } /** * 上传合同图片 * 通过流的方式上传文件 * @RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象 */ @ResponseBody @RequestMapping("/fileUpload") public ResponseJson fileUpload(HttpServletRequest request,@RequestParam("file") CommonsMultipartFile file) throws IOException { ResponseJson responseJson = new ResponseJson(); System.out.println("fileName:"+file.getOriginalFilename()); //获取项目根目录 WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext(); ServletContext servletContext = webApplicationContext.getServletContext(); String realPath = servletContext.getRealPath(ResultInfo.CONTRACT_IMAGE); String imgUrl = new Date().getTime()+file.getOriginalFilename(); try { //获取输出流 OutputStream os=new FileOutputStream(realPath+imgUrl); //获取输入流 CommonsMultipartFile 中可以直接得到文件的流 InputStream is=file.getInputStream(); byte[] bts = new byte[1024]; //一个一个字节的读取并写入 while(is.read(bts)!=-1) { os.write(bts); } os.flush(); os.close(); is.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } responseJson.setReturnCode(200); responseJson.addResponseKeyValue("imgUrl",ResultInfo.CONTRACT_IMAGE+imgUrl); return responseJson; } /** * base图片上传 * @throws IOException */ @SuppressWarnings({ "deprecation", "restriction" }) @ResponseBody @RequestMapping("/baseFileUpload") public ResponseJson baseFileUpload(HttpServletRequest req) throws IOException{ ResponseJson responseJson = new ResponseJson(); //取到前台传过来的 base64图片 并创建要存放在项目中的路径 WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext(); ServletContext servletContext = webApplicationContext.getServletContext(); String imgFilePath = servletContext.getRealPath(ResultInfo.CONTRACT_IMAGE); try { isExitsPath(imgFilePath); } catch (InterruptedException e) { e.printStackTrace(); } //创建分享对象 并存入数据库 //String postParams = StrUtils.getPostParamsStr(req); //data:image/png;base64,iVBORw0 String postParams = req.getParameter("file"); String[] shareImgUrlArr = org.apache.commons.lang3.StringUtils.split(postParams, ","); String imgUrl = ""; HashMap map = new HashMap(); if(shareImgUrlArr != null && shareImgUrlArr.length > 0){ // 防止内存溢出 String suffixName = new String (shareImgUrlArr[0].split(";")[0].split("/")[1]); String shareImgUrl = new String (shareImgUrlArr[1]); // 通过文件头判断后缀 byte [] b = new byte[28]; shareImgUrl.getBytes(0, 27, b, 0); String temp = new String(b); b = decoder.decodeBuffer(temp); // 二进制转换文件头 String type = null; String fileHead = byte2hex(b); fileHead = fileHead.toUpperCase(); // 获取系统支持的文件类型,如果不支持,返回默认的图片 FileType[] fileTypes = FileType.values(); for (FileType fileType : fileTypes) { if (fileHead.startsWith(fileType.getValue())) { type = fileType.getSuffix(); break; } } if (type == null) { System.err.println("上传图片异常..." + fileHead); } else { String fileName = getFileName(suffixName, type); boolean flag = generateImage(shareImgUrl, imgFilePath + "/" + fileName); if(flag){ responseJson.setReturnCode(200); responseJson.addResponseKeyValue("imgUrl","//rst.iamberry.com/"+ResultInfo.CONTRACT_IMAGE+fileName); }else{ responseJson.setReturnCode(500); } } }else{ responseJson.setReturnCode(500); } //返回值拼凑 /*String requurl = req.getContextPath();//获取web项目的路径*/ return responseJson; } /** * 判断路径是否存在 不存在则创建 * create date 2016年6月1日 * @author 穆再兴 * @param path * @return * @throws InterruptedException */ public boolean isExitsPath(String path)throws InterruptedException{ String [] paths=path.split("/"); StringBuilder fullPath=new StringBuilder(); for (int i = 0; i < paths.length; i++) { fullPath.append(paths[i]).append("/"); File file=new File(fullPath.toString()); if(!file.exists()){ file.mkdir(); } } File file=new File(fullPath.toString());//目录全路径 if (!file.exists()) { return true; }else{ return false; } } public static String byte2hex(byte[] b) { //一个字节的数, // 转成16进制字符串 String hs = ""; String tmp = ""; for (int n = 0; n < b.length; n++) { //整数转成十六进制表示 tmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); if (tmp.length() == 1) { hs = hs + "0" + tmp; } else { hs = hs + tmp; } } tmp = null; return hs; } /** * 获取 file 的文件名 * create date 2016年6月1日 * @author 穆再兴 * @param * @param * @return */ private String getFileName(String suffixName, String type){ long time = new Date().getTime(); Long timeStr = new Long(time); return timeStr + "." + type; } /** * 对字节数组字符串进行Base64解码生成图片 并存入对应的路径 * create date 2016年6月1日 * @author 穆再兴 * @param imgStr Base64字符串 * @param imgFilePath 存入的路径 * @return */ private boolean generateImage(String imgStr, String imgFilePath) { if (imgStr == null) {return false;}// 图像数据为空 try { // Base64解码 @SuppressWarnings("restriction") byte[] bytes = decoder.decodeBuffer(imgStr); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) {// 调整异常数据 bytes[i] += 256; } } // 生成图片 File file = new File(imgFilePath); FileUtils.writeByteArrayToFile(file, bytes); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 查询我发起的/我审核的列表 * @param approvalInfo * @param pageSize * @param pageNO * @param totalNum * @param selectType 1.我发起的 2.我审核的 3.仓库确认 * @param * @return * @throws Exception */ @ResponseBody @RequestMapping(value = "/approvalInfoList") public ResponseJson listApprovalInfo(HttpServletRequest request, ApprovalInfo approvalInfo,Integer selectType, @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize, @RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNO, @RequestParam(value = "totalNum", defaultValue = "0", required = false) Integer totalNum) throws Exception { ResponseJson rj =new ResponseJson(200, "查询成功", 200); //获取openid,根据openid查询当前登录人信息 String openId = WechatUtils.getUserBySession(request).getUserOpenid(); /*String openId = "oEy-_s5C7LwmLGTpEGfJoecfsYZc";*/ Admin admin = sysService.getByOpenid(openId); if(admin == null){ return new ResponseJson(200, "未查询到登录人信息", 500); } if(selectType != null){ if(selectType == 1){//我发起的 approvalInfo.setAdminId(admin.getAdminId()); }else if(selectType == 2){//我审核的 approvalInfo.setApprovalAdminId(admin.getAdminId()); } if(selectType == 3){ //判断是否是仓库人员 AdminApprovalAuthority adminApprovalAuthority = approvalOrderService.getApprovalAuthority(admin.getAdminId()); if(adminApprovalAuthority.getIsWarehouse() == 2){ List list = new ArrayList<>(); return new ResponseJson(200, "非仓库人员不得查询", 501).addResponseKeyValue("approvalList",list); } } } String stareDate = request.getParameter("stareDate"); if(stareDate != null ){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); approvalInfo.setApprovalCreateTime(format.parse(stareDate)); } PagedResult pagedResult = approvalOrderService.listApprovalInfo(pageNO, pageSize,approvalInfo,totalNum == null); if (totalNum != 0) { pagedResult.setTotal(totalNum); } rj.addResponseKeyValue("total", pagedResult.getTotal()); rj.addResponseKeyValue("approvalList",pagedResult.getDataList()); rj.addResponseKeyValue("admin",admin); return rj; } /** * 修改审批状态 (1 通过 2驳回 3撤销 4评论) * @param * @return * @throws Exception */ @ResponseBody @RequestMapping(value = "/updateApprovalInfo") public ResponseJson updateApprovalInfo(HttpServletRequest request, ApprovalInfo approvalInfo,Integer status) throws Exception { ResponseJson rj =new ResponseJson(200, "操作成功", 200); if(status == null){ return ResponseJson.getFAILURE(); } if(approvalInfo == null){ return ResponseJson.getFAILURE(); } //获取openid,根据openid查询当前登录人信息 String openId = WechatUtils.getUserBySession(request).getUserOpenid(); /*String openId = "oEy-_s_o_1oIv6h_KAs7QkeTQiJY";*/ Admin admin = sysService.getByOpenid(openId); if(admin == null){ return new ResponseJson(200, "未查询到登录人信息", 500); } rj = approvalOrderService.auditApprovalinfo(approvalInfo,admin,status); return rj; } /** * 查询审批订单详情 * @param approvalInfo * @return * @throws Exception */ @ResponseBody @RequestMapping(value = "/getApprovalInfo") public ResponseJson getApprovalInfo(HttpServletRequest request, ApprovalInfo approvalInfo) throws Exception { ResponseJson rj =new ResponseJson(200, "查询成功", 200); if(approvalInfo == null){ return ResponseJson.getFAILURE(); } //获取openid,根据openid查询当前登录人信息 String openId = WechatUtils.getUserBySession(request).getUserOpenid(); /*String openId = "oEy-_s5C7LwmLGTpEGfJoecfsYZc";*/ Admin admin = sysService.getByOpenid(openId); if(admin == null){ return new ResponseJson(200, "未查询到登录人信息", 500); } //判断是否是仓库人员 int isDelivery = 2; /*if(admin.getAdminDept() == 8){//8为仓储部门 isDelivery = 1; }*/ //判断是否是仓库人员 AdminApprovalAuthority adminApprovalAuthority = approvalOrderService.getApprovalAuthority(admin.getAdminId()); if(adminApprovalAuthority.getIsWarehouse() == 1){ isDelivery = 1; } HistoricalRecord historicalRecord = new HistoricalRecord(); historicalRecord.setApprovalId(approvalInfo.getApprovalId()); historicalRecord.setAdminId(admin.getAdminId()); historicalRecord.setRecordState(1); historicalRecord = approvalOrderService.getHistoricalRecord(historicalRecord); List info = approvalOrderService.getApprovalInfo(approvalInfo); if(info.size() < 1){ return ResponseJson.getFAILURE(); } rj.addResponseKeyValue("approvalInfo",info.get(0)); rj.addResponseKeyValue("historicalRecord",historicalRecord); rj.addResponseKeyValue("adminid",admin.getAdminId()); rj.addResponseKeyValue("isDelivery",isDelivery); return rj; } /** * 添加入库信息 * @param request * @param inventoryRecordss * @return */ @ResponseBody @RequestMapping(value = "/addInventoryRecords") public ResponseJson addInventoryRecords(HttpServletRequest request, String inventoryRecordss){ if (inventoryRecordss == null || inventoryRecordss.equals("")) { return ResponseJson.getFAILURE(); } //获取openid,根据openid查询当前登录人信息 String openId = WechatUtils.getUserBySession(request).getUserOpenid(); /*String openId = "oEy-_s5C7LwmLGTpEGfJoecfsYZc";*/ Admin admin = sysService.getByOpenid(openId); if(admin == null){ return new ResponseJson(200, "未查询到登录人信息", 500); } return approvalOrderService.insertInventoryRecords(inventoryRecordss,admin); } /** * 获取某个审批的整个审批流程及仓库审批人信息 1:生成订单审批;2:提货申请 * @param * @return * @throws Exception */ @ResponseBody @RequestMapping(value = "/getFindApplyRu") public ResponseJson getFindApplyRu(HttpServletRequest request, Integer type) throws Exception { ResponseJson rj =new ResponseJson(200, "查询成功", 200); if(type == null){ return ResponseJson.getFAILURE(); } //获取openid,根据openid查询当前登录人信息 String openId = WechatUtils.getUserBySession(request).getUserOpenid(); Admin currentAdmin = sysService.getByOpenid(openId); if(currentAdmin == null){ return new ResponseJson(200, "未查询到登录人信息", 500); } List info = approvalTaskService.findApplyRu(type); if(info.size() < 1){ return ResponseJson.getFAILURE(); } List listString = new ArrayList<>(); for (Object obj : info){ String[] strings = (String[])obj; String[] str = new String[strings.length]; for(int i = 0;i< strings.length;i++){ Admin admin = sysService.getByName(strings[i]); if(admin == null){ return new ResponseJson(200, "未找到管理员信息("+strings[i]+")", 500); } str[i] = admin.getAdminName(); } listString.add(str); } AdminApprovalAuthority authority = new AdminApprovalAuthority(); authority.setIsWarehouse(1); List listAuthority = approvalOrderService.listApprovalAuthority(authority); Admin adminTwo = new Admin(); adminTwo.setAdminId(listAuthority.get(0).getAdminId()); List listAdmin = sysService.listAdminInfo(adminTwo); rj.addResponseKeyValue("list",listString); rj.addResponseKeyValue("warehouseAdmin",listAdmin); rj.addResponseKeyValue("currentAdmin",currentAdmin); return rj; } /** * 查询审批产品项 * @param approvalId * @return productionItem * @throws Exception */ @ResponseBody @RequestMapping(value = "/getProductionItem") public ResponseJson getProductionItem(HttpServletRequest request,Integer approvalId) throws Exception { ResponseJson rj =new ResponseJson(200, "查询成功", 200); if(approvalId == null){ return ResponseJson.getFAILURE(); } ApprovalProduction approvalProduction = new ApprovalProduction(); approvalProduction.setApprovalId(approvalId); List approvalProductionList = approvalProductionService.getApprovalProductionList(approvalProduction); approvalProduction = approvalProductionList.get(0); ApprovalProductionItem approvalProductionItem = new ApprovalProductionItem(); approvalProductionItem.setProductionId(approvalProduction.getProductionId()); List approvalProductionItemList = approvalProductionService.getApprovalProductionItemList(approvalProductionItem); rj.addResponseKeyValue("apItemList",approvalProductionItemList); return rj; } /** * 查询入库记录 * @param approvalId * @return productionItem * @throws Exception */ @ResponseBody @RequestMapping(value = "/listInventoryRecords") public ResponseJson listInventoryRecords(HttpServletRequest request,Integer approvalId) throws Exception { ResponseJson rj =new ResponseJson(200, "查询成功", 200); if(approvalId == null){ return ResponseJson.getFAILURE(); } List listRecords = approvalOrderService.listInventoryRecords(approvalId); rj.addResponseKeyValue("listRecords",listRecords); return rj; } /** * 获取优尼雅管理员权限 * @param request * @return */ @ResponseBody @RequestMapping(value = "/getApprovalAuthority") public ResponseJson getApprovalAuthority(HttpServletRequest request){ ResponseJson rj =new ResponseJson(200, "查询成功", 200); //获取openid,根据openid查询当前登录人信息 String openId = WechatUtils.getUserBySession(request).getUserOpenid(); Admin currentAdmin = sysService.getByOpenid(openId); if(currentAdmin == null){ return new ResponseJson(200, "未查询到登录人信息", 500); } if(currentAdmin.getAdminDept() !=9){//9:优尼雅 return new ResponseJson(200, "不允许非优尼雅部门员工查询", 500); } AdminApprovalAuthority adminApprovalAuthority = approvalOrderService.getApprovalAuthority(currentAdmin.getAdminId()); rj.addResponseKeyValue("adminApprovalAuthority",adminApprovalAuthority); return rj; } /** * 查询待办数量 * @param request * @return */ @ResponseBody @RequestMapping(value = "/getTo_do") public ResponseJson getTo_do(HttpServletRequest request){ //获取openid,根据openid查询当前登录人信息 String openId = WechatUtils.getUserBySession(request).getUserOpenid(); /*String openId = "oEy-_s_o_1oIv6h_KAs7QkeTQiJY";*/ Admin admin = sysService.getByOpenid(openId); ResponseJson rj =new ResponseJson(200, "查询成功", 200); if(admin == null){ return new ResponseJson(200, "未查询到登录人信息", 500); } if(admin.getAdminDept() !=9){//9:优尼雅 return new ResponseJson(200, "不允许非优尼雅部门员工查询", 500); } AdminApprovalAuthority adminApprovalAuthority = approvalOrderService.getApprovalAuthority(admin.getAdminId()); if(adminApprovalAuthority == null){ return new ResponseJson(200, "未找到权限信息", 500); } if(adminApprovalAuthority.getIsSales() == 1){//查询销售 提货数量 ApprovalInfo approvalInfo = new ApprovalInfo(); approvalInfo.setAdminId(admin.getAdminId()); approvalInfo.setSelectState(5);//查询待审批条数 List listInfo = approvalOrderService.getApprovalInfo(approvalInfo); rj.addResponseKeyValue("salesPickNum",listInfo.size()); }else{ rj.addResponseKeyValue("salesPickNum",0); } if(adminApprovalAuthority.getIsPmc() == 1 || adminApprovalAuthority.getIsManagement() == 1){ ApprovalInfo approvalInfo = new ApprovalInfo(); approvalInfo.setApprovalAdminId(admin.getAdminId()); approvalInfo.setSelectState(1);//查询待审批条数 List listInfo = approvalOrderService.getApprovalInfo(approvalInfo); rj.addResponseKeyValue("approvalNum",listInfo.size()); }else{ rj.addResponseKeyValue("approvalNum",0); } if(adminApprovalAuthority.getIsWarehouse() == 1){ ApprovalInfo approvalInfo = new ApprovalInfo(); approvalInfo.setSelectState(3); List listInfo = approvalOrderService.getApprovalInfo(approvalInfo); rj.addResponseKeyValue("warehouseNum",listInfo.size()); }else{ rj.addResponseKeyValue("warehouseNum",0); } return rj; } /*@ResponseBody @RequestMapping(value = "/deploymentPrecessDefinition") public void deploymentPrecessDefinition(){ getBpmnModel(); repositoryService.deleteDeployment("310008",true); repositoryService.deleteDeployment("427508",true); repositoryService.deleteDeployment("427501",true); repositoryService.deleteDeployment("422501",true); *//*Deployment deployment = repositoryService.createDeployment() .key("GenerationApproval") .name("提交审批流程") .addClasspathResource("RST.bpmn") .deploy(); Deployment deployment = repositoryService.createDeployment() .key("ApplicationForDelivery") .name("提交提货订单审批流程") .addClasspathResource("pickRst.bpmn") .deploy();*//* //获取审批任务id Map taskinfo = approvalTaskService.salesmanSubmitApply(1); String[] split = (String[])taskinfo.get("users"); for (String name : split){ System.out.println(name); } Map taskinfo2 = approvalTaskService.salesmanSubmitApply(2); String[] split2 = (String[])taskinfo2.get("users"); for (String name : split2){ System.out.println(name); } }*/ }