package com.slodon.b2b2c.controller.promotion.seller;

import cn.hutool.core.date.DateUtil;
import com.slodon.b2b2c.aop.VendorLogger;
import com.slodon.b2b2c.core.constant.CouponConst;
import com.slodon.b2b2c.core.constant.RedisConst;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.*;
import com.slodon.b2b2c.goods.pojo.Goods;
import com.slodon.b2b2c.model.goods.GoodsModel;
import com.slodon.b2b2c.model.promotion.CouponGoodsModel;
import com.slodon.b2b2c.model.promotion.CouponMemberModel;
import com.slodon.b2b2c.model.promotion.CouponModel;
import com.slodon.b2b2c.promotion.dto.CouponAddDTO;
import com.slodon.b2b2c.promotion.dto.CouponUpdateDTO;
import com.slodon.b2b2c.promotion.example.CouponExample;
import com.slodon.b2b2c.promotion.example.CouponGoodsExample;
import com.slodon.b2b2c.promotion.example.CouponMemberExample;
import com.slodon.b2b2c.promotion.pojo.Coupon;
import com.slodon.b2b2c.promotion.pojo.CouponGoods;
import com.slodon.b2b2c.promotion.pojo.CouponMember;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.vo.promotion.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(tags = "seller-店铺优惠券")
@RestController
@Slf4j
@RequestMapping("v3/promotion/seller/coupon")
public class SellerCouponController extends BaseController {

    @Resource
    private CouponModel couponModel;
    @Resource
    private CouponGoodsModel couponGoodsModel;
    @Resource
    private CouponMemberModel couponMemberModel;
    @Resource
    private GoodsModel goodsModel;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate<String, Object> objectRedisTemplate;

    @ApiOperation("优惠券列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponName", value = "优惠券名称", paramType = "query"),
            @ApiImplicitParam(name = "couponType", value = "优惠券类型(1-固定金额券；2-折扣券（折扣比例）；3-随机金额券）", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "优惠券状态(1-未开始;2-进行中;3-已失效;4-已结束)", paramType = "query"),
            @ApiImplicitParam(name = "publishType", value = "获取方式(1-免费领取；3-活动赠送）", paramType = "query"),
            @ApiImplicitParam(name = "publishStartTime", value = "活动开始时间", paramType = "query"),
            @ApiImplicitParam(name = "publishEndTime", value = "活动结束时间", paramType = "query"),
            @ApiImplicitParam(name = "effectiveStart", value = "使用开始时间", paramType = "query"),
            @ApiImplicitParam(name = "effectiveEnd", value = "使用结束时间", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<CouponVO>> list(HttpServletRequest request, String couponName, Integer couponType,
                                             Integer state, Integer publishType, Date publishStartTime,
                                             Date publishEndTime, Date effectiveStart, Date effectiveEnd) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        CouponExample example = new CouponExample();
        example.setStoreId(vendor.getStoreId());
        example.setCouponNameLike(couponName);
        example.setCouponType(couponType);
        example.setPublishType(publishType);
        example.setPublishStartTimeBefore(publishEndTime);
        example.setPublishEndTimeAfter(publishStartTime);
        example.setEffectiveStartBefore(effectiveEnd);
        example.setEffectiveEndAfter(effectiveStart);
        if (!StringUtil.isNullOrZero(state)) {
            if (state == CouponConst.STATE_1) {
                example.setState(CouponConst.STATE_1);
                example.setPublishStartTimeAfter(new Date());
            } else if (state == CouponConst.STATE_2) {
                example.setState(CouponConst.STATE_1);
                example.setPublishStartTimeBefore(new Date());
                example.setPublishEndTimeAfter(new Date());
            } else if (state == CouponConst.STATE_3) {
                example.setState(CouponConst.STATE_2);
            } else if (state == CouponConst.STATE_4) {
                example.setState(CouponConst.STATE_1);
                example.setPublishEndTimeBefore(new Date());
            }
        }
        example.setStateNotEquals(CouponConst.ACTIVITY_STATE_3);
        List<Coupon> list = couponModel.getCouponList(example, pager);
        List<CouponVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(coupon -> {
                vos.add(new CouponVO(coupon));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("优惠券详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "优惠券id", required = true)
    })
    @GetMapping("detail")
    public JsonResult<CouponDetailVO> detail(HttpServletRequest request, Integer couponId) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        AssertUtil.notNullOrZero(couponId, "优惠券id不能为空");
        Coupon coupon = couponModel.getCouponByCouponId(couponId);
        AssertUtil.notNull(coupon, "获取优惠券信息为空，请重试！");
        AssertUtil.isTrue(!coupon.getStoreId().equals(vendor.getStoreId()), "无权限");

        return SldResponse.success(new CouponDetailVO(coupon));
    }

    @ApiOperation("优惠券指定商品列表")
    @GetMapping("goodsList")
    public JsonResult<PageVO<CouponGoodsVO>> goodsList(HttpServletRequest request, Integer couponId) {
        AssertUtil.notNullOrZero(couponId, "优惠券id不能为空");
        CouponGoodsExample example = new CouponGoodsExample();
        example.setCouponId(couponId);
        List<CouponGoods> list = couponGoodsModel.getCouponGoodsList(example, null);
        List<CouponGoodsVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (CouponGoods couponGoods : list) {
                Goods goods = goodsModel.getGoodsByGoodsId(couponGoods.getGoodsId());
                AssertUtil.notNull(goods, "获取商品信息为空，请重试！");

                vos.add(new CouponGoodsVO(goods));
            }
        }
        return SldResponse.success(new PageVO<>(vos, null));
    }

