package com.fs.app.controller;


import cn.hutool.core.util.IdUtil;
import com.fs.app.annotation.Login;
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.store.domain.FsStoreCouponIssue;
import com.fs.store.domain.FsStoreCouponUser;
import com.fs.store.param.*;
import com.fs.store.service.*;
import com.fs.store.vo.FsStoreCouponIssueVO;
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="/app/coupon")
public class CouponController extends  AppBaseController {

    @Autowired
    private IFsStoreCouponIssueService fsStoreCouponIssueService;
    @Autowired
    private IFsStoreCouponService couponService;
    @Autowired
    private IFsStoreCouponUserService fsStoreCouponUserService;
    @Autowired
    RedisCache redisCache;

    @Login
    @ApiOperation("获取优惠券列表")
    @GetMapping("/getCouponIssueList")
    public R getCouponIssueList(BaseQueryParam param, HttpServletRequest request){
        PageHelper.startPage(param.getPage(), param.getPageSize());
        try {
            FsStoreCouponIssue map=new FsStoreCouponIssue();
            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 {
            FsStoreCouponUser map=new FsStoreCouponUser();
            map.setIsDel(0);
            map.setUserId(Long.parseLong(getUserId()));
            List<FsStoreCouponUser> list=fsStoreCouponUserService.selectFsStoreCouponUserList(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=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("获取我的优惠券列表")
    @GetMapping("/getMyEnableCouponList")
    public R getMyEnableCouponList(FsCouponUserEnableParam param, HttpServletRequest request){
        try {
            param.setUserId(Long.parseLong(getUserId()));
            List<FsStoreCouponUser> 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("操作异常");
        }
    }


}
