package com.stylefeng.guns.modular.system.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.modular.crossCity.model.OrderCrossCity; import com.stylefeng.guns.modular.crossCity.server.IOrderCrossCityService; import com.stylefeng.guns.modular.smallLogistics.model.OrderLogistics; import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsService; import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar; import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService; import com.stylefeng.guns.modular.system.dao.UserMerchantCouponMapper; import com.stylefeng.guns.modular.system.model.*; import com.stylefeng.guns.modular.system.service.*; import com.stylefeng.guns.modular.system.util.ResultUtil; import com.stylefeng.guns.modular.system.util.UUIDUtil; import com.stylefeng.guns.modular.system.warpper.MerchantCouponListWarpper; import com.stylefeng.guns.modular.system.warpper.MerchantCouponWarpper; import com.stylefeng.guns.modular.system.warpper.UserMerchantCouponWapper; import com.stylefeng.guns.modular.taxi.model.OrderTaxi; import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.*; import java.util.stream.Collectors; @Service public class UserMerchantCouponServiceImpl extends ServiceImpl implements IUserMerchantCouponService { @Autowired private IOrderPrivateCarService orderPrivateCarService; @Autowired private IOrderTaxiService orderTaxiService; @Autowired private IOrderCrossCityService orderCrossCityService; @Autowired private IOrderLogisticsService orderLogisticsService; @Autowired private IMerchantActivityService merchantActivityService; @Autowired private IMerchantActivitySlaveService merchantActivitySlaveService; @Autowired private IUserMerchantCouponService userMerchantCouponService; @Autowired private IMerchantCouponService merchantCouponService; @Autowired private IMerchantService merchantService; @Autowired private IUserInfoService userInfoService; @Resource private RedisTemplate redisTemplate; @Override public ResultUtil getUserMerchantCoupon(Integer uid, String code) throws Exception { Merchant merchant = merchantService.selectOne(new EntityWrapper().eq("userType", 1).eq("userId", uid).ne("state", 3)); if (merchant.getAuditStatus() == 1) { return ResultUtil.error("账户正在审核中"); } if(merchant.getAuditStatus() == 3){ return ResultUtil.error("账户审核不通过"); } if(merchant.getState() == 2){ return ResultUtil.error("账户被冻结"); } UserMerchantCoupon userMerchantCoupon = userMerchantCouponService.selectOne(new EntityWrapper().eq("code", code).eq("state", 1)); MerchantCoupon merchantCoupon = merchantCouponService.selectById(userMerchantCoupon.getMerchantCouponId()); if(merchantCoupon.getMerchantId().compareTo(merchant.getId()) != 0){ return ResultUtil.error("无效的核销码"); } UserMerchantCouponWapper userMerchantCoupon1 = this.baseMapper.getUserMerchantCoupon(code); return ResultUtil.success(userMerchantCoupon1); } @Override public ResultUtil writeOffMerchantCoupon(Integer uid, String code) throws Exception { UserMerchantCoupon userMerchantCoupon = this.selectOne(new EntityWrapper().eq("code", code).eq("state", 1)); if(null == userMerchantCoupon){ return ResultUtil.error("无效的核销码"); } if(System.currentTimeMillis() > userMerchantCoupon.getEndTime().getTime() || userMerchantCoupon.getStatus() == 3){ return ResultUtil.error("优惠券已过期"); } if(userMerchantCoupon.getStatus() == 2){ return ResultUtil.error("优惠券已使用"); } userMerchantCoupon.setStatus(2); userMerchantCoupon.setWriteOffTime(new Date()); userMerchantCoupon.setWriteOffUserType(1); userMerchantCoupon.setWriteOffUserId(uid); this.updateById(userMerchantCoupon); return ResultUtil.success(); } @Override public List> getWriteOffHistory(Integer activityId, Integer id, Integer pageNum, Integer size) throws Exception { pageNum = (pageNum - 1) * size; return this.baseMapper.getWriteOffHistory(activityId, id, pageNum, size); } @Override public List getMyMerchantCoupon(Integer uid, Integer type) throws Exception { return this.baseMapper.getMyMerchantCoupon(uid, type); } @Override public void updateExpired() throws Exception { this.baseMapper.updateExpired(); } @Override public List giveAwayMerchantCoupon(Integer uid, Integer orderId, Integer orderType) throws Exception { List merchantActivities = new ArrayList<>(); Integer state = 0; if(orderType == 1){ OrderPrivateCar orderPrivateCar = orderPrivateCarService.selectById(orderId); List list = orderPrivateCarService.queryOrder(uid, 8, 9); merchantActivities = merchantActivityService.selectList(new EntityWrapper().eq("companyId", orderPrivateCar.getCompanyId()).eq("activityType", 1).eq("auditStatus", 2).eq("status", 1) .eq("state", 1).like("orderType", "%1%").where("((orderAmountFull is not null and orderAmountFull <= " + orderPrivateCar.getOrderMoney() + ") or (orderNumber is not null and orderNumber <= " + list.size() + "))") .in("id", merchantActivitySlaveService.selectList(new EntityWrapper().gt("laveNumber", 0)).stream().map(MerchantActivitySlave::getMerchantActivityId).collect(Collectors.toList())) .notIn("id", userMerchantCouponService.selectList(new EntityWrapper().eq("userType", 1).eq("userId", uid).eq("state", 1)).stream().map(UserMerchantCoupon::getMerchantActivityId).collect(Collectors.toList())) ); state = orderPrivateCar.getState(); } if(orderType == 2){ OrderTaxi orderTaxi = orderTaxiService.selectById(orderId); List list = orderTaxiService.queryOrder(uid, 8, 9); merchantActivities = merchantActivityService.selectList(new EntityWrapper().eq("companyId", orderTaxi.getCompanyId()).eq("activityType", 1).eq("auditStatus", 2).eq("status", 1) .eq("state", 1).like("orderType", "%2%").where("((orderAmountFull is not null and orderAmountFull <= " + orderTaxi.getOrderMoney() + ") or (orderNumber is not null and orderNumber <= " + list.size() + "))") .in("id", merchantActivitySlaveService.selectList(new EntityWrapper().gt("laveNumber", 0)).stream().map(MerchantActivitySlave::getMerchantActivityId).collect(Collectors.toList())) .notIn("id", userMerchantCouponService.selectList(new EntityWrapper().eq("userType", 1).eq("userId", uid).eq("state", 1)).stream().map(UserMerchantCoupon::getMerchantActivityId).collect(Collectors.toList())) ); state = orderTaxi.getState(); } if(orderType == 3){ OrderCrossCity orderCrossCity = orderCrossCityService.selectById(orderId); List list = orderCrossCityService.queryOrder(uid, 8, 9); merchantActivities = merchantActivityService.selectList(new EntityWrapper().eq("companyId", orderCrossCity.getCompanyId()).eq("activityType", 1).eq("auditStatus", 2).eq("status", 1) .eq("state", 1).like("orderType", "%3%").where("((orderAmountFull is not null and orderAmountFull <= " + orderCrossCity.getOrderMoney() + ") or (orderNumber is not null and orderNumber <= " + list.size() + "))") .in("id", merchantActivitySlaveService.selectList(new EntityWrapper().gt("laveNumber", 0)).stream().map(MerchantActivitySlave::getMerchantActivityId).collect(Collectors.toList())) .notIn("id", userMerchantCouponService.selectList(new EntityWrapper().eq("userType", 1).eq("userId", uid).eq("state", 1)).stream().map(UserMerchantCoupon::getMerchantActivityId).collect(Collectors.toList())) ); state = orderCrossCity.getState(); } if(orderType == 4){ OrderLogistics orderLogistics = orderLogisticsService.selectById(orderId); List list = orderLogisticsService.selectList(new EntityWrapper().eq("userId", uid).eq("type", orderLogistics.getType()).eq("state", 9).eq("isDelete", 1)); merchantActivities = merchantActivityService.selectList(new EntityWrapper().eq("companyId", orderLogistics.getCompanyId()).eq("activityType", 1).eq("auditStatus", 2).eq("status", 1) .eq("state", 1).like("orderType", "%4%").where("((orderAmountFull is not null and orderAmountFull <= " + orderLogistics.getOrderMoney() + ") or (orderNumber is not null and orderNumber <= " + list.size() + "))") .in("id", merchantActivitySlaveService.selectList(new EntityWrapper().gt("laveNumber", 0)).stream().map(MerchantActivitySlave::getMerchantActivityId).collect(Collectors.toList())) .notIn("id", userMerchantCouponService.selectList(new EntityWrapper().eq("userType", 1).eq("userId", uid).eq("state", 1)).stream().map(UserMerchantCoupon::getMerchantActivityId).collect(Collectors.toList())) ); state = orderLogistics.getState(); } if(orderType == 5){ OrderLogistics orderLogistics = orderLogisticsService.selectById(orderId); List list = orderLogisticsService.selectList(new EntityWrapper().eq("userId", uid).eq("type", orderLogistics.getType()).eq("state", 9).eq("isDelete", 1)); merchantActivities = merchantActivityService.selectList(new EntityWrapper().eq("companyId", orderLogistics.getCompanyId()).eq("activityType", 1).eq("auditStatus", 2).eq("status", 1) .eq("state", 1).like("orderType", "%5%").where("((orderAmountFull is not null and orderAmountFull <= " + orderLogistics.getOrderMoney() + ") or (orderNumber is not null and orderNumber <= " + list.size() + "))") .in("id", merchantActivitySlaveService.selectList(new EntityWrapper().gt("laveNumber", 0)).stream().map(MerchantActivitySlave::getMerchantActivityId).collect(Collectors.toList())) .notIn("id", userMerchantCouponService.selectList(new EntityWrapper().eq("userType", 1).eq("userId", uid).eq("state", 1)).stream().map(UserMerchantCoupon::getMerchantActivityId).collect(Collectors.toList())) ); state = orderLogistics.getState(); } List list = new ArrayList<>(); if((state == 8 || state == 9) && merchantActivities.size() > 0){ for(MerchantActivity merchantActivity :merchantActivities){ List merchantActivitySlaves = merchantActivitySlaveService.selectList(new EntityWrapper().eq("merchantActivityId", merchantActivity.getId()).gt("laveNumber", 0)); for(MerchantActivitySlave merchantActivitySlave : merchantActivitySlaves){ UserMerchantCoupon userMerchantCoupon = new UserMerchantCoupon(); try { userMerchantCoupon.setCode(UUIDUtil.getRandomCode(16)); } catch (Exception e) { e.printStackTrace(); } userMerchantCoupon.setUserType(1); userMerchantCoupon.setUserId(uid); userMerchantCoupon.setMerchantActivityId(merchantActivity.getId()); userMerchantCoupon.setMerchantCouponId(merchantActivitySlave.getMerchantCouponId()); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + merchantActivity.getEffectiveDays()); userMerchantCoupon.setEndTime(calendar.getTime()); userMerchantCoupon.setStatus(1); userMerchantCoupon.setState(1); userMerchantCoupon.setCreateTime(new Date()); this.insert(userMerchantCoupon); MerchantCoupon merchantCoupon = merchantCouponService.selectById(merchantActivitySlave.getMerchantCouponId()); MerchantCouponListWarpper merchantCouponListWarpper = new MerchantCouponListWarpper(); merchantCouponListWarpper.setId(merchantActivitySlave.getMerchantCouponId()); merchantCouponListWarpper.setName(merchantCoupon.getName()); merchantCouponListWarpper.setType(merchantCoupon.getType()); merchantCouponListWarpper.setFullAmount(merchantCoupon.getFullAmount()); merchantCouponListWarpper.setDiscount(merchantCoupon.getDiscount()); list.add(merchantCouponListWarpper); merchantActivitySlave.setLaveNumber(merchantActivitySlave.getLaveNumber() - 1); if(merchantActivitySlave.getLaveNumber() == 0){ merchantActivity.setStatus(2); } } if(merchantActivitySlaves.size() > 0){ merchantActivitySlaveService.updateBatchById(merchantActivitySlaves); } } merchantActivityService.updateBatchById(merchantActivities); } return list; } @Override public synchronized List getMerchantCoupon(Integer uid) throws Exception { UserInfo userInfo = userInfoService.selectById(uid); String value = (String) redisTemplate.opsForValue().get("merchantVoucher"); List listWarppers = new ArrayList<>(); if(ToolUtil.isNotEmpty(value)){ JSONObject jsonObject = JSON.parseObject(value); JSONArray jsonArray = jsonObject.getJSONArray(userInfo.getPhone()); if(null != jsonArray){ listWarppers = jsonArray.toJavaList(MerchantCouponListWarpper.class); } jsonObject.put(userInfo.getPhone(), new JSONArray()); redisTemplate.opsForValue().set("merchantVoucher", jsonObject.toJSONString()); } return listWarppers; } }