package com.sdtool.webcore.rest;

import com.sdtool.common.api.ERPService;
import com.sdtool.common.base.BaseService;
import com.sdtool.common.datas.ERPHeader;
import com.sdtool.common.datas.HttpCode;
import com.sdtool.common.entity.base.DataBaseMultiItemEx;
import com.sdtool.common.entity.site.ERPTokenUser;
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.lang3.StringUtils;
import org.rex.RMap;

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

/**
 * Created by jlutt on 2020-07-29
 * ERP平台基础服务
 *
 * @author jlutt
 */
@AutoLoad(false)
@Local
@RestService(name = "erp", moduleid = 1, comment = "00ERP平台基础服务")
@SuppressWarnings({"rawtypes", "unused"})
public class ERPRest extends BaseService {

    @Resource(name = "property.saasHostName")
    private String saasHostName;

    @Resource
    ERPService erpService;

    @RestMapping(name = "checkERPClientExe", sort = 0, comment = "Delphi客户端鉴别", methods = {"GET", "POST"})
    public CompletableFuture<RMap> checkERPClientExe(
            @RestParam(name = "&", comment = "当前用户，不需要传入") ERPTokenUser currentUser) {
        return CompletableFuture.supplyAsync(
                () -> {
                    return RMapUtils.success();
                }, getExecutor()
        );
    }

    @RestMapping(name = "getERPSupplierList", sort = 1, comment = "获取平台所有工厂客户列表_中心服务", methods = {"GET", "POST"})
    @WebApiBean(type = DataBaseMultiItemEx.class, result = true)
    public CompletableFuture<RMap> getERPSupplierList(
            @RestParam(name = "&", comment = "当前用户，不需要传入") ERPTokenUser currentUser) {
        return CompletableFuture.supplyAsync(
                () -> {
                    List<DataBaseMultiItemEx> itemList = erpService.getERPSupplierList();
                    return RMapUtils.successV2(itemList, null, null);
                }, getExecutor()
        );
    }

    @RestMapping(name = "getERPSupplierInfo", sort = 2, comment = "获取平台工厂客户信息_中心服务", methods = {"GET", "POST"})
    @WebApiBean(type = DataBaseMultiItemEx.class, result = true)
    public CompletableFuture<RMap> getERPSupplierInfo(
            @RestParam(name = "id", comment = "平台工厂客户id") long id,
            @RestParam(name = "&", comment = "当前用户，不需要传入") ERPTokenUser currentUser) {
        return CompletableFuture.supplyAsync(
                () -> {
                    DataBaseMultiItemEx item = erpService.getERPSupplierInfo(id);
                    return RMapUtils.successV2(item, null, null);
                }, getExecutor()
        );
    }

    @RestMapping(name = "getERPSupplierInfoByCode", sort = 3, comment = "通过编号获取平台工厂客户信息_中心服务", methods = {"GET", "POST"})
    @WebApiBean(type = DataBaseMultiItemEx.class, result = true)
    public CompletableFuture<RMap> getERPSupplierInfoByCode(
            HttpRequest httpRequest,
            @RestParam(name = "code", comment = "平台工厂客户code", required = false) String code,
            @RestParam(name = "&", comment = "当前用户，不需要传入") ERPTokenUser currentUser) {
        return CompletableFuture.supplyAsync(
                () -> {
                    String host = httpRequest.getHost();
                    if (host.toLowerCase().contains(saasHostName)) {
                        //saas域名,取第一段
                        DataBaseMultiItemEx item = erpService.getERPSupplierInfoByCode(host.split("\\.")[0]);
                        if (item == null) {
                            return RMapUtils.error(HttpCode.NOT_FOUND.value(), "无效的编号");
                        } else {
                            if (item.getVoidFlag() == 1) {
                                return RMapUtils.error(HttpCode.NOT_FOUND.value(), "无效的编号");
                            }
                            item.setSaasFlag(1);
                            return RMapUtils.successV2(item, null, null);
                        }
                    } else {
                        //客户自己的域名，不要，但是开发没法调试。。。因为开发或者独立域名是在nginx中配置的
                        //从nginx的头中获取
                        String supplierCode = httpRequest.getHeader(ERPHeader.HTTPHEADER_SUPPLIER, "");
                        if (StringUtils.isEmpty(supplierCode)) {
                            return RMapUtils.error(HttpCode.NOT_FOUND.value(), "无效的编号");
                        } else {
                            DataBaseMultiItemEx item = erpService.getERPSupplierInfoBySharding(Long.parseLong(supplierCode));
                            if ((item == null) || (item.getVoidFlag() == 1)) {
                                return RMapUtils.error(HttpCode.NOT_FOUND.value(), "无效的编号");
                            }
                            item.setSaasFlag(0);
                            return RMapUtils.successV2(item, null, null);
                        }

                    }
                }, getExecutor()
        );
    }

    @RestMapping(name = "remoteGetERPSupplierList", sort = 20, comment = "获取平台所有工厂客户列表_中心服务", methods = {"GET", "POST"})
    @WebApiBean(type = DataBaseMultiItemEx.class, result = true)
    public CompletableFuture<RetResult<List<DataBaseMultiItemEx>>> remoteGetERPSupplierList(
            @RestParam(name = "&", comment = "当前用户，不需要传入") ERPTokenUser currentUser) {
        return CompletableFuture.supplyAsync(
                () -> {
                    List<DataBaseMultiItemEx> itemList = erpService.getERPSupplierList();
                    return RetResult.<List<DataBaseMultiItemEx>>successT().result(itemList);
                }, getExecutor()
        );
    }

    @RestMapping(name = "remoteGetERPSupplierInfo", sort = 21, comment = "获取平台所有工厂客户列表_中心服务", methods = {"GET", "POST"})
    @WebApiBean(type = DataBaseMultiItemEx.class, result = true)
    public CompletableFuture<RetResult<DataBaseMultiItemEx>> remoteGetERPSupplierInfo(
            @RestParam(name = "id", comment = "平台工厂客户id") long id,
            @RestParam(name = "&", comment = "当前用户，不需要传入") ERPTokenUser currentUser) {
        return CompletableFuture.supplyAsync(
                () -> {
                    DataBaseMultiItemEx item = erpService.getERPSupplierInfo(id);
                    if (item == null) {
                        return RetResult.<DataBaseMultiItemEx>errorT().retinfo("工厂不存在");
                    }

                    return RetResult.<DataBaseMultiItemEx>successT().result(item);
                }, getExecutor()
        );
    }
}
