package com.cweb.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hai.common.Base64Util;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.AESEncodeUtil;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighDiscountAgentCode;
import com.hai.entity.HighDiscountAgentRel;
import com.hai.model.ResponseData;
import com.hai.service.HighDiscountAgentCodeService;
import com.hai.service.HighDiscountAgentRelService;
import com.hai.service.HighDiscountCouponRelService;
import com.hai.service.HighDiscountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/4/4 14:46
 */
@Controller
    @RequestMapping(value = "/discount")
@Api(value = "优惠券接口")
public class HighDiscountController {

    private static Logger log = LoggerFactory.getLogger(HighDiscountController.class);

    @Resource
    private HighDiscountAgentRelService highDiscountAgentRelService;

    @Resource
    private HighDiscountCouponRelService highDiscountCouponRelService;

    @Resource
    private HighDiscountAgentCodeService highDiscountAgentCodeService;

    @Resource
    private HighDiscountService highDiscountService;

    @RequestMapping(value="/getDiscountByQrCode",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据二维码Code查询")
    public ResponseData getDiscountByQrCode(@RequestParam(name = "code", required = true) String code) {
        try {
            String jsonData;
            try {
                jsonData = AESEncodeUtil.aesDecrypt(Base64Util.decode(code));
            } catch (Exception e) {
                log.error("HighDiscountController -> getDiscountByQrCode() error!","code解码错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "code解码错误");
            }

            JSONObject jsonObject = JSON.parseObject(jsonData);
            String type = jsonObject.getString("type");
            Long id = jsonObject.getLong("id");

            HighDiscountAgentCode discountAgentCode = highDiscountAgentCodeService.getCodeById(id);
            if (discountAgentCode != null) {
                HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(discountAgentCode.getDiscountAgentId());
                if (rel != null) {
                    rel.setHighDiscountAgentCode(discountAgentCode);
                    return ResponseMsgUtil.success(rel);
                }
            }
            return ResponseMsgUtil.success(null);

        } catch (Exception e) {
            log.error("HighDiscountController -> getDiscountByQrCode() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getDiscountByCode",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据Code查询优惠券")
    public ResponseData getDiscountByCode(@RequestParam(name = "code", required = true) String code) {
        try {
            System.out.println(code);
            String discountAgentId;
            try {
                discountAgentId = AESEncodeUtil.aesDecrypt(URLDecoder.decode(code, "UTF-8"));
            } catch (Exception e) {
                log.error("HighDiscountController -> getDiscountByQrCode() error!","code解码错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "code解码错误");
            }
            if (StringUtils.isBlank(discountAgentId)) {
                log.error("HighDiscountController -> getDiscountByQrCode() error!","code解码错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "code解码错误");
            }
            // 查询优惠券与代理商关系id
            HighDiscountAgentRel discountAgentRel = highDiscountAgentRelService.getRelById(Long.parseLong(discountAgentId));
            if (discountAgentRel == null) {
                log.error("HighDiscountController -> getDiscountByQrCode() error!","code解码错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "code解码错误");
            }
            return ResponseMsgUtil.success(discountAgentRel);

        } catch (Exception e) {
            log.error("HighDiscountController -> getDiscountByQrCode() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getDiscountByDiscountAgentId",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据优惠券和代理商id 查询")
    public ResponseData getDiscountByDiscountAgentId(@RequestParam(name = "discountAgentId", required = true) Long discountAgentId) {
        try {

            return ResponseMsgUtil.success(highDiscountAgentRelService.getRelById(discountAgentId));

        } catch (Exception e) {
            log.error("HighDiscountController -> getDiscountByDiscountAgentId() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getDiscountById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id 查询优惠券详情")
    public ResponseData getDiscountById(@RequestParam(name = "id", required = true) Long id) {
        try {

            return ResponseMsgUtil.success(highDiscountService.getDiscountById(id));

        } catch (Exception e) {
            log.error("HighDiscountController -> getDiscountById() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getCouponByDiscount",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据优惠券查询卡券")
    public ResponseData getCouponByDiscount(@RequestParam(name = "discountId", required = true) Long discountId) {
        try {

            return ResponseMsgUtil.success(highDiscountCouponRelService.getRelByDiscount(discountId));

        } catch (Exception e) {
            log.error("HighDiscountController -> getCouponByDiscount() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
