ItemPrice.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. package beans.itemprice;
  2. import beans.itemclass.ItemClass;
  3. import common.BaseClass;
  4. import common.Controller;
  5. import common.YosException;
  6. import common.data.Rows;
  7. import common.data.SQLFactory;
  8. import utility.tools.Encryption;
  9. import utility.tools.Math;
  10. import java.math.BigDecimal;
  11. import java.util.ArrayList;
  12. import java.util.Calendar;
  13. import java.util.HashMap;
  14. public class ItemPrice extends BaseClass {
  15. private Controller controller;
  16. private long sys_enterpriseid = -1;//企业id
  17. private long itemid;//商品id
  18. private BigDecimal marketprice = new BigDecimal(-1);//牌价,市场价
  19. private int pricegrade = 0;//价格等级
  20. private BigDecimal price = new BigDecimal(-1);//等级价格(商品原价)
  21. private BigDecimal gradediscountrate = new BigDecimal(0);//商户等级折扣率
  22. private BigDecimal graderateprice = new BigDecimal(-1);//商户等级折扣价格
  23. private BigDecimal contractprice = new BigDecimal(-1);//框架合同价格
  24. private BigDecimal projectcontractprice = new BigDecimal(-1);//框架合同价格
  25. private String cachekey = null;
  26. private ItemPrice(Controller controller, long itemid) {
  27. this.controller = controller;
  28. this.itemid = itemid;
  29. }
  30. private ItemPrice(Controller controller, long sys_enterpriseid, long itemid) {
  31. this.controller = controller;
  32. this.sys_enterpriseid = sys_enterpriseid;
  33. this.itemid = itemid;
  34. }
  35. /**
  36. * 获取商品牌价
  37. *
  38. * @return
  39. */
  40. public BigDecimal getMarketprice() throws YosException {
  41. if (marketprice.compareTo(BigDecimal.ZERO) < 0) {
  42. Rows rows = dbConnect.runSqlQuery("select marketprice from plm_item where siteid='" + controller.siteid + "'and itemid=" + itemid);
  43. if (rows.isNotEmpty()) {
  44. marketprice = rows.get(0).getBigDecimal("marketprice");
  45. } else {
  46. throw new YosException("找不到商品牌价信息itemid:" + itemid);
  47. }
  48. }
  49. return marketprice;
  50. }
  51. /**
  52. * 商品等级价格
  53. *
  54. * @return
  55. */
  56. public BigDecimal getPrice() throws YosException {
  57. if (price.floatValue() < 0) {
  58. SQLFactory sqlFactory = new SQLFactory(this, "商品等级价格查询");
  59. sqlFactory.addParameter("siteid", controller.siteid);
  60. sqlFactory.addParameter("itemid", itemid);
  61. sqlFactory.addParameter("sys_enterpriseid", this.sys_enterpriseid);
  62. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  63. if (rows.isNotEmpty()) {
  64. pricegrade = rows.get(0).getInteger("pricegrade");
  65. price = rows.get(0).getBigDecimal("price");
  66. gradediscountrate = rows.get(0).getBigDecimal("discountrate");
  67. //如果折扣率为0,则默认按照1计算
  68. gradediscountrate = (gradediscountrate.compareTo(BigDecimal.ZERO) == 0 ? new BigDecimal(1) : gradediscountrate);
  69. } else {
  70. throw new YosException("找不到商品等级价格信息itemid:" + itemid);
  71. }
  72. }
  73. return price;
  74. }
  75. /**
  76. * 价格等级
  77. *
  78. * @return
  79. * @throws YosException
  80. */
  81. public int getPricegrade() throws YosException {
  82. if (pricegrade < 0) {
  83. getPrice();
  84. }
  85. return pricegrade;
  86. }
  87. /**
  88. * 商户等级折后价
  89. *
  90. * @return
  91. */
  92. public BigDecimal getGraderateprice() throws YosException {
  93. if (graderateprice.compareTo(BigDecimal.ZERO) < 0) {
  94. graderateprice = getPrice().multiply(gradediscountrate);
  95. }
  96. return graderateprice;
  97. }
  98. private static HashMap<String, Rows> contractRowscacheMap = new HashMap<>();
  99. /**
  100. * 获取商户协议价,1:获取协议商品折扣;2:获取协议营销类别折扣(按最低价);3:获取协议折扣;4获取商户等级折扣价:5获取商品等级原价
  101. *
  102. * @return
  103. */
  104. public BigDecimal getContractprice() throws YosException {
  105. if (contractprice.compareTo(BigDecimal.ZERO) < 0) {
  106. /*
  107. * 第一步:查询框架合同并试图获取价格:优先级 商品价格、商品折扣==》类别折扣==》合同折扣
  108. */
  109. {
  110. SQLFactory contractQuery = new SQLFactory(this, "商户框架合同查询");
  111. contractQuery.addParameter("siteid", controller.siteid);
  112. contractQuery.addParameter("sys_enterpriseid", sys_enterpriseid);
  113. Rows contractRows = dbConnect.runSqlQuery(contractQuery.getSQL());
  114. if (contractRows.size() > 1) {
  115. throw new YosException("存在一个以上的价格协议信息:" + itemid);
  116. } else if (contractRows.size() == 1) {
  117. long sa_contractid = contractRows.get(0).getLong("sa_contractid");//合同id
  118. /*
  119. * 第二步:尝试从合同商品价格协议中获取价格
  120. */
  121. stepTwo(sa_contractid);
  122. /*
  123. *第三步:如果商品价格还是小于0,则尝试从合同类别折扣协议中去获取价格
  124. */
  125. stepThree(sa_contractid, getGraderateprice());
  126. /*
  127. * 第四步:如果商品价格还是小于0,则从合同折扣协议中去获取价格
  128. */
  129. BigDecimal discountrate = contractRows.get(0).getBigDecimal("discountrate");//折扣率
  130. if (contractprice.compareTo(BigDecimal.ZERO) <= 0 && discountrate.compareTo(BigDecimal.ZERO) > 0) {
  131. contractprice = getScalePrice(getGraderateprice().multiply(discountrate));
  132. }
  133. }
  134. }
  135. /*
  136. * 如果框架合同中没有获取到有效的价格,则返回商户等级价格信息
  137. */
  138. if (contractprice.compareTo(BigDecimal.ZERO) <= 0) {
  139. contractprice = getGraderateprice();
  140. }
  141. /*
  142. * 如果没有获取到有效的价格,则返回原价格信息
  143. */
  144. if (contractprice.compareTo(BigDecimal.ZERO) <= 0) {
  145. contractprice = getPrice();
  146. }
  147. }
  148. return contractprice;
  149. }
  150. /**
  151. * 获取项目合同价,项目合同中必须包含此商品
  152. *
  153. * @param sa_contractid 合同id
  154. * @return
  155. */
  156. public BigDecimal getContractprice(long sa_contractid) throws YosException {
  157. if (contractprice.compareTo(BigDecimal.ZERO) < 0) {
  158. /*
  159. * 第一步:查询框架合同并试图获取价格:优先级 商品价格、商品折扣==》类别折扣==》合同折扣
  160. */
  161. {
  162. /*
  163. * 第二步:尝试从合同商品价格协议中获取价格
  164. */
  165. stepTwo_Contract(sa_contractid);
  166. /*
  167. *第三步:如果商品价格还是小于0,则尝试从合同类别折扣协议中去获取价格
  168. */
  169. stepThree(sa_contractid, getMarketprice());
  170. /*
  171. * 第四步:如果商品价格还是小于0,则从合同折扣协议中去获取价格
  172. */
  173. Rows rows = dbConnect.runSqlQuery("SELECT discountrate from sa_contract WHERE sa_contractid=" + sa_contractid + " and siteid = '" + controller.siteid + "'");
  174. BigDecimal discountrate = BigDecimal.valueOf(1);//折扣率
  175. if (rows.isNotEmpty()) {
  176. discountrate = rows.get(0).getBigDecimal("discountrate");//折扣率
  177. }
  178. if (contractprice.compareTo(BigDecimal.ZERO) <= 0 && discountrate.compareTo(BigDecimal.ZERO) > 0) {
  179. contractprice = getScalePrice(getMarketprice().multiply(discountrate));
  180. }
  181. }
  182. /*
  183. * 如果框架合同中没有获取到有效的价格,则返回商户等级价格信息
  184. */
  185. if (contractprice.compareTo(BigDecimal.ZERO) <= 0) {
  186. // contractprice = getGraderateprice();
  187. contractprice = getMarketprice();
  188. }
  189. /*
  190. * 如果没有获取到有效的价格,则返回原价格信息
  191. */
  192. if (contractprice.compareTo(BigDecimal.ZERO) <= 0) {
  193. // contractprice = getPrice();
  194. contractprice = getMarketprice();
  195. }
  196. }
  197. return contractprice;
  198. }
  199. /**
  200. * 第二步:尝试从合同商品价格协议中获取价格
  201. *
  202. * @param sa_contractid
  203. * @throws YosException
  204. */
  205. public void stepTwo(Long sa_contractid) throws YosException {
  206. SQLFactory contract_itemQuery = new SQLFactory(this, "商户框架合同商品折扣查询");
  207. contract_itemQuery.addParameter("siteid", controller.siteid);
  208. contract_itemQuery.addParameter("sa_contractid", sa_contractid);
  209. contract_itemQuery.addParameter("itemid", itemid);
  210. Rows contract_itemRows = dbConnect.runSqlQuery(contract_itemQuery.getSQL());
  211. if (contract_itemRows.size() > 1) {
  212. throw new YosException("存在一个以上的价格协议信息:" + itemid);
  213. } else if (contract_itemRows.size() == 1) {
  214. //类型:折扣系数、指定单价
  215. String type = contract_itemRows.get(0).getString("type");
  216. //商品折扣率
  217. BigDecimal discountrate_item = contract_itemRows.get(0).getBigDecimal("discountrate");
  218. //商品价格
  219. BigDecimal price = contract_itemRows.get(0).getBigDecimal("price");
  220. if (type.equals("折扣系数") && discountrate_item.compareTo(BigDecimal.ZERO) > 0) {
  221. contractprice = getScalePrice(getGraderateprice().multiply(discountrate_item));
  222. } else if (type.equals("指定单价") && price.compareTo(BigDecimal.ZERO) > 0) {
  223. contractprice = price;
  224. }
  225. }
  226. }
  227. /**
  228. * 第二步:尝试从合同商品价格协议中获取价格
  229. *
  230. * @param sa_contractid
  231. * @throws YosException
  232. */
  233. public void stepTwo_Contract(Long sa_contractid) throws YosException {
  234. SQLFactory contract_itemQuery = new SQLFactory(this, "商户框架合同商品折扣查询");
  235. contract_itemQuery.addParameter("siteid", controller.siteid);
  236. contract_itemQuery.addParameter("sa_contractid", sa_contractid);
  237. contract_itemQuery.addParameter("itemid", itemid);
  238. Rows contract_itemRows = dbConnect.runSqlQuery(contract_itemQuery.getSQL());
  239. if (contract_itemRows.size() > 1) {
  240. throw new YosException("存在一个以上的价格协议信息:" + itemid);
  241. } else if (contract_itemRows.size() == 1) {
  242. //商品价格
  243. BigDecimal price = contract_itemRows.get(0).getBigDecimal("price");
  244. contractprice = price;
  245. }
  246. }
  247. /**
  248. * 第三步:如果商品价格还是小于0,则尝试从合同类别折扣协议中去获取价格
  249. *
  250. * @param sa_contractid
  251. */
  252. public void stepThree(Long sa_contractid, BigDecimal price) throws YosException {
  253. if (contractprice.compareTo(BigDecimal.ZERO) <= 0) {
  254. //获取商品的营销类别信息
  255. Rows itemclassidRows = dbConnect.runSqlQuery("select itemclassid from sa_itemsaleclass where siteid='" + controller.siteid + "' and itemid=" + itemid);
  256. ArrayList<Long> ids = itemclassidRows.toArrayList("itemclassid", new ArrayList<>());
  257. //获取所有的上级营销分类
  258. ArrayList<Long> itemclassids = ItemClass.getParentItemClassIds(controller, ids);
  259. //合并商品营销分类
  260. itemclassids.addAll(ids);
  261. //如果商品存在多个分类时,价格取值原则,折扣最小原则(默认原则),从后往前原则(依具体客户的需求实现)
  262. {
  263. /*
  264. * 最小原则取价
  265. */
  266. SQLFactory contract_itemclassQuery = new SQLFactory(this, "商户框架合同商品营销类别最低折扣查询");
  267. contract_itemclassQuery.addParameter("siteid", controller.siteid);
  268. contract_itemclassQuery.addParameter("sa_contractid", sa_contractid);
  269. contract_itemclassQuery.addParameter_in("itemclassid", itemclassids);
  270. Rows contract_itemsaleclassRows = dbConnect.runSqlQuery(contract_itemclassQuery.getSQL());
  271. if (contract_itemsaleclassRows.isNotEmpty()) {
  272. //商品类别折扣率
  273. BigDecimal discountrate_itemclass = contract_itemsaleclassRows.get(0).getBigDecimal("discountrate");
  274. if (discountrate_itemclass.compareTo(BigDecimal.ZERO) > 0) {
  275. contractprice = getScalePrice(price.multiply(discountrate_itemclass));
  276. }
  277. }
  278. }
  279. }
  280. }
  281. /**
  282. * 根据商户id及商品id获取商品价格,sys_enterpriseid为0时,只能获取到牌价信息
  283. *
  284. * @param controller
  285. * @param sys_enterpriseid
  286. * @param itemid
  287. * @return
  288. * @throws YosException
  289. */
  290. public static ItemPrice getItemPrice(Controller controller, long sys_enterpriseid, long itemid) throws YosException {
  291. return new ItemPrice(controller, sys_enterpriseid, itemid);
  292. }
  293. /**
  294. * 根据商户id及商品id获取商品价格,sys_enterpriseid为0时,只能获取到牌价信息
  295. *
  296. * @param controller
  297. * @param sys_enterpriseid
  298. * @param itemids
  299. * @return
  300. * @throws YosException
  301. */
  302. public static HashMap<Long, ItemPrice> getItemPrice(Controller controller, long sys_enterpriseid, ArrayList<Long> itemids) throws YosException {
  303. //批量查询时,为了减少数据库查询量,建立创建一个临时key,用于缓存部分通用信息
  304. String cachekey = Encryption.Encode_MD5(controller.usersiteid + Math.random(2) + Calendar.getInstance().getTimeInMillis());
  305. HashMap<Long, ItemPrice> map = new HashMap<>();
  306. for (long itemid : itemids) {
  307. ItemPrice itemPrice = new ItemPrice(controller, sys_enterpriseid, itemid);
  308. itemPrice.cachekey = cachekey;
  309. map.put(itemid, itemPrice);
  310. }
  311. contractRowscacheMap.remove(cachekey);
  312. return map;
  313. }
  314. /**
  315. * 根据商户id及商品id获取商品价格,只能获取到牌价信息
  316. *
  317. * @param controller
  318. * @param itemid
  319. * @return
  320. * @throws YosException
  321. */
  322. public static ItemPrice getItemPrice(Controller controller, long itemid) throws YosException {
  323. return getItemPrice(controller, 0, itemid);
  324. }
  325. /**
  326. * 根据商户id及商品id获取商品价格,只能获取到牌价信息
  327. *
  328. * @param controller
  329. * @param itemids
  330. * @return
  331. * @throws YosException
  332. */
  333. public static HashMap<Long, ItemPrice> getItemPrice(Controller controller, ArrayList<Long> itemids) throws YosException {
  334. return getItemPrice(controller, 0, itemids);
  335. }
  336. /**
  337. * 根据商户id及商品组id,返回商品组的每个商品价格信息
  338. *
  339. * @param controller
  340. * @param sys_enterpriseid
  341. * @param sa_itemgroupid
  342. * @return <itemid,ItemPrice>
  343. * @throws YosException
  344. */
  345. public static HashMap<Long, ItemPrice> getItemGroupPrice(Controller controller, long sys_enterpriseid, long sa_itemgroupid) throws YosException {
  346. ArrayList<ItemPrice> itemPriceList = new ArrayList<>();
  347. Rows rows = controller.dbConnect.runSqlQuery("select itemid from sa_itemgroupmx where siteid='" + controller.siteid + "' and sa_itemgroupid=" + sa_itemgroupid);
  348. return getItemPrice(controller, sys_enterpriseid, rows.toArrayList("itemid", new ArrayList<>()));
  349. }
  350. /**
  351. * 根据商户id及商品组id,返回商品组的每个商品价格信息
  352. *
  353. * @param controller
  354. * @param sys_enterpriseid
  355. * @param sa_itemgroupids
  356. * @return <sa_itemgroupid,<itemid,ItemPrice>>
  357. * @throws YosException
  358. */
  359. public static HashMap<Long, HashMap<Long, ItemPrice>> getItemGroupPrice(Controller controller, long sys_enterpriseid, long[] sa_itemgroupids) throws YosException {
  360. HashMap<Long, HashMap<Long, ItemPrice>> map = new HashMap<>();
  361. for (long sa_itemgroupid : sa_itemgroupids) {
  362. map.put(sa_itemgroupid, getItemGroupPrice(controller, sys_enterpriseid, sa_itemgroupid));
  363. }
  364. return map;
  365. }
  366. /**
  367. * 根据商品组id,返回商品组的每个商品价格信息
  368. *
  369. * @param controller
  370. * @param sa_itemgroupid
  371. * @return <itemid,ItemPrice>
  372. * @throws YosException
  373. */
  374. public static HashMap<Long, ItemPrice> getItemGroupPrice(Controller controller, long sa_itemgroupid) throws YosException {
  375. ArrayList<ItemPrice> itemPriceList = new ArrayList<>();
  376. Rows rows = controller.dbConnect.runSqlQuery("select itemid from sa_itemgroupmx where siteid='" + controller.siteid + "' and sa_itemgroupid=" + sa_itemgroupid);
  377. return getItemPrice(controller, rows.toArrayList("itemid", new ArrayList<>()));
  378. }
  379. /**
  380. * 根据商品组id,返回商品组的每个商品价格信息
  381. *
  382. * @param controller
  383. * @param sa_itemgroupids
  384. * @return <sa_itemgroupid,<itemid,ItemPrice>>
  385. * @throws YosException
  386. */
  387. public static HashMap<Long, HashMap<Long, ItemPrice>> getItemGroupPrice(Controller controller, long[] sa_itemgroupids) throws YosException {
  388. HashMap<Long, HashMap<Long, ItemPrice>> map = new HashMap<>();
  389. for (long sa_itemgroupid : sa_itemgroupids) {
  390. map.put(sa_itemgroupid, getItemGroupPrice(controller, sa_itemgroupid));
  391. }
  392. return map;
  393. }
  394. /**
  395. * @param price
  396. * @return
  397. */
  398. public BigDecimal getScalePrice(BigDecimal price) {
  399. return (price).setScale(2, BigDecimal.ROUND_HALF_UP);
  400. }
  401. }