|
|
@@ -0,0 +1,296 @@
|
|
|
+package com.uas.platform.b2b.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.dubbo.common.utils.CollectionUtils;
|
|
|
+import com.uas.platform.b2b.dao.ProductDao;
|
|
|
+import com.uas.platform.b2b.dao.ProductUsersDao;
|
|
|
+import com.uas.platform.b2b.dao.RoleDao;
|
|
|
+import com.uas.platform.b2b.model.Product;
|
|
|
+import com.uas.platform.b2b.model.ProductUsers;
|
|
|
+import com.uas.platform.b2b.model.Role;
|
|
|
+import com.uas.platform.b2b.service.ProductUsersService;
|
|
|
+import com.uas.platform.b2b.service.RoleService;
|
|
|
+import com.uas.platform.b2b.support.SystemSession;
|
|
|
+import com.uas.platform.b2b.support.UsageBufferedLogger;
|
|
|
+import com.uas.platform.core.logging.BufferedLoggerManager;
|
|
|
+import com.uas.platform.core.model.Constant;
|
|
|
+import com.uas.platform.core.model.PageInfo;
|
|
|
+import com.uas.search.b2b.model.SPage;
|
|
|
+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.data.domain.Page;
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.ui.ModelMap;
|
|
|
+
|
|
|
+import javax.persistence.criteria.CriteriaBuilder;
|
|
|
+import javax.persistence.criteria.CriteriaQuery;
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
+import javax.persistence.criteria.Root;
|
|
|
+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;
|
|
|
+
|
|
|
+ private final static UsageBufferedLogger logger = BufferedLoggerManager.getLogger(UsageBufferedLogger.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RoleService roleService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public SPage<ProductUsers> findProductInfoByPageInfo(PageInfo pageInfo) {
|
|
|
+ Page<ProductUsers> products = findAllProudctByPageInfo(pageInfo);
|
|
|
+ SPage<ProductUsers> prods = new SPage<ProductUsers>();
|
|
|
+ prods.setContent(products.getContent());
|
|
|
+ prods.setPage(products.getNumber());
|
|
|
+ prods.setSize(products.getSize());
|
|
|
+ prods.setTotalElement(products.getTotalElements());
|
|
|
+ prods.setTotalPage(products.getTotalPages());
|
|
|
+ return prods;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Page<ProductUsers> findAllProudctByPageInfo(final PageInfo pageInfo) {
|
|
|
+ return productUsersDao.findAll(new Specification<ProductUsers>() {
|
|
|
+ public Predicate toPredicate(Root<ProductUsers> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
|
|
+ pageInfo.filter("enuu", SystemSession.getUser().getEnterprise().getUu());
|
|
|
+ pageInfo.filter("useruu", SystemSession.getUser().getUserUU());
|
|
|
+ query.where(pageInfo.getPredicates(root, query, builder));
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }, pageInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ModelMap addNewProduct(Product prodInfo) {
|
|
|
+ ModelMap map = new ModelMap();
|
|
|
+ // 先查找以前的个人物料是否存在
|
|
|
+ Long enuu = SystemSession.getUser().getEnterprise().getUu();
|
|
|
+ Long useruu = SystemSession.getUser().getUserUU();
|
|
|
+ List<ProductUsers> productUsers = productUsersDao.findByEnuuAndUseruuAndTitleAndSpecAndBrand(enuu, useruu, prodInfo.getTitle(), prodInfo.getSpec(), prodInfo.getBrand());
|
|
|
+ if(CollectionUtils.isEmpty(productUsers)) {// 如果不存在则进行其他操作,存在不处理
|
|
|
+ List<Product> productList = productDao.findByTitleAndSpecAndBrandAndEnUU(prodInfo.getTitle(), prodInfo.getSpec(), prodInfo.getBrand(), SystemSession.getUser().getEnterprise().getUu());
|
|
|
+ if(!CollectionUtils.isEmpty(productList)) { // 如果存在,找出其中一个进行绑定
|
|
|
+ ProductUsers prod = new ProductUsers();
|
|
|
+ prod.setDate(new Date());
|
|
|
+ prod.setEnuu(enuu);
|
|
|
+ prod.setPrid(productList.get(0).getId());
|
|
|
+ prod.setUseruu(useruu);
|
|
|
+ productUsersDao.save(prod);
|
|
|
+ logger.log("个人产品库增加产品", "关联:" + productList.get(0).getId());
|
|
|
+ map.put("success", "新增成功");
|
|
|
+ } else {// 不存在的话新增再绑定
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("mmsssss");
|
|
|
+ if(null == prodInfo.getCode()) {
|
|
|
+ String code = SystemSession.getUser().getEnterprise().getUu() + sdf.format(new Date());
|
|
|
+ prodInfo.setCode(code);
|
|
|
+ } else {
|
|
|
+ List<Product> products = productDao.findByEnUUAndCode(enuu, prodInfo.getCode());
|
|
|
+ String code = SystemSession.getUser().getEnterprise().getUu() + 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.getName().equals("业务员") || role.getName().equals("销售经理")) {
|
|
|
+ prodInfo.setIsSale(Constant.YES);
|
|
|
+ } else if(role.getName().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(enuu);
|
|
|
+ prod.setPrid(prodInfo.getId());
|
|
|
+ prod.setUseruu(useruu);
|
|
|
+ productUsersDao.save(prod);
|
|
|
+ logger.log("个人产品库增加产品", "关联:" + prodInfo.getId());
|
|
|
+ map.put("success", "新增成功");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ map.put("success", "新增成功");
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteById(Long id) {
|
|
|
+ productUsersDao.delete(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ModelMap releaseByWorkbook(Workbook workbook) {
|
|
|
+ ModelMap modelMap = new ModelMap();
|
|
|
+ List<ProductUsers> productUsers = new ArrayList<ProductUsers>();
|
|
|
+ Long enuu = SystemSession.getUser().getEnterprise().getUu();
|
|
|
+ Long useruu = SystemSession.getUser().getUserUU();
|
|
|
+ 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());
|
|
|
+ product.setSpec(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 = SystemSession.getUser().getEnterprise().getUu() + sdf.format(new Date());
|
|
|
+ product.setCode(code);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ // 生成随机物料编码
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("mmsssss");
|
|
|
+ String code = SystemSession.getUser().getEnterprise().getUu() + sdf.format(new Date());
|
|
|
+ product.setCode(code);
|
|
|
+ }
|
|
|
+ List<ProductUsers> existProds = productUsersDao.findByEnuuAndUseruuAndTitleAndSpecAndBrand
|
|
|
+ (enuu, useruu, product.getTitle(), product.getSpec(), product.getBrand());
|
|
|
+ if(CollectionUtils.isEmpty(existProds)) {
|
|
|
+ List<Product> prods = productDao.findByTitleAndSpecAndBrandAndEnUU(
|
|
|
+ product.getTitle(), product.getSpec(), product.getBrand(), enuu);
|
|
|
+ if (CollectionUtils.isEmpty(prods)) {
|
|
|
+ // 如果物料不存在,先生成物料信息
|
|
|
+ product.setEnUU(SystemSession.getUser().getEnterprise().getUu());
|
|
|
+ product.setUserUU(SystemSession.getUser().getUserUU());
|
|
|
+ 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.getName().equals("业务员") || role.getName().equals("销售经理")) {
|
|
|
+ product.setIsSale(Constant.YES);
|
|
|
+ } else if(role.getName().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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ modelMap.put("total", total);
|
|
|
+ if (!CollectionUtils.isEmpty(productUsers)) {
|
|
|
+ try {
|
|
|
+ productUsers = productUsersDao.save(productUsers);
|
|
|
+ modelMap.put("success", productUsers.size());
|
|
|
+ } catch (Exception e) {
|
|
|
+ modelMap.put("error", "物料添加失败");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ modelMap.put("success", 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return modelMap;
|
|
|
+ }
|
|
|
+}
|