|
@@ -0,0 +1,165 @@
|
|
|
|
+/**
|
|
|
|
+* @版权信息 (@copyright Copyright 2017-XXXX JDJR.COM All Right Reserved);
|
|
|
|
+* @see
|
|
|
|
+* @author 于海涛 京东金融【技术研发部-证券及营销平台研发部-营销平台研发部】
|
|
|
|
+* @version 1.0
|
|
|
|
+* @date 2018年7月17日
|
|
|
|
+*/
|
|
|
|
+
|
|
|
|
+package com.zskk.shop.service;
|
|
|
|
+
|
|
|
|
+import java.text.DecimalFormat;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.UUID;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
+
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+
|
|
|
|
+import com.zskk.shop.controller.bean.UserBean;
|
|
|
|
+import com.zskk.shop.dao.ExchangeCodeMapper;
|
|
|
|
+import com.zskk.shop.dao.entry.ExchangeCode;
|
|
|
|
+import com.zskk.shop.dao.entry.ExchangeCodeItem;
|
|
|
|
+import com.zskk.shop.exception.ErrorConstant;
|
|
|
|
+import com.zskk.shop.exception.ZSKKException;
|
|
|
|
+import com.zskk.shop.utils.ToolsUtil;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+public class ExchangeCodeService {
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 兑换码状态 启用
|
|
|
|
+ */
|
|
|
|
+ public static final Integer EXCHANGE_CODE_STATUS_OK = 1;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 兑换码状态 禁用
|
|
|
|
+ */
|
|
|
|
+ public static final Integer EXCHANGE_CODE_STATUS_NO = 0;
|
|
|
|
+
|
|
|
|
+ private static final Integer PAGE_SIZE = 100;
|
|
|
|
+
|
|
|
|
+ private static final Long INCREMENT_INIT_VALUE = 1L;
|
|
|
|
+
|
|
|
|
+ private static final DecimalFormat DAY_TIME_FORMAT = new DecimalFormat("000");
|
|
|
|
+ private static final DecimalFormat ITEM_CODE_FORMAT = new DecimalFormat("0000");
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ExchangeCodeMapper exchangeCodeMapper;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisTemplate<String, String> redisTemplate;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取兑换码批次列表
|
|
|
|
+ * @param search
|
|
|
|
+ * @param status
|
|
|
|
+ * @param gid
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<ExchangeCode> queryAllExchangeCode(String search, Integer status, Integer gid){
|
|
|
|
+ return exchangeCodeMapper.queryAllExchangeCode(search, status, gid);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查询一个兑换码批次
|
|
|
|
+ * @param id
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public ExchangeCode queryOneExchangeCode(Integer id){
|
|
|
|
+ return exchangeCodeMapper.queryOneExchangeCode(id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建兑换码批次
|
|
|
|
+ * @param user 创建用户
|
|
|
|
+ * @param gid 商品ID
|
|
|
|
+ * @param num 兑换码数量
|
|
|
|
+ * @param remark 备注
|
|
|
|
+ * @param endtime 兑换码有效截止日期
|
|
|
|
+ */
|
|
|
|
+ @Transactional
|
|
|
|
+ public void createExchangeCode(UserBean user, Integer gid, Integer num, String remark, Integer endtime){
|
|
|
|
+ Integer now = ToolsUtil.getNow();
|
|
|
|
+
|
|
|
|
+ ExchangeCode exchangeCode = new ExchangeCode();
|
|
|
|
+ exchangeCode.setCuid(user.getUserid());
|
|
|
|
+ exchangeCode.setCuname(user.getName());
|
|
|
|
+ exchangeCode.setGid(gid);
|
|
|
|
+ exchangeCode.setNum(num);
|
|
|
|
+ exchangeCode.setStatus(EXCHANGE_CODE_STATUS_OK);
|
|
|
|
+ exchangeCode.setRemark(remark);
|
|
|
|
+ exchangeCode.setEndtime(endtime);
|
|
|
|
+ exchangeCode.setCtime(now);
|
|
|
|
+ exchangeCode.setUtime(now);
|
|
|
|
+ exchangeCodeMapper.insertExchangeCode(exchangeCode);
|
|
|
|
+ Integer eid = exchangeCode.getId();
|
|
|
|
+
|
|
|
|
+ String timeKey = ToolsUtil.formatTime(new Date(), ToolsUtil.getYYMMFormat());
|
|
|
|
+ String dayTimesKey = this.getTimesKey(timeKey);
|
|
|
|
+ Long dayTimesValue = redisTemplate.opsForValue().increment(dayTimesKey, INCREMENT_INIT_VALUE);
|
|
|
|
+ if (dayTimesValue.equals(INCREMENT_INIT_VALUE)){
|
|
|
|
+ redisTemplate.expire(dayTimesKey, 24 *3600, TimeUnit.SECONDS);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<ExchangeCodeItem> items = new ArrayList<>(Math.min(num, PAGE_SIZE));
|
|
|
|
+ for (int i = 1; i <= num; ++i){
|
|
|
|
+ ExchangeCodeItem item = new ExchangeCodeItem();
|
|
|
|
+ item.setEicode(this.getItemCode(timeKey, dayTimesValue, i));
|
|
|
|
+ item.setEid(eid);
|
|
|
|
+ item.setEipwd(this.randomPwd().toUpperCase());
|
|
|
|
+ item.setExtend("");
|
|
|
|
+ item.setStatus(0);
|
|
|
|
+ item.setUtime(0);
|
|
|
|
+ items.add(item);
|
|
|
|
+
|
|
|
|
+ if (items.size() == PAGE_SIZE){
|
|
|
|
+ exchangeCodeMapper.insertExchangeCodeItems(items);
|
|
|
|
+ items.clear();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (!items.isEmpty()){
|
|
|
|
+ exchangeCodeMapper.insertExchangeCodeItems(items);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ public Boolean update(ExchangeCode data, String remark, Integer status, Integer endtime){
|
|
|
|
+ data.setRemark(remark);
|
|
|
|
+ data.setStatus(status);
|
|
|
|
+ data.setEndtime(endtime);
|
|
|
|
+ if (exchangeCodeMapper.updateExchangeCode(data) != 1){
|
|
|
|
+ throw new ZSKKException(ErrorConstant.SERVER_ERROR);
|
|
|
|
+ }
|
|
|
|
+ return Boolean.TRUE;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public List<ExchangeCodeItem> getItems(Integer eid, Integer status){
|
|
|
|
+ return exchangeCodeMapper.queryAllExchangeCodeItems(eid, status);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getTimesKey(String timeKey){
|
|
|
|
+ return "SHOP_EXCHANGE_CODE_DAY_TIMES" + timeKey;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public String getItemCode(String timeKey, Long dayTimes, Integer i){
|
|
|
|
+ if (i < 10000){
|
|
|
|
+ return timeKey + DAY_TIME_FORMAT.format(dayTimes) + ITEM_CODE_FORMAT.format(i);
|
|
|
|
+ }else{
|
|
|
|
+ return timeKey + DAY_TIME_FORMAT.format(dayTimes) + i;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String randomPwd(){
|
|
|
|
+ return UUID.randomUUID().toString().substring(0, 6);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|