package com.org.hktshop.controller.coupon;

import com.org.hktshop.Interface.coupon.HktCouponService;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.controller.BaseController;
import com.org.hktshop.entity.product.HktCoupon;
import com.org.hktshop.entity.user.HktUser;
import com.org.hktshop.utils.HttpCode;
import com.org.hktshop.utils.StringUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 优惠券
 * @author hotking03
 */
@RestController
public class HktCouponController extends BaseController {

    @Autowired
    private HktCouponService hktCouponService;


    /**
     * 新增/修改优惠券信息
     * @param hktCoupon
     */
    @RequestMapping("/async/coupon/updateCoupon")
    public Object updateCoupon(@RequestBody HktCoupon hktCoupon){
        try {
            hktCouponService.updateCoupon(hktCoupon);
            return BaseResult.success();
        }catch (Exception e){
            logger.info("新增/修改优惠券错误 error = {}",e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    /**
     * 删除优惠券信息
     * @param hktCoupon
     */
    @RequestMapping("/async/coupon/delCoupon")
    public Object delCoupon(@RequestBody HktCoupon hktCoupon){
        try {
            hktCouponService.delCoupon(hktCoupon);
            return BaseResult.success();
        }catch (Exception e){
            logger.info("删除优惠券信息错误 error = {}",e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    /**
     * 查找优惠券信息
     * @param hktCoupon
     */
    @RequestMapping("/coupon/findCoupon")
    public Object findCoupon(@RequestBody HktCoupon hktCoupon){
        try {
            return BaseResult.successWithData(hktCouponService.findCoupon(hktCoupon));
        }catch (Exception e){
            logger.info("查找优惠券信息错误 error = {}",e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }


    /**
     * 查找优惠券信息列表
     * @param hktCoupon
     */
    @RequestMapping("/async/coupon/selectCoupon")
    public Object selectCouponList(@RequestBody HktCoupon hktCoupon){
        try {
            return BaseResult.successWithData(hktCouponService.selectCouponList(hktCoupon));
        }catch (Exception e){
            logger.info("查找优惠券信息列表错误 error = {}",e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    /**
     * 查找优惠券信息列表
     * @param hktCoupon
     */
    @RequestMapping("/shop/coupon/selectCoupon")
    public Object selectCouponByUser(@RequestBody HktCoupon hktCoupon, HttpServletRequest request){
        try {
            HktUser hktUser = (HktUser) getSessionUserId(request);
            if(!StringUtils.isNull(hktUser.getId())){
                hktCoupon.setUserId(hktUser.getId());
            }
            return BaseResult.successWithData(hktCouponService.selectCoupon(hktCoupon));
        }catch (Exception e){
            logger.info("查找优惠券信息列表错误 error = {}",e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }


    /**
     * 领取优惠券
     * @param
     */
    @RequestMapping("/shop/coupon/receiveCoupon")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "优惠券id", required = true)
    })
    public Object receiveCoupon(@RequestBody Map<String,String> map,HttpServletRequest request){
        try {
            HktUser hktUser = (HktUser) getSessionUserId(request);
            if(StringUtils.isNull(hktUser.getId())){
                return BaseResult.fail("您不是内部账户无法领取");
            }
            map.put("userId",hktUser.getId());
            hktCouponService.receiveCoupon(map);
            return BaseResult.success();
        }catch (Exception e){
            logger.info("领取优惠券错误 error = {}",e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }


    /**
     * 已领取优惠券
     * @param
     */
    @RequestMapping("/shop/coupon/alreadyReceiveCoupon")
    public Object alreadyReceiveCoupon(@RequestBody HktCoupon hktCoupon,HttpServletRequest request){
        try {
            HktUser hktUser = (HktUser) getSessionUserId(request);
            if(StringUtils.isNull(hktUser.getId())){
                return BaseResult.fail("您不是内部账户无法查看");
            }
            hktCoupon.setUserId(hktUser.getId());
            hktCoupon.setUsed(0);
            return BaseResult.successWithData(hktCouponService.alreadyReceiveCoupon(hktCoupon));
        }catch (Exception e){
            logger.info("已领取优惠券错误 error = {}",e.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

}
