package com.sdtool.webcore.rest;

import com.sdtool.common.api.DocInfoService;
import com.sdtool.common.api.SystemService;
import com.sdtool.common.base.BaseService;
import com.sdtool.common.datas.ERPHeader;
import com.sdtool.common.datas.ERPModule;
import com.sdtool.common.datas.HttpCode;
import com.sdtool.common.entity.base.*;
import com.sdtool.common.entity.doc.BusinessScript;
import com.sdtool.common.entity.site.ERPTokenUser;
import com.sdtool.common.entity.system.CompanyInfo;
import com.dySweetFishPlugin.sql.RMapUtils;
import com.sweetfish.net.http.*;
import com.sweetfish.service.Local;
import com.sweetfish.service.RetResult;
import com.sweetfish.source.PageFlipper;
import com.sweetfish.util.AutoLoad;
import org.rex.RMap;

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

@AutoLoad(false)
@Local
@RestService(name = "doc", comment = "02基础档案模块")
@SuppressWarnings({"rawtypes", "unused"})
public class DocInfoRest extends BaseService {
    @Resource
    private DocInfoService docInfoService;
    @Resource
    private SystemService systemService;

    //region 业务执行脚本
    @RestMapping(name = "getBusinessScriptList", auth = true, sort = 500, comment = "获取业务脚本列表", methods = {"GET", "POST"})
    @WebApiBean(result = true, type = BusinessScript.class)
    public CompletableFuture<RMap> getBusinessScriptList(
            @RestParam(name = "params", comment = "搜索内容", required = false) RMap searchData,
            @RestParam(name = "page", comment = "分页信息", required = false) PageFlipper pageFlipper,
            @RestParam(name = "orderBy", comment = "排序", required = false) String orderBy,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> docInfoService.getBusinessScriptList(searchData, pageFlipper, dataSourceId, Long.parseLong(supplierCode)), getExecutor()
        );
    }

    @RestMapping(name = "getBusinessScriptInfo", auth = true, sort = 501, comment = "获取业务脚本信息", methods = {"GET", "POST"})
    @WebApiBean(result = true, type = BusinessScript.class)
    public CompletableFuture<RMap> getBusinessScriptInfo(
            @RestParam(name = "businessCode", comment = "业务代码") String businessCode,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> docInfoService.getBusinessScriptInfo(businessCode, dataSourceId, Long.parseLong(supplierCode)), getExecutor()
        );
    }

    @RestMapping(name = "getScriptModuleList", auth = true, sort = 508, comment = "获取脚本模块列表", methods = {"POST"})
    public CompletableFuture<RMap> getScriptModuleList(
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    List<IdValueItem> itemList = ERPModule.stream().map((v) -> new IdValueItem(v.getValue(), v.getModuleDisplayName())).collect(Collectors.toList());

                    return RMapUtils.successV2(itemList, null, null);
                }, getExecutor()
        );
    }

    @RestMapping(name = "addBusinessScript", auth = true, sort = 502, logging = 1, comment = "增加业务脚本", methods = {"POST"})
    public CompletableFuture<RMap> addBusinessScript(
            @RestParam(name = "script", comment = "业务脚本信息") BusinessScript script,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> docInfoService.addBusinessScript(script, currentUser, dataSourceId, Long.parseLong(supplierCode)), getExecutor()
        );
    }

    @RestMapping(name = "updateBusinessScript", auth = true, sort = 503, logging = 1, comment = "修改业务脚本", methods = {"POST"})
    public CompletableFuture<RMap> updateBusinessScript(
            @RestParam(name = "script", comment = "业务脚本信息") BusinessScript script,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> docInfoService.updateBusinessScript(script, currentUser, dataSourceId, Long.parseLong(supplierCode)), getExecutor()
        );
    }

    @RestMapping(name = "voidBusinessScript", auth = true, sort = 504, logging = 1, comment = "启用、禁用业务脚本", methods = {"POST"})
    public CompletableFuture<RMap> voidBusinessScript(
            @RestParam(name = "script", comment = "业务脚本信息") BusinessScript script,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> docInfoService.voidBusinessScript(script, currentUser, dataSourceId, Long.parseLong(supplierCode)), getExecutor()
        );
    }

    @RestMapping(name = "getBusinessScriptContent", auth = true, sort = 505, comment = "获取业务脚本内容", methods = {"GET", "POST"})
    public CompletableFuture<RMap> getBusinessScriptContent(
            @RestParam(name = "businessCode", comment = "业务代码") String businessCode,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    RetResult<ScriptContent> result = docInfoService.getBusinessScriptContent(businessCode, dataSourceId, Long.parseLong(supplierCode));
                    if (result.isSuccess()) {
                        return RMapUtils.successV2(result.getResult(), null, null);
                    }
                    return RMapUtils.error(HttpCode.BAD_REQUEST.value(), result.getRetinfo());
                }, getExecutor()
        );
    }

    @RestMapping(name = "saveBusinessScriptContent", auth = true, sort = 506, logging = 1, comment = "保存业务脚本内容", methods = {"POST"})
    public CompletableFuture<RMap> saveBusinessScriptContent(
            @RestBody(comment = "脚本对象，一定要用body传") ScriptContent scriptContent,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    if (scriptContent == null) {
                        return RMapUtils.error(HttpCode.BAD_REQUEST.value(), "please send content in http body");
                    }
                    int iResult = docInfoService.saveBusinessScriptContent(scriptContent, true, dataSourceId, Long.parseLong(supplierCode), currentUser);
                    if (iResult == 1) {
                        return RMapUtils.success();
                    }
                    return RMapUtils.error(HttpCode.BAD_REQUEST.value(), "脚本内容正在保存，请稍后");
                }, getExecutor()
        );
    }

    @RestMapping(name = "compileBusinessScriptContent", auth = true, sort = 507, logging = 1, comment = "编译业务脚本内容", methods = {"POST"})
    public CompletableFuture<RMap> compileBusinessScriptContent(
            @RestBody(comment = "脚本对象，一定要用body传") ScriptContent scriptContent,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    if (scriptContent == null) {
                        return RMapUtils.error(HttpCode.BAD_REQUEST.value(), "please send content in http body");
                    }
                    return docInfoService.compileBusinessScriptContent(scriptContent, dataSourceId, Long.parseLong(supplierCode));
                }, getExecutor()
        );
    }

    //endregion

    //region 分厂信息

    @RestMapping(name = "getCompanyInfoList", auth = true, sort = 800, comment = "获取分厂信息列表", methods = {"GET", "POST"})
    @WebApiBean(result = true, type = CompanyInfo.class)
    public CompletableFuture<RMap> getCompanyInfoList(
            @RestParam(name = "params", comment = "搜索内容", required = false) RMap searchData,
            @RestParam(name = "page", comment = "分页信息", required = false) PageFlipper pageFlipper,
            @RestParam(name = "orderBy", comment = "排序", required = false) String orderBy,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    RetResult<List<CompanyInfo>> result = docInfoService.getCompanyInfoList(searchData, pageFlipper, dataSourceId, Long.parseLong(supplierCode));
                    if (result.isSuccess())
                        return RMapUtils.successV2(result.getResult(), null, result.getPageFlipper());
                    else return RMapUtils.error(HttpCode.BAD_REQUEST.value(), result.getRetinfo());
                }, getExecutor()
        );
    }

    @RestMapping(name = "getAllCompanyInfoList", auth = true, sort = 805, comment = "获取全部分厂信息列表，下拉选择等", methods = {"GET", "POST"})
    @WebApiBean(result = true, type = CompanyInfo.class)
    public CompletableFuture<RMap> getAllCompanyInfoList(
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    RetResult<List<CompanyInfo>> result = docInfoService.getAllCompanyInfoList(dataSourceId, Long.parseLong(supplierCode));
                    if (result.isSuccess()) {
                        return RMapUtils.success();
                    }
                    return RMapUtils.error(HttpCode.BAD_REQUEST.value(), result.getRetinfo());
                }, getExecutor()
        );
    }

    @RestMapping(name = "getCompanyInfo", auth = true, sort = 801, comment = "获取分厂信息", methods = {"GET", "POST"})
    @WebApiBean(result = true, type = CompanyInfo.class)
    public CompletableFuture<RMap> getCompanyInfo(
            @RestParam(name = "companyId", comment = "分厂id") long companyId,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    CompanyInfo result = docInfoService.getCompanyInfo(companyId, Long.parseLong(supplierCode));
                    return RMapUtils.successV2(result, null, null);
                }, getExecutor()
        );
    }

    @RestMapping(name = "addCompanyInfo", auth = true, sort = 802, logging = 1, comment = "增加分厂信息", methods = {"POST"})
    public CompletableFuture<RMap> addCompanyInfo(
            @RestParam(name = "companyInfo", comment = "分厂信息") CompanyInfo companyInfo,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    RetResult<CompanyInfo> result = docInfoService.addCompanyInfo(companyInfo, currentUser, dataSourceId, Long.parseLong(supplierCode));
                    if (result.isSuccess()) {
                        return RMapUtils.success();
                    }
                    return RMapUtils.error(HttpCode.BAD_REQUEST.value(), result.getRetinfo());
                }, getExecutor()
        );
    }

    @RestMapping(name = "updateCompanyInfo", auth = true, sort = 803, logging = 1, comment = "修改分厂信息", methods = {"POST"})
    public CompletableFuture<RMap> updateCompanyInfo(
            @RestParam(name = "companyInfo", comment = "分厂信息") CompanyInfo companyInfo,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    RetResult<CompanyInfo> result = docInfoService.updateCompanyInfo(companyInfo, currentUser, dataSourceId, Long.parseLong(supplierCode));
                    if (result.isSuccess()) {
                        return RMapUtils.success();
                    }
                    return RMapUtils.error(HttpCode.BAD_REQUEST.value(), result.getRetinfo());
                }, getExecutor()
        );
    }

    @RestMapping(name = "voidCompanyInfo", auth = true, sort = 804, logging = 1, comment = "启用、禁用分厂信息", methods = {"POST"})
    public CompletableFuture<RMap> voidCompanyInfo(
            @RestParam(name = "companyInfo", comment = "分厂信息") CompanyInfo companyInfo,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    RetResult<CompanyInfo> result = docInfoService.voidCompanyInfo(companyInfo, currentUser, dataSourceId, Long.parseLong(supplierCode));
                    if (result.isSuccess()) {
                        return RMapUtils.success();
                    }
                    return RMapUtils.error(HttpCode.BAD_REQUEST.value(), result.getRetinfo());
                }, getExecutor()
        );
    }

    //endregion

}