    @ApiOperation("新建优惠券")
    @VendorLogger(option = "新建优惠券")
    @PostMapping("add")
    public JsonResult addCoupon(HttpServletRequest request, CouponAddDTO couponAddDTO) {
        AssertUtil.isTrue(!"1".equals(stringRedisTemplate.opsForValue().get("coupon_is_enable")), "优惠券活动未开启");
        if (couponAddDTO.getPublishType().equals(CouponConst.PUBLISH_TYPE_3)) {
            //活动赠送，默认领取时间，开始时间\结束时间为当前时间
            couponAddDTO.setPublishStartTime(new Date());
            couponAddDTO.setPublishEndTime(TimeUtil.getYearAgoDate(new Date(), 100));
        }
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        Coupon coupon = new Coupon();
        AssertUtil.isTrue(couponAddDTO.getLimitReceive() > couponAddDTO.getPublishNum(), "每人限领次数不能超过发放总数");
        AssertUtil.isTrue(couponAddDTO.getPublishStartTime().before(TimeUtil.getDayStartFormatYMDHMS(new Date())), "活动开始时间应在当前日期之后，请重新输入");
        BeanUtils.copyProperties(couponAddDTO, coupon);
        //拼接优惠内容
        if (couponAddDTO.getLimitQuota().equals(BigDecimal.ZERO)) {
            if (couponAddDTO.getCouponType() == CouponConst.COUPON_TYPE_1) {
                coupon.setCouponContent("减" + couponAddDTO.getPublishValue() + "元");
            } else if (couponAddDTO.getCouponType() == CouponConst.COUPON_TYPE_2) {
                AssertUtil.isTrue(StringUtil.isNullOrZero(couponAddDTO.getDiscountLimitAmount()), "请填写折扣最多优惠金额且必须大于0");
                coupon.setCouponContent("打" + couponAddDTO.getPublishValue().divide(new BigDecimal(10)) + "折");
            } else if (couponAddDTO.getCouponType() == CouponConst.COUPON_TYPE_3) {
                AssertUtil.isTrue(couponAddDTO.getRandomMin().compareTo(couponAddDTO.getRandomMax()) > 0, "随机金额券最小值不大于最大值，请重新输入");
                AssertUtil.isTrue(couponAddDTO.getRandomMin().multiply(new BigDecimal(couponAddDTO.getPublishNum())).compareTo(couponAddDTO.getPublishAmount()) > 0
                                || couponAddDTO.getPublishAmount().compareTo(couponAddDTO.getRandomMax().multiply(new BigDecimal(couponAddDTO.getPublishNum()))) > 0,
                        "优惠总金额必须在" + couponAddDTO.getRandomMin().multiply(new BigDecimal(couponAddDTO.getPublishNum())) + "~" + couponAddDTO.getRandomMax().multiply(new BigDecimal(couponAddDTO.getPublishNum())) + "元之间，请重新输入");
                coupon.setCouponContent("随机减");
            }
        } else {
            if (couponAddDTO.getCouponType() == CouponConst.COUPON_TYPE_1) {
                AssertUtil.isTrue(couponAddDTO.getLimitQuota().compareTo(couponAddDTO.getPublishValue()) <= 0, "当满减金额有门槛时,满额必须大于减额");
                coupon.setCouponContent("满" + couponAddDTO.getLimitQuota() + "元减" + couponAddDTO.getPublishValue() + "元");
            } else if (couponAddDTO.getCouponType() == CouponConst.COUPON_TYPE_2) {
                AssertUtil.isTrue(StringUtil.isNullOrZero(couponAddDTO.getDiscountLimitAmount()), "请填写折扣最多优惠金额且必须大于0");
                coupon.setCouponContent("满" + couponAddDTO.getLimitQuota() + "元打" + couponAddDTO.getPublishValue().divide(new BigDecimal(10)) + "折");
            } else if (couponAddDTO.getCouponType() == CouponConst.COUPON_TYPE_3) {
                AssertUtil.isTrue(couponAddDTO.getRandomMin().compareTo(couponAddDTO.getRandomMax()) > 0, "随机金额券最小值不大于最大值，请重新输入");
                AssertUtil.isTrue(couponAddDTO.getRandomMin().multiply(new BigDecimal(couponAddDTO.getPublishNum())).compareTo(couponAddDTO.getPublishAmount()) > 0
                                || couponAddDTO.getPublishAmount().compareTo(couponAddDTO.getRandomMax().multiply(new BigDecimal(couponAddDTO.getPublishNum()))) > 0,
                        "优惠总金额必须在" + couponAddDTO.getRandomMin().multiply(new BigDecimal(couponAddDTO.getPublishNum())) + "~" + couponAddDTO.getRandomMax().multiply(new BigDecimal(couponAddDTO.getPublishNum())) + "元之间，请重新输入");
                coupon.setCouponContent("满" + couponAddDTO.getLimitQuota() + "元随机减");
            }
        }
        //如果cycle为空，说明是固定起始时间；否则就是领券当日起cycle天内可用
        if (StringUtil.isNullOrZero(couponAddDTO.getCycle())) {
            if (couponAddDTO.getPublishType().equals(CouponConst.PUBLISH_TYPE_3)) {
                //活动赠送类型，校验提示语不同
                AssertUtil.isTrue(couponAddDTO.getEffectiveStart().before(DateUtil.beginOfDay(new Date())), "使用开始时间应在当前时间之后，请重新输入");
                AssertUtil.isTrue(couponAddDTO.getEffectiveEnd().before(new Date()), "使用结束时间应在当前时间之后，请重新输入");
            } else {
                AssertUtil.isTrue(couponAddDTO.getEffectiveStart().before(couponAddDTO.getPublishStartTime()), "使用开始时间应在活动开始时间之后，请重新输入");
                AssertUtil.isTrue(couponAddDTO.getEffectiveEnd().before(couponAddDTO.getPublishEndTime()), "使用结束时间应在活动结束时间之后，请重新输入");
            }
            coupon.setEffectiveTimeType(CouponConst.EFFECTIVE_TIME_TYPE_1);
        } else {
            coupon.setEffectiveTimeType(CouponConst.EFFECTIVE_TIME_TYPE_2);
        }
        coupon.setStoreId(vendor.getStoreId());
        coupon.setStoreName(vendor.getStore().getStoreName());
        coupon.setCreateUserId(vendor.getVendorId());
        coupon.setCreateUserName(vendor.getVendorName());
        Integer couponId = couponModel.saveCoupon(coupon, couponAddDTO.getGoodsIds(), null);

        //随机金额券随机数存入redis，领取的时候直接取
        if (couponAddDTO.getCouponType() == CouponConst.COUPON_TYPE_3) {
            List<BigDecimal> list = RedBagUtils.genRedPacket(couponAddDTO.getRandomMin(), couponAddDTO.getRandomMax(),
                    couponAddDTO.getPublishAmount(), couponAddDTO.getPublishNum());
            for (int i = 0, listSize = list.size(); i < listSize; i++) {
                BigDecimal amount = list.get(i);
                stringRedisTemplate.opsForHash().put(RedisConst.COUPON_PREFIX + couponId, i + "", amount.toString());
            }
            long remainedTime = couponAddDTO.getPublishEndTime().getTime() - System.currentTimeMillis();
            objectRedisTemplate.expire(RedisConst.COUPON_PREFIX + couponId, remainedTime, TimeUnit.MILLISECONDS);
        }
        return SldResponse.success("添加成功");
    }

