package com.platform.yijia.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.platform.yijia.config.YiJiaRequest;
import com.platform.yijia.pojo.Coupon;
import com.platform.yijia.pojo.UserCoupon;
import com.platform.yijia.service.CouponService;
import com.platform.yijia.utils.CodeMsg;
import com.platform.yijia.utils.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/*
 * <Title> CouponController </Title>
 * <Description> 优惠券 </Description>
 * @Author JK
 * @Date 2021年7月6日
 */
@Controller
@YiJiaRequest()
@CrossOrigin
@Api(tags = "优惠券")
public class CouponController {
    private static Logger logger =(Logger) LoggerFactory.getLogger(CouponController.class);

    @Resource
    private CouponService couponService;


    //获取优惠券信息
    @RequestMapping(value = "/selectStaGrantCouponList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取油站已发放的优惠券信息", httpMethod = "GET", notes = "获取油站已发放的优惠券信息")
    public String selectStaGrantCouponList(@ApiParam(value = "油站ID", required = true) @RequestParam Integer stationId){
        Gson gson =new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        ResultData resultData = null;
        try {

            Coupon coupon = new Coupon();
            coupon.setStationId(stationId);
            coupon.setIssueType("1");
            //领取油站所分发的优惠券
            List<Coupon> coupons = couponService.selectStaGrantCouponList(coupon);

            resultData = ResultData.success(coupons);
        }catch (Exception e){
            logger.error("获取油站已发放的优惠券错误信息：" + e.getMessage());
            resultData = ResultData.error(CodeMsg.SEARCH_FAIL);
        }
        return  gson.toJson(resultData);
    }


    //获取优惠券信息
    @RequestMapping(value = "/selectCouponInfo", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取优惠券信息", httpMethod = "GET", notes = "获取优惠券信息")
    public String selectCouponInfo(@ApiParam(value = "优惠券ID", required = true) @RequestParam Integer id){
        Gson gson =new Gson();
        ResultData resultData = null;
        try {
            Coupon coupon = new Coupon();
            coupon.setId(id);
            Coupon couponInfo = couponService.selectCouponInfo(coupon);
            resultData = ResultData.success(couponInfo);
        }catch (Exception e){
            logger.error("获取优惠券错误信息：" + e.getMessage());
            resultData = ResultData.error(CodeMsg.SEARCH_FAIL);
        }
        return  gson.toJson(resultData);
    }

    //获取客户所拥有的优惠券
    @RequestMapping(value = "/selectUserCoupons", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取客户所拥有的优惠券", httpMethod = "GET", notes = "获取客户所拥有的优惠券")
    public String selectUserCoupons(@ApiParam(value = "油站ID", required = true) @RequestParam Integer stationId,
                                    @ApiParam(value = "用户unionId", required = true) @RequestParam String unionId){
        Gson gson =new Gson();
        ResultData resultData = null;
        try {
            UserCoupon userCoupon = new UserCoupon();
            userCoupon.setStationId(stationId);
            userCoupon.setUnionId(unionId);
            List<UserCoupon> userCoupons = couponService.selectUserCoupons(userCoupon);
            resultData = ResultData.success(userCoupons);
        }catch (Exception e){
            logger.error("获取客户所拥有的优惠券错误信息：" + e.getMessage());
            resultData = ResultData.error(CodeMsg.SEARCH_FAIL);
        }
        return  gson.toJson(resultData);
    }

    //用户领取优惠券
    @RequestMapping(value = "/addUserCoupon", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "用户领取优惠券", httpMethod = "POST", notes = "用户领取优惠券")
    public String addUserCoupon(@ApiParam(value = "用户优惠券实体类", required = true) @RequestBody UserCoupon userCoupon){
        Gson gson = new Gson();
        ResultData resultData = null;
        try{
            if(userCoupon.getCouponId() !=null){
                Coupon coupon = new Coupon();
                coupon.setId(userCoupon.getCouponId());
                //获取该优惠券信息
                Coupon couponInfo = couponService.selectCouponInfo(coupon);
                //该优惠券是否存在
                if(couponInfo !=null){
                    //优惠券是否已发放
                    if(couponInfo.getGrantStatus() !=null && couponInfo.getGrantStatus().equals("1")){
                        //该优惠券是否有效
                        if(couponInfo.getStatus() !=null && couponInfo.getStatus().equals("1")){
                            userCoupon.setCouponIsEffective("1");
                            userCoupon.setCouponIsUsed("0");
                            userCoupon.setCouponNo("123456789");

                            userCoupon.setGetCouponTime(new Date());
                            couponService.addUserCoupon(userCoupon);
                            resultData = ResultData.success("领取成功！");
                        }else {
                            resultData = ResultData.error(CodeMsg.COUPON_LOSE_EFFECTIVE);
                        }
                    }else {
                        resultData = ResultData.error(CodeMsg.COUPON_NO_GRANT);
                    }
                }else {
                    resultData = ResultData.error(CodeMsg.NO_COUPON);
                }
            }else {
                resultData = ResultData.error(CodeMsg.REQUEST_FAIL);
            }
        }catch (Exception e){
            logger.error("用户领取优惠券错误信息：" + e.getMessage());
            resultData = ResultData.error(CodeMsg.INSERT_FAIL);
        }
        return gson.toJson(resultData);
    }

}
