package com.caimei.modules.weisha.service; import com.alibaba.fastjson.JSONArray; import com.caimei.modules.bulkpurchase.entity.PurchaseProduct; import com.caimei.modules.cibe.entity.Shop; import com.caimei.modules.product.entity.Product; import com.caimei.modules.weisha.dao.CmOrganizePromotionDao; import com.caimei.modules.weisha.entity.CmOrganizePromotion; import com.caimei.utils.AppUtils; import com.caimei.utils.MathUtil; import com.caimei.utils.StringUtils; import com.foxinmy.weixin4j.util.StringUtil; import com.thinkgem.jeesite.common.config.Global; import com.thinkgem.jeesite.common.persistence.Page; import com.thinkgem.jeesite.common.service.CrudService; import com.thinkgem.jeesite.common.utils.DateUtils; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; @Service @Transactional(readOnly = true) public class CmOrganizePromotionService extends CrudService { @Autowired private CmOrganizePromotionDao cmOrganizePromotionDao; @Override public CmOrganizePromotion get(String id) { return super.get(id); } /** * 促销活动列表 */ @Override public Page findPage(Page page, CmOrganizePromotion cmOrganizePromotion) { Page promotionPage = super.findPage(page, cmOrganizePromotion); List promotionList = promotionPage.getList(); List promotionProducts = new ArrayList<>(); List giftProducts = new ArrayList<>(); List promotionShops = new ArrayList<>(); if (CollectionUtils.isNotEmpty(promotionList)) { for (CmOrganizePromotion promotion : promotionList ) { if (StringUtils.isNotBlank(cmOrganizePromotion.getProductName())) { promotion.setProductName(cmOrganizePromotion.getProductName()); } if (StringUtils.isNotBlank(cmOrganizePromotion.getShopName())) { promotion.setShopName(cmOrganizePromotion.getShopName()); } //未关闭 if ("0".equals(promotion.getDelFlag1())) { //永久活动设置为进行中 if ("1".equals(promotion.getStatus())) { promotion.setDelFlag1("2"); } else { Date nowTime = new Date(); if (null != promotion.getBeginTime() && null != promotion.getEndTime()) { boolean effectiveDate = DateUtils.isEffectiveDate(nowTime, promotion.getBeginTime(), promotion.getEndTime()); if (effectiveDate) { //进行中 promotion.setDelFlag1("2"); } else if (nowTime.compareTo(promotion.getEndTime()) > 0) { //已结束 promotion.setDelFlag1("3"); } else { //未开始 promotion.setDelFlag1("1"); } }else { promotion.setDelFlag1("0"); } } }else if("1".equals(promotion.getDelFlag1())){ promotion.setDelFlag1("0"); }else if("2".equals(promotion.getDelFlag1())){ promotion.setDelFlag1("4"); } //非店铺促销下设置促销产品 if (!"3".equals(promotion.getType())) { //根据促销id和商品名查询 promotionProducts = findPromotionProduct(promotion); promotion.setPromotionProducts(promotionProducts); }else { promotionShops = findPromotionShops(promotion); promotion.setPromotionShops(promotionShops); } //赠品模式下设置赠品 if ("3".equals(promotion.getMode())) { giftProducts = findGiftProduct(promotion.getId()); Integer giftNumber = 0; Integer giftTypeNumber = 0; for (Product product : giftProducts) { giftNumber += product.getGiftNumber(); giftTypeNumber++; } promotion.setGiftProducts(giftProducts); promotion.setGiftNumber(giftNumber); promotion.setGiftTypeNumber(giftTypeNumber); } } } return promotionPage; } /** * 根据促销活动id查询促销供应商 */ public List findPromotionShops(CmOrganizePromotion cmOrganizePromotion) { List shops = cmOrganizePromotionDao.findPromotionShops(cmOrganizePromotion); for (Shop shop : shops) { if (shop != null) { shop.setStoreStatus(true); } } return shops; } /** * 根据促销活动id查询促销商品 */ public List findPromotionProduct(CmOrganizePromotion cmOrganizePromotion) { List products = cmOrganizePromotionDao.findPromotionProduct(cmOrganizePromotion); String wwwServer = Global.getConfig("wwwServer"); for (Product product : products) { if (product != null) { product.setStoreStatus(true); product.setMainImage(AppUtils.getImageURL("product", product.getMainImage(), 0, wwwServer)); } } return products; } /** * 根据促销活动id查询促销赠品 */ public List findGiftProduct(String id) { List products = cmOrganizePromotionDao.findGiftProduct(id); String wwwServer = Global.getConfig("wwwServer"); for (Product product : products) { if (product != null) { product.setStoreStatus(true); product.setMainImage(AppUtils.getImageURL("product", product.getMainImage(), 0, wwwServer)); } } return products; } /** * 根据促销活动id查询促销赠品(订单) */ public List findGiftPurchaseProduct(String id){ List list = cmOrganizePromotionDao.findGiftPurchaseProduct(id); for (PurchaseProduct p : list) { if (p != null) { //1固定成本 2比例成 String costCheckFlag = p.getCostCheckFlag(); //固定成本价 if (com.thinkgem.jeesite.common.utils.StringUtils.equals("1", costCheckFlag)) { String costPrice = p.getCostPrice(); //只有固定成本,应付供应商的金额=添加商品时的成本价 p.setShopFee1(costPrice); } //比例成本价 if (com.thinkgem.jeesite.common.utils.StringUtils.equals("2", costCheckFlag)) { String costProportional = p.getCostProportional(); String price = p.getPrice(); if (com.thinkgem.jeesite.common.utils.StringUtils.isNotEmpty(costProportional)) { BigDecimal shopFee1 = (MathUtil.div(MathUtil.mul(costProportional, price), 100)).setScale(2, BigDecimal.ROUND_UP); p.setShopFee1(String.valueOf(shopFee1)); } } BigDecimal shopFee = MathUtil.mul(p.getNum(), p.getShopFee1()); p.setShopFee(shopFee.toString()); } } return list; } /** * 查询促销可用商品 */ public Page findProductPage(Page productPage, Product product) { product.setPage(productPage); List list = new ArrayList<>(); //已添加的商品id不能被查询到 if (StringUtil.isNotBlank(product.getProductIds())) { if (product.getProductIds().contains(",")) { String[] split = product.getProductIds().split(","); for (String productId : split) { if (StringUtil.isNotBlank(productId)) { list.add(Integer.valueOf(productId)); } } } else { list.add(Integer.valueOf(product.getProductIds())); } } product.setIds(list); //已删除的商品id能被查询到 List delList = new ArrayList<>(); if (StringUtils.isNotBlank(product.getDelProductIds())) { if (product.getDelProductIds().contains(",")) { String split[] = product.getDelProductIds().split(","); for (String productId : split) { if (StringUtils.isNotBlank(productId)) { delList.add(Integer.parseInt(productId)); } } }else { delList.add(Integer.parseInt(product.getDelProductIds())); } } product.setDelIdList(delList); List productList = new ArrayList<>(); //当选择促销商品时,需要排除掉相同促销类型下已参与促销的商品 productList = cmOrganizePromotionDao.findAllProduct(product); String wwwServer = Global.getConfig("wwwServer"); DecimalFormat decimalFormat = new DecimalFormat("0000"); productList.forEach(item ->{ //图片处理 item.setMainImage(AppUtils.getImageURL("product", item.getMainImage(), 0, wwwServer)); //格式化组织商品Id item.setProductIdStr(decimalFormat.format(item.getProductID())); }); productPage.setList(productList); return productPage; } /** * 查询促销可用供应商 */ public Page findShopPage(Page shopPage, Shop shop) { shop.setPage(shopPage); List list = new ArrayList<>(); if (StringUtil.isNotBlank(shop.getShopIds()) ) { if (shop.getShopIds().contains(",")) { String[] split = shop.getShopIds().split(","); for (String shopId : split) { if (StringUtil.isNotBlank(shopId)) { list.add(Integer.valueOf(shopId)); } } } else { list.add(Integer.valueOf(shop.getShopIds())); } } shop.setIds(list); List delIdlist = new ArrayList<>(); if (StringUtils.isNotBlank(shop.getDelShopIds())) { String[] split = shop.getDelShopIds().split(","); for (String delShopId : split) { if (StringUtil.isNotBlank(delShopId)) { delIdlist.add(Integer.valueOf(delShopId)); } } } shop.setDelShopIdList(delIdlist); List shopList = new ArrayList<>(); shopList = cmOrganizePromotionDao.findAllShop(shop); shopPage.setList(shopList); return shopPage; } /** * 保存促销活动 */ @Transactional(readOnly = false) @Override public void save(CmOrganizePromotion cmOrganizePromotion) { if (StringUtil.isBlank(cmOrganizePromotion.getId())) { cmOrganizePromotion.setAddTime(new Date()); cmOrganizePromotion.setDelFlag("0"); } cmOrganizePromotion.setUpdateTime(new Date()); if (StringUtils.isNotBlank(cmOrganizePromotion.getType()) && StringUtils.isNotBlank(cmOrganizePromotion.getMode())) { if ("1".equals(cmOrganizePromotion.getType()) && "1".equals(cmOrganizePromotion.getMode())){ // 优惠价 cmOrganizePromotion.setName("优惠价"); }else{ String type = "1".equals(cmOrganizePromotion.getType()) ? "单品" : "2".equals(cmOrganizePromotion.getType()) ? "凑单" : "3".equals(cmOrganizePromotion.getType()) ? "店铺" : "优惠"; String mode = "2".equals(cmOrganizePromotion.getMode()) ? "满减" : "3".equals(cmOrganizePromotion.getMode()) ? "满赠" : "优惠"; cmOrganizePromotion.setName(type + mode); } } if (cmOrganizePromotion.getIsNewRecord()){ cmOrganizePromotion.preInsert(); cmOrganizePromotionDao.insert(cmOrganizePromotion); }else{ cmOrganizePromotion.preUpdate(); cmOrganizePromotionDao.update(cmOrganizePromotion); } String promotionId = cmOrganizePromotion.getId(); //删除数据库中该促销活动原来的商品、赠品和商店 cmOrganizePromotionDao.deleteAllPromotionProductsAndShops(promotionId); cmOrganizePromotionDao.deleteAllPromotionGifts(promotionId); if (StringUtil.isNotBlank(cmOrganizePromotion.getProductIds())) { List list = JSONArray.parseArray(cmOrganizePromotion.getProductIds(), HashMap.class); for (HashMap map : list) { String productId = (String) map.get("productId"); cmOrganizePromotionDao.insertPromotionProduct(promotionId, productId); } } if (StringUtil.isNotBlank(cmOrganizePromotion.getGiftIds())) { List list = JSONArray.parseArray(cmOrganizePromotion.getGiftIds(), HashMap.class); for (HashMap map : list) { String giftId = (String) map.get("productId"); Integer number = Integer.parseInt(map.get("number").toString()); cmOrganizePromotionDao.insertGiftProduct(promotionId, giftId, number); } } if (StringUtil.isNotBlank(cmOrganizePromotion.getShopIds())) { cmOrganizePromotionDao.insertPromotionShop(promotionId, cmOrganizePromotion.getShopIds()); } } /** * 更新促销活动 */ @Transactional(readOnly = false) public void update(CmOrganizePromotion cmOrganizePromotion) { cmOrganizePromotion.setUpdateTime(new Date()); cmOrganizePromotion.preUpdate(); cmOrganizePromotionDao.update(cmOrganizePromotion); } /** * 查询供应商信息 */ public Shop findShop(String shopId) { return cmOrganizePromotionDao.findShop(shopId); } /** * 查询产品对应的促销活动 */ public CmOrganizePromotion findProductPromotion(String shopId, Long productId) { return cmOrganizePromotionDao.findProductPromotion(shopId, productId); } /** * 获取组织商品信息 * @param productId * @return */ public Product getProduct(String productId) { return cmOrganizePromotionDao.getProductByProductId(productId); } }