package com.hai.service.impl; import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; import com.hai.dao.*; import com.hai.entity.*; import com.hai.model.UserInfoModel; import com.hai.service.*; import com.hai.service.HighDiscountPackageService; import org.apache.commons.collections4.MapUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.*; /** * @serviceName HighDiscountPackageServiceImpl.java * @author Sum1Dream * @version 1.0.0 * @Description // 优惠券包 接口 * @createTime 12:21 下午 2021/11/25 **/ @Service("highDiscountPackageService") public class HighDiscountPackageServiceImpl implements HighDiscountPackageService { @Resource private HighDiscountPackageMapper highDiscountPackageMapper; @Resource private HighDiscountPackageRecordMapper highDiscountPackageRecordMapper; @Resource private HighDiscountPackageDetailsService highDiscountPackageDetailsService; @Resource private HighDiscountAgentRelService highDiscountAgentRelService; @Resource private HighDiscountAgentCodeService highDiscountAgentCodeService; @Resource private HighDiscountPackageActualMapper discountPackageActualMapper; @Resource private HighDiscountPackageDiscountActualMapper discountPackageDiscountActualMapper; @Resource private HighDiscountInventoryRecordMapper discountInventoryRecordMapper; @Override public List getDiscountPackageList(Map map) { HighDiscountPackageExample example = new HighDiscountPackageExample(); HighDiscountPackageExample.Criteria criteria = example.createCriteria(); if (MapUtils.getInteger(map, "companyId") != null) { criteria.andCompanyIdEqualTo(MapUtils.getInteger(map, "companyId")); } if (MapUtils.getInteger(map, "usingAttribution") != null) { criteria.andUsingAttributionEqualTo(MapUtils.getInteger(map, "usingAttribution")); } if (MapUtils.getString(map, "title") != null) { criteria.andTitleLike("%" + MapUtils.getString(map, "title") + "%"); } if (MapUtils.getInteger(map, "salesType") != null) { criteria.andSalesTypeEqualTo(MapUtils.getInteger(map, "salesType")); } if (MapUtils.getInteger(map, "status") != null) { criteria.andStatusEqualTo(MapUtils.getInteger(map, "status")); } else { criteria.andStatusNotEqualTo(4); } example.setOrderByClause("created_time desc"); return highDiscountPackageMapper.selectByExample(example); } @Override public HighDiscountPackage findDiscountPackageById(Integer id) { return highDiscountPackageMapper.selectByPrimaryKey(id); } @Override public HighDiscountPackage getCallExclusive(Integer usingAttribution, Integer companyId) { HighDiscountPackageExample example = new HighDiscountPackageExample(); HighDiscountPackageExample.Criteria criteria = example.createCriteria(); criteria.andUsingAttributionEqualTo(usingAttribution).andStatusNotEqualTo(4).andCompanyIdEqualTo(companyId); List list = highDiscountPackageMapper.selectByExample(example); if (list.size() == 0) { return null; } else { return list.get(0); } } @Override public void insertDiscountPackage(HighDiscountPackage highDiscountPackage) { highDiscountPackageMapper.insert(highDiscountPackage); } @Override public void updateDiscountPackage(HighDiscountPackage highDiscountPackage) { highDiscountPackageMapper.updateByPrimaryKey(highDiscountPackage); } @Override public List getDiscountPackageRecordList(Map map) { HighDiscountPackageRecordExample example = new HighDiscountPackageRecordExample(); HighDiscountPackageRecordExample.Criteria criteria = example.createCriteria(); if (MapUtils.getInteger(map, "companyId") != null) { criteria.andCompanyIdEqualTo(MapUtils.getInteger(map, "companyId")); } if (MapUtils.getInteger(map, "usingAttribution") != null) { criteria.andUsingAttributionEqualTo(MapUtils.getInteger(map, "usingAttribution")); } if (MapUtils.getString(map, "discountPackageTitle") != null) { criteria.andDiscountPackageTitleLike("%" + MapUtils.getString(map, "discountPackageTitle") + "%"); } if (MapUtils.getString(map, "recordNo") != null) { criteria.andRecordNoLike("%" + MapUtils.getString(map, "recordNo") + "%"); } if (MapUtils.getInteger(map, "salesType") != null) { criteria.andSalesTypeEqualTo(MapUtils.getInteger(map, "salesType")); } if (MapUtils.getString(map, "phone") != null) { criteria.andExt1Like("%" + MapUtils.getString(map, "phone") + "%"); } return highDiscountPackageRecordMapper.selectByExample(example); } @Override @Transactional(propagation = Propagation.REQUIRES_NEW) public void addDiscountPackageStock(HighDiscountPackage highDiscountPackage, UserInfoModel userInfoModel, Integer num) { Map mapRule = new HashMap<>(); mapRule.put("discountPackageId", highDiscountPackage.getId()); // 查询优惠券包规则 List discountPackageDetailsList = highDiscountPackageDetailsService.getDiscountPackageDetailsList(mapRule); // 生成优惠券包实际库存实体 HighDiscountPackageActual discountPackageActual = new HighDiscountPackageActual(); discountPackageActual.setDiscountPackageId(highDiscountPackage.getId()); discountPackageActual.setCreatedUserId(userInfoModel.getSecUser().getId().intValue()); // 状态: 1: 待分配 2:预分配(售卖)3:已分配 discountPackageActual.setStatus(1); for (int i = 0; i < num; i++) { discountPackageActual.setCreatedTime(new Date()); discountPackageActualMapper.insert(discountPackageActual); // 循环 优惠券包规则详情 列表 for (HighDiscountPackageDetails detailsList : discountPackageDetailsList) { // 查询代理商与优惠券关系 列表 HighDiscountAgentRel discountAgentRel = highDiscountAgentRelService.getRelByDiscountAgent(detailsList.getDiscountId().longValue(), detailsList.getAgentId()); List discountAgentCodeList = highDiscountAgentCodeService.getDiscountCodeByStatus(discountAgentRel.getId() , 1); if (discountAgentCodeList.size() < detailsList.getNum()) { throw ErrorHelp.genException(SysCode.System , ErrorCode.COMMON_ERROR , detailsList.getDiscountName() +"优惠券库存不足"); } // 赠送优惠券 for (HighDiscountAgentCode discountAgentCode : discountAgentCodeList.subList(0, detailsList.getNum())) { HighDiscountPackageDiscountActual discountPackageDiscountActual = new HighDiscountPackageDiscountActual(); discountPackageDiscountActual.setDiscountPackageId(highDiscountPackage.getId()); discountPackageDiscountActual.setDiscountPackageActualId(discountPackageActual.getId()); discountPackageDiscountActual.setCreatedUserId(userInfoModel.getSecUser().getId().intValue()); discountPackageDiscountActual.setCreatedTime(new Date()); discountPackageDiscountActual.setAgentDiscountCodeId(discountAgentCode.getId()); discountPackageDiscountActual.setStatus(1); discountPackageDiscountActual.setDiscountId(detailsList.getDiscountId().longValue()); discountPackageDiscountActual.setAgentId(detailsList.getAgentId()); discountPackageDiscountActualMapper.insert(discountPackageDiscountActual); // 修改优惠券二维码状态 discountAgentCode.setStatus(5); highDiscountAgentCodeService.updateCode(discountAgentCode); } } } // 设置库存 int surplusStock = highDiscountPackage.getSurplusStock() == null ? 0 :highDiscountPackage.getSurplusStock(); int totalStock = highDiscountPackage.getTotalStock() == null ? 0 :highDiscountPackage.getTotalStock(); // 新增补库记录 HighDiscountInventoryRecord discountInventoryRecord = new HighDiscountInventoryRecord(); discountInventoryRecord.setDiscountPackageId(highDiscountPackage.getId()); discountInventoryRecord.setDiscountPackageTitle(highDiscountPackage.getTitle()); discountInventoryRecord.setCreatedUserId(userInfoModel.getSecUser().getId().intValue()); discountInventoryRecord.setCompanyId(userInfoModel.getBsCompany().getId().intValue()); discountInventoryRecord.setCreatedTime(new Date()); discountInventoryRecord.setStatus(1); discountInventoryRecord.setStockNum(num); discountInventoryRecord.setFrontStockNum(highDiscountPackage.getSurplusStock()); discountInventoryRecord.setNowStockNum(surplusStock + num); discountInventoryRecordMapper.insert(discountInventoryRecord); // 设置库存 highDiscountPackage.setTotalStock(totalStock + num); highDiscountPackageMapper.updateByPrimaryKey(highDiscountPackage); } }