Browse Source

客户管家 导入模板修改 产品导入修改

Min 1 year ago
parent
commit
35976102a0

+ 4 - 4
fhKeeper/formulahousekeeper/management-crm/src/main/java/com/management/platform/controller/ProductController.java

@@ -64,9 +64,9 @@ public class ProductController {
     * @Date: 2024/5/21
     */
     @RequestMapping("/list")
-    public HttpRespMsg list(String userId,String productName,String productCode,Integer productType,Integer status,Integer pageIndex,Integer pageSize){
+    public HttpRespMsg list(String userId,String productName,String productCode,Integer productType,Integer status,String startTime,String endTime,Integer pageIndex,Integer pageSize){
         Integer companyId = userMapper.selectById(request.getHeader("token")).getCompanyId();
-        return productService.getList(companyId,userId,productName,productCode,productType,status,pageIndex,pageSize);
+        return productService.getList(companyId,userId,productName,productCode,productType,status,startTime,endTime,pageIndex,pageSize);
     }
 
     /**
@@ -197,8 +197,8 @@ public class ProductController {
     * @Date: 2024/5/21
     */
     @RequestMapping("/exportData")
-    public HttpRespMsg exportData(String userId,String productName,String productCode,Integer productType,Integer status) throws Exception {
-        return productService.exportData(userId,productName,productCode,productType,status);
+    public HttpRespMsg exportData(String userId,String productName,String productCode,Integer productType,Integer status,String startTime,String endTime) throws Exception {
+        return productService.exportData(userId,productName,productCode,productType,status,startTime,endTime);
     }
 
     /**

+ 2 - 2
fhKeeper/formulahousekeeper/management-crm/src/main/java/com/management/platform/service/ProductService.java

@@ -17,11 +17,11 @@ import java.lang.reflect.InvocationTargetException;
  */
 public interface ProductService extends IService<Product> {
 
-    HttpRespMsg getList(Integer companyId,String userId, String productName, String productCode,Integer productType,Integer status, Integer pageIndex, Integer pageSize);
+    HttpRespMsg getList(Integer companyId,String userId, String productName, String productCode,Integer productType,Integer status,String startDate,String endDate, Integer pageIndex, Integer pageSize);
 
     HttpRespMsg importData(MultipartFile multipartFile);
 
-    HttpRespMsg exportData(String userId, String productName, String productCode,Integer productType,Integer status) throws Exception;
+    HttpRespMsg exportData(String userId, String productName, String productCode,Integer productType,Integer status,String startDate,String endDate) throws Exception;
 
     HttpRespMsg getDetail(Integer id);
 

+ 50 - 25
fhKeeper/formulahousekeeper/management-crm/src/main/java/com/management/platform/service/impl/ProductServiceImpl.java

@@ -19,6 +19,11 @@ 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.apache.poi.xssf.streaming.SXSSFWorkbook;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
@@ -30,7 +35,9 @@ import javax.servlet.http.HttpServletRequest;
 import java.io.*;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.math.BigDecimal;
 import java.time.DateTimeException;
+import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
 import java.time.format.DateTimeFormatter;
@@ -75,7 +82,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
     private AttachmentCenterMapper attachmentCenterMapper;
 
     @Override
-    public HttpRespMsg getList(Integer companyId,String userId, String productName, String productCode,Integer productType,Integer status, Integer pageIndex, Integer pageSize) {
+    public HttpRespMsg getList(Integer companyId,String userId, String productName, String productCode,Integer productType,Integer status,String startDate,String endDate, Integer pageIndex, Integer pageSize) {
         HttpRespMsg msg=new HttpRespMsg();
         User targetUsr = userMapper.selectById(request.getHeader("token"));
         List<Department> departments = departmentMapper.selectList(new LambdaQueryWrapper<Department>().eq(Department::getCompanyId, companyId));
@@ -133,6 +140,12 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
         if(status!=null){
             queryWrapper.eq(Product::getStatus,status);
         }
+        if(startDate!=null && endDate!=null){
+            DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy-MM-dd");
+            LocalDateTime start = LocalDate.parse(startDate, df).atTime(LocalTime.MIN);
+            LocalDateTime end = LocalDate.parse(endDate, df).atTime(LocalTime.MAX);
+            queryWrapper.between(Product::getCreateTime,start,end);
+        }
         if(pageIndex==null&&pageSize==null){
             pageIndex=-1;
             pageSize=-1;
@@ -189,9 +202,9 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
             inputStream.close();
             outputStream.close();
             //解析表格
-            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
+            XSSFWorkbook workbook = new XSSFWorkbook(file);
             //我们只需要第一个sheet
-            HSSFSheet sheet = workbook.getSheetAt(0);
+            XSSFSheet sheet = workbook.getSheetAt(0);
             //由于第一行需要指明列对应的标题
             int rowNum = sheet.getLastRowNum();
             //获取当前表单模板 校验规则
@@ -207,7 +220,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
             List<String> userNameList=new ArrayList<>();
             HttpRespMsg respMsg=new HttpRespMsg();
             for (int rowIndex = 0; rowIndex <= rowNum; rowIndex++) {
-                HSSFRow row = sheet.getRow(rowIndex);
+                XSSFRow row = sheet.getRow(rowIndex);
                 if (row == null) {
                     continue;
                 }
@@ -220,7 +233,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
                 for (int i = 0; i < cellNum; i++) {
                     JSONObject item = configObJSONArray.getJSONObject(i);
                     String modelName = item.getString("model");
-                    HSSFCell cell = row.getCell(i);
+                    XSSFCell cell = row.getCell(i);
                     if(cell!=null){
                         switch (item.getString("type")){
                             case "time":cell.setCellType(CellType.NUMERIC);
@@ -245,8 +258,9 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
                 }
             }
             List<User> targetUserList= (List<User>) respMsg.data;
-            for (int rowIndex = 0; rowIndex <= rowNum; rowIndex++) {
-                HSSFRow row = sheet.getRow(rowIndex);
+            //直接忽略空行 从row1开始
+            for (int rowIndex = 1; rowIndex <= rowNum; rowIndex++) {
+                XSSFRow row = sheet.getRow(rowIndex);
                 if (row == null) {
                     continue;
                 }
@@ -265,7 +279,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
                     String className = modelName.substring(0, 1).toUpperCase() + modelName.substring(1);
                     String getter="get"+className;
                     String setter="set"+className;
-                    HSSFCell cell = row.getCell(i);
+                    XSSFCell cell = row.getCell(i);
                     if(cell!=null){
                         switch (item.getString("type")){
                             case "time":cell.setCellType(CellType.NUMERIC);
@@ -274,8 +288,6 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
                         }
                     }
 //                    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");
@@ -293,9 +305,9 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
                             if(first.isPresent()){
                                 product.setId(first.get().getId());
                             }
+                            product.setProductCode(cell.getStringCellValue());
                         }
-                    }
-                    if(modelName.equals("inchargerId")){
+                    }else if(modelName.equals("inchargerId")){
                         if(!StringUtils.isEmpty(cell.getStringCellValue())){
                             String userName = cell.getStringCellValue();
                             Optional<User> first;
@@ -308,11 +320,11 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
                             if (first.isPresent()) {
                                 product.setInchargerId(first.get().getId());
                             } else {
-                                throw new Exception("["+userName+"]在系统中不存在");
+                                msg.setError("["+userName+"]在系统中不存在");
+                                return msg;
                             }
                         }
-                    }
-                    if(modelName.equals("status")){
+                    }else if(modelName.equals("status")){
                         if(!StringUtils.isEmpty(cell.getStringCellValue())){
                             switch (cell.getStringCellValue()){
                                 case "下架":product.setStatus(0);
@@ -321,18 +333,25 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
                                 break;
                             }
                         }
-                    }
-                    if(modelName.equals("type")){
+                    }else if(modelName.equals("price")){
+                        if(!StringUtils.isEmpty(cell.getStringCellValue())){
+                            product.setPrice(new BigDecimal(cell.getStringCellValue()));
+                        }
+                    }else if(modelName.equals("inventory")){
+                        if(!StringUtils.isEmpty(cell.getStringCellValue())){
+                            product.setInventory(new BigDecimal(cell.getStringCellValue()).intValue());
+                        }
+                    }else if(modelName.equals("type")){
                         if(!StringUtils.isEmpty(cell.getStringCellValue())){
                             Optional<SysDict> first = sysDictOfProductType.stream().filter(s -> s.getName().equals(cell.getStringCellValue())).findFirst();
                             if(first.isPresent()){
                                 product.setType(first.get().getId());
                             }else {
-                                throw new Exception("产品类型["+cell.getStringCellValue()+"不存在,请在系统字典中增加");
+                                msg.setError("产品类型["+cell.getStringCellValue()+"]不存在,请在系统字典中增加");
+                                return msg;
                             }
                         }
-                    }
-                    if(modelName.equals("unit")){
+                    } else if(modelName.equals("unit")){
                         if(!StringUtils.isEmpty(cell.getStringCellValue())){
                             Optional<SysDict> first = sysDictOfProductUnit.stream().filter(s -> s.getName().equals(cell.getStringCellValue())).findFirst();
                             if(first.isPresent()){
@@ -341,10 +360,14 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
                                 throw new Exception("产品单位["+cell.getStringCellValue()+"不存在,请在系统字典中增加");
                             }
                         }
+                    }else {
+                        if(!StringUtils.isEmpty(cell.getStringCellValue())){
+                            Class<Product> productClass = Product.class;
+                            Method method = productClass.getMethod(setter,String.class);
+                            method.invoke(product,cell.getStringCellValue());
+                        }
                     }
-                    if(!StringUtils.isEmpty(cell.getStringCellValue())){
-                        method.invoke(product,cell.getStringCellValue());
-                    }
+
                 }
                 importProductList.add(product);
             }
@@ -362,12 +385,14 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
             e.printStackTrace();
         } catch (Exception e) {
             e.printStackTrace();
+            msg.setError("验证失败");
+            return msg;
         }
         return msg;
     }
 
     @Override
-    public HttpRespMsg exportData(String userId, String productName, String productCode,Integer productType,Integer status) throws Exception {
+    public HttpRespMsg exportData(String userId, String productName, String productCode,Integer productType,Integer status,String startDate,String endDate) 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()));
@@ -381,7 +406,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
             titleList.add(item.getString("label"));
         }
         dataList.add(titleList);
-        HttpRespMsg respMsg = getList(user.getCompanyId(), userId, productName, productCode,productType,status, null, null);
+        HttpRespMsg respMsg = getList(user.getCompanyId(), userId, productName, productCode,productType,status,startDate,endDate, null, null);
         Map<String, Object> msgData = (Map<String, Object>) respMsg.getData();
         List<Product> productList = (List<Product>) msgData.get("record");
         for (Product product : productList) {

+ 10 - 153
fhKeeper/formulahousekeeper/management-crm/src/main/java/com/management/platform/service/impl/SysFormServiceImpl.java

@@ -11,11 +11,17 @@ import com.management.platform.mapper.SysFormMapper;
 import com.management.platform.mapper.UserMapper;
 import com.management.platform.service.SysFormService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.management.platform.util.ExcelUtil;
 import com.management.platform.util.HttpRespMsg;
 import com.management.platform.util.MessageUtils;
 import org.apache.poi.hssf.usermodel.*;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddressList;
+import org.apache.poi.xssf.streaming.SXSSFCell;
+import org.apache.poi.xssf.streaming.SXSSFRow;
+import org.apache.poi.xssf.streaming.SXSSFSheet;
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
+import org.apache.poi.xssf.usermodel.XSSFColor;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
@@ -89,164 +95,15 @@ public class SysFormServiceImpl extends ServiceImpl<SysFormMapper, SysForm> impl
             default:title="";
         }
         String result=MessageUtils.message("file.excelScu");
-        String fileName= title+".xls";
         try {
-//            response.reset();
-//            response.setHeader("Content-disposition",
-//                "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
-//            //设置文件头编码格式
-//            response.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");//设置类型
-//            response.setHeader("Cache-Control","no-cache");//设置头
-//            response.setDateHeader("Expires", 0);//设置日期头
-            // 创建工作簿
-            HSSFWorkbook workBook = new HSSFWorkbook();
-            // 创建工作类
-            HSSFSheet sheet = workBook.createSheet();
-            //设置首行冻结
-            sheet.createFreezePane(0, 1);
-            sheet.setDefaultColumnWidth(16);
-            //设置字体样式
-            HSSFFont headFont = workBook.createFont();
-            headFont.setBold(true);
-            headFont.setFontHeightInPoints((short) 10);
-            headFont.setFontName("黑体");
-
-            HSSFFont titleFont = workBook.createFont();
-            titleFont.setBold(true);
-            titleFont.setFontHeightInPoints((short) 10);
-            titleFont.setFontName("黑体");
-
-            HSSFFont font = workBook.createFont();
-            font.setFontHeightInPoints((short) 10);
-            font.setFontName("宋体");
-
-            //设置单元格样式
-            CellStyle headStyle = workBook.createCellStyle();
-            headStyle.setFont(headFont);
-            headStyle.setAlignment(HorizontalAlignment.CENTER);
-            headStyle.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);
-            headStyle.setWrapText(true);
-            headStyle.setBorderBottom(BorderStyle.THIN); //下边框
-            headStyle.setBorderLeft(BorderStyle.THIN);//左边框
-            headStyle.setBorderTop(BorderStyle.THIN);//上边框
-            headStyle.setBorderRight(BorderStyle.THIN);//右边框
-
-            String color = "c0c0c0";    //此处得到的color为16进制的字符串
-            //转为RGB码
-            int r = Integer.parseInt((color.substring(0,2)),16);   //转为16进制
-            int g = Integer.parseInt((color.substring(2,4)),16);
-            int b = Integer.parseInt((color.substring(4,6)),16);
-
-            //自定义cell颜色
-            HSSFPalette palette = workBook.getCustomPalette();
-            //这里的9是索引
-            palette.setColorAtIndex((short)9, (byte) r, (byte) g, (byte) b);
-
-            CellStyle titleStyle = workBook.createCellStyle();
-            titleStyle.setFont(titleFont);
-            titleStyle.setAlignment(HorizontalAlignment.CENTER);
-            titleStyle.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);
-            titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);  //填充单元格
-            titleStyle.setFillForegroundColor((short)9);    //填色
-            titleStyle.setWrapText(true);
-            titleStyle.setBorderBottom(BorderStyle.THIN); //下边框
-            titleStyle.setBorderLeft(BorderStyle.THIN);//左边框
-            titleStyle.setBorderTop(BorderStyle.THIN);//上边框
-            titleStyle.setBorderRight(BorderStyle.THIN);//右边框
-
-            CellStyle cellStyle = workBook.createCellStyle();
-            cellStyle.setFont(font);
-            cellStyle.setAlignment(HorizontalAlignment.CENTER);
-            cellStyle.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);
-            cellStyle.setWrapText(true);
-            cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
-            cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
-            cellStyle.setBorderTop(BorderStyle.THIN);//上边框
-            cellStyle.setBorderRight(BorderStyle.THIN);//右边框
-            cellStyle.setDataFormat(workBook.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
-            if(allList.size() > 0) {
-                int start = 0;
-                for(List<String> rowList : allList) {
-                    HSSFRow row = sheet.createRow(start);
-                    row.setHeightInPoints(24);
-                    for(int i = 0; i < rowList.size(); i++) {
-                        HSSFCell cell = row.createCell(i);
-                        ClientAnchor anchor = new HSSFClientAnchor();
-                        if(start == 0) {
-                            cell.setCellStyle(titleStyle);
-                            // 关键修改
-                            anchor.setDx1(0);
-                            anchor.setDx2(0);
-                            anchor.setDy1(0);
-                            anchor.setDy2(0);
-                            anchor.setCol1(cell.getColumnIndex());
-                            anchor.setRow1(cell.getRowIndex());
-                            anchor.setCol2(cell.getColumnIndex() + 5);
-                            anchor.setRow2(cell.getRowIndex() + 6);
-                            // 结束
-                            Drawing drawing = sheet.createDrawingPatriarch();
-                            Comment comment = null;
-                            String[] textList=null;
-                            DVConstraint constraint =null;
-                            CellRangeAddressList regions =null;
-                            HSSFDataValidation data_validation_list =null;
-//                            switch (rowList.get(i)){
-//                                case "是否为公共项目":
-//                                case "Whether it is a public project":
-//                                    // 加载下拉列表内容
-//                                    //textList= new String[]{"是", "否"};
-//                                    textList= new String[]{MessageUtils.message("excel.yes"), MessageUtils.message("excel.no")};
-//                                    constraint = DVConstraint
-//                                            .createExplicitListConstraint(textList);
-//                                    // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
-//                                    regions = new CellRangeAddressList(1,
-//                                            1000, i, i);
-//                                    // 数据有效性对象
-//                                    data_validation_list = new HSSFDataValidation(
-//                                            regions, constraint);
-//                                    sheet.addValidationData(data_validation_list);
-//                                    comment = drawing.createCellComment(anchor);
-//                                    // 输入批注信息
-//                                    //comment.setString(new HSSFRichTextString("是否为公共项目\n" +
-//                                    //"是:公共项目\n" +
-//                                    //"否:普通项目"));
-//                                    comment.setString(new HSSFRichTextString(MessageUtils.message("excel.publicProject")+"\n" +
-//                                            MessageUtils.message("excel.yesPublic")+"\n" +
-//                                            MessageUtils.message("excel.noPublic")));
-//                                    cell.setCellComment(comment);
-//                                    break;
-//                            }
-                        }else {
-                            cell.setCellStyle(cellStyle);
-                        }
-                        cell.setCellValue(rowList.get(i));
-                    }
-                    start++;
-                }
-            }
-            //用于非传统ajax;
-//            String headStr = "attachment; filename=\"" + fileName + "\"";
-//            response.setContentType("APPLICATION/OCTET-STREAM");//返回格式为流
-//            response.setHeader("Content-Disposition", headStr);
-//            //普通下载不需要以上三行,注掉即可
-//            OutputStream os = response.getOutputStream();//在线下载
-            File dir = null;
-            dir = new File(path);
-            // D://cloud/upload 文件上传后所存储的位置,部署到服务器上时配置服务器地址即可
-            if (!dir.exists()) {
-                dir.mkdirs();
-            }
-            FileOutputStream os = new FileOutputStream(path+fileName);//保存到本地
-            workBook.write(os);
-            os.flush();
-            os.close();
+            String resp = ExcelUtil.exportGeneralExcelByTitleAndList(title, allList, path);
+            msg.setData(resp);
+            return msg;
         }catch(Exception e) {
             e.printStackTrace();
             //msg.setError("已存在模板名称为["+fileName+"]的文件,请删除后重新下载");
-            msg.setError(MessageUtils.message("file.duplicateTemplate",fileName));
+            msg.setError(MessageUtils.message("file.duplicateTemplate",title));
             return msg;
         }
-        msg.data= "/upload/"+fileName;
-        return msg;
     }
 }