|
|
package com.yoho.ufo.coupon.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.core.common.utils.DateUtil;
|
|
|
import com.yoho.service.model.order.utils.MD5Utils;
|
|
|
import com.yoho.ufo.coupon.service.ICouponService;
|
|
|
import com.yoho.ufo.dal.CouponMapper;
|
|
|
import com.yoho.ufo.dal.CouponProductLimitMapper;
|
|
|
import com.yoho.ufo.model.coupon.Coupon;
|
|
|
import com.yoho.ufo.model.coupon.CouponProductLimit;
|
|
|
import com.yoho.ufo.service.model.ApiResponse;
|
|
|
import com.yohobuy.ufo.coupon.req.CouponQueryReq;
|
|
|
import com.yohobuy.ufo.coupon.req.CouponSaveUpdateReq;
|
|
|
import com.yohobuy.ufo.coupon.resp.CouponQueryResp;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
* Created by shengguo.cai on 2018/11/20.
|
|
|
*/
|
|
|
@Service
|
|
|
public class CouponServiceImpl implements ICouponService{
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(CouponServiceImpl.class);
|
|
|
@Autowired
|
|
|
private CouponMapper couponMapper;
|
|
|
@Autowired
|
|
|
private CouponProductLimitMapper couponProductLimitMapper;
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse queryCoupons(CouponQueryReq req) {
|
|
|
LOGGER.info("enter queryCoupons,param is {}",req);
|
|
|
int total = couponMapper.selectTotalByCondition(req);
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
jsonObject.put("total",total);
|
|
|
if(total == 0){
|
|
|
return new ApiResponse.ApiResponseBuilder().data(jsonObject).build();
|
|
|
}
|
|
|
List<Coupon> coupons = couponMapper.selectByCondition(req);
|
|
|
List<CouponQueryResp> couponQueryResps = initCouponQueryResps(coupons);
|
|
|
jsonObject.put("coupons",couponQueryResps);
|
|
|
return new ApiResponse.ApiResponseBuilder().data(jsonObject).build();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse saveOrUpdateCoupon(CouponSaveUpdateReq req) {
|
|
|
LOGGER.info("enter saveOrUpdateCoupon,param is {}",req);
|
|
|
if(!checkSaveOrUpdateCouponParam(req)){
|
|
|
return new ApiResponse.ApiResponseBuilder().code(500).message("参数有误").build();
|
|
|
}
|
|
|
if(req.getId() != null){
|
|
|
LOGGER.info("before saveOrUpdateCoupon#deleteByCouponId,couponId is {}",req.getId());
|
|
|
couponProductLimitMapper.deleteByCouponId(req.getId());
|
|
|
}
|
|
|
if(null == req.getId()){
|
|
|
req.setCouponToken(MD5Utils.md5(UUID.randomUUID().toString()));
|
|
|
req.setCreateTime(DateUtil.getCurrentTimeSecond());
|
|
|
//新增时暂时写死的值
|
|
|
req.setProductLimitType(Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT);
|
|
|
req.setCouponType(100);
|
|
|
}
|
|
|
couponMapper.insertOrUpdate(req);
|
|
|
LOGGER.info("after saveOrUpdateCoupon#couponMapper.insertOrUpdate,req is {}",req);
|
|
|
String[] productIds = req.getProductLimitValue().split(",");
|
|
|
LOGGER.info("before saveOrUpdateCoupon#couponProductLimitMapper.insertBatchByProductIds,productIds is {}",productIds);
|
|
|
couponProductLimitMapper.insertBatchByProductIds(productIds,Coupon.PRODUCTLIMITTYPE_SPECIALPRODUCT,req.getId());
|
|
|
return new ApiResponse();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse getCouponInfo(Integer id) {
|
|
|
LOGGER.info("enter getCouponInfo, id is {}",id);
|
|
|
Coupon coupon = couponMapper.selectById(id);
|
|
|
List<CouponProductLimit> productLimitList = couponProductLimitMapper.selectByCouponId(id);
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
jsonObject.put("coupon",coupon);
|
|
|
jsonObject.put("productLimits",productLimitList);
|
|
|
return new ApiResponse.ApiResponseBuilder().data(jsonObject).build();
|
|
|
}
|
|
|
|
|
|
private boolean checkSaveOrUpdateCouponParam(CouponSaveUpdateReq req) {
|
|
|
if(req == null){
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! req is null");
|
|
|
return false;
|
|
|
}
|
|
|
if(StringUtils.isBlank(req.getCouponName())){
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! couponName is blank");
|
|
|
return false;
|
|
|
}
|
|
|
if(null == req.getCouponAmount() || req.getCouponAmount()<=0){
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! couponAmount error! couponAmount is {}",req.getCouponAmount());
|
|
|
return false;
|
|
|
}
|
|
|
if(null == req.getCouponNum() || req.getCouponNum()<=0){
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! couponNum error! couponNum is {}",req.getCouponNum());
|
|
|
return false;
|
|
|
}
|
|
|
if(null == req.getUseNum() || req.getUseNum()<=0){
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! useNum error! useNum is {}",req.getUseNum());
|
|
|
return false;
|
|
|
}
|
|
|
if(null == req.getStartTime() || req.getStartTime()<=0){
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! startTime error! startTime is {}",req.getStartTime());
|
|
|
return false;
|
|
|
}
|
|
|
if(null == req.getEndTime() || req.getEndTime()<=0){
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! endTime error! endTime is {}",req.getEndTime());
|
|
|
return false;
|
|
|
}
|
|
|
if(req.getEndTime()<req.getStartTime()){
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! endTime < startTime!endTime is {},startTime is {}"
|
|
|
,req.getEndTime(),req.getStartTime());
|
|
|
return false;
|
|
|
}
|
|
|
if(StringUtils.isBlank(req.getRemark())){
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! remark is blank.");
|
|
|
return false;
|
|
|
}
|
|
|
if(StringUtils.isBlank(req.getProductLimitValue())){
|
|
|
LOGGER.info("checkSaveOrUpdateCouponParam failed! productLimitValue is blank.");
|
|
|
return false;
|
|
|
}
|
|
|
return checkUpdateCouponParam(req);
|
|
|
}
|
|
|
|
|
|
private boolean checkUpdateCouponParam(CouponSaveUpdateReq req) {
|
|
|
if(req.getId() == null){
|
|
|
return true;
|
|
|
}
|
|
|
Coupon coupon = couponMapper.selectById(req.getId());
|
|
|
if(coupon == null){
|
|
|
LOGGER.info("checkUpdateCouponParam failed! coupon is null.id is {}",req.getId());
|
|
|
return false;
|
|
|
}
|
|
|
if(coupon.getStartTime()< DateUtil.currentTimeSeconds()){
|
|
|
LOGGER.info("checkUpdateCouponParam failed! coupon is using! startTime is {}",coupon.getStartTime());
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
private List<CouponQueryResp> initCouponQueryResps(List<Coupon> coupons) {
|
|
|
if(CollectionUtils.isEmpty(coupons)){
|
|
|
return null;
|
|
|
}
|
|
|
List<CouponQueryResp> resps = new ArrayList<>();
|
|
|
for(Coupon coupon : coupons){
|
|
|
CouponQueryResp resp = new CouponQueryResp(coupon.getId(),coupon.getCouponName(),coupon.getCouponAmount(),
|
|
|
coupon.getStartTime(),coupon.getEndTime(),coupon.getRemark(), Coupon.CouponStatusEnum.getKey(coupon.getStatus()));
|
|
|
resps.add(resp);
|
|
|
}
|
|
|
return resps;
|
|
|
}
|
|
|
} |
...
|
...
|
|