CmOrganizePromotionService.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package com.caimei.modules.weisha.service;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.caimei.modules.bulkpurchase.entity.PurchaseProduct;
  4. import com.caimei.modules.cibe.entity.Shop;
  5. import com.caimei.modules.product.entity.Product;
  6. import com.caimei.modules.weisha.dao.CmOrganizePromotionDao;
  7. import com.caimei.modules.weisha.entity.CmOrganizePromotion;
  8. import com.caimei.utils.AppUtils;
  9. import com.caimei.utils.MathUtil;
  10. import com.caimei.utils.StringUtils;
  11. import com.foxinmy.weixin4j.util.StringUtil;
  12. import com.thinkgem.jeesite.common.config.Global;
  13. import com.thinkgem.jeesite.common.persistence.Page;
  14. import com.thinkgem.jeesite.common.service.CrudService;
  15. import com.thinkgem.jeesite.common.utils.DateUtils;
  16. import org.apache.commons.collections.CollectionUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import java.math.BigDecimal;
  21. import java.text.DecimalFormat;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. @Service
  27. @Transactional(readOnly = true)
  28. public class CmOrganizePromotionService extends CrudService<CmOrganizePromotionDao, CmOrganizePromotion> {
  29. @Autowired
  30. private CmOrganizePromotionDao cmOrganizePromotionDao;
  31. @Override
  32. public CmOrganizePromotion get(String id) {
  33. return super.get(id);
  34. }
  35. /**
  36. * 促销活动列表
  37. */
  38. @Override
  39. public Page<CmOrganizePromotion> findPage(Page<CmOrganizePromotion> page, CmOrganizePromotion cmOrganizePromotion) {
  40. Page<CmOrganizePromotion> promotionPage = super.findPage(page, cmOrganizePromotion);
  41. List<CmOrganizePromotion> promotionList = promotionPage.getList();
  42. List<Product> promotionProducts = new ArrayList<>();
  43. List<Product> giftProducts = new ArrayList<>();
  44. List<Shop> promotionShops = new ArrayList<>();
  45. if (CollectionUtils.isNotEmpty(promotionList)) {
  46. for (CmOrganizePromotion promotion : promotionList
  47. ) {
  48. if (StringUtils.isNotBlank(cmOrganizePromotion.getProductName())) {
  49. promotion.setProductName(cmOrganizePromotion.getProductName());
  50. }
  51. if (StringUtils.isNotBlank(cmOrganizePromotion.getShopName())) {
  52. promotion.setShopName(cmOrganizePromotion.getShopName());
  53. }
  54. //未关闭
  55. if ("0".equals(promotion.getDelFlag1())) {
  56. //永久活动设置为进行中
  57. if ("1".equals(promotion.getStatus())) {
  58. promotion.setDelFlag1("2");
  59. } else {
  60. Date nowTime = new Date();
  61. if (null != promotion.getBeginTime() && null != promotion.getEndTime()) {
  62. boolean effectiveDate = DateUtils.isEffectiveDate(nowTime, promotion.getBeginTime(), promotion.getEndTime());
  63. if (effectiveDate) {
  64. //进行中
  65. promotion.setDelFlag1("2");
  66. } else if (nowTime.compareTo(promotion.getEndTime()) > 0) {
  67. //已结束
  68. promotion.setDelFlag1("3");
  69. } else {
  70. //未开始
  71. promotion.setDelFlag1("1");
  72. }
  73. }else {
  74. promotion.setDelFlag1("0");
  75. }
  76. }
  77. }else if("1".equals(promotion.getDelFlag1())){
  78. promotion.setDelFlag1("0");
  79. }else if("2".equals(promotion.getDelFlag1())){
  80. promotion.setDelFlag1("4");
  81. }
  82. //非店铺促销下设置促销产品
  83. if (!"3".equals(promotion.getType())) {
  84. //根据促销id和商品名查询
  85. promotionProducts = findPromotionProduct(promotion);
  86. promotion.setPromotionProducts(promotionProducts);
  87. }else {
  88. promotionShops = findPromotionShops(promotion);
  89. promotion.setPromotionShops(promotionShops);
  90. }
  91. //赠品模式下设置赠品
  92. if ("3".equals(promotion.getMode())) {
  93. giftProducts = findGiftProduct(promotion.getId());
  94. Integer giftNumber = 0;
  95. Integer giftTypeNumber = 0;
  96. for (Product product : giftProducts) {
  97. giftNumber += product.getGiftNumber();
  98. giftTypeNumber++;
  99. }
  100. promotion.setGiftProducts(giftProducts);
  101. promotion.setGiftNumber(giftNumber);
  102. promotion.setGiftTypeNumber(giftTypeNumber);
  103. }
  104. }
  105. }
  106. return promotionPage;
  107. }
  108. /**
  109. * 根据促销活动id查询促销供应商
  110. */
  111. public List<Shop> findPromotionShops(CmOrganizePromotion cmOrganizePromotion) {
  112. List<Shop> shops = cmOrganizePromotionDao.findPromotionShops(cmOrganizePromotion);
  113. for (Shop shop : shops) {
  114. if (shop != null) {
  115. shop.setStoreStatus(true);
  116. }
  117. }
  118. return shops;
  119. }
  120. /**
  121. * 根据促销活动id查询促销商品
  122. */
  123. public List<Product> findPromotionProduct(CmOrganizePromotion cmOrganizePromotion) {
  124. List<Product> products = cmOrganizePromotionDao.findPromotionProduct(cmOrganizePromotion);
  125. String wwwServer = Global.getConfig("wwwServer");
  126. for (Product product : products) {
  127. if (product != null) {
  128. product.setStoreStatus(true);
  129. product.setMainImage(AppUtils.getImageURL("product", product.getMainImage(), 0, wwwServer));
  130. }
  131. }
  132. return products;
  133. }
  134. /**
  135. * 根据促销活动id查询促销赠品
  136. */
  137. public List<Product> findGiftProduct(String id) {
  138. List<Product> products = cmOrganizePromotionDao.findGiftProduct(id);
  139. String wwwServer = Global.getConfig("wwwServer");
  140. for (Product product : products) {
  141. if (product != null) {
  142. product.setStoreStatus(true);
  143. product.setMainImage(AppUtils.getImageURL("product", product.getMainImage(), 0, wwwServer));
  144. }
  145. }
  146. return products;
  147. }
  148. /**
  149. * 根据促销活动id查询促销赠品(订单)
  150. */
  151. public List<PurchaseProduct> findGiftPurchaseProduct(String id){
  152. List<PurchaseProduct> list = cmOrganizePromotionDao.findGiftPurchaseProduct(id);
  153. for (PurchaseProduct p : list) {
  154. if (p != null) {
  155. //1固定成本 2比例成
  156. String costCheckFlag = p.getCostCheckFlag();
  157. //固定成本价
  158. if (com.thinkgem.jeesite.common.utils.StringUtils.equals("1", costCheckFlag)) {
  159. String costPrice = p.getCostPrice();
  160. //只有固定成本,应付供应商的金额=添加商品时的成本价
  161. p.setShopFee1(costPrice);
  162. }
  163. //比例成本价
  164. if (com.thinkgem.jeesite.common.utils.StringUtils.equals("2", costCheckFlag)) {
  165. String costProportional = p.getCostProportional();
  166. String price = p.getPrice();
  167. if (com.thinkgem.jeesite.common.utils.StringUtils.isNotEmpty(costProportional)) {
  168. BigDecimal shopFee1 = (MathUtil.div(MathUtil.mul(costProportional, price), 100)).setScale(2, BigDecimal.ROUND_UP);
  169. p.setShopFee1(String.valueOf(shopFee1));
  170. }
  171. }
  172. BigDecimal shopFee = MathUtil.mul(p.getNum(), p.getShopFee1());
  173. p.setShopFee(shopFee.toString());
  174. }
  175. }
  176. return list;
  177. }
  178. /**
  179. * 查询促销可用商品
  180. */
  181. public Page findProductPage(Page<Product> productPage, Product product) {
  182. product.setPage(productPage);
  183. List<Integer> list = new ArrayList<>();
  184. //已添加的商品id不能被查询到
  185. if (StringUtil.isNotBlank(product.getProductIds())) {
  186. if (product.getProductIds().contains(",")) {
  187. String[] split = product.getProductIds().split(",");
  188. for (String productId : split) {
  189. if (StringUtil.isNotBlank(productId)) {
  190. list.add(Integer.valueOf(productId));
  191. }
  192. }
  193. } else {
  194. list.add(Integer.valueOf(product.getProductIds()));
  195. }
  196. }
  197. product.setIds(list);
  198. //已删除的商品id能被查询到
  199. List<Integer> delList = new ArrayList<>();
  200. if (StringUtils.isNotBlank(product.getDelProductIds())) {
  201. if (product.getDelProductIds().contains(",")) {
  202. String split[] = product.getDelProductIds().split(",");
  203. for (String productId : split) {
  204. if (StringUtils.isNotBlank(productId)) {
  205. delList.add(Integer.parseInt(productId));
  206. }
  207. }
  208. }else {
  209. delList.add(Integer.parseInt(product.getDelProductIds()));
  210. }
  211. }
  212. product.setDelIdList(delList);
  213. List<Product> productList = new ArrayList<>();
  214. //当选择促销商品时,需要排除掉相同促销类型下已参与促销的商品
  215. productList = cmOrganizePromotionDao.findAllProduct(product);
  216. String wwwServer = Global.getConfig("wwwServer");
  217. DecimalFormat decimalFormat = new DecimalFormat("0000");
  218. productList.forEach(item ->{
  219. //图片处理
  220. item.setMainImage(AppUtils.getImageURL("product", item.getMainImage(), 0, wwwServer));
  221. //格式化组织商品Id
  222. item.setProductIdStr(decimalFormat.format(item.getProductID()));
  223. });
  224. productPage.setList(productList);
  225. return productPage;
  226. }
  227. /**
  228. * 查询促销可用供应商
  229. */
  230. public Page findShopPage(Page<Shop> shopPage, Shop shop) {
  231. shop.setPage(shopPage);
  232. List<Integer> list = new ArrayList<>();
  233. if (StringUtil.isNotBlank(shop.getShopIds()) ) {
  234. if (shop.getShopIds().contains(",")) {
  235. String[] split = shop.getShopIds().split(",");
  236. for (String shopId : split) {
  237. if (StringUtil.isNotBlank(shopId)) {
  238. list.add(Integer.valueOf(shopId));
  239. }
  240. }
  241. } else {
  242. list.add(Integer.valueOf(shop.getShopIds()));
  243. }
  244. }
  245. shop.setIds(list);
  246. List<Integer> delIdlist = new ArrayList<>();
  247. if (StringUtils.isNotBlank(shop.getDelShopIds())) {
  248. String[] split = shop.getDelShopIds().split(",");
  249. for (String delShopId : split) {
  250. if (StringUtil.isNotBlank(delShopId)) {
  251. delIdlist.add(Integer.valueOf(delShopId));
  252. }
  253. }
  254. }
  255. shop.setDelShopIdList(delIdlist);
  256. List<Shop> shopList = new ArrayList<>();
  257. shopList = cmOrganizePromotionDao.findAllShop(shop);
  258. shopPage.setList(shopList);
  259. return shopPage;
  260. }
  261. /**
  262. * 保存促销活动
  263. */
  264. @Transactional(readOnly = false)
  265. @Override
  266. public void save(CmOrganizePromotion cmOrganizePromotion) {
  267. if (StringUtil.isBlank(cmOrganizePromotion.getId())) {
  268. cmOrganizePromotion.setAddTime(new Date());
  269. cmOrganizePromotion.setDelFlag("0");
  270. }
  271. cmOrganizePromotion.setUpdateTime(new Date());
  272. if (StringUtils.isNotBlank(cmOrganizePromotion.getType()) && StringUtils.isNotBlank(cmOrganizePromotion.getMode())) {
  273. if ("1".equals(cmOrganizePromotion.getType()) && "1".equals(cmOrganizePromotion.getMode())){
  274. // 优惠价
  275. cmOrganizePromotion.setName("优惠价");
  276. }else{
  277. String type = "1".equals(cmOrganizePromotion.getType()) ? "单品" : "2".equals(cmOrganizePromotion.getType()) ? "凑单" : "3".equals(cmOrganizePromotion.getType()) ? "店铺" : "优惠";
  278. String mode = "2".equals(cmOrganizePromotion.getMode()) ? "满减" : "3".equals(cmOrganizePromotion.getMode()) ? "满赠" : "优惠";
  279. cmOrganizePromotion.setName(type + mode);
  280. }
  281. }
  282. if (cmOrganizePromotion.getIsNewRecord()){
  283. cmOrganizePromotion.preInsert();
  284. cmOrganizePromotionDao.insert(cmOrganizePromotion);
  285. }else{
  286. cmOrganizePromotion.preUpdate();
  287. cmOrganizePromotionDao.update(cmOrganizePromotion);
  288. }
  289. String promotionId = cmOrganizePromotion.getId();
  290. //删除数据库中该促销活动原来的商品、赠品和商店
  291. cmOrganizePromotionDao.deleteAllPromotionProductsAndShops(promotionId);
  292. cmOrganizePromotionDao.deleteAllPromotionGifts(promotionId);
  293. if (StringUtil.isNotBlank(cmOrganizePromotion.getProductIds())) {
  294. List<HashMap> list = JSONArray.parseArray(cmOrganizePromotion.getProductIds(), HashMap.class);
  295. for (HashMap map : list) {
  296. String productId = (String) map.get("productId");
  297. cmOrganizePromotionDao.insertPromotionProduct(promotionId, productId);
  298. }
  299. }
  300. if (StringUtil.isNotBlank(cmOrganizePromotion.getGiftIds())) {
  301. List<HashMap> list = JSONArray.parseArray(cmOrganizePromotion.getGiftIds(), HashMap.class);
  302. for (HashMap map : list) {
  303. String giftId = (String) map.get("productId");
  304. Integer number = Integer.parseInt(map.get("number").toString());
  305. cmOrganizePromotionDao.insertGiftProduct(promotionId, giftId, number);
  306. }
  307. }
  308. if (StringUtil.isNotBlank(cmOrganizePromotion.getShopIds())) {
  309. cmOrganizePromotionDao.insertPromotionShop(promotionId, cmOrganizePromotion.getShopIds());
  310. }
  311. }
  312. /**
  313. * 更新促销活动
  314. */
  315. @Transactional(readOnly = false)
  316. public void update(CmOrganizePromotion cmOrganizePromotion) {
  317. cmOrganizePromotion.setUpdateTime(new Date());
  318. cmOrganizePromotion.preUpdate();
  319. cmOrganizePromotionDao.update(cmOrganizePromotion);
  320. }
  321. /**
  322. * 查询供应商信息
  323. */
  324. public Shop findShop(String shopId) {
  325. return cmOrganizePromotionDao.findShop(shopId);
  326. }
  327. /**
  328. * 查询产品对应的促销活动
  329. */
  330. public CmOrganizePromotion findProductPromotion(String shopId, Long productId) {
  331. return cmOrganizePromotionDao.findProductPromotion(shopId, productId);
  332. }
  333. /**
  334. * 获取组织商品信息
  335. * @param productId
  336. * @return
  337. */
  338. public Product getProduct(String productId) {
  339. return cmOrganizePromotionDao.getProductByProductId(productId);
  340. }
  341. }