|
|
@@ -0,0 +1,349 @@
|
|
|
+package com.uas.ps.product.service.impl;
|
|
|
+
|
|
|
+import com.uas.ps.core.util.CollectionUtils;
|
|
|
+import com.uas.ps.entity.Product;
|
|
|
+import com.uas.ps.entity.ProductUsers;
|
|
|
+import com.uas.ps.product.entity.Constant;
|
|
|
+import com.uas.ps.product.repository.ProductDao;
|
|
|
+import com.uas.ps.product.repository.ProductUsersDao;
|
|
|
+import com.uas.ps.product.service.ProductUsersService;
|
|
|
+import com.uas.ps.product.util.ThreadUtils;
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.ui.ModelMap;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by hejq on 2017-11-08.
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class ProductUsersServiceImpl implements ProductUsersService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ProductUsersDao productUsersDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ProductDao productDao;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存个人物料信息
|
|
|
+ *
|
|
|
+ * @param productUsers 个人物料信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void save(List<ProductUsers> productUsers) {
|
|
|
+ productUsersDao.save(productUsers);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ModelMap addNewProduct(Product prodInfo) {
|
|
|
+ ModelMap map = new ModelMap();
|
|
|
+ // 先查找以前的个人物料是否存在
|
|
|
+ List<ProductUsers> productUsers = productUsersDao.findByEnuuAndUseruuAndTitleAndCmpCodeAndBrand(prodInfo.getEnUU(), prodInfo.getUserUU(), prodInfo.getTitle(), prodInfo.getCmpCode(), prodInfo.getBrand());
|
|
|
+ if(CollectionUtils.isEmpty(productUsers)) {// 如果不存在则进行其他操作,存在不处理(先查询非标)
|
|
|
+ List<Product> productList = productDao.findByTitleAndCmpCodeAndBrandAndEnUU(prodInfo.getTitle(), prodInfo.getCmpCode(), prodInfo.getBrand(), prodInfo.getEnUU());
|
|
|
+ if(CollectionUtils.isEmpty(productList)) {// 如果非标不存在再查询标准
|
|
|
+ productList = productDao.findByTitleAndPCmpCodeAndPBrandAndEnUU(prodInfo.getTitle(), prodInfo.getCmpCode(), prodInfo.getBrand(), prodInfo.getEnUU());
|
|
|
+ }
|
|
|
+ if(!CollectionUtils.isEmpty(productList)) { // 如果存在,找出其中一个进行绑定
|
|
|
+ ProductUsers prod = new ProductUsers();
|
|
|
+ prod.setDate(new Date());
|
|
|
+ prod.setEnuu(prodInfo.getEnUU());
|
|
|
+ prod.setPrid(productList.get(0).getId());
|
|
|
+ prod.setUseruu(prodInfo.getUserUU());
|
|
|
+ productUsersDao.save(prod);
|
|
|
+// logger.log("个人产品库增加产品", "关联:" + productList.get(0).getId());
|
|
|
+ map.put("success", "新增成功");
|
|
|
+ } else {// 不存在的话新增再绑定
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("mmsssss");
|
|
|
+ if(null == prodInfo.getCode()) {
|
|
|
+ String code = prodInfo.getEnUU() + sdf.format(new Date());
|
|
|
+ prodInfo.setCode(code);
|
|
|
+ } else {
|
|
|
+ List<Product> products = productDao.findByEnUUAndCode(prodInfo.getEnUU(), prodInfo.getCode());
|
|
|
+ String code = prodInfo.getEnUU() + sdf.format(new Date());
|
|
|
+ if(products.size() != 0) // 如果该企业下该编号的物料存在,重新赋值
|
|
|
+ prodInfo.setCode(code);
|
|
|
+
|
|
|
+ }
|
|
|
+// List<Role> roles = roleService.findByEnUUAndUserUU(enuu, useruu);
|
|
|
+// if(!CollectionUtils.isEmpty(roles)) {
|
|
|
+// for(Role role : roles) {
|
|
|
+// if(role.getDesc().equals("业务员") || role.getDesc().equals("销售经理")) {
|
|
|
+// prodInfo.setIsSale(Constant.YES);
|
|
|
+// } else if(role.getDesc().equals("采购员")) {
|
|
|
+// prodInfo.setIsPurchase(Constant.YES);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// prodInfo.setEnUU(enuu);
|
|
|
+ if (null == prodInfo.getUnit())
|
|
|
+ prodInfo.setUnit("PCS");
|
|
|
+ prodInfo.setSourceApp("B2B");
|
|
|
+ prodInfo.setIsPubsale(Constant.NO);
|
|
|
+ prodInfo.setIsShow(Constant.NO);
|
|
|
+ prodInfo.setStandard(Constant.NO);
|
|
|
+ prodInfo.setErpDate(new Date());
|
|
|
+ prodInfo = productDao.save(prodInfo);
|
|
|
+ ProductUsers prod = new ProductUsers();
|
|
|
+ prod.setDate(new Date());
|
|
|
+ prod.setEnuu(prodInfo.getEnUU());
|
|
|
+ prod.setPrid(prodInfo.getId());
|
|
|
+ prod.setUseruu(prodInfo.getUserUU());
|
|
|
+ productUsersDao.save(prod);
|
|
|
+// logger.log("个人产品库增加产品", "关联:" + prodInfo.getId());
|
|
|
+ map.put("success", "新增成功");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ map.put("info", "产品“" + prodInfo.getTitle() + "”已存在");
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteById(Long id) {
|
|
|
+ productUsersDao.delete(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ModelMap releaseByWorkbook(Workbook workbook, Long enUU, Long userUU) {
|
|
|
+ ModelMap modelMap = new ModelMap();
|
|
|
+ List<ProductUsers> productUsers = new ArrayList<>();
|
|
|
+ List<String> alters = new ArrayList<>();
|
|
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
+ int rowNum = sheet.getLastRowNum();
|
|
|
+ Row headerRow = sheet.getRow(0);
|
|
|
+ int total = 0;
|
|
|
+ if (headerRow != null) {
|
|
|
+ for (int r = 3; r <= rowNum; r++) {
|
|
|
+ Row row = sheet.getRow(r);
|
|
|
+ if (row != null && row.getCell(0) != null && row.getCell(0).getCellType() != Cell.CELL_TYPE_BLANK) {
|
|
|
+ total++;
|
|
|
+ Product product = new Product();
|
|
|
+ ProductUsers prod = new ProductUsers();
|
|
|
+ // 物料编号
|
|
|
+ if (row.getCell(0) != null) {
|
|
|
+ row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
+ product.setCode(row.getCell(0).getStringCellValue().trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 物料名称
|
|
|
+ if (row.getCell(1) != null) {
|
|
|
+ row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
+ product.setTitle(row.getCell(1).getStringCellValue().trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 原厂型号
|
|
|
+ if (row.getCell(2) != null) {
|
|
|
+ row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
+ product.setCmpCode(row.getCell(2).getStringCellValue().trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 品牌
|
|
|
+ if (row.getCell(3) != null) {
|
|
|
+ row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
+ product.setBrand(row.getCell(3).getStringCellValue().trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 单位
|
|
|
+ if (row.getCell(4) != null) {
|
|
|
+ row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
+ product.setUnit(row.getCell(4).getStringCellValue().trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 最小包装量
|
|
|
+ if (row.getCell(5) != null) {
|
|
|
+ row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
+ if (null != row.getCell(5).getStringCellValue()
|
|
|
+ && !row.getCell(5).getStringCellValue().trim().equals("")) {
|
|
|
+ product.setMinPack(Double.valueOf(row.getCell(5).getStringCellValue()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 最小订购量
|
|
|
+ if (row.getCell(6) != null) {
|
|
|
+ row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
+ if (null != row.getCell(6).getStringCellValue().trim()
|
|
|
+ && !row.getCell(6).getStringCellValue().trim().equals("")) {
|
|
|
+ product.setMinOrder(Double.valueOf(row.getCell(6).getStringCellValue()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 交货周期
|
|
|
+ if (row.getCell(7) != null) {
|
|
|
+ row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
+ if (null != row.getCell(7).getStringCellValue().trim()
|
|
|
+ && !row.getCell(7).getStringCellValue().trim().equals("")) {
|
|
|
+ product.setLeadTime(Double.valueOf(row.getCell(7).getStringCellValue()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 交货提前期
|
|
|
+ if (row.getCell(8) != null) {
|
|
|
+ row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
|
|
|
+ if (null != row.getCell(8).getStringCellValue().trim()
|
|
|
+ && !row.getCell(8).getStringCellValue().trim().equals("")) {
|
|
|
+ product.setLtinstock(Double.valueOf(row.getCell(8).getStringCellValue()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 判断物料编号是否存在
|
|
|
+ if(product.getCode() != null ) {
|
|
|
+ List<Product> prods = productDao.findByEnUUAndCode(enUU, product.getCode());
|
|
|
+ if(prods.size() > 0) {
|
|
|
+ // 生成随机物料编码
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("mmsssss");
|
|
|
+ String code = enUU + sdf.format(new Date());
|
|
|
+ product.setCode(code);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ // 生成随机物料编码
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("mmsssss");
|
|
|
+ String code = enUU + sdf.format(new Date());
|
|
|
+ product.setCode(code);
|
|
|
+ }
|
|
|
+ List<ProductUsers> existProds = productUsersDao.findByEnuuAndUseruuAndTitleAndCmpCodeAndBrand
|
|
|
+ (enUU, userUU, product.getTitle(), product.getCmpCode(), product.getBrand());
|
|
|
+ if(CollectionUtils.isEmpty(existProds)) {
|
|
|
+ List<Product> prods = productDao.findByTitleAndCmpCodeAndBrandAndEnUU(
|
|
|
+ product.getTitle(), product.getCmpCode(), product.getBrand(), enUU);
|
|
|
+ if(CollectionUtils.isEmpty(prods)) {// 如果非标不存在再查询标准
|
|
|
+ prods = productDao.findByTitleAndPCmpCodeAndPBrandAndEnUU(product.getTitle(), product.getCmpCode(), product.getBrand(), enUU);
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isEmpty(prods)) {
|
|
|
+ // 如果物料不存在,先生成物料信息
|
|
|
+ product.setEnUU(enUU);
|
|
|
+ product.setUserUU(userUU);
|
|
|
+ product.setSourceApp("B2B");
|
|
|
+ product.setIsPubsale(Constant.NO);
|
|
|
+ product.setIsShow(Constant.NO);
|
|
|
+ product.setStandard(Constant.NO);
|
|
|
+ product.setErpDate(new Date());
|
|
|
+// List<Role> roles = roleService.findByEnUUAndUserUU(enuu, useruu);
|
|
|
+// if(!CollectionUtils.isEmpty(roles)) {
|
|
|
+// for(Role role : roles) {
|
|
|
+// if(role.getDesc().equals("业务员") || role.getDesc().equals("销售经理")) {
|
|
|
+// product.setIsSale(Constant.YES);
|
|
|
+// } else if(role.getDesc().equals("采购员")) {
|
|
|
+// product.setIsPurchase(Constant.YES);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+ product = productDao.save(product);
|
|
|
+ prod.setDate(new Date());
|
|
|
+ prod.setEnuu(enUU);
|
|
|
+ prod.setPrid(product.getId());
|
|
|
+ prod.setUseruu(userUU);
|
|
|
+ } else {
|
|
|
+ Product oldProd = prods.get(0);
|
|
|
+ prod.setDate(new Date());
|
|
|
+ prod.setEnuu(enUU);
|
|
|
+ prod.setPrid(oldProd.getId());
|
|
|
+ prod.setUseruu(userUU);
|
|
|
+ }
|
|
|
+ productUsers.add(prod);
|
|
|
+ } else {
|
|
|
+ alters.add(existProds.get(0).getProduct().getTitle());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ modelMap.put("total", total);
|
|
|
+ if (!CollectionUtils.isEmpty(productUsers)) {
|
|
|
+ try {
|
|
|
+ productUsers = productUsersDao.save(productUsers);
|
|
|
+ modelMap.put("success", productUsers.size());
|
|
|
+ if(alters.size() > 0)
|
|
|
+ modelMap.put("alters", alters);
|
|
|
+ } catch (Exception e) {
|
|
|
+ modelMap.put("error", "物料添加失败");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ modelMap.put("success", 0);
|
|
|
+ if(alters.size() > 0)
|
|
|
+ modelMap.put("alters", alters);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return modelMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ModelMap coverToUserProdByIds(String ids, Long userUU, Long enUU) {
|
|
|
+ ids = ids.replace("["," ");
|
|
|
+ ids = ids.replace("]"," ");
|
|
|
+ Integer count = productUsersDao.coverToUserProdByIds(ids, userUU, enUU);
|
|
|
+// // 存储过程改为代码实现
|
|
|
+// List<Long> prIds = JSON.parseArray(ids, Long.class);
|
|
|
+// // 保存的个人产品
|
|
|
+// List<ProductUsers> productUsersList = new ArrayList<>();
|
|
|
+// for (Long prId : prIds) {
|
|
|
+// List<ProductUsers> productUsers = productUsersDao.findByEnuuAndUseruuAndPrid(enUU, userUU, prId);
|
|
|
+// // 不存在才保存
|
|
|
+// if (CollectionUtils.isEmpty(productUsers)) {
|
|
|
+// ProductUsers productUser = new ProductUsers(enUU, userUU, prId);
|
|
|
+// productUsersList.add(productUser);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// productUsersList = productUsersDao.save(productUsersList);
|
|
|
+// logger.log("我的产品库", "新增了我产品信息", "size:" + count);
|
|
|
+ return new ModelMap("count", count);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void coverToUserProdById(Long id, Long userUU, Long enUU) {
|
|
|
+ List<ProductUsers> prods = productUsersDao.findByEnuuAndUseruuAndPrid(enUU, userUU, id);
|
|
|
+ if(CollectionUtils.isEmpty(prods)) {
|
|
|
+// Product product = productDao.findOne(id);
|
|
|
+// List<Role> roles = roleService.findByEnUUAndUserUU(enuu, useruu);
|
|
|
+// // 根据当前用户角色更新物料销售采购属性
|
|
|
+// if(!CollectionUtils.isEmpty(roles)) {
|
|
|
+// for(Role role : roles) {
|
|
|
+// if(role.getDesc().equals("业务员") || role.getDesc().equals("销售经理")) {
|
|
|
+// if(product.getIsSale() == null || product.getIsSale().equals(Constant.NO)) {
|
|
|
+// product.setIsSale(Constant.YES);
|
|
|
+// productDao.save(product);
|
|
|
+// }
|
|
|
+// } else if(role.getDesc().equals("采购员")) {
|
|
|
+// if(product.getIsPurchase() == null || product.getIsPurchase().equals(Constant.NO)) {
|
|
|
+// product.setIsPurchase(Constant.YES);
|
|
|
+// productDao.save(product);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+ ProductUsers prod = new ProductUsers();
|
|
|
+ prod.setUseruu(userUU);
|
|
|
+ prod.setEnuu(enUU);
|
|
|
+ prod.setPrid(id);
|
|
|
+ prod.setDate(new Date());
|
|
|
+ prod = productUsersDao.save(prod);
|
|
|
+// logger.log("我的产品库", "新增了我产品信息", "id:" + prod.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteByBatch(List<Long> idList) {
|
|
|
+ List<ProductUsers> prods = productUsersDao.findAll(idList);
|
|
|
+ productUsersDao.delete(prods);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将物料信息同步到个人信息库
|
|
|
+ *
|
|
|
+ * @param productId
|
|
|
+ */
|
|
|
+ public void coverToMyProduct(final Long productId, final Long enuu, final Long useruu, final String method) {
|
|
|
+// logger.log("我的产品库", method + ":" + productId);
|
|
|
+ ThreadUtils.task(new Runnable() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ productUsersDao.coverToUserByPrid(productId, enuu, useruu);
|
|
|
+ }
|
|
|
+ }).run();
|
|
|
+ }
|
|
|
+}
|