|
@@ -1,20 +1,33 @@
|
|
|
package com.management.platform.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.management.platform.entity.*;
|
|
|
import com.management.platform.mapper.*;
|
|
|
-import com.management.platform.service.DepartmentService;
|
|
|
-import com.management.platform.service.OrderService;
|
|
|
+import com.management.platform.service.*;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import com.management.platform.service.SysFunctionService;
|
|
|
+import com.management.platform.util.ExcelUtil;
|
|
|
import com.management.platform.util.HttpRespMsg;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+import org.apache.poi.ss.usermodel.CellType;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.*;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -43,6 +56,14 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
private DepartmentOtherManagerMapper departmentOtherManagerMapper;
|
|
|
@Resource
|
|
|
private SysDictMapper sysDictMapper;
|
|
|
+ @Resource
|
|
|
+ private ExcelExportService excelExportService;
|
|
|
+ @Resource
|
|
|
+ private SysFormMapper sysFormMapper;
|
|
|
+ @Resource
|
|
|
+ private WxCorpInfoService wxCorpInfoService;
|
|
|
+ @Value(value = "${upload.path}")
|
|
|
+ private String path;
|
|
|
|
|
|
@Override
|
|
|
public HttpRespMsg getList(String userId, String orderName,String orderCode, String productCode, Integer pageIndex, Integer pageSize,Integer isDelete) {
|
|
@@ -126,6 +147,219 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|
|
return msg;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg importData(MultipartFile multipartFile) {
|
|
|
+ HttpRespMsg msg=new HttpRespMsg();
|
|
|
+ String fileName = multipartFile.getOriginalFilename();
|
|
|
+ File file = new File(fileName == null ? "file" : fileName);
|
|
|
+ User user = userMapper.selectById(request.getHeader("token"));
|
|
|
+ Integer companyId = user.getCompanyId();
|
|
|
+ WxCorpInfo wxCorpInfo = wxCorpInfoService.getOne(new LambdaQueryWrapper<WxCorpInfo>().eq(WxCorpInfo::getCompanyId, companyId));
|
|
|
+ List<Order> orderList = orderMapper.selectList(new LambdaQueryWrapper<Order>().eq(Order::getCompanyId, companyId));
|
|
|
+ List<User> userList = userMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getCompanyId, companyId));
|
|
|
+ List<SysDict> sysDictOfProductType = sysDictMapper.selectList(new LambdaQueryWrapper<SysDict>().eq(SysDict::getCompanyId, companyId).eq(SysDict::getCode, "ProductType"));
|
|
|
+ List<SysDict> sysDictOfProductUnit = sysDictMapper.selectList(new LambdaQueryWrapper<SysDict>().eq(SysDict::getCompanyId, companyId).eq(SysDict::getCode, "ProductUnit"));
|
|
|
+ InputStream inputStream = null;
|
|
|
+ OutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ inputStream = multipartFile.getInputStream();
|
|
|
+ outputStream = new FileOutputStream(file);
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int temp = 0;
|
|
|
+ while ((temp = inputStream.read(buffer, 0, 4096)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, temp);
|
|
|
+ }
|
|
|
+ inputStream.close();
|
|
|
+ outputStream.close();
|
|
|
+ //解析表格
|
|
|
+ HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
|
|
|
+ //我们只需要第一个sheet
|
|
|
+ HSSFSheet sheet = workbook.getSheetAt(0);
|
|
|
+ //由于第一行需要指明列对应的标题
|
|
|
+ int rowNum = sheet.getLastRowNum();
|
|
|
+ //获取当前表单模板 校验规则
|
|
|
+ SysForm sysForm = sysFormMapper.selectOne(new LambdaQueryWrapper<SysForm>().eq(SysForm::getCode, "Product").eq(SysForm::getCompanyId, companyId).eq(SysForm::getIsCurrent, 1));
|
|
|
+ if(sysForm==null){
|
|
|
+ msg.setError("当前模块未配置自定义模板,需先完成配置");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ String config = sysForm.getConfig();
|
|
|
+ JSONObject configOb = JSON.parseObject(config);
|
|
|
+ JSONArray configObJSONArray = configOb.getJSONArray("list");
|
|
|
+ List<Order> importOrderList=new ArrayList<>();
|
|
|
+ List<String> userNameList=new ArrayList<>();
|
|
|
+ HttpRespMsg respMsg=new HttpRespMsg();
|
|
|
+ for (int rowIndex = 0; rowIndex <= rowNum; rowIndex++) {
|
|
|
+ HSSFRow row = sheet.getRow(rowIndex);
|
|
|
+ if (row == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //跳过空行
|
|
|
+ if (ExcelUtil.isRowEmpty(row)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //获取到当前行的列数据
|
|
|
+ int cellNum = row.getLastCellNum();
|
|
|
+ for (int i = 0; i < cellNum; i++) {
|
|
|
+ JSONObject item = configObJSONArray.getJSONObject(i);
|
|
|
+ String modelName = item.getString("model");
|
|
|
+ HSSFCell cell = row.getCell(i);
|
|
|
+ if(cell!=null){
|
|
|
+ switch (item.getString("type")){
|
|
|
+ case "time":cell.setCellType(CellType.NUMERIC);
|
|
|
+ break;
|
|
|
+ default:cell.setCellType(CellType.STRING);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(modelName.equals("inchargerId")){
|
|
|
+ if(!StringUtils.isEmpty(cell.getStringCellValue())){
|
|
|
+ userNameList.add(cell.getStringCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("参与搜素的人员列表"+userNameList + userNameList.size());
|
|
|
+ if(wxCorpInfo!=null&&wxCorpInfo.getSaasSyncContact()==1&&userNameList.size()>0){
|
|
|
+ respMsg = wxCorpInfoService.getBatchSearchUserInfo(wxCorpInfo, userNameList,null);
|
|
|
+ if(respMsg.code.equals("0")){
|
|
|
+ msg.setError("姓名为["+String.valueOf(respMsg.data)+"]的人员存在重复,请使用工号!");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<User> targetUserList= (List<User>) respMsg.data;
|
|
|
+ for (int rowIndex = 0; rowIndex <= rowNum; rowIndex++) {
|
|
|
+ HSSFRow row = sheet.getRow(rowIndex);
|
|
|
+ if (row == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //跳过空行
|
|
|
+ if (ExcelUtil.isRowEmpty(row)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //获取到当前行的列数据
|
|
|
+ int cellNum = row.getLastCellNum();
|
|
|
+ Order order=new Order();
|
|
|
+ order.setCompanyId(companyId);
|
|
|
+ order.setCreatorId(user.getId());
|
|
|
+ for (int i = 0; i < cellNum; i++) {
|
|
|
+ JSONObject item = configObJSONArray.getJSONObject(i);
|
|
|
+ String modelName = item.getString("model");
|
|
|
+ String className = modelName.substring(0, 1).toUpperCase() + modelName.substring(1);
|
|
|
+ String getter="get"+className;
|
|
|
+ String setter="set"+className;
|
|
|
+ HSSFCell cell = row.getCell(i);
|
|
|
+ if(cell!=null){
|
|
|
+ switch (item.getString("type")){
|
|
|
+ case "time":cell.setCellType(CellType.NUMERIC);
|
|
|
+ break;
|
|
|
+ default:cell.setCellType(CellType.STRING);
|
|
|
+ }
|
|
|
+ }
|
|
|
+// Class<?> productClass = Class.forName("com.management.platform.entity.Product");
|
|
|
+ Class<Product> productClass = Product.class;
|
|
|
+ Method method = productClass.getMethod(setter, String.class);
|
|
|
+ //校验当前列是否为必填
|
|
|
+ JSONObject options = item.getJSONObject("options");
|
|
|
+ JSONObject rules = options.getJSONObject("rules");
|
|
|
+ Boolean required = rules.getBoolean("required");
|
|
|
+ if(required){
|
|
|
+ if(StringUtils.isEmpty(cell.getStringCellValue())){
|
|
|
+ msg.setError(item.getString("label")+"值不能为空值");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(modelName.equals("orderCode")){
|
|
|
+ if(!StringUtils.isEmpty(cell.getStringCellValue())){
|
|
|
+ //系统中同公司已存在的产品编码 更新
|
|
|
+ Optional<Order> first = orderList.stream().filter(p ->p.getOrderCode()!=null&& p.getOrderCode().equals(cell.getStringCellValue())).findFirst();
|
|
|
+ if(first.isPresent()){
|
|
|
+ order.setId(first.get().getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(modelName.equals("inchargerId")){
|
|
|
+ if(!StringUtils.isEmpty(cell.getStringCellValue())){
|
|
|
+ String userName = cell.getStringCellValue();
|
|
|
+ Optional<User> first;
|
|
|
+ if(wxCorpInfo!=null&&wxCorpInfo.getSaasSyncContact()==1){
|
|
|
+ Optional<User> optional = targetUserList.stream().filter(tl -> tl.getName().equals(userName)).findFirst();
|
|
|
+ first= userList.stream().filter(u ->(u.getJobNumber()!=null&&u.getJobNumber().equals(userName))||(optional.isPresent()&&u.getCorpwxUserid()!=null&&u.getCorpwxUserid().equals(optional.get().getCorpwxUserid()))).findFirst();
|
|
|
+ }else {
|
|
|
+ first= userList.stream().filter(u -> u.getName().equals(userName)||(u.getJobNumber()!=null&&u.getJobNumber().equals(userName))).findFirst();
|
|
|
+ }
|
|
|
+ if (first.isPresent()) {
|
|
|
+ order.setInchargerId(first.get().getId());
|
|
|
+ } else {
|
|
|
+ throw new Exception("["+userName+"]在系统中不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!StringUtils.isEmpty(cell.getStringCellValue())){
|
|
|
+ method.invoke(order,cell.getStringCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ importOrderList.add(order);
|
|
|
+ }
|
|
|
+ if(importOrderList.size()>0){
|
|
|
+ if(!saveOrUpdateBatch(importOrderList)){
|
|
|
+ msg.setError("验证失败");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException | NoSuchMethodException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg exportData(String userId, String orderName, String orderCode, String productCode) throws Exception {
|
|
|
+ User user = userMapper.selectById(request.getHeader("token"));
|
|
|
+ SysForm sysForm = sysFormMapper.selectOne(new LambdaQueryWrapper<SysForm>().eq(SysForm::getCompanyId, user.getCompanyId()).eq(SysForm::getCode, "Product").eq(SysForm::getIsCurrent, 1));
|
|
|
+ WxCorpInfo wxCorpInfo = wxCorpInfoService.getOne(new LambdaQueryWrapper<WxCorpInfo>().eq(WxCorpInfo::getCompanyId, user.getCompanyId()));
|
|
|
+ String config = sysForm.getConfig();
|
|
|
+ JSONObject configOb = JSON.parseObject(config);
|
|
|
+ JSONArray configObJSONArray = configOb.getJSONArray("list");
|
|
|
+ List<List<String>> dataList=new ArrayList<>();
|
|
|
+ List<String> titleList=new ArrayList<>();
|
|
|
+ for (int i = 0; i < configObJSONArray.size(); i++) {
|
|
|
+ JSONObject item = configObJSONArray.getJSONObject(i);
|
|
|
+ titleList.add(item.getString("label"));
|
|
|
+ }
|
|
|
+ dataList.add(titleList);
|
|
|
+ HttpRespMsg respMsg = getList( userId, orderName,orderCode, productCode, null, null,0);
|
|
|
+ Map<String, Object> msgData = (Map<String, Object>) respMsg.getData();
|
|
|
+ List<Product> productList = (List<Product>) msgData.get("record");
|
|
|
+ for (Product product : productList) {
|
|
|
+ List<String> item=new ArrayList<>();
|
|
|
+ for (int i = 0; i < configObJSONArray.size(); i++) {
|
|
|
+ JSONObject target = configObJSONArray.getJSONObject(i);
|
|
|
+ String model = target.getString("model");
|
|
|
+ String targetName = model.substring(0, 1).toUpperCase() + model.substring(1);
|
|
|
+ Class<? extends Product> aClass = product.getClass();
|
|
|
+ String value = String.valueOf(aClass.getMethod("get" + targetName).invoke(product)==null?"":aClass.getMethod("get" + targetName).invoke(product));
|
|
|
+ if(model.equals("inchargerId")){
|
|
|
+ if(wxCorpInfo!=null&&wxCorpInfo.getSaasSyncContact()==1){
|
|
|
+ value = "$userName"+String.valueOf(aClass.getMethod("getInchargerName").invoke(product))+"$";
|
|
|
+ }else {
|
|
|
+ value = String.valueOf(aClass.getMethod("getInchargerName").invoke(product));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ item.add(value);
|
|
|
+ }
|
|
|
+ dataList.add(item);
|
|
|
+ }
|
|
|
+ String fileName="销售表导出_"+ System.currentTimeMillis();
|
|
|
+ return excelExportService.exportGeneralExcelByTitleAndList(wxCorpInfo,fileName,dataList,path);
|
|
|
+ }
|
|
|
+
|
|
|
//递归获取子部门
|
|
|
public List<Integer> getBranchDepartment(Integer departmentId, List<Department> departmentList) {
|
|
|
List<Integer> list = new ArrayList<>();
|