parent
24c953eb2c
commit
385b2227cf
@ -0,0 +1,76 @@ |
||||
package com.cweb.controller; |
||||
|
||||
import com.hai.common.exception.ErrorCode; |
||||
import com.hai.common.exception.ErrorHelp; |
||||
import com.hai.common.exception.SysCode; |
||||
import com.hai.common.security.AESEncodeUtil; |
||||
import com.hai.common.utils.ResponseMsgUtil; |
||||
import com.hai.model.ResponseData; |
||||
import com.hai.service.HighDiscountAgentRelService; |
||||
import com.hai.service.HighDiscountCouponRelService; |
||||
import com.hai.service.HighDiscountUserRelService; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.stereotype.Controller; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import javax.annotation.Resource; |
||||
|
||||
/** |
||||
* @Auther: 胡锐 |
||||
* @Description: |
||||
* @Date: 2021/4/4 14:46 |
||||
*/ |
||||
@Controller |
||||
@RequestMapping(value = "/discount") |
||||
@Api(value = "优惠券接口") |
||||
public class HighDiscountController { |
||||
|
||||
private static Logger log = LoggerFactory.getLogger(HighDiscountController.class); |
||||
|
||||
@Resource |
||||
private HighDiscountAgentRelService highDiscountAgentRelService; |
||||
|
||||
@Resource |
||||
private HighDiscountCouponRelService highDiscountCouponRelService; |
||||
|
||||
@Resource |
||||
private HighDiscountUserRelService highDiscountUserRelService; |
||||
|
||||
@RequestMapping(value="/getDiscountByQrCode",method = RequestMethod.GET) |
||||
@ResponseBody |
||||
@ApiOperation(value = "根据二维码Code查询") |
||||
public ResponseData getDiscountByQrCode(@RequestParam(name = "code", required = true) String code) { |
||||
try { |
||||
String relId; |
||||
try { |
||||
relId = AESEncodeUtil.aesDecrypt(code); |
||||
} catch (Exception e) { |
||||
log.error("HighDiscountController -> getDiscountByQrCode() error!","code解码错误"); |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "code解码错误"); |
||||
} |
||||
return ResponseMsgUtil.success(highDiscountAgentRelService.getRelById(Long.parseLong(relId))); |
||||
|
||||
} catch (Exception e) { |
||||
log.error("HighDiscountController -> getDiscountByQrCode() error!",e); |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
} |
||||
|
||||
@RequestMapping(value="/getCouponByDiscount",method = RequestMethod.GET) |
||||
@ResponseBody |
||||
@ApiOperation(value = "根据优惠券查询卡券") |
||||
public ResponseData getCouponByDiscount(@RequestParam(name = "discountId", required = true) Long discountId) { |
||||
try { |
||||
|
||||
return ResponseMsgUtil.success(highDiscountCouponRelService.getRelByDiscount(discountId)); |
||||
|
||||
} catch (Exception e) { |
||||
log.error("HighDiscountController -> getCouponByDiscount() error!",e); |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,97 @@ |
||||
package com.cweb.controller; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.github.pagehelper.PageHelper; |
||||
import com.github.pagehelper.PageInfo; |
||||
import com.hai.common.exception.ErrorCode; |
||||
import com.hai.common.exception.ErrorHelp; |
||||
import com.hai.common.exception.SysCode; |
||||
import com.hai.common.security.SessionObject; |
||||
import com.hai.common.security.UserCenter; |
||||
import com.hai.common.utils.ResponseMsgUtil; |
||||
import com.hai.model.HighUserModel; |
||||
import com.hai.model.ResponseData; |
||||
import com.hai.service.HighDiscountUserRelService; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Controller; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import javax.annotation.Resource; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @Auther: 胡锐 |
||||
* @Description: |
||||
* @Date: 2021/4/4 15:41 |
||||
*/ |
||||
@Controller |
||||
@RequestMapping(value = "/userDiscount") |
||||
@Api(value = "用户优惠券接口") |
||||
public class HighUserDiscountController { |
||||
|
||||
private static Logger log = LoggerFactory.getLogger(HighUserDiscountController.class); |
||||
|
||||
@Autowired |
||||
private UserCenter userCenter; |
||||
|
||||
@Resource |
||||
private HighDiscountUserRelService highDiscountUserRelService; |
||||
|
||||
@RequestMapping(value="/receiveDiscount",method = RequestMethod.POST) |
||||
@ResponseBody |
||||
@ApiOperation(value = "用户领取优惠券") |
||||
public ResponseData receiveDiscount(@RequestBody String reqBody, HttpServletRequest request) { |
||||
try { |
||||
|
||||
// 用户
|
||||
SessionObject sessionObject = userCenter.getSessionObject(request); |
||||
HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject(); |
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(reqBody); |
||||
Long discountAgentId = jsonObject.getLong("discountAgentId"); |
||||
if (discountAgentId == null) { |
||||
log.error("HighDiscountController -> receiveDiscount() error!", "参数错误"); |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); |
||||
} |
||||
// 领取成功
|
||||
highDiscountUserRelService.receiveDiscount(userInfoModel.getHighUser().getId(), discountAgentId); |
||||
return ResponseMsgUtil.success("操作成功"); |
||||
|
||||
} catch (Exception e) { |
||||
log.error("HighDiscountController -> receiveDiscount() error!",e); |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
} |
||||
|
||||
@RequestMapping(value="/getUserDiscountList",method = RequestMethod.POST) |
||||
@ResponseBody |
||||
@ApiOperation(value = "获取用户的优惠券列表") |
||||
public ResponseData getUserDiscountList(@RequestParam(name = "status", required = false) Integer status, |
||||
@RequestParam(name = "pageNum", required = true) Integer pageNum, |
||||
@RequestParam(name = "pageSize", required = true) Integer pageSize, |
||||
HttpServletRequest request) { |
||||
try { |
||||
|
||||
// 用户
|
||||
SessionObject sessionObject = userCenter.getSessionObject(request); |
||||
HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject(); |
||||
|
||||
Map<String,Object> map = new HashMap<>(); |
||||
map.put("userId", userInfoModel.getHighUser().getId()); |
||||
map.put("status", status); |
||||
|
||||
PageHelper.startPage(pageNum,pageSize); |
||||
return ResponseMsgUtil.success(new PageInfo<>(highDiscountUserRelService.getDiscountList(map))); |
||||
|
||||
} catch (Exception e) { |
||||
log.error("HighDiscountController -> receiveDiscount() error!",e); |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,34 @@ |
||||
package com.hai.model; |
||||
|
||||
import com.hai.entity.HighAgent; |
||||
import com.hai.entity.HighDiscount; |
||||
|
||||
/** |
||||
* @Auther: 胡锐 |
||||
* @Description: |
||||
* @Date: 2021/4/4 15:37 |
||||
*/ |
||||
public class HighDiscountUserRelModel { |
||||
|
||||
// 优惠券信息
|
||||
private HighDiscount highDiscount; |
||||
|
||||
// 代理商信息
|
||||
private HighAgent highAgent; |
||||
|
||||
public HighDiscount getHighDiscount() { |
||||
return highDiscount; |
||||
} |
||||
|
||||
public void setHighDiscount(HighDiscount highDiscount) { |
||||
this.highDiscount = highDiscount; |
||||
} |
||||
|
||||
public HighAgent getHighAgent() { |
||||
return highAgent; |
||||
} |
||||
|
||||
public void setHighAgent(HighAgent highAgent) { |
||||
this.highAgent = highAgent; |
||||
} |
||||
} |
@ -0,0 +1,37 @@ |
||||
package com.hai.service; |
||||
|
||||
import com.hai.entity.HighDiscountAgentRel; |
||||
import com.hai.entity.HighDiscountUserRel; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @Auther: 胡锐 |
||||
* @Description: |
||||
* @Date: 2021/4/4 15:10 |
||||
*/ |
||||
public interface HighDiscountUserRelService { |
||||
|
||||
/** |
||||
* @Author 胡锐 |
||||
* @Description 领取卡券 |
||||
* @Date 2021/4/4 15:12 |
||||
**/ |
||||
void receiveDiscount(Long userId,Long discountAgentId); |
||||
|
||||
/** |
||||
* @Author 胡锐 |
||||
* @Description |
||||
* @Date 2021/4/4 15:45 |
||||
**/ |
||||
HighDiscountUserRel getRelByUserDiscount(Long userId,Long discountId); |
||||
|
||||
/** |
||||
* @Author 胡锐 |
||||
* @Description 查询优惠券列表 |
||||
* @Date 2021/4/4 15:35 |
||||
**/ |
||||
List<HighDiscountUserRel> getDiscountList(Map<String, Object> map); |
||||
|
||||
} |
@ -0,0 +1,120 @@ |
||||
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.HighDiscountUserRelMapper; |
||||
import com.hai.entity.HighDiscount; |
||||
import com.hai.entity.HighDiscountAgentRel; |
||||
import com.hai.entity.HighDiscountUserRel; |
||||
import com.hai.entity.HighDiscountUserRelExample; |
||||
import com.hai.service.HighAgentService; |
||||
import com.hai.service.HighDiscountAgentRelService; |
||||
import com.hai.service.HighDiscountService; |
||||
import com.hai.service.HighDiscountUserRelService; |
||||
import org.apache.commons.collections4.MapUtils; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.transaction.annotation.Propagation; |
||||
import org.springframework.transaction.annotation.Transactional; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.Calendar; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @Auther: 胡锐 |
||||
* @Description: |
||||
* @Date: 2021/4/4 15:12 |
||||
*/ |
||||
@Service("highDiscountUserRelService") |
||||
public class HighDiscountUserRelServiceImpl implements HighDiscountUserRelService { |
||||
|
||||
@Resource |
||||
private HighDiscountUserRelMapper highDiscountUserRelMapper; |
||||
|
||||
@Resource |
||||
private HighDiscountAgentRelService highDiscountAgentRelService; |
||||
|
||||
@Resource |
||||
private HighAgentService highAgentService; |
||||
|
||||
@Resource |
||||
private HighDiscountService highDiscountService; |
||||
|
||||
@Override |
||||
@Transactional(propagation= Propagation.REQUIRES_NEW) |
||||
public void receiveDiscount(Long userId, Long discountAgentId) { |
||||
|
||||
// 查询优惠券信息
|
||||
HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(discountAgentId); |
||||
if (rel == null || rel.getHighDiscount() == null || rel.getAgentId() == null){ |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_DISCOUNT, ""); |
||||
} |
||||
// 校验卡卷状态
|
||||
if (rel.getHighDiscount().getStatus() != 2) { |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "卡券已下架"); |
||||
} |
||||
if (rel.getStockCount() <= 0) { |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.DISCOUNT_STOCK_COUNT_ERROR, ""); |
||||
} |
||||
|
||||
// 校验是否重复领取
|
||||
HighDiscountUserRelExample example = new HighDiscountUserRelExample(); |
||||
example.createCriteria().andUserIdEqualTo(userId).andDiscountIdEqualTo(rel.getDiscountId()).andStatusEqualTo(1); |
||||
if (highDiscountUserRelMapper.selectByExample(example).size() > 0) { |
||||
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "重复领取卡券,请使用后再进行领取"); |
||||
} |
||||
|
||||
HighDiscountUserRel userRel = new HighDiscountUserRel(); |
||||
userRel.setDiscountId(rel.getDiscountId()); |
||||
userRel.setAgentId(rel.getAgentId()); |
||||
userRel.setUserId(userId); |
||||
userRel.setStatus(1); // 状态 0:已过期 1:未使用 2:已使用
|
||||
userRel.setCreateTime(new Date()); |
||||
// 计算使用有效期
|
||||
Calendar userEndTime = Calendar.getInstance(); |
||||
userEndTime.add(Calendar.DATE, rel.getHighDiscount().getEffectiveDay()); |
||||
if (userEndTime.getTime().compareTo(rel.getHighDiscount().getSalesEndTime()) == 1) { |
||||
userRel.setUseEndTime(rel.getHighDiscount().getSalesEndTime()); |
||||
} else { |
||||
userRel.setUseEndTime(userEndTime.getTime()); |
||||
} |
||||
highDiscountUserRelMapper.insert(userRel); |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public HighDiscountUserRel getRelByUserDiscount(Long userId, Long discountId) { |
||||
HighDiscountUserRelExample example = new HighDiscountUserRelExample(); |
||||
example.createCriteria().andUserIdEqualTo(userId).andDiscountIdEqualTo(discountId); |
||||
List<HighDiscountUserRel> list = highDiscountUserRelMapper.selectByExample(example); |
||||
if (list.size() > 0) { |
||||
return list.get(0); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public List<HighDiscountUserRel> getDiscountList(Map<String, Object> map) { |
||||
HighDiscountUserRelExample example = new HighDiscountUserRelExample(); |
||||
HighDiscountUserRelExample.Criteria criteria = example.createCriteria(); |
||||
|
||||
if (MapUtils.getLong(map, "userId") != null) { |
||||
criteria.andUserIdEqualTo(MapUtils.getLong(map, "userId")); |
||||
} |
||||
|
||||
if (MapUtils.getInteger(map, "status") != null) { |
||||
criteria.andStatusEqualTo(MapUtils.getInteger(map, "status")); |
||||
} |
||||
|
||||
example.setOrderByClause("create_time desc"); |
||||
List<HighDiscountUserRel> list = highDiscountUserRelMapper.selectByExample(example); |
||||
for (HighDiscountUserRel rel : list) { |
||||
rel.setHighDiscount(highDiscountService.getDiscountById(rel.getDiscountId())); |
||||
rel.setHighAgent(highAgentService.findByAgentMsgId(rel.getAgentId())); |
||||
} |
||||
return list; |
||||
} |
||||
} |
Loading…
Reference in new issue