package com.fs.app.controller.store;


import cn.hutool.core.util.IdUtil;
import com.fs.app.annotation.Login;
import com.fs.app.controller.AppBaseController;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.param.BaseQueryParam;
import com.fs.common.utils.StringUtils;
import com.fs.hisStore.domain.FsStoreCouponIssueScrm;
import com.fs.hisStore.domain.FsStoreCouponUserScrm;
import com.fs.hisStore.param.*;
import com.fs.hisStore.service.IFsStoreCouponIssueScrmService;
import com.fs.hisStore.service.IFsStoreCouponScrmService;
import com.fs.hisStore.service.IFsStoreCouponUserScrmService;
import com.fs.hisStore.vo.FsStoreCouponIssueVO;
import com.fs.hisStore.vo.FsStoreCouponUserVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


@Api("优惠券接口")
@RestController
@RequestMapping(value="/store/app/coupon")
public class CouponScrmController extends AppBaseController {

    @Autowired
    private IFsStoreCouponIssueScrmService fsStoreCouponIssueService;
    @Autowired
    private IFsStoreCouponScrmService couponService;
    @Autowired
    private IFsStoreCouponUserScrmService fsStoreCouponUserService;
    @Autowired
    RedisCache redisCache;

    @Login
    @ApiOperation("获取优惠券列表")
    @GetMapping("/getCouponIssueList")
    public R getCouponIssueList(BaseQueryParam param, HttpServletRequest request){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        try {
            FsStoreCouponIssueScrm map=new FsStoreCouponIssueScrm();
            map.setIsDel(0);
            map.setCouponType(0);
            map.setStatus(1);
            List<FsStoreCouponIssueVO> list=fsStoreCouponIssueService.selectFsStoreCouponIssueListQueryVO(getUserId(),map);
            PageInfo<FsStoreCouponIssueVO> listPageInfo=new PageInfo<>(list);
            return R.ok().put("data",listPageInfo);
        } catch (Exception e){
            return R.error("操作异常");
        }
    }
    @Login
    @ApiOperation("获取我的优惠券列表")
    @GetMapping("/getMyCouponList")
    public R getMyCouponList(FsCouponUserParam param, HttpServletRequest request){
        try {
            FsStoreCouponUserScrm map=new FsStoreCouponUserScrm();
            map.setIsDel(0);
            map.setUserId(Long.parseLong(getUserId()));
            List<FsStoreCouponUserVO> list;
            if (param.getCouponType() != null && param.getCouponType() == 3) {
                // 处理 couponType=3 的情况，查询立减金
                list = fsStoreCouponUserService.selectFsStoreCouponUserListAppVOForCash(map);
            } else {
                // 处理其他 couponType 的情况，查询普通优惠券
                list = fsStoreCouponUserService.selectFsStoreCouponUserListVO(map);
            }

            return R.ok().put("data",list);
        } catch (Exception e){
            return R.error("操作异常");
        }
    }

    @Login
    @ApiOperation("领取")
    @PostMapping("/receive")
    public R receive(@RequestBody FsStoreCouponReceiveParam param, HttpServletRequest request){
        return fsStoreCouponIssueService.receive(getUserId(),param);
    }


    @Login
    @ApiOperation("获取销售可用套餐卷列表")
    @GetMapping("/getCompanyCouponIssueList")
    public R getCompanyCouponIssueList(FsCouponIssueParam param, HttpServletRequest request){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        try {
            List<FsStoreCouponIssueVO> list;
            if (param.getCouponType() != null && param.getCouponType() == 3) {
                // 处理 couponType=3 的情况，调用专门查询立减金的方法
                list = fsStoreCouponIssueService.selectFsStoreCouponIssueListVOForCash(new FsStoreCouponIssueScrm());
            } else {
                // 处理其他 couponType 的情况
                list = fsStoreCouponIssueService.getCompanyCouponIssueList(param);
            }
            PageInfo<FsStoreCouponIssueVO> listPageInfo=new PageInfo<>(list);
            return R.ok().put("data",listPageInfo);
        } catch (Exception e){
            return R.error("操作异常");
        }
    }




    @ApiOperation("获取销售可用套餐卷")
    @GetMapping("/getCouponIssueById")
    public R getCouponIssueById(@RequestParam(value="id") Long id, HttpServletRequest request){
        String code = IdUtil.getSnowflake(0, 0).nextIdStr();
        redisCache.setCacheObject("coupon:"+code,id.toString(),10, TimeUnit.MINUTES);
        FsStoreCouponIssueVO couponIssue=fsStoreCouponIssueService.selectFsStoreCouponIssueVOById(id);
        return R.ok().put("data",couponIssue).put("code",code);
    }
    @Login
    @ApiOperation("领取")
    @PostMapping("/companyReceive")
    public R companyReceive(@RequestBody FsStoreCouponReceiveParam param, HttpServletRequest request){
        String code=redisCache.getCacheObject("coupon:"+param.getCode());
        if(StringUtils.isEmpty(code)){
            return R.error("此券已失效");
        }
        return fsStoreCouponIssueService.receive(getUserId(),param);
    }

    @Login
    @ApiOperation("购买立减金")
    @PostMapping("/purchaseCashCoupon")
    public R purchaseCashCoupon(@RequestBody FsStoreCouponPurchaseParam param, HttpServletRequest request) {
        return fsStoreCouponIssueService.purchaseCashCoupon(getUserId(), param);
    }


    @Login
    @ApiOperation("获取我的优惠券列表")
    @GetMapping("/getMyEnableCouponList")
    public R getMyEnableCouponList(FsCouponUserEnableParam param, HttpServletRequest request){
        try {
            param.setUserId(Long.parseLong(getUserId()));
            List<FsStoreCouponUserScrm> list;
            if (param.getCouponType() != null && param.getCouponType() == 3) {
                list = fsStoreCouponUserService.selectFsStoreCouponUserListByEnableForCash(param);
            } else {
                list = fsStoreCouponUserService.selectFsStoreCouponUserListByEnable(param);
            }
            list=list.stream().filter(s->(s.getCouponPrice().compareTo(param.getUseMinPrice())==-1 )).collect(Collectors.toList());
            list=list.stream().filter(s->(s.getUseMinPrice().compareTo(param.getUseMinPrice())==-1 )).collect(Collectors.toList());
            return R.ok().put("data",list);
        } catch (Exception e){
            return R.error("操作异常");
        }
    }

    @Login
    @ApiOperation("申请立减金退款")
    @PostMapping("/applyCashCouponRefund")
    public R applyCashCouponRefund(@RequestBody FsStoreCashCouponRefundParam param, HttpServletRequest request) {
        return fsStoreCouponIssueService.applyCashCouponRefund(getUserId(), param);
    }
    /**
     * 发送会员优惠券
     */
    @RepeatSubmit
    @Login
    @PostMapping("/sendCoupon")
    public R sendCoupon(@RequestBody FsUserCouponScrmSendParam param)
    {
        Long companyUserId = getCompanyUserId();
        if(companyUserId==null){
            return R.error("销售未登录!");
        }
        param.setSetSendUserId(companyUserId);
        return fsStoreCouponIssueService.sendFsUserCoupon(param);
    }


}
