123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- 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<CmOrganizePromotionDao, CmOrganizePromotion> {
- @Autowired
- private CmOrganizePromotionDao cmOrganizePromotionDao;
- @Override
- public CmOrganizePromotion get(String id) {
- return super.get(id);
- }
- /**
- * 促销活动列表
- */
- @Override
- public Page<CmOrganizePromotion> findPage(Page<CmOrganizePromotion> page, CmOrganizePromotion cmOrganizePromotion) {
- Page<CmOrganizePromotion> promotionPage = super.findPage(page, cmOrganizePromotion);
- List<CmOrganizePromotion> promotionList = promotionPage.getList();
- List<Product> promotionProducts = new ArrayList<>();
- List<Product> giftProducts = new ArrayList<>();
- List<Shop> 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<Shop> findPromotionShops(CmOrganizePromotion cmOrganizePromotion) {
- List<Shop> shops = cmOrganizePromotionDao.findPromotionShops(cmOrganizePromotion);
- for (Shop shop : shops) {
- if (shop != null) {
- shop.setStoreStatus(true);
- }
- }
- return shops;
- }
- /**
- * 根据促销活动id查询促销商品
- */
- public List<Product> findPromotionProduct(CmOrganizePromotion cmOrganizePromotion) {
- List<Product> 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<Product> findGiftProduct(String id) {
- List<Product> 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<PurchaseProduct> findGiftPurchaseProduct(String id){
- List<PurchaseProduct> 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<Product> productPage, Product product) {
- product.setPage(productPage);
- List<Integer> 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<Integer> 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<Product> 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<Shop> shopPage, Shop shop) {
- shop.setPage(shopPage);
- List<Integer> 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<Integer> 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<Shop> 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<HashMap> 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<HashMap> 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);
- }
- }
|