| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- package com.uas.ps.pricing.service.impl;
- import com.uas.ps.pricing.dao.BomDao;
- import com.uas.ps.pricing.dao.BomDetailDao;
- import com.uas.ps.pricing.dao.PriceLibraryDao;
- import com.uas.ps.pricing.model.Bom;
- import com.uas.ps.pricing.model.BomDetail;
- import com.uas.ps.pricing.model.PriceLibrary;
- import com.uas.ps.pricing.service.PriceLibraryService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.ui.ModelMap;
- import org.springframework.util.CollectionUtils;
- import org.springframework.util.StringUtils;
- import java.util.*;
- /**
- * 价格库实现类
- *
- * @author liusw
- * @date 2018-04-23 11:12
- */
- @Service
- public class PriceLibraryServiceImpl implements PriceLibraryService{
- @Autowired
- private PriceLibraryDao priceLibraryDao;
- @Autowired
- private BomDetailDao bomDetailDao;
- @Autowired
- private BomDao bomDao;
- /**
- * 核价规则
- */
- private static String[] PRICERULES = {"VALIDPURCPRICE", "ORDERPURCPRICE", "CHECKUNITPRICE", "WAREHOUSINGPRICE", "MATSTANDARDUNITPRICE", "REPAVGUNITPRICE", "OFFERPRICE"};
- @Override
- public ModelMap savePrice(List<PriceLibrary> priceLibraries) {
- ModelMap modelMap = new ModelMap();
- try {
- priceLibraries = priceLibraryDao.save(priceLibraries);
- modelMap.put("success", true);
- modelMap.put("data", priceLibraries.size());
- } catch (Exception e) {
- e.printStackTrace();
- modelMap.put("success", false);
- modelMap.put("message", e.getMessage());
- }
- return modelMap;
- }
- @Override
- public ModelMap bomPricing(Bom bom) {
- ModelMap modelMap = new ModelMap();
- try {
- List<BomDetail> bomDetails = bom.getBomDetails();
- List<PriceLibrary> priceLibraries = new ArrayList<>();
- bom = bomDao.save(bom);
- for (BomDetail bomDetail : bomDetails) {
- if (!StringUtils.isEmpty(bomDetail.getTaxUnitPrice()) && bomDetail.getTaxUnitPrice() != 0) {
- // 进行核价
- bomDetail = countPrice(bomDetail, bom.getEnuu());
- bomDetail.setBdId(bom.getBid());
- bomDetail.setBomId(bom.getBomId());
- bomDetailDao.save(bomDetail);
- // 保存经过成本计算的价格
- priceLibraries.add(bomDetailConvert(bomDetail, bom.getEnuu()));
- }
- }
- List<Bom> boms = bomDao.findByEnuuAndBomId(bom.getEnuu(), bom.getBomId());
- if (CollectionUtils.isEmpty(boms)) {
- priceLibraryDao.save(priceLibraries);
- }
- bom.setBomDetails(bomDetails);
- modelMap.put("success", true);
- modelMap.put("data", bom);
- } catch (Exception e) {
- e.printStackTrace();
- modelMap.put("success", false);
- modelMap.put("message", e.getMessage());
- }
- return modelMap;
- }
- private PriceLibrary bomDetailConvert(BomDetail bomDetail, Long buyerUU) {
- PriceLibrary priceLirary = new PriceLibrary();
- priceLirary.setBrand(bomDetail.getBrand());
- priceLirary.setBuyerEnuu(buyerUU);
- priceLirary.setCmpCode(bomDetail.getCmpCode());
- priceLirary.setCode(bomDetail.getCode());
- priceLirary.setCurrency(bomDetail.getCurrency());
- priceLirary.setInbrand(bomDetail.getInbrand());
- priceLirary.setKind(bomDetail.getProductTitle());
- priceLirary.setPrice(bomDetail.getTaxUnitPrice());
- priceLirary.setTaxRate(bomDetail.getTaxRate());
- priceLirary.setSource("ERP");
- priceLirary.setVendEnuu(bomDetail.getVendUU());
- priceLirary.setVendName(bomDetail.getVendName());
- priceLirary.setUasCode(bomDetail.getProductCode());
- priceLirary.setSpec(bomDetail.getSpec());
- priceLirary.setPriceType("BOMCOUNTPRICE");
- return priceLirary;
- }
- /**
- * 核价
- * @param bomDetail
- * @return
- */
- private BomDetail countPrice(BomDetail bomDetail, Long enuu) {
- // 匹配 如果bomDetail里面有原厂型号和品牌
- Set<PriceLibrary> prices = matchPrice(bomDetail, enuu);
- if (!CollectionUtils.isEmpty(prices)) {
- // 去最小价格
- Map<String, PriceLibrary> priceLibraryMap = getMinValues(prices);
- // 核价
- for (String type : PRICERULES) {
- PriceLibrary priceLibrary = priceLibraryMap.get(type);
- if (!StringUtils.isEmpty(priceLibrary)) {
- if (StringUtils.isEmpty(bomDetail.getTaxRate())) {
- bomDetail.setTaxRate(0d);
- }
- double taxPrice = priceLibrary.getNoTaxPrice() * (1 + bomDetail.getTaxRate() / 100);
- if (!StringUtils.isEmpty(priceLibrary) && !StringUtils.isEmpty(priceLibrary.getPrice()) && priceLibrary.getPrice() != 0 && taxPrice <= bomDetail.getTaxUnitPrice()) {
- bomDetail.setPid(priceLibrary.getId());
- double price = ((bomDetail.getTaxUnitPrice() - taxPrice) * 0.618) + taxPrice;
- bomDetail.setPricingPrice((double) Math.round(price * 1000000) / 1000000);
- break;
- }
- }
- }
- }
- bomDetail.setPricingTime(new Date());
- return bomDetail;
- }
- /**
- * 匹配价格
- * @param bomDetail
- * @return
- */
- public Set<PriceLibrary> matchPrice(BomDetail bomDetail, Long enuu) {
- Set<PriceLibrary> prices = new HashSet<>();
- if (StringUtils.isEmpty(bomDetail.getCurrency())) {
- bomDetail.setCurrency("RMB");
- }
- if (!StringUtils.isEmpty(bomDetail.getCmpCode()) && !StringUtils.isEmpty(bomDetail.getInbrand())) {
- prices = priceLibraryDao.findByCmpCodeAndInbrandAndCurrencyAndPriceIsNotNullAndVendEnuuNotAndPriceNot(bomDetail.getCmpCode(), bomDetail.getInbrand(), bomDetail.getCurrency(), enuu, 0d);
- } else if (!StringUtils.isEmpty(bomDetail.getCmpCode())) {
- prices = priceLibraryDao.findByCmpCodeAndCurrencyAndPriceIsNotNullAndVendEnuuNotAndPriceNot(bomDetail.getCmpCode(), bomDetail.getCurrency(), enuu, 0d);
- }else if (!StringUtils.isEmpty(bomDetail.getCode()) && !StringUtils.isEmpty(bomDetail.getBrand())) {// 如果bomDetail里面有型号和品牌
- prices = priceLibraryDao.findByCodeAndBrandAndCurrencyAndPriceIsNotNullAndVendEnuuNotAndPriceNot(bomDetail.getCode(), bomDetail.getBrand(), bomDetail.getCurrency(), enuu, 0d);
- } else if (!StringUtils.isEmpty(bomDetail.getCode())) {// 如果bomDetail只有型号
- prices = priceLibraryDao.findByCodeAndCurrencyAndPriceIsNotNullAndVendEnuuNotAndPriceNot(bomDetail.getCode(), bomDetail.getCurrency(), enuu, 0d);
- } else if (!StringUtils.isEmpty(bomDetail.getSpec())) {// 如果bomDetail 规格不为空
- prices = priceLibraryDao.findBySpecAndCurrencyAndPriceIsNotNullAndVendEnuuNotAndPriceNot(bomDetail.getSpec(), bomDetail.getCurrency(), enuu, 0d);
- }
- return prices;
- }
- /**
- * 取出各个价格中最小值
- * @param prices
- * @return
- */
- public Map<String, PriceLibrary> getMinValues(Set<PriceLibrary> prices) {
- Map<String, PriceLibrary> priceLibraryMap = new HashMap<>();
- for (PriceLibrary priceLibrary : prices) {
- PriceLibrary oldPriceLibrary = priceLibraryMap.get(priceLibrary.getPriceType());
- if (StringUtils.isEmpty(oldPriceLibrary)) {
- priceLibraryMap.put(priceLibrary.getPriceType(), priceLibrary);
- } else {
- if (oldPriceLibrary.getNoTaxPrice() > priceLibrary.getNoTaxPrice()) {
- priceLibraryMap.put(priceLibrary.getPriceType(), priceLibrary);
- }
- }
- }
- return priceLibraryMap;
- }
- }
|