    @ApiOperation("编辑优惠券")
    @VendorLogger(option = "编辑优惠券")
    @PostMapping("update")
    public JsonResult updateCoupon(HttpServletRequest request, CouponUpdateDTO couponUpdateDTO) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        AssertUtil.isTrue(couponUpdateDTO.getLimitReceive() > couponUpdateDTO.getPublishNum(), "每人限领次数不能超过发放总数");
        AssertUtil.isTrue(couponUpdateDTO.getPublishStartTime().before(TimeUtil.getDayStartFormatYMDHMS(new Date())), "活动开始时间应在当前日期之后，请重新输入");

        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(couponUpdateDTO, coupon);
        //拼接优惠内容
        if (couponUpdateDTO.getLimitQuota().equals(BigDecimal.ZERO)) {
            if (couponUpdateDTO.getCouponType() == CouponConst.COUPON_TYPE_1) {
                coupon.setCouponContent("减" + couponUpdateDTO.getPublishValue() + "元");
            } else if (couponUpdateDTO.getCouponType() == CouponConst.COUPON_TYPE_2) {
                coupon.setCouponContent("打" + couponUpdateDTO.getPublishValue().divide(new BigDecimal(10)) + "折");
            } else if (couponUpdateDTO.getCouponType() == CouponConst.COUPON_TYPE_3) {
                AssertUtil.isTrue(couponUpdateDTO.getRandomMin().compareTo(couponUpdateDTO.getRandomMax()) > 0, "随机金额券最小值不大于最大值，请重新输入");
                AssertUtil.isTrue(couponUpdateDTO.getRandomMin().multiply(new BigDecimal(couponUpdateDTO.getPublishNum())).compareTo(couponUpdateDTO.getPublishAmount()) > 0
                                || couponUpdateDTO.getPublishAmount().compareTo(couponUpdateDTO.getRandomMax().multiply(new BigDecimal(couponUpdateDTO.getPublishNum()))) > 0,
                        "优惠总金额必须在" + couponUpdateDTO.getRandomMin().multiply(new BigDecimal(couponUpdateDTO.getPublishNum())) + "~" + couponUpdateDTO.getRandomMax().multiply(new BigDecimal(couponUpdateDTO.getPublishNum())) + "元之间，请重新输入");
                coupon.setCouponContent("随机减");
            }
        } else {
            if (couponUpdateDTO.getCouponType() == CouponConst.COUPON_TYPE_1) {
                AssertUtil.isTrue(couponUpdateDTO.getLimitQuota().compareTo(couponUpdateDTO.getPublishValue()) <= 0, "当满减金额有门槛时,满额必须大于减额");
                coupon.setCouponContent("满" + couponUpdateDTO.getLimitQuota() + "元减" + couponUpdateDTO.getPublishValue() + "元");
            } else if (couponUpdateDTO.getCouponType() == CouponConst.COUPON_TYPE_2) {
                coupon.setCouponContent("满" + couponUpdateDTO.getLimitQuota() + "元打" + couponUpdateDTO.getPublishValue().divide(new BigDecimal(10)) + "折");
            } else if (couponUpdateDTO.getCouponType() == CouponConst.COUPON_TYPE_3) {
                AssertUtil.isTrue(couponUpdateDTO.getRandomMin().compareTo(couponUpdateDTO.getRandomMax()) > 0, "随机金额券最小值不大于最大值，请重新输入");
                AssertUtil.isTrue(couponUpdateDTO.getRandomMin().multiply(new BigDecimal(couponUpdateDTO.getPublishNum())).compareTo(couponUpdateDTO.getPublishAmount()) > 0
                                || couponUpdateDTO.getPublishAmount().compareTo(couponUpdateDTO.getRandomMax().multiply(new BigDecimal(couponUpdateDTO.getPublishNum()))) > 0,
                        "优惠总金额必须在" + couponUpdateDTO.getRandomMin().multiply(new BigDecimal(couponUpdateDTO.getPublishNum())) + "~" + couponUpdateDTO.getRandomMax().multiply(new BigDecimal(couponUpdateDTO.getPublishNum())) + "元之间，请重新输入");
                coupon.setCouponContent("满" + couponUpdateDTO.getLimitQuota() + "元随机减");
            }
        }
        //如果cycle为空，说明是固定起始时间；否则就是领券当日起cycle天内可用
        if (StringUtil.isNullOrZero(couponUpdateDTO.getCycle())) {
            coupon.setEffectiveTimeType(CouponConst.EFFECTIVE_TIME_TYPE_1);
            AssertUtil.isTrue(couponUpdateDTO.getEffectiveStart().before(couponUpdateDTO.getPublishStartTime()), "使用开始时间应在活动开始时间之后，请重新输入");
            AssertUtil.isTrue(couponUpdateDTO.getEffectiveEnd().before(couponUpdateDTO.getPublishEndTime()), "使用结束时间应在活动结束时间之后，请重新输入");
        } else {
            coupon.setEffectiveTimeType(CouponConst.EFFECTIVE_TIME_TYPE_2);
        }
        coupon.setStoreId(vendor.getStoreId());
        coupon.setStoreName(vendor.getStore().getStoreName());
        couponModel.updateCoupon(coupon, couponUpdateDTO.getGoodsIds(), null);

