package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeDiscountCoupon;
import shop.alien.entity.store.UserLoginInfo;
import shop.alien.entity.store.dto.LifeDiscountCouponDto;
import shop.alien.entity.store.dto.LifeDiscountCouponUserDto;
import shop.alien.entity.store.vo.LifeCouponStatusVo;
import shop.alien.entity.store.vo.LifeDiscountCouponUserWebVo;
import shop.alien.entity.store.vo.LifeDiscountCouponVo;
import shop.alien.store.service.LifeDiscountCouponService;
import shop.alien.store.service.LifeDiscountCouponUserService;
import shop.alien.util.common.TokenInfo;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠券表 前端控制器
 * </p>
 *
 * @author ssk
 * @since 2025-02-07
 */
@Api(tags = {"二期-优惠券"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/life-discount-coupon")
@RequiredArgsConstructor
public class LifeDiscountCouponController {

    private final LifeDiscountCouponService lifeDiscountCouponService;

    private final LifeDiscountCouponUserService lifeDiscountCouponUserService;

    @ApiOperation(value = "新增门店优惠券", notes = "仅写入 life_discount_coupon（满减/折扣券）。不创建代金券。couponStatus=0 表示草稿。")
    @ApiOperationSupport(order = 1)
    @PostMapping("/addDiscountCoupon")
    public R<Boolean> addDiscountCoupon(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo, @RequestBody LifeDiscountCouponDto lifeDiscountCouponDto) {
        log.info("LifeDiscountCouponController.addDiscountCoupon userId={} name={} couponStatus={}",
                userLoginInfo.getUserId(), lifeDiscountCouponDto.getName(), lifeDiscountCouponDto.getCouponStatus());
        try {
            boolean save = lifeDiscountCouponService.addDiscountCoupon(lifeDiscountCouponDto);
            if (!save) {
                return R.fail("新增失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("LifeDiscountCouponController.addDiscountCoupon biz: {}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.addDiscountCoupon ERROR Msg={}", e.getMessage(), e);
            return R.fail("新增失败");
        }
        return R.success("新增成功");
    }

    @ApiOperation("修改优惠券信息")
    @ApiOperationSupport(order = 2)
    @PutMapping("/editDiscountCoupon")
    public R<Boolean> editDiscountCoupon(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo, @RequestBody LifeDiscountCouponDto lifeDiscountCouponDto) {
        log.info("LifeDiscountCouponController.editDiscountCoupon?lifeDiscountCoupon={}", lifeDiscountCouponDto.toString());
        try {
            boolean edited = lifeDiscountCouponService.editDiscountCoupon(lifeDiscountCouponDto);
            if (!edited) {
                return R.fail("修改失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("LifeDiscountCouponController.editDiscountCoupon biz: {}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.editDiscountCoupon ERROR Msg={}", e.getMessage(), e);
            return R.fail("修改失败");
        }
        return R.success("修改成功");
    }

    @ApiOperation("删除优惠券信息")
    @ApiOperationSupport(order = 3)
    @DeleteMapping("/deleteDiscountCoupon/{id}")
    public R<Boolean> deleteDiscountCoupon(@PathVariable("id") Integer id) {
        log.info("LifeDiscountCouponController.deleteDiscountCoupon?id={}", id);
        try {
            boolean deleted = lifeDiscountCouponService.deleteDiscountCoupon(id);
            if (!deleted) {
                return R.fail("删除失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("LifeDiscountCouponController.deleteDiscountCoupon biz: {}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.deleteDiscountCoupon ERROR Msg={}", e.getMessage(), e);
            return R.fail("删除失败");
        }
        return R.success("删除成功");
    }

    @ApiOperation("切换优惠券进行状态")
    @ApiOperationSupport(order = 4)
    @PutMapping("/cutDiscountCouponStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "counponId", value = "优惠券id", dataType = "String", paramType = "query", required = true)
    })
    public R<Boolean> cutDiscountCouponStatus(@RequestParam(value = "counponId") String counponId) {
        log.info("LifeDiscountCouponController.cutDiscountCouponStatus?counponId={}", counponId);
        try {
            boolean save = lifeDiscountCouponService.cutDiscountCouponStatus(counponId);
            if (!save) {
                return R.fail("切换失败");
            }
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.cutDiscountCouponStatus ERROR Msg={}", e.getMessage());
            return R.fail("切换失败");
        }
        return R.success("切换成功");
    }

    @ApiOperation("获取优惠券详情（含好友赠券本店铺已送出数量）")
    @ApiOperationSupport(order = 55)
    @GetMapping("/getCouponDetailWithOwnedQty")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "优惠券id（life_discount_coupon.id）", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeId", value = "店铺id（store_info.id）", dataType = "String", paramType = "query", required = true)
    })
    public R<LifeDiscountCouponVo> getCouponDetailWithOwnedQty(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                                                               @RequestParam(value = "couponId") String couponId,
                                                               @RequestParam(value = "storeId") String storeId) {
        log.info("LifeDiscountCouponController.getCouponDetailWithOwnedQty?couponId={}, storeId={}", couponId, storeId);
        try {
            LifeDiscountCouponVo vo = lifeDiscountCouponService.getCouponDetailWithFriendOwnedQty(couponId, storeId);
            if (vo == null) {
                return R.fail("优惠券不存在或参数无效");
            }
            return R.data(vo);
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.getCouponDetailWithOwnedQty ERROR Msg={}", e.getMessage(), e);
            return R.fail("查询失败");
        }
    }

    @ApiOperation("获取该优惠券的明细信息")
    @ApiOperationSupport(order = 5)
    @GetMapping("/getCounponDetailById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "counponId", value = "优惠券id", dataType = "String", paramType = "query", required = true)
    })
    public R<LifeDiscountCouponVo> getCounponDetailById(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                                                        @RequestParam(value = "counponId") String counponId) {
        log.info("LifeDiscountCouponController.getCounponDetailById?counponId={}", counponId);
        try {
            LifeDiscountCouponVo counponDetail = lifeDiscountCouponService.getCounponDetailById(counponId, userLoginInfo);
            return R.data(counponDetail);
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.getCounponDetailById ERROR Msg={}", e.getMessage());
            return R.fail("查询失败");
        }
    }

    @ApiOperation("领取优惠券")
    @ApiOperationSupport(order = 6)
    @PostMapping("/receiveCoupon")
    public R<Boolean> receiveCoupon(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo, @RequestBody LifeDiscountCouponUserDto lifeDiscountCouponUserDto) {
        log.info("LifeDiscountCouponController.receiveCoupon?lifeDiscountCouponUserDto={}", lifeDiscountCouponUserDto.toString());
        return lifeDiscountCouponUserService.receiveCoupon(lifeDiscountCouponUserDto);
    }

    @ApiOperation("获取该用户该店铺优惠券列表。couponType=1仅满减券，couponType=2仅折扣券，不传返回全部优惠券")
    @ApiOperationSupport(order = 7)
    @GetMapping("/getStoreUserCouponList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "couponType", value = "优惠券类型：1=仅满减券，2=仅折扣券，不传=全部优惠券（可选）", dataType = "Integer", paramType = "query", required = false)
    })
    public R<List<LifeDiscountCouponVo>> getStoreUserCouponList(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                                                                @RequestParam(value = "storeId") String storeId,
                                                                @RequestParam(value = "couponType", required = false) Integer couponType) {
        log.info("LifeDiscountCouponController.getStoreUserCouponList?storeId={}, couponType={}", storeId, couponType);
        try {
            List<LifeDiscountCouponVo> storeCouponList = lifeDiscountCouponService.getStoreUserCouponList(storeId, userLoginInfo, couponType);
            return R.data(storeCouponList);
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.getStoreUserCouponList ERROR Msg={}", e.getMessage());
            return R.fail("查询失败");
        }
    }

    @ApiOperation("获取该用户该店铺优惠券已领可用不可用列表。couponType=1仅满减券，couponType=2仅折扣券，不传返回全部优惠券")
    @ApiOperationSupport(order = 8)
    @GetMapping("/getStoreUserUsableCouponList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "amount", value = "消费金额", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "couponType", value = "优惠券类型：1=仅满减券，2=仅折扣券，不传=全部优惠券（可选）", dataType = "Integer", paramType = "query", required = false)
    })
    public R<Map> getStoreUserUsableCouponList(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                                               @RequestParam(value = "storeId") String storeId,
                                               @RequestParam(value = "amount") BigDecimal amount,
                                               @RequestParam(value = "couponType", required = false) Integer couponType
    ) {
        log.info("LifeDiscountCouponController.getStoreUserUsableCouponList?storeId={}, couponType={}", storeId, couponType);
        try {
            return R.data(lifeDiscountCouponService.getStoreUserUsableCouponList(storeId, userLoginInfo, amount, couponType));
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.getStoreUserUsableCouponList ERROR Msg={}", e.getMessage());
            return R.fail("查询失败");
        }
    }


    @ApiOperation("获取该用户所有的优惠券列表。couponType=1仅满减券，couponType=2仅折扣券，不传返回全部优惠券；storeKeyword 非空时仅返回门店名称包含关键字的优惠券")
    @ApiOperationSupport(order = 9)
    @GetMapping("/getUserCouponList")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页页数", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "tabType", value = "分页类型（0：全部（未使用），1：即将过期，2：已使用，3：已过期/已失效）", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "券类型（不传或传1：仅优惠券；代金券 type=4 已下线）", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "couponType", value = "优惠券类型：1=仅满减券，2=仅折扣券，不传=全部优惠券（可选）", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "storeId", value = "商铺ID，可为空，传则仅返回该商铺的优惠券", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "storeKeyword", value = "店铺名称关键字，非空则只展示门店名称含关键字的优惠券（可选，最长100字符）", dataType = "String", paramType = "query", required = false)
    })
    public R<List<LifeDiscountCouponVo>> getUserCouponList(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                                                           @RequestParam(value = "tabType") String tabType,
                                                           @RequestParam(defaultValue = "1") int page,
                                                           @RequestParam(defaultValue = "10") int size,
                                                           @RequestParam(required = false) Integer type,
                                                           @RequestParam(value = "couponType", required = false) Integer couponType,
                                                           @RequestParam(value = "storeId", required = false) String storeId,
                                                           @RequestParam(value = "storeKeyword", required = false) String storeKeyword) {
        try {
            // 参数校验
            if (StringUtils.isEmpty(tabType)) {
                return R.fail("分页类型不能为空");
            }
            if (page < 1) {
                page = 1;
            }
            if (size < 1 || size > 100) {
                size = 10;
            }
            if (type != null && type != 1) {
                return R.fail("券类型参数错误：仅支持不传或1（优惠券）；代金券已下线");
            }
            if (couponType != null && couponType != 1 && couponType != 2) {
                return R.fail("优惠券类型参数错误，必须为1（满减券）或2（折扣券）");
            }
            String keywordParam = null;
            if (storeKeyword != null) {
                String t = storeKeyword.trim();
                if (!t.isEmpty()) {
                    keywordParam = t.length() > 100 ? t.substring(0, 100) : t;
                }
            }
            
            log.info("LifeDiscountCouponController.getUserCouponList?userId={}, tabType={}, page={}, size={}, type={}, couponType={}, storeId={}, storeKeyword={}",
                    userLoginInfo.getUserId(), tabType, page, size, type, couponType, storeId, keywordParam);
            
            List<LifeDiscountCouponVo> storeCouponList = lifeDiscountCouponService.getUserCouponList(
                    userLoginInfo, page, size, tabType, type, couponType, storeId, keywordParam);
            return R.data(storeCouponList);
        } catch (IllegalArgumentException e) {
            log.error("LifeDiscountCouponController.getUserCouponList 参数错误, userId={}, error={}", 
                    userLoginInfo != null ? userLoginInfo.getUserId() : "unknown", e.getMessage(), e);
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.getUserCouponList ERROR, userId={}, error={}", 
                    userLoginInfo != null ? userLoginInfo.getUserId() : "unknown", e.getMessage(), e);
            return R.fail("查询失败");
        }
    }

    @ApiOperation("获取该店铺所有优惠券（分页）, 好友优惠券。couponType=1仅满减券，couponType=2仅折扣券，不传返回全部优惠券")
    @ApiOperationSupport(order = 10)
    @GetMapping("/getStoreAllCouponList")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页页数", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "storeId", value = "商户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "couponName", value = "优惠券名称", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "tab", value = "分页类型(0:全部，1:进行中，2:已结束，3:草稿，4:未开始，5:已下架，6:已清库)", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "couponsFromType", required = false, value = "查询类型(1:我的优惠券，2:好友的优惠券)", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "couponStatus", required = false, value = "优惠券状态(0:草稿，1:正式)", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "couponType", value = "优惠券类型：1=仅满减券，2=仅折扣券，不传=全部优惠券（可选）", dataType = "Integer", paramType = "query", required = false),
    })
    public R<IPage<LifeDiscountCouponVo>> getStoreAllCouponList(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                                                                @RequestParam(defaultValue = "1") int page,
                                                                @RequestParam(defaultValue = "10") int size,
                                                                @RequestParam(value = "storeId") String storeId,
                                                                @RequestParam(value = "couponName", required = false) String couponName,
                                                                @RequestParam(value = "tab") String tab,
                                                                @RequestParam(value = "couponsFromType", defaultValue = "1") int couponsFromType,
                                                                @RequestParam(value = "couponStatus", defaultValue = "1", required = false) int couponStatus,
                                                                @RequestParam(value = "couponType", required = false) Integer couponType
    ) {
        try {
            // 参数校验
            if (StringUtils.isEmpty(storeId)) {
                return R.fail("商户id不能为空");
            }
            if (StringUtils.isEmpty(tab)) {
                return R.fail("分页类型不能为空");
            }
            if (page < 1) {
                page = 1;
            }
            if (size < 1 || size > 100) {
                size = 10;
            }
            if (couponsFromType != 1 && couponsFromType != 2) {
                couponsFromType = 1;
            }
            if (couponStatus != 0 && couponStatus != 1) {
                couponStatus = 1;
            }
            if (couponType != null && couponType != 1 && couponType != 2) {
                return R.fail("优惠券类型参数错误，必须为1（满减券）或2（折扣券）");
            }
            
            log.info("LifeDiscountCouponController.getStoreAllCouponList?storeId={}, couponName={}, tab={}, page={}, size={}, couponsFromType={}, couponStatus={}, couponType={}", 
                    storeId, couponName, tab, page, size, couponsFromType, couponStatus, couponType);
            
            IPage<LifeDiscountCouponVo> storeCouponList = lifeDiscountCouponService.getStoreAllCouponList(
                    storeId, userLoginInfo, page, size, couponName, tab, couponsFromType, couponStatus, couponType);
            return R.data(storeCouponList);
        } catch (IllegalArgumentException e) {
            log.error("LifeDiscountCouponController.getStoreAllCouponList 参数错误, storeId={}, error={}", storeId, e.getMessage(), e);
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.getStoreAllCouponList ERROR, storeId={}, error={}", storeId, e.getMessage(), e);
            return R.fail("查询失败");
        }
    }

    @ApiOperation("获取该店铺所有优惠券 代金券（不分页）。couponType=1仅满减券，couponType=2仅折扣券，不传返回全部优惠券")
    @ApiOperationSupport(order = 11)
    @GetMapping("/getStoreAllCouponListPaginateNot")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "类型：1-优惠券（默认），4-代金券", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "couponType", value = "优惠券类型：1=仅满减券，2=仅折扣券，不传=全部优惠券（可选，仅当type不为4时有效）", dataType = "Integer", paramType = "query", required = false)
    })
    public R<List<LifeDiscountCouponVo>> getStoreAllCouponListPaginateNot(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                                                                          @RequestParam(value = "storeId") String storeId,@RequestParam(value = "status", required = false) String status,
                                                                          @RequestParam(value = "couponStatus", defaultValue = "1", required = false) int couponStatus,
                                                                          @RequestParam(value = "type", defaultValue = "1", required = false) Integer type,
                                                                          @RequestParam(value = "couponType", required = false) Integer couponType) {
        log.info("LifeDiscountCouponController.getStoreAllCouponListPaginateNot?storeId={}, status={}, couponStatus={}, type={}, couponType={}", storeId, status, couponStatus, type, couponType);
        try {
            List<LifeDiscountCouponVo> storeCouponList = lifeDiscountCouponService.getStoreAllCouponListPaginateNot(status, storeId, userLoginInfo, couponStatus, type, couponType);
            return R.data(storeCouponList);
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.getStoreAllCouponListPaginateNot ERROR Msg={}", e.getMessage());
            return R.fail("查询失败");
        }
    }

    @ApiOperation("获取该优惠券/代金券使用规则")
    @ApiOperationSupport(order = 12)
    @GetMapping("/getCouponRule")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "优惠券id（type=1 必传）", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "type", value = "券类型（不传：优惠券与代金券规则都返回，1：优惠券，4：代金券）", dataType = "Integer", paramType = "query", required = false),
            @ApiImplicitParam(name = "voucherId", value = "代金券id（type=4 时传此参数或 id）", dataType = "String", paramType = "query", required = false),
    })
    public R<String> getCouponRule(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                                   @RequestParam(value = "couponId", required = false) String couponId,
                                   @RequestParam(required = false) Integer type,
                                   @RequestParam(value = "voucherId", required = false) String voucherId
                                   ) {
        log.info("LifeDiscountCouponController.getCouponRule?couponId={}, type={}, voucherId={}", couponId, type, voucherId);
        try {
            String couponRule = lifeDiscountCouponService.getCouponRule(couponId, userLoginInfo, type, voucherId);
            return R.success(couponRule);
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.getCouponRule ERROR Msg={}", e.getMessage());
            return R.fail("查询失败");
        }
    }

    @ApiOperation("我创建的优惠券列表（当前店铺创建的 life_discount_coupon）")
    @ApiOperationSupport(order = 12)
    @GetMapping("/getMyCreatedDiscountCouponList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态筛选", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "couponStatus", value = "优惠券状态：0草稿，1正式", dataType = "int", paramType = "query", required = false),
    })
    public R<List<LifeDiscountCouponVo>> getMyCreatedDiscountCouponList(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                                                                         @RequestParam(value = "storeId") String storeId,
                                                                         @RequestParam(value = "status", required = false) String status,
                                                                         @RequestParam(value = "couponStatus", defaultValue = "1", required = false) int couponStatus) {
        log.info("LifeDiscountCouponController.getMyCreatedDiscountCouponList?storeId={}", storeId);
        try {
            List<LifeDiscountCouponVo> list = lifeDiscountCouponService.getStoreAllCouponListPaginateNot(status, storeId, userLoginInfo, couponStatus, 1, null);
            return R.data(list);
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.getMyCreatedDiscountCouponList ERROR Msg={}", e.getMessage());
            return R.fail("查询失败");
        }
    }

    @ApiOperation("我创建的代金券列表（当前店铺创建的 life_coupon，type=1 代金券）")
    @ApiOperationSupport(order = 12)
    @GetMapping("/getMyCreatedVoucherList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态筛选", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "couponStatus", value = "代金券状态：0草稿，1正式", dataType = "int", paramType = "query", required = false),
    })
    public R<List<LifeDiscountCouponVo>> getMyCreatedVoucherList(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo,
                                                                 @RequestParam(value = "storeId") String storeId,
                                                                 @RequestParam(value = "status", required = false) String status,
                                                                 @RequestParam(value = "couponStatus", defaultValue = "1", required = false) int couponStatus) {
        log.info("LifeDiscountCouponController.getMyCreatedVoucherList?storeId={}", storeId);
        try {
            List<LifeDiscountCouponVo> list = lifeDiscountCouponService.getStoreAllCouponListPaginateNot(status, storeId, userLoginInfo, couponStatus, 4, null);
            return R.data(list);
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.getMyCreatedVoucherList ERROR Msg={}", e.getMessage());
            return R.fail("查询失败");
        }
    }

    @ApiOperation("获取优惠券状态")
    @ApiOperationSupport(order = 13)
    @GetMapping("/getCouponStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "优惠券id", dataType = "String", paramType = "query", required = true),
    })
    public R<LifeCouponStatusVo> getCouponStatus(@RequestParam(value = "couponId") String couponId) {
        log.info("LifeDiscountCouponController.getCouponStatus?couponId={}", couponId);
        try {
            LifeCouponStatusVo lifeCouponStatusVo = lifeDiscountCouponService.getCouponStatus(couponId);
            return R.data(lifeCouponStatusVo);
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.getCouponStatus ERROR Msg={}", e.getMessage());
            return R.fail("查询失败");
        }
    }

    /**
     * 获取平台优惠券列表
     *
     * @param pageNum    分页页码
     * @param pageSize   分页大小
     * @param name       优惠券名称
     * @param createDate 创建时间
     * @param couponType 类型:(1,优惠券 2,红包 3,平台优惠券)
     * @return IPage<LifeDiscountCouponVo>
     */
    @ApiOperation("获取平台优惠券列表-web")
    @ApiOperationSupport(order = 14)
    @GetMapping("/getPlatformCouponList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "店铺名称", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "店铺名称", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "优惠券名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createDate", value = "创建时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "couponType", value = "类型:(1:优惠券, 2:红包, 3:平台优惠券)", dataType = "String", paramType = "query")
    })
    public R<IPage<LifeDiscountCoupon>> getPlatformCouponList(@RequestParam(defaultValue = "1") Integer pageNum,
                                                              @RequestParam(defaultValue = "10") Integer pageSize,
                                                              @RequestParam(value = "name") String name,
                                                              @RequestParam(value = "createDate") String createDate,
                                                              @RequestParam(value = "couponType", defaultValue = "3") Integer couponType) {
        return R.data(lifeDiscountCouponService.getPlatformCouponList(pageNum, pageSize, name, createDate, couponType));
    }

    @ApiOperation("新增平台优惠券-web")
    @ApiOperationSupport(order = 15)
    @PostMapping("/addPlatformCoupon")
    private R<Boolean> addPlatformCoupon(@RequestBody LifeDiscountCoupon lifeDiscountCoupon) {
        return R.status(lifeDiscountCouponService.addPlatformCoupon(lifeDiscountCoupon));
    }

    @ApiOperation("查询平台优惠券详情-web")
    @ApiOperationSupport(order = 16)
    @GetMapping("/getOnePlatformCoupon")
    @ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query", required = true)
    private R<LifeDiscountCoupon> getOnePlatformCoupon(@RequestParam(value = "id") Integer id) {
        return R.data(lifeDiscountCouponService.getOnePlatformCoupon(id));
    }

    /**
     * 发放平台优惠券
     *
     * @param userIds  用户主键List
     * @param couponId 优惠券id
     * @return boolean
     */
    @ApiOperation("发放平台优惠券-web")
    @ApiOperationSupport(order = 17)
    @GetMapping("/issuePlatformCoupon")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userIds", value = "用户主键List", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "couponId", value = "优惠券id", dataType = "Integer", paramType = "query", required = true)
    })
    private R<Boolean> issuePlatformCoupon(@RequestParam(value = "userIds") List<Integer> userIds, @RequestParam(value = "couponId") Integer couponId) {
        return R.status(lifeDiscountCouponService.issuePlatformCoupon(userIds, couponId));
    }

    /**
     * 收回平台优惠券
     *
     * @param couponId 优惠券id
     * @return boolean
     */
    @ApiOperation("收回平台优惠券-web")
    @ApiOperationSupport(order = 18)
    @GetMapping("/recoverPlatformCoupon")
    @ApiImplicitParam(name = "couponId", value = "优惠券id", dataType = "Integer", paramType = "query", required = true)
    private R<Boolean> recoverPlatformCoupon(@RequestParam(value = "couponId") Integer couponId) {
        return R.status(lifeDiscountCouponService.recoverPlatformCoupon(couponId));
    }

    /**
     * 删除平台优惠券
     *
     * @param couponId 优惠券id
     * @return boolean
     */
    @ApiOperation("删除平台优惠券-web")
    @ApiOperationSupport(order = 19)
    @GetMapping("/deletePlatformCoupon")
    @ApiImplicitParam(name = "couponId", value = "优惠券id", dataType = "Integer", paramType = "query", required = true)
    private R<Boolean> deletePlatformCoupon(@RequestParam(value = "couponId") Integer couponId) {
        if (lifeDiscountCouponService.deletePlatformCoupon(couponId)) {
            return R.success("删除成功");
        }
        return R.fail("删除失败, 该券已发放, 请先收回");
    }

    @ApiOperation("分页查询该券发放记录-web")
    @ApiOperationSupport(order = 20)
    @GetMapping("/getPlatformCouponLogList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "店铺名称", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "店铺名称", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "couponId", value = "优惠券id", dataType = "String", paramType = "query")
    })
    public R<IPage<LifeDiscountCouponUserWebVo>> getPlatformCouponLogList(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                                                          @RequestParam(value = "userName") String userName,
                                                                          @RequestParam(value = "phone") String phone,
                                                                          @RequestParam(value = "couponId") Integer couponId) {
        return R.data(lifeDiscountCouponService.getPlatformCouponLogList(pageNum, pageSize, userName, phone, couponId));
    }

    /**
     * 获取平台优惠券列表
     *
     * @param couponId 优惠券ID
     * @param couponType 类型:(1,优惠券 2,红包 3,平台优惠券)
     * @return List<LifeDiscountCoupon>
     */
    @ApiOperation("获取平台优惠券列表")
    @ApiOperationSupport(order = 15)
    @GetMapping("/getPlatformCoupon")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "优惠券ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "couponType", value = "优惠券类型", dataType = "Integer", paramType = "query")
    })
    public R<List<LifeDiscountCoupon>> getPlatformCoupon(@RequestParam(value = "couponId") Integer couponId,
                                                         @RequestParam(defaultValue = "3") Integer couponType) {
        return R.data(lifeDiscountCouponService.getPlatformCoupon(couponId, couponType));
    }

}
