PriceLibraryServiceImpl.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.uas.ps.pricing.service.impl;
  2. import com.uas.ps.pricing.dao.BomDao;
  3. import com.uas.ps.pricing.dao.BomDetailDao;
  4. import com.uas.ps.pricing.dao.PriceLibraryDao;
  5. import com.uas.ps.pricing.model.Bom;
  6. import com.uas.ps.pricing.model.BomDetail;
  7. import com.uas.ps.pricing.model.PriceLibrary;
  8. import com.uas.ps.pricing.service.PriceLibraryService;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.ui.ModelMap;
  12. import org.springframework.util.CollectionUtils;
  13. import org.springframework.util.StringUtils;
  14. import java.util.*;
  15. /**
  16. * 价格库实现类
  17. *
  18. * @author liusw
  19. * @date 2018-04-23 11:12
  20. */
  21. @Service
  22. public class PriceLibraryServiceImpl implements PriceLibraryService{
  23. @Autowired
  24. private PriceLibraryDao priceLibraryDao;
  25. @Autowired
  26. private BomDetailDao bomDetailDao;
  27. @Autowired
  28. private BomDao bomDao;
  29. /**
  30. * 核价规则
  31. */
  32. private static String[] PRICERULES = {"VALIDPURCPRICE", "ORDERPURCPRICE", "CHECKUNITPRICE", "WAREHOUSINGPRICE", "MATSTANDARDUNITPRICE", "REPAVGUNITPRICE", "OFFERPRICE"};
  33. @Override
  34. public ModelMap savePrice(List<PriceLibrary> priceLibraries) {
  35. ModelMap modelMap = new ModelMap();
  36. try {
  37. priceLibraries = priceLibraryDao.save(priceLibraries);
  38. modelMap.put("success", true);
  39. modelMap.put("data", priceLibraries.size());
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. modelMap.put("success", false);
  43. modelMap.put("message", e.getMessage());
  44. }
  45. return modelMap;
  46. }
  47. @Override
  48. public ModelMap bomPricing(Bom bom) {
  49. ModelMap modelMap = new ModelMap();
  50. try {
  51. List<BomDetail> bomDetails = bom.getBomDetails();
  52. List<PriceLibrary> priceLibraries = new ArrayList<>();
  53. bom = bomDao.save(bom);
  54. for (BomDetail bomDetail : bomDetails) {
  55. if (!StringUtils.isEmpty(bomDetail.getTaxUnitPrice()) && bomDetail.getTaxUnitPrice() != 0) {
  56. // 进行核价
  57. bomDetail = countPrice(bomDetail, bom.getEnuu());
  58. bomDetail.setBdId(bom.getBid());
  59. bomDetail.setBomId(bom.getBomId());
  60. bomDetailDao.save(bomDetail);
  61. // 保存经过成本计算的价格
  62. priceLibraries.add(bomDetailConvert(bomDetail, bom.getEnuu()));
  63. }
  64. }
  65. List<Bom> boms = bomDao.findByEnuuAndBomId(bom.getEnuu(), bom.getBomId());
  66. if (CollectionUtils.isEmpty(boms)) {
  67. priceLibraryDao.save(priceLibraries);
  68. }
  69. bom.setBomDetails(bomDetails);
  70. modelMap.put("success", true);
  71. modelMap.put("data", bom);
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. modelMap.put("success", false);
  75. modelMap.put("message", e.getMessage());
  76. }
  77. return modelMap;
  78. }
  79. private PriceLibrary bomDetailConvert(BomDetail bomDetail, Long buyerUU) {
  80. PriceLibrary priceLirary = new PriceLibrary();
  81. priceLirary.setBrand(bomDetail.getBrand());
  82. priceLirary.setBuyerEnuu(buyerUU);
  83. priceLirary.setCmpCode(bomDetail.getCmpCode());
  84. priceLirary.setCode(bomDetail.getCode());
  85. priceLirary.setCurrency(bomDetail.getCurrency());
  86. priceLirary.setInbrand(bomDetail.getInbrand());
  87. priceLirary.setKind(bomDetail.getProductTitle());
  88. priceLirary.setPrice(bomDetail.getTaxUnitPrice());
  89. priceLirary.setTaxRate(bomDetail.getTaxRate());
  90. priceLirary.setSource("ERP");
  91. priceLirary.setVendEnuu(bomDetail.getVendUU());
  92. priceLirary.setVendName(bomDetail.getVendName());
  93. priceLirary.setUasCode(bomDetail.getProductCode());
  94. priceLirary.setSpec(bomDetail.getSpec());
  95. priceLirary.setPriceType("BOMCOUNTPRICE");
  96. return priceLirary;
  97. }
  98. /**
  99. * 核价
  100. * @param bomDetail
  101. * @return
  102. */
  103. private BomDetail countPrice(BomDetail bomDetail, Long enuu) {
  104. // 匹配 如果bomDetail里面有原厂型号和品牌
  105. Set<PriceLibrary> prices = matchPrice(bomDetail, enuu);
  106. if (!CollectionUtils.isEmpty(prices)) {
  107. // 去最小价格
  108. Map<String, PriceLibrary> priceLibraryMap = getMinValues(prices);
  109. // 核价
  110. for (String type : PRICERULES) {
  111. PriceLibrary priceLibrary = priceLibraryMap.get(type);
  112. if (!StringUtils.isEmpty(priceLibrary)) {
  113. if (StringUtils.isEmpty(bomDetail.getTaxRate())) {
  114. bomDetail.setTaxRate(0d);
  115. }
  116. double taxPrice = priceLibrary.getNoTaxPrice() * (1 + bomDetail.getTaxRate() / 100);
  117. if (!StringUtils.isEmpty(priceLibrary) && !StringUtils.isEmpty(priceLibrary.getPrice()) && priceLibrary.getPrice() != 0 && taxPrice <= bomDetail.getTaxUnitPrice()) {
  118. bomDetail.setPid(priceLibrary.getId());
  119. double price = ((bomDetail.getTaxUnitPrice() - taxPrice) * 0.618) + taxPrice;
  120. bomDetail.setPricingPrice((double) Math.round(price * 1000000) / 1000000);
  121. break;
  122. }
  123. }
  124. }
  125. }
  126. bomDetail.setPricingTime(new Date());
  127. return bomDetail;
  128. }
  129. /**
  130. * 匹配价格
  131. * @param bomDetail
  132. * @return
  133. */
  134. public Set<PriceLibrary> matchPrice(BomDetail bomDetail, Long enuu) {
  135. Set<PriceLibrary> prices = new HashSet<>();
  136. if (StringUtils.isEmpty(bomDetail.getCurrency())) {
  137. bomDetail.setCurrency("RMB");
  138. }
  139. if (!StringUtils.isEmpty(bomDetail.getCmpCode()) && !StringUtils.isEmpty(bomDetail.getInbrand())) {
  140. prices = priceLibraryDao.findByCmpCodeAndInbrandAndCurrencyAndPriceIsNotNullAndVendEnuuNotAndPriceNot(bomDetail.getCmpCode(), bomDetail.getInbrand(), bomDetail.getCurrency(), enuu, 0d);
  141. } else if (!StringUtils.isEmpty(bomDetail.getCmpCode())) {
  142. prices = priceLibraryDao.findByCmpCodeAndCurrencyAndPriceIsNotNullAndVendEnuuNotAndPriceNot(bomDetail.getCmpCode(), bomDetail.getCurrency(), enuu, 0d);
  143. }else if (!StringUtils.isEmpty(bomDetail.getCode()) && !StringUtils.isEmpty(bomDetail.getBrand())) {// 如果bomDetail里面有型号和品牌
  144. prices = priceLibraryDao.findByCodeAndBrandAndCurrencyAndPriceIsNotNullAndVendEnuuNotAndPriceNot(bomDetail.getCode(), bomDetail.getBrand(), bomDetail.getCurrency(), enuu, 0d);
  145. } else if (!StringUtils.isEmpty(bomDetail.getCode())) {// 如果bomDetail只有型号
  146. prices = priceLibraryDao.findByCodeAndCurrencyAndPriceIsNotNullAndVendEnuuNotAndPriceNot(bomDetail.getCode(), bomDetail.getCurrency(), enuu, 0d);
  147. } else if (!StringUtils.isEmpty(bomDetail.getSpec())) {// 如果bomDetail 规格不为空
  148. prices = priceLibraryDao.findBySpecAndCurrencyAndPriceIsNotNullAndVendEnuuNotAndPriceNot(bomDetail.getSpec(), bomDetail.getCurrency(), enuu, 0d);
  149. }
  150. return prices;
  151. }
  152. /**
  153. * 取出各个价格中最小值
  154. * @param prices
  155. * @return
  156. */
  157. public Map<String, PriceLibrary> getMinValues(Set<PriceLibrary> prices) {
  158. Map<String, PriceLibrary> priceLibraryMap = new HashMap<>();
  159. for (PriceLibrary priceLibrary : prices) {
  160. PriceLibrary oldPriceLibrary = priceLibraryMap.get(priceLibrary.getPriceType());
  161. if (StringUtils.isEmpty(oldPriceLibrary)) {
  162. priceLibraryMap.put(priceLibrary.getPriceType(), priceLibrary);
  163. } else {
  164. if (oldPriceLibrary.getNoTaxPrice() > priceLibrary.getNoTaxPrice()) {
  165. priceLibraryMap.put(priceLibrary.getPriceType(), priceLibrary);
  166. }
  167. }
  168. }
  169. return priceLibraryMap;
  170. }
  171. }