        //随机金额券随机数存入redis，领取的时候直接取
        if (couponUpdateDTO.getCouponType() == CouponConst.COUPON_TYPE_3) {
            List<BigDecimal> list = RedBagUtils.genRedPacket(couponUpdateDTO.getRandomMin(), couponUpdateDTO.getRandomMax(),
                    couponUpdateDTO.getPublishAmount(), couponUpdateDTO.getPublishNum());
            for (int i = 0, listSize = list.size(); i < listSize; i++) {
                BigDecimal amount = list.get(i);
                stringRedisTemplate.opsForHash().put(RedisConst.COUPON_PREFIX + couponUpdateDTO.getCouponId(), i + "", amount.toString());
            }
            long remainedTime = couponUpdateDTO.getPublishEndTime().getTime() - System.currentTimeMillis();
            objectRedisTemplate.expire(RedisConst.COUPON_PREFIX + couponUpdateDTO.getCouponId(), remainedTime, TimeUnit.MILLISECONDS);
        }
        return SldResponse.success("编辑成功");
    }

    @ApiOperation("删除优惠券")
    @VendorLogger(option = "删除优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "优惠券id", required = true)
    })
    @PostMapping("del")
    public JsonResult delCoupon(HttpServletRequest request, Integer couponId) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        AssertUtil.notNullOrZero(couponId, "优惠券id不能为空");
        Coupon coupon = couponModel.getCouponByCouponId(couponId);
        AssertUtil.notNull(coupon, "优惠券id:" + couponId + "详情为空");
        AssertUtil.isTrue(!coupon.getStoreId().equals(vendor.getStoreId()), "无权限");

        //查询是否有用户领取未使用的优惠券，有则不能删除
        CouponMemberExample example = new CouponMemberExample();
        example.setCouponId(couponId);
        example.setEffectiveStartBefore(new Date());
        example.setEffectiveEndAfter(new Date());
        example.setUseState(CouponConst.USE_STATE_1);
        List<CouponMember> list = couponMemberModel.getCouponMemberList(example, null);
        AssertUtil.isTrue(!CollectionUtils.isEmpty(list), "该优惠券已有用户领取未使用不能删除");

        couponModel.deleteCoupon(couponId);
        return SldResponse.success("删除成功");
    }

    @ApiOperation("失效优惠券")
    @VendorLogger(option = "失效优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "优惠券id", required = true)
    })
    @PostMapping("invalid")
    public JsonResult invalid(HttpServletRequest request, Integer couponId) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        AssertUtil.notNullOrZero(couponId, "优惠券id不能为空");
        Coupon couponDb = couponModel.getCouponByCouponId(couponId);
        AssertUtil.notNull(couponDb, "优惠券id:" + couponId + "详情为空");
        AssertUtil.isTrue(!couponDb.getStoreId().equals(vendor.getStoreId()), "无权限");

        Coupon coupon = new Coupon();
        coupon.setCouponId(couponId);
        coupon.setState(CouponConst.ACTIVITY_STATE_2);
        couponModel.updateCoupon(coupon);

        //将存入redis的随机金额券随机数删除
        if (couponDb.getCouponType() == CouponConst.COUPON_TYPE_3) {
            stringRedisTemplate.delete(RedisConst.COUPON_PREFIX + couponId);
        }
        return SldResponse.success("失效成功");
    }

    @ApiOperation("优惠券领取详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "优惠券id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "memberName", value = "会员名称", paramType = "query"),
            @ApiImplicitParam(name = "useState", value = "使用状态(1-未使用；2-已使用；3-已过期）", paramType = "query"),
            @ApiImplicitParam(name = "receiveStartTime", value = "领取开始时间", paramType = "query"),
            @ApiImplicitParam(name = "receiveEndTime", value = "领取结束时间", paramType = "query"),
            @ApiImplicitParam(name = "useStartTime", value = "使用开始时间", paramType = "query"),
            @ApiImplicitParam(name = "useEndTime", value = "使用结束时间", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("receiveDetails")
    public JsonResult<PageVO<CouponMemberVO>> receiveDetails(HttpServletRequest request, Integer couponId, String memberName,
                                                             Integer useState, Date receiveStartTime, Date receiveEndTime,
                                                             Date useStartTime, Date useEndTime) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        CouponMemberExample example = new CouponMemberExample();
        example.setStoreId(vendor.getStoreId());
        example.setCouponId(couponId);
        example.setMemberNameLike(memberName);
        if (!StringUtil.isNullOrZero(useState)) {
            if (useState == CouponConst.USE_STATE_3) {
                //查询已过期或者后台执行失效操作的数据
                example.setEffectiveEndBefore(new Date());
            } else if (useState == CouponConst.USE_STATE_1) {
                example.setUseState(CouponConst.USE_STATE_1);
                example.setEffectiveEndAfter(new Date());
            } else {
                example.setUseState(CouponConst.USE_STATE_2);
            }
        }
        example.setReceiveTimeAfter(receiveStartTime);
        example.setReceiveTimeBefore(receiveEndTime);
        example.setUseTimeAfter(useStartTime);
        example.setUseTimeBefore(useEndTime);
        List<CouponMember> list = couponMemberModel.getCouponMemberList(example, pager);
        List<CouponMemberVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(member -> {
                vos.add(new CouponMemberVO(member));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("赠送优惠券列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponName", value = "优惠券名称", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("sendList")
    public JsonResult<PageVO<CouponSendVO>> sendList(HttpServletRequest request, String couponName) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        CouponExample example = new CouponExample();
        example.setStoreId(vendor.getStoreId());
        example.setCouponNameLike(couponName);
        example.setState(CouponConst.ACTIVITY_STATE_1);
        example.setPublishType(CouponConst.PUBLISH_TYPE_3);
        List<Coupon> list = couponModel.getCouponList(example, pager);
        List<CouponSendVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(coupon -> {
                vos.add(new CouponSendVO(coupon));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }
}
