package com.sdtool.webcore.rest;

import com.sdtool.common.api.SystemService;
import com.sdtool.common.api.mall.MallAdminService;
import com.sdtool.common.base.BaseService;
import com.sdtool.common.datas.ERPHeader;
import com.sdtool.common.datas.HttpCode;
import com.sdtool.common.entity.base.InvokeCallParams;
import com.sdtool.common.entity.base.InvokeCallResult;
import com.sdtool.common.entity.site.ERPTokenUser;
import com.sdtool.common.tool.ERPUtils;
import com.dySweetFishPlugin.sql.RMapUtils;
import com.sweetfish.net.http.*;
import com.sweetfish.service.Local;
import com.sweetfish.service.RetResult;
import com.sweetfish.util.AutoLoad;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.rex.RMap;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

@AutoLoad(false)
@Local
@RestService(name = "mallAdmin", comment = "商城后台")
public class MallAdminRest extends BaseService {

    @Resource
    MallAdminService mallAdminService;

    @Resource
    SystemService systemService;

    @RestMapping(name = "mallFormToken", auth = true, sort = 1, comment = "请求token", methods = {"POST"})
    @SuppressWarnings("rawtypes")
    public CompletableFuture<RMap> mallFormToken(
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    String token = systemService.requestFormToken("mallAdminForm", Long.parseLong(supplierCode));
                    return RMapUtils.successV2(token, null, null);
                }, getExecutor()
        );
    }

    @RestMapping(name = "callMall", auth = true, sort = 2, logging = 4, comment = "调用商城后台脚本", methods = {"GET", "POST"})
    @WebApiFile(fileName = "mallAdminRest.xml")
    public CompletableFuture<InvokeCallResult> callMall(
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestBody(comment = "提交数据") InvokeCallParams callParams,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    RetResult<InvokeCallResult> infoResult = mallAdminService.callMall(callParams, currentUser, dataSourceId, Long.parseLong(supplierCode));
                    if (infoResult.isSuccess()) {
                        return infoResult.getResult();
                    } else {
                        if (infoResult.getRetcode() == HttpCode.UNAUTHORIZED.value()) {
                            return InvokeCallResult.error(HttpCode.UNAUTHORIZED.value(), infoResult.getRetinfo());
                        }
                        return InvokeCallResult.error(HttpCode.BAD_REQUEST.value(), infoResult.getRetinfo());
                    }
                }, getExecutor()
        );
    }

    // 无需验证登录，验证签名
    @RestMapping(name = "callMallSign", sort = 3, comment = "对接ERP，ERP后台调用专用接口", methods = {"GET", "POST"})
    public CompletableFuture<InvokeCallResult> callMallSign(
            @RestParam(name = "invokeParams", comment = "订单") String invokeParams,
            @RestParam(name = "nonceStr", comment = "参数") String nonceStr,
            @RestParam(name = "sign", comment = "参数") String sign,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {

                    //     todo todo 签名验证
                    String md5Sign = DigestUtils.md5Hex(nonceStr + "").toUpperCase();
                    //String md5Sign = DigestUtils.md5Hex(nonceStr + erpMchKey).toUpperCase();
                    if (!StringUtils.equals(md5Sign, sign)) {
                        return InvokeCallResult.error(HttpCode.BAD_REQUEST.value(), "签名不正确");
                    }

                    InvokeCallParams invokeCallParams = new InvokeCallParams();
                    invokeCallParams.setParams(invokeParams);

                    RetResult<InvokeCallResult> infoResult = mallAdminService.callMall(invokeCallParams, ERPUtils.getSysRemoteErpUser(),
                            dataSourceId, Long.parseLong(supplierCode));
                    if (infoResult.isSuccess()) {
                        return infoResult.getResult();
                    } else {
                        if (infoResult.getRetcode() == HttpCode.UNAUTHORIZED.value()) {
                            return InvokeCallResult.error(HttpCode.UNAUTHORIZED.value(), infoResult.getRetinfo());
                        }
                        return InvokeCallResult.error(HttpCode.BAD_REQUEST.value(), infoResult.getRetinfo());
                    }
                }, getExecutor()
        );
    }




}
