package com.fs.app.controller;


import cn.hutool.Hutool;
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.his.domain.FsCoupon;
import com.fs.his.domain.FsPackage;
import com.fs.his.param.*;
import com.fs.his.service.IFsCouponService;
import com.fs.his.service.IFsDoctorService;
import com.fs.his.service.IFsPackageService;
import com.fs.his.vo.FsCouponListUVO;
import com.fs.his.vo.FsDoctorListUVO;
import com.fs.his.vo.FsPackageListUVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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


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

    @Autowired
    private IFsCouponService couponService;

    @Autowired
    private RedisCache redisCache;

    @ApiOperation("获取优惠券列表")
    @GetMapping("/getCouponList")
    public R getCouponList(FsCouponListUParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsCouponListUVO> list=couponService.selectFsCouponListUVO(param);
        PageInfo<FsCouponListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }


    @ApiOperation("获取优惠券详情")
    @GetMapping("/getCouponById")
    public R getCouponById(@RequestParam("couponId")Long couponId)
    {
        FsCoupon coupon =couponService.selectFsCouponByCouponId(couponId);
        return R.ok().put("data",coupon);
    }

    @ApiOperation("生成随机数")
    @GetMapping("/genCode")
    public R genCode(@RequestParam("couponId")Long couponId)
    {
        String code = IdUtil.randomUUID();
        redisCache.setCacheObject(code,couponId.toString(),100, TimeUnit.MINUTES);
        return R.ok().put("data",code);
    }
    @Login
    @ApiOperation("领取")
    @PostMapping("/receive")
    @Synchronized
    public R receive(HttpServletRequest request, @Validated @RequestBody FsCouponReceiveParam param)
    {
//        String couponId=redisCache.getCacheObject(param.getCode());
//        if(couponId==null){
//            return R.error("此券已过期");
//        }
//        if(!couponId.equals(param.getCouponId().toString())){
//            return R.error("非法操作");
//        }
        param.setUserId(Long.parseLong(getUserId()));
        return couponService.receive(param);
    }


}
