package com.hai.service.impl; import com.hai.common.QRCodeGenerator; import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; import com.hai.common.utils.DateUtil; import com.hai.dao.HighCouponCodeMapper; import com.hai.dao.HighCouponCodeMapperExt; import com.hai.entity.HighChildOrder; import com.hai.entity.HighCouponCode; import com.hai.entity.HighCouponCodeExample; import com.hai.entity.HighOrder; import com.hai.model.UserInfoModel; import com.hai.service.HighCouponCodeService; import com.hai.service.HighOrderService; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Auther: 胡锐 * @Description: * @Date: 2021/3/14 17:45 */ @Service("highCouponCodeService") public class HighCouponCodeServiceImpl implements HighCouponCodeService { @Resource private HighCouponCodeMapper highCouponCodeMapper; @Resource private HighCouponCodeMapperExt highCouponCodeMapperExt; @Resource private HighOrderService highOrderService; @Override public void insertCouponCode(HighCouponCode highCouponCode) { highCouponCodeMapper.insert(highCouponCode); } @Override public void insertList(List list) throws Exception { for (HighCouponCode highCouponCode: list) { // 生成二维码 String qrCodeImg = DateUtil.date2String(new Date(),"yyyyMMddHHmmss")+".png"; String qrCodeUrl = "/home/project/hsg/filesystem/couponCode/" + qrCodeImg; QRCodeGenerator.generateQRCodeImage(list.get(0).getSalesCode(), 350, 350, qrCodeUrl); highCouponCode.setExt1(qrCodeImg); } highCouponCodeMapperExt.insertList(list); } @Override public void updateCouponCode(HighCouponCode highCouponCode) { highCouponCodeMapper.updateByPrimaryKey(highCouponCode); } @Override @Transactional(propagation= Propagation.REQUIRES_NEW) public void useCouponCode(String code, UserInfoModel userInfoModel) { // 查询销售码 HighCouponCode salesCode = getCouponCodeBySalesCode(code); if (salesCode == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_COUPON_CODE, ""); } // 状态:1.待销售 2.未使用 3.已使用 99.预支付 if (salesCode.getStatus() != 2) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_CODE_STATUS, ""); } // 使用时间已到期 if (salesCode.getUseEndTime().compareTo(new Date()) == -1) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_CODE_OVERDUE, ""); } salesCode.setStoreId(userInfoModel.getMerchantStore().getId()); salesCode.setConsumeTime(new Date()); salesCode.setStatus(3); updateCouponCode(salesCode); highOrderService.childOrderComplete(salesCode.getChildOrderId()); } @Override public HighCouponCode getCouponCodeByOrderId(Long childOrderId) { HighCouponCodeExample example = new HighCouponCodeExample(); example.createCriteria().andChildOrderIdEqualTo(childOrderId); List codes = highCouponCodeMapper.selectByExample(example); if (codes != null && codes.size() > 0) { return codes.get(0); } return null; } @Override public HighCouponCode getCouponCodeBySalesCode(String code) { HighCouponCodeExample example = new HighCouponCodeExample(); example.createCriteria().andSalesCodeEqualTo(code); List codes = highCouponCodeMapper.selectByExample(example); if (codes != null && codes.size() > 0) { return codes.get(0); } return null; } @Override public HighCouponCode getCouponCodeById(Long id) { return highCouponCodeMapper.selectByPrimaryKey(id); } @Override public Integer getStockCountByCoupon(Long couponId) { HighCouponCodeExample example = new HighCouponCodeExample(); example.createCriteria().andCouponIdEqualTo(couponId).andStatusEqualTo(1).andSalesEndTimeGreaterThan(new Date()); return highCouponCodeMapper.selectByExample(example).size(); } @Override public List getNoSaleCode(Long couponId) { Map map = new HashMap<>(); map.put("couponId", couponId); map.put("status", 1); return getCouponCodeList(map); } @Override public List getCouponCodeList(Map map) { HighCouponCodeExample example = new HighCouponCodeExample(); HighCouponCodeExample.Criteria criteria = example.createCriteria(); if (MapUtils.getLong(map, "couponId") != null) { criteria.andCouponIdEqualTo(MapUtils.getLong(map, "couponId")); } if (MapUtils.getInteger(map, "status") != null) { criteria.andStatusEqualTo(MapUtils.getInteger(map, "status")); } if (StringUtils.isNotBlank(MapUtils.getString(map, "salesCode"))) { criteria.andSalesCodeLike("%" + MapUtils.getString(map, "salesCode") + "%"); } example.setOrderByClause("create_time desc"); return highCouponCodeMapper.selectByExample(example); } }