package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.DateUtil;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.*;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.*;
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.beans.factory.annotation.Autowired;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/4/20 23:47
 */
@Controller
@RequestMapping(value = "/couponAgent")
@Api(value = "代理商接口")
public class HighCouponAgentController {

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

    @Resource
    private HighCouponAgentService highCouponAgentService;

    @Resource
    private HighCouponService highCouponService;

    @Resource
    private HighCouponCodeService highCouponCodeService;

    @RequestMapping(value = "/useConvertCode", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "兑换码使用")
    public ResponseData useConvertCode(@RequestBody JSONObject body) {
        try {

            if (body == null || StringUtils.isBlank(body.getString("code"))) {
                log.error("HighCouponAgentController -> queryConvertCode() error!","");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 查询兑换码
            HighCouponAgentCode convertCode = highCouponAgentService.getConvertCode(body.getString("code"));
            if (convertCode == null) {
                log.error("HighCouponAgentController -> queryConvertCode() error!","无效的兑换码");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无效的兑换码");
            }
            if (convertCode.getStatus() == 3) {
                log.error("HighCouponAgentController -> queryConvertCode() error!","兑换码已被使用");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "兑换码已被使用");
            }
            if (convertCode.getCouponCodeId() != null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "兑换码已被使用");
            }

            return ResponseMsgUtil.success(highCouponAgentService.useConvertCode(body.getString("phone"), body.getString("code")));

        } catch (DeadlockLoserDataAccessException deadlockLoserDataAccessException) {
            log.error("HighActivityController -> userLottery() error!", "服务器繁忙");
            return ResponseMsgUtil.builderResponse(ErrorCode.SERVER_BUSY_ERROR.getCode(),ErrorCode.SERVER_BUSY_ERROR.getMsg(),null);

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

    @RequestMapping(value = "/convertCodeDetail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "兑换码详情")
    public ResponseData convertCodeDetail(@RequestParam(name = "code", required = true) String code) {
        try {

            // 查询兑换码
            HighCouponAgentCode couponAgentCode = highCouponAgentService.getConvertCode(code);
            if (couponAgentCode == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到销售码");
            }

            Map<String, Object> map = new HashMap<>();
            map.put("couponInfo", highCouponService.getCouponById(couponAgentCode.getCouponId()));
            map.put("couponCode", highCouponCodeService.getCouponCodeById(couponAgentCode.getCouponCodeId()));
            map.put("couponAgentCode", couponAgentCode);
            return ResponseMsgUtil.success(map);

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


}
