package com.v1.controller;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.ApiMerchants;
import com.hai.model.ResponseData;
import com.hai.service.*;
import com.v1.config.ToolConfig;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @serviceName rechargeProductController.java
 * @author Sum1Dream
 * @version 1.0.0
 * @Description // 充值产品接口
 * @createTime 17:14 2022/6/9
 **/
@Controller
@RequestMapping(value = "/order")
@Api(value = "订单")
public class HighOrderController {

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

    @Resource
    private ApiMerchantsService apiMerchantsService;

    @Resource
    private SecConfigService secConfigService;

    @Resource
    private OutRechargeOrderService outRechargeOrderService;

    @Resource
    private HighOrderService highOrderService;

    @RequestMapping(value = "/getCallOrderList", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取话费订单列表")
    public ResponseData getCallOrderList(@RequestBody JSONObject object , HttpServletRequest request ) {
        try {

            String ip = "";
            // 有的user可能使用代理，为处理用户使用代理的情况，使用x-forwarded-for
            if  (request.getHeader("x-forwarded-for") == null)  {
                ip = request.getRemoteAddr();
            }  else  {
                ip = request.getHeader("x-forwarded-for");
            }

            if (!secConfigService.isConfig("IP_WHITE" , ip)) {
                log.error("getRechargeProduct error!", "非法ip地址，请联系管理人员！");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.IP_ERROR, "非法ip地址，请联系管理人员！");
            }

            if (StringUtils.isBlank(object.getString("apiKey")) ||
                    StringUtils.isBlank(object.getString("merchId")) ||
                    StringUtils.isBlank(object.getString("createTimeS")) ||
                    StringUtils.isBlank(object.getString("createTimeE")) ||
                    StringUtils.isBlank(object.getString("merchId")) ||
                    StringUtils.isBlank(object.getString("timetable")) ||
                    StringUtils.isBlank(object.getString("sign"))
            ) {
                log.error("getRechargeProduct error!", "请求参数校验失败！");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR);
            }

            ApiMerchants apiMerchants = apiMerchantsService.findByMchId(object.getString("merchId"));

            if (apiMerchants == null) {
                log.error("getRechargeProduct error!", "商户号错误！");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_NOT_EXIST);
            }

            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , object.getString("apiKey"));
            map.put("timetable" , object.getString("timetable"));
            map.put("merchId" , object.getString("merchId"));
            map.put("createTimeS" , object.getString("createTimeS"));
            map.put("createTimeE" , object.getString("createTimeE"));

            if (!secConfigService.isSignVerify(map , object.getString("sign"))) {
                log.error("getRechargeProduct error!", "签名校验失败！");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SIGN_VERIFY);
            }

            if (!ToolConfig.timetableCheck(15L , object.getLong("timetable"))) {
                log.error("getRechargeProduct error!", "请求时间超过15分钟！");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.TIME_OUT);
            }

            Map<String, Object> postMap = new HashMap<>();
            postMap.put("merchId" , object.getString("merchId"));
            postMap.put("createTimeS", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(object.getLong("createTimeS")));
            postMap.put("createTimeE", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(object.getLong("createTimeE")));


            return ResponseMsgUtil.success(outRechargeOrderService.getListRechargeOrder(postMap));

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

    @RequestMapping(value = "/getOrderList", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取订单列表")
    public ResponseData getOrderList(@RequestBody JSONObject object , HttpServletRequest request ) {
        try {

            String ip = "";
            // 有的user可能使用代理，为处理用户使用代理的情况，使用x-forwarded-for
            if  (request.getHeader("x-forwarded-for") == null)  {
                ip = request.getRemoteAddr();
            }  else  {
                ip = request.getHeader("x-forwarded-for");
            }

            if (!secConfigService.isConfig("IP_WHITE" , ip)) {
                log.error("getRechargeProduct error!", "非法ip地址，请联系管理人员！");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.IP_ERROR, "非法ip地址，请联系管理人员！");
            }

            if (StringUtils.isBlank(object.getString("apiKey")) ||
                    StringUtils.isBlank(object.getString("merchId")) ||
                    StringUtils.isBlank(object.getString("createTimeS")) ||
                    StringUtils.isBlank(object.getString("createTimeE")) ||
                    StringUtils.isBlank(object.getString("timetable")) ||
                    StringUtils.isBlank(object.getString("sign"))
            ) {
                log.error("getRechargeProduct error!", "请求参数校验失败！");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR);
            }

            ApiMerchants apiMerchants = apiMerchantsService.findByMchId(object.getString("merchId"));

            if (apiMerchants == null) {
                log.error("getRechargeProduct error!", "商户号错误！");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_NOT_EXIST);
            }

            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , object.getString("apiKey"));
            map.put("timetable" , object.getString("timetable"));
            map.put("merchId" , object.getString("merchId"));
            map.put("createTimeS" , object.getString("createTimeS"));
            map.put("createTimeE" , object.getString("createTimeE"));

            if (!secConfigService.isSignVerify(map , object.getString("sign"))) {
                log.error("getRechargeProduct error!", "签名校验失败！");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SIGN_VERIFY);
            }

            if (!ToolConfig.timetableCheck(15L , object.getLong("timetable"))) {
                log.error("getRechargeProduct error!", "请求时间超过15分钟！");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.TIME_OUT);
            }

            Map<String, Object> postMap = new HashMap<>();
            postMap.put("merchId" , object.getString("merchId"));
            postMap.put("createTimeS", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(object.getLong("createTimeS")));
            postMap.put("createTimeE", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(object.getLong("createTimeE")));


            return ResponseMsgUtil.success(highOrderService.getOrderList(postMap));

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