package com.edi.thoth.service;


import com.edi.thoth.service.auxiliary.RequestHelp;
import com.edi.thoth.service.mongodb.ClientUsersRepository;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.thoth.domain.RequestData;
import com.thoth.domain.user.ClientUsers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;

@Service
public class OrderApiService
{

    private static final Logger Logger = LoggerFactory.getLogger(OrderApiService.class);


    private final ClientUsersRepository clientUsers;

    private final RequestHelp requestHelp;

    private final StandardService standardService;

    private final SpecialService specialService;

    @Autowired
    public OrderApiService(ClientUsersRepository clientUsers, RequestHelp requestHelp,
                           StandardService standardService, SpecialService specialService) {
        this.clientUsers = clientUsers;
        this.requestHelp = requestHelp;
        this.standardService = standardService;
        this.specialService = specialService;
    }

    /**
     * 保存数据
     *
     * @param data
     * @return
     */
    @HystrixCommand(fallbackMethod = "saveOrderHy")
    public String saveOrder(String serviceName, String data, HttpServletRequest request) {

        Logger.info(MessageFormat.format("接收到的数据是 serviceName {0}    data {1}  Parametrs {2}  ",
                serviceName, data, requestHelp.getParameters(request)));
        String outString = saveOrderDate(serviceName, data, request);
        Logger.info(MessageFormat.format(" 响应的数据是 serviceName   {0}   outString {1}   ",
                serviceName, outString));
        return outString;
    }

    public String saveOrderHy(String serviceName, String data, HttpServletRequest request) {
        return "降级";
    }

    /**
     * 处理方法
     *
     * @param data
     * @param request
     * @return
     */
    private String saveOrderDate(String serviceName, String data, HttpServletRequest request) {
        long a = System.currentTimeMillis();

        RequestData requestData = new RequestData(data, request.getParameterMap());
        try {
            ClientUsers user = clientUsers.findByclientFlag(serviceName);
            if (user == null) return "未查询到该用户";
            if (!user.getIps().contains(requestHelp.getIp2(request))) return " 未查询到 Ip ";

            // 调用特殊验证处理
            if (user.getVerificationPattern() == 1) {
                return specialService.specialTreatment(user, requestData);
            }
            // 标准验证处理
            else return standardService.standardMode(requestData, user);

        } catch (Exception e) {
            Logger.error(" 未知异常 ", e);
            //return callApi.bodyApi("failure", requestData);
            return "未知异常";
        } finally {
            Logger.debug(" 执行耗时 : " + (System.currentTimeMillis() - a));
        }

    }


}
