package com.hai.service.impl; import com.alibaba.fastjson.JSON; import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; import com.hai.dao.HighCouponHandselMapper; import com.hai.dao.HighCouponMapper; import com.hai.entity.*; import com.hai.model.HighCouponHandselModel; import com.hai.model.HighCouponModel; import com.hai.service.*; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; import java.util.stream.Collectors; /** * @Auther: 胡锐 * @Description: * @Date: 2021/3/11 22:07 */ @Service("highCouponService") public class HighCouponServiceImpl implements HighCouponService { @Resource private HighCouponMapper highCouponMapper; @Resource private HighCouponHandselService highCouponHandselService; @Resource private HighGoodsPriceReferService highGoodsPriceReferService; @Resource private HighCouponCodeService highCouponCodeService; @Resource private HighMerchantService highMerchantService; @Resource private HighApproveService highApproveService; @Override @Transactional(propagation= Propagation.REQUIRES_NEW) public void insertCoupon(HighCouponModel highCouponModel) { highCouponMapper.insert(highCouponModel); // 增加赠送卡卷 if (highCouponModel.getHandselCouponList() != null && highCouponModel.getHandselCouponList().size() > 0) { for (HighCouponHandselModel handsel : highCouponModel.getHandselCouponList()) { handsel.setCouponId(highCouponModel.getId()); highCouponHandselService.insertCouponHandsel(handsel); } } HighGoodsPriceRefer goodsPriceRefer = new HighGoodsPriceRefer(); goodsPriceRefer.setObjectType(1); goodsPriceRefer.setObjectId(highCouponModel.getId()); goodsPriceRefer.setNewSalePrice(highCouponModel.getSalesPrice()); goodsPriceRefer.setPromptlyType(true); goodsPriceRefer.setEffectiveTime(new Date()); goodsPriceRefer.setRemark("【创建产品】初始销售价格"); goodsPriceRefer.setStatus(1); // 状态: 0:删除 1:待编辑 2:未生效 3:已生效 4:已失效 101.审批中 102.审批驳回 goodsPriceRefer.setCreateTime(new Date()); goodsPriceRefer.setOperatorId(highCouponModel.getOperatorId()); goodsPriceRefer.setOperatorName(highCouponModel.getOperatorName()); highGoodsPriceReferService.insertPriceRefer(goodsPriceRefer); } @Override @Transactional(propagation= Propagation.REQUIRES_NEW) public void updateCoupon(HighCouponModel highCouponModel) { highCouponMapper.updateByPrimaryKey(highCouponModel); // 赠送卡卷 if (highCouponModel.getHandselCouponList() != null && highCouponModel.getHandselCouponList().size() > 0) { for (HighCouponHandselModel handsel : highCouponModel.getHandselCouponList()) { // 状态标记为删除的,进行删除操作 if (handsel.getStatus() == 0) { highCouponHandselService.updateCouponHandsel(handsel); } // id为空的,进行增加操作 if (handsel.getId() == null) { handsel.setCouponId(highCouponModel.getId()); highCouponHandselService.insertCouponHandsel(handsel); } } } // 查询卡卷 如果卡卷销售价格进行了修改 HighCouponModel coupon = getCouponById(highCouponModel.getId()); if (!coupon.getCouponPrice().equals(highCouponModel.getCouponPrice())) { // 所有价格失效 highGoodsPriceReferService.couponAllPriceInvalid(highCouponModel.getId()); // 增加新价格 HighGoodsPriceRefer goodsPriceRefer = new HighGoodsPriceRefer(); goodsPriceRefer.setObjectType(1); goodsPriceRefer.setObjectId(highCouponModel.getId()); goodsPriceRefer.setNewSalePrice(highCouponModel.getSalesPrice()); goodsPriceRefer.setPromptlyType(true); goodsPriceRefer.setEffectiveTime(new Date()); goodsPriceRefer.setRemark("【修改卡卷】修改销售价格"); goodsPriceRefer.setStatus(1); // 状态: 0:删除 1:待编辑 2:未生效 3:已生效 4:已失效 101.审批中 102.审批驳回 goodsPriceRefer.setCreateTime(new Date()); goodsPriceRefer.setOperatorId(highCouponModel.getOperatorId()); goodsPriceRefer.setOperatorName(highCouponModel.getOperatorName()); highGoodsPriceReferService.insertPriceRefer(goodsPriceRefer); } } @Override public void update(HighCoupon highCoupon) { highCouponMapper.updateByPrimaryKey(highCoupon); } @Override @Transactional(propagation= Propagation.REQUIRES_NEW) public void upShelfApprove(Long id) { // 查询卡卷 HighCouponModel coupon = getCouponById(id); if (coupon == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_COUPON, ""); } coupon.setStatus(101); // 状态:0.删除 1.编辑中 2.已上架 3.已下架 101.上架审批中 102.上架审批驳回 coupon.setUpdateTime(new Date()); update(coupon); } @Override @Transactional(propagation= Propagation.REQUIRES_NEW) public void upShelfApproveResult(HighApprove highApprove) { // 查询卡卷 HighCouponModel couponDetail = getCouponDetail(highApprove.getObjectId()); if (couponDetail == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_COUPON, ""); } if (couponDetail.getStatus() != 101) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_UNABLE_UP_SHELF, ""); } // 状态: 1:待审批 2:驳回 3:通过 if (highApprove.getStatus() == 1) { couponDetail.setStatus(2); Map map = new HashMap<>(); map.put("objectType", 1); map.put("objectId", couponDetail.getId()); map.put("status", 1); List list = highGoodsPriceReferService.getPriceList(map); if (list != null && list.size() > 0) { for (HighGoodsPriceRefer priceRefer : list) { priceRefer.setEffectiveTime(new Date()); priceRefer.setStatus(3); highGoodsPriceReferService.updatePriceRefer(priceRefer); } } } if (highApprove.getStatus() == 2) { couponDetail.setStatus(102); } highCouponMapper.updateByPrimaryKey(couponDetail); } @Override @Transactional(propagation= Propagation.REQUIRES_NEW) public void ofShelfCoupon(Long id) { HighCouponModel coupon = getCouponById(id); if (coupon == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_COUPON, ""); } //状态:0.删除 1.编辑中 2.已上架 3.已下架 101.上架审批中 102.上架审批驳回 if (coupon.getStatus() != 2) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_UNABLE_UP_SHELF, ""); } coupon.setStatus(3); highCouponMapper.updateByPrimaryKey(coupon); // 卡卷价格全部失效。但保留当前生效的价格 highGoodsPriceReferService.couponAllInvalidKeepCurrent(coupon.getId()); } @Override public void deleteCoupon(Long id) { HighCouponModel coupon = getCouponById(id); if (coupon == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_COUPON, ""); } //状态:0.删除 1.编辑中 2.已上架 3.已下架 101.上架审批中 102.上架审批驳回 if (coupon.getStatus() != 1 && coupon.getStatus() != 102) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_UNABLE_UP_SHELF, ""); } coupon.setStatus(0); highCouponMapper.updateByPrimaryKey(coupon); } @Override @Transactional(propagation= Propagation.REQUIRES_NEW) public void priceApproveResult(HighApprove highApprove) { HighGoodsPriceRefer highGoodsPriceRefer = highGoodsPriceReferService.getPriceReferById(highApprove.getObjectId()); if (highGoodsPriceRefer == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.GOODS_PRICE_REFER_ERROR, ""); } // 状态: 0:删除 1:待编辑 2:待生效 3:已生效 4:已失效 101.审批中 102.审批驳回 if (highGoodsPriceRefer.getStatus() != 101) { throw ErrorHelp.genException(SysCode.System, ErrorCode.STATUS_ERROR, ""); } // 卡卷信息 HighCouponModel coupon = getCouponById(highGoodsPriceRefer.getObjectId()); if (coupon == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_COUPON, ""); } // 状态: 1:待审批 2:驳回 3:通过 if (highApprove.getStatus() == 1) { // 及时生效 0:否 1:是 if (highGoodsPriceRefer.getPromptlyType() == true) { coupon.setCouponPrice(highGoodsPriceRefer.getNewSalePrice()); update(coupon); } highGoodsPriceRefer.setStatus(2); } if (highApprove.getStatus() == 2) { highGoodsPriceRefer.setStatus(102); } highGoodsPriceReferService.updatePriceRefer(highGoodsPriceRefer); } @Override public HighCouponModel getCouponById(Long id) { HighCoupon highCoupon = highCouponMapper.selectByPrimaryKey(id); if (highCoupon == null) { return null; } HighCouponModel model = new HighCouponModel(); BeanUtils.copyProperties(highCoupon, model); // 查询有效库存 model.setSalesCount(highCouponCodeService.getStockCountByCoupon(highCoupon.getId())); // 查询赠送卡卷列表 model.setHandselCouponList(highCouponHandselService.getHandselListByCoupon(highCoupon.getId())); return model; } @Override public HighCouponModel getCouponDetail(Long id) { HighCoupon highCoupon = highCouponMapper.selectByPrimaryKey(id); if (highCoupon == null) { return null; } HighCouponModel model = new HighCouponModel(); BeanUtils.copyProperties(highCoupon, model); // 查询有效库存 model.setSalesCount(highCouponCodeService.getStockCountByCoupon(highCoupon.getId())); return model; } @Override public List getCouponList(Map map) { HighCouponExample example = new HighCouponExample(); HighCouponExample.Criteria criteria = example.createCriteria(); if (MapUtils.getLong(map, "companyId") != null) { criteria.andCompanyIdEqualTo(MapUtils.getLong(map, "companyId")); } if (MapUtils.getLong(map, "merchantId") != null) { criteria.andMerchantIdEqualTo(MapUtils.getLong(map, "merchantId")); } if (StringUtils.isNotBlank(MapUtils.getString(map, "couponName"))) { criteria.andCouponNameLike("%" + MapUtils.getString(map, "couponName") + "%"); } if (MapUtils.getInteger(map, "couponType") != null) { criteria.andCouponTypeEqualTo(MapUtils.getInteger(map, "couponType")); } if (MapUtils.getInteger(map, "status") != null) { criteria.andStatusEqualTo(MapUtils.getInteger(map, "status")); } List coupons = highCouponMapper.selectByExample(example); if (coupons != null && coupons.size() > 0) { List models = JSON.parseArray(JSON.toJSONString(coupons), HighCouponModel.class); for (HighCouponModel highCouponModel : models) { highCouponModel.setMerchantName(highMerchantService.getMerchantById(highCouponModel.getMerchantId()).getMerchantName()); highCouponModel.setStockCount(highCouponCodeService.getStockCountByCoupon(highCouponModel.getId())); } return models; } return new ArrayList<>(); } }