package com.stylefeng.guns.modular.system.controller.general;
|
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.stylefeng.guns.core.base.controller.BaseController;
|
import com.stylefeng.guns.core.base.tips.SuccessTip;
|
import com.stylefeng.guns.core.shiro.ShiroKit;
|
import com.stylefeng.guns.core.util.DateUtil;
|
import com.stylefeng.guns.modular.system.controller.req.CouponSendReq;
|
import com.stylefeng.guns.modular.system.enums.CouponStatusEnum;
|
import com.stylefeng.guns.modular.system.enums.StatusEnum;
|
import com.stylefeng.guns.modular.system.model.TAgent;
|
import com.stylefeng.guns.modular.system.model.TUserToCoupon;
|
import com.stylefeng.guns.modular.system.service.ITUserToCouponService;
|
import io.swagger.annotations.ApiOperation;
|
import org.apache.poi.hdf.extractor.TC;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.util.Assert;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.StringUtils;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.ui.Model;
|
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import com.stylefeng.guns.core.log.LogObjectHolder;
|
import org.springframework.web.bind.annotation.RequestParam;
|
import com.stylefeng.guns.modular.system.model.TCoupon;
|
import com.stylefeng.guns.modular.system.service.ITCouponService;
|
|
import java.time.LocalDate;
|
import java.time.ZoneId;
|
import java.util.*;
|
|
/**
|
* 控制器
|
*
|
* @author fengshuonan
|
* @Date 2023-02-14 10:59:23
|
*/
|
@Controller
|
@RequestMapping("/tCoupon")
|
public class TCouponController extends BaseController {
|
|
private String PREFIX = "/system/tCoupon/";
|
|
@Autowired
|
private ITCouponService tCouponService;
|
@Autowired
|
private ITUserToCouponService tUserToCouponService;
|
|
/**
|
* 跳转到首页
|
*/
|
@RequestMapping("")
|
public String index() {
|
return PREFIX + "tCoupon.html";
|
}
|
|
/**
|
* 跳转到添加
|
*/
|
@RequestMapping("/tCoupon_add")
|
public String tCouponAdd() {
|
return PREFIX + "tCoupon_add.html";
|
}
|
|
/**
|
* 跳转到修改
|
*/
|
@RequestMapping("/tCoupon_update/{tCouponId}")
|
public String tCouponUpdate(@PathVariable Integer tCouponId, Model model) {
|
TCoupon tCoupon = tCouponService.selectById(tCouponId);
|
model.addAttribute("item",tCoupon);
|
LogObjectHolder.me().set(tCoupon);
|
return PREFIX + "tCoupon_edit.html";
|
}
|
|
/**
|
* 获取列表
|
*/
|
@RequestMapping(value = "/list")
|
@ResponseBody
|
public Object list(Integer couponType,Integer couponServiceType,String createTime) {
|
EntityWrapper<TCoupon> wrapper = new EntityWrapper<>();
|
if(Objects.nonNull(couponType)){
|
wrapper.eq("coupon_type",couponType);
|
}
|
if(Objects.nonNull(couponServiceType)){
|
wrapper.eq("coupon_service_type",couponServiceType);
|
}
|
// 开始,结束时间
|
if(StringUtils.hasLength(createTime)){
|
String[] split = createTime.split(" - ");
|
Date startTime = DateUtil.getDate_str3(split[0]+" 00:00:00");
|
Date endTime = DateUtil.getDate_str3(split[1]+" 23:59:59");
|
wrapper.between("create_time",startTime,endTime);
|
}
|
wrapper.ne("status", StatusEnum.DELETE.getCode());
|
wrapper.orderBy(true,"create_time",false);
|
return tCouponService.selectList(wrapper);
|
}
|
|
/**
|
* 获取活动券列表
|
*/
|
@RequestMapping(value = "/activityCouponList")
|
@ResponseBody
|
public Object activityCouponList(String couponName) {
|
EntityWrapper<TCoupon> wrapper = new EntityWrapper<>();
|
if(StringUtils.hasLength(couponName)){
|
wrapper.like("coupon_name",couponName);
|
}
|
wrapper.eq("coupon_type",1);
|
wrapper.eq("coupon_state",1);
|
wrapper.orderBy(true,"create_time",false);
|
return tCouponService.selectList(wrapper);
|
}
|
|
/**
|
* 获取列表
|
*/
|
@RequestMapping(value = "/list-back")
|
@ResponseBody
|
public Object listBack(String condition) {
|
return tCouponService.selectList(null);
|
}
|
|
/**
|
* 新增
|
*/
|
@RequestMapping(value = "/add")
|
@ResponseBody
|
public Object add(TCoupon tCoupon) {
|
// 校验是否添加的是同类型同名称的优惠券
|
Boolean isExit = tCouponService.isExit(tCoupon);
|
if(isExit){
|
return new SuccessTip(500,"该优惠券名称与类型已存在!");
|
}
|
// 如果是新人优惠券,查询是否存在已启用的新人优惠券
|
if(tCoupon.getCouponType() == 2){
|
int count = tCouponService.selectCount(new EntityWrapper<TCoupon>()
|
.eq("coupon_type", 2)
|
.eq("coupon_state", 1)
|
.eq("status", true));
|
if(count>0){
|
return new SuccessTip(500,"已存在已启用的新人优惠券!");
|
}
|
}
|
tCouponService.insert(tCoupon);
|
return SUCCESS_TIP;
|
}
|
|
/**
|
* 删除
|
*/
|
@RequestMapping(value = "/delete")
|
@ResponseBody
|
public Object delete(@RequestParam Integer tCouponId) {
|
/*TCoupon tCoupon = tCouponService.selectById(tCouponId);
|
tCoupon.setCouponStatus(StatusEnum.DELETE.getCode());
|
tCouponService.updateById(tCoupon);*/
|
tCouponService.deleteById(tCouponId);
|
return SUCCESS_TIP;
|
}
|
|
/**
|
* 修改优惠券状态
|
*/
|
@RequestMapping(value = "/update-status")
|
@ResponseBody
|
public Object updateStatus(Integer id,Integer status) {
|
TCoupon tCoupon = tCouponService.selectById(id);
|
|
// 查询已启用的新人券数量
|
int count = tCouponService.selectCount(new EntityWrapper<TCoupon>().eq("coupon_state", 1)
|
.eq("coupon_type",2));
|
|
// 判断是否为新人券,新人券只可启用一条记录
|
if(count > 0 && 2 == tCoupon.getCouponType() && 2 == status){
|
return new SuccessTip(500,"已有启动的新人券!");
|
}
|
|
if(1 == status){
|
tCoupon.setCouponState(2);
|
}
|
if(2 == status){
|
tCoupon.setCouponState(1);
|
}
|
tCouponService.updateById(tCoupon);
|
return SUCCESS_TIP;
|
}
|
|
/**
|
* 修改
|
*/
|
@RequestMapping(value = "/update")
|
@ResponseBody
|
public Object update(TCoupon tCoupon) {
|
tCouponService.updateById(tCoupon);
|
return SUCCESS_TIP;
|
}
|
|
/**
|
* 详情
|
*/
|
@RequestMapping(value = "/detail/{tCouponId}")
|
@ResponseBody
|
public Object detail(@PathVariable("tCouponId") Integer tCouponId) {
|
return tCouponService.selectById(tCouponId);
|
}
|
|
/**
|
* 发放优惠券
|
*/
|
@ApiOperation(value = "发放优惠券")
|
@RequestMapping(value = "/sendCouponToUser")
|
@ResponseBody
|
public Object sendCouponToUser(CouponSendReq couponSendReq) {
|
|
List<Integer> userIds = couponSendReq.getUserIds();
|
|
// 查询选择的优惠券
|
TCoupon tCoupon = tCouponService.selectById(couponSendReq.getCouponId());
|
|
List<TUserToCoupon> tUserToCoupons = new ArrayList<>(userIds.size());
|
|
for (Integer userId : userIds) {
|
// 创建用户优惠券关联表
|
TUserToCoupon tUserToCoupon = new TUserToCoupon();
|
tUserToCoupon.setCouponId(tCoupon.getId());
|
tUserToCoupon.setUserId(userId);
|
tUserToCoupon.setCouponTotal(1);
|
tUserToCoupon.setValidCount(1);
|
Date expireTime = Date.from(LocalDate.now().plusDays(tCoupon.getCouponValidity()).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
|
tUserToCoupon.setExpireTime(expireTime);
|
|
// 添加发放人id和类型
|
if(ShiroKit.getUser().getRoleType() == 1){
|
tUserToCoupon.setObjectId(1);
|
}else {
|
tUserToCoupon.setObjectId(ShiroKit.getUser().getObjectId());
|
}
|
tUserToCoupon.setRoleType(ShiroKit.getUser().getRoleType());
|
|
tUserToCoupons.add(tUserToCoupon);
|
}
|
tUserToCouponService.insertBatch(tUserToCoupons);
|
|
// 查询该优惠券的列表
|
/*List<TCoupon> tCoupons = tCouponService.selectList(new EntityWrapper<TCoupon>().eq("coupon_name", couponSendReq.getCouponName())
|
.eq("coupon_status",CouponStatusEnum.UNISSUED.getCode()));
|
Assert.isTrue(!CollectionUtils.isEmpty(tCoupons),"该优惠券不存在!");
|
List<TCoupon> tCouponList = new ArrayList<>();
|
// 判断是选中的人多还是优惠券的数量较多
|
if(tCoupons.size() > userIds.size()){
|
for (Integer userId : userIds) {
|
Iterator<TCoupon> iterator = tCoupons.iterator();
|
while (iterator.hasNext()){
|
TCoupon next = iterator.next();
|
next.setCouponStatus(CouponStatusEnum.NOT_USED.getCode());
|
next.setUserId(userId);
|
tCouponList.add(next);
|
iterator.remove();
|
break;
|
}
|
}
|
}else {
|
for (TCoupon tCoupon : tCoupons) {
|
Iterator<Integer> iterator = userIds.iterator();
|
while (iterator.hasNext()){
|
tCoupon.setCouponStatus(CouponStatusEnum.NOT_USED.getCode());
|
tCoupon.setUserId(iterator.next());
|
tCouponList.add(tCoupon);
|
iterator.remove();
|
break;
|
}
|
}
|
}
|
if(!CollectionUtils.isEmpty(tCouponList)){
|
tCouponService.updateBatchById(tCouponList);
|
}*/
|
return SUCCESS_TIP;
|
}
|
|
}
|