package com.sdtool.webcore.rest;

import com.sdtool.common.api.AddressService;
import com.sdtool.common.base.BaseService;
import com.sdtool.common.datas.ERPHeader;
import com.sdtool.common.datas.HttpCode;
import com.sdtool.common.entity.base.ProcessStringItem;
import com.sdtool.common.entity.site.ERPTokenUser;
import com.sdtool.common.entity.system.ParseAreaResult;
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.rex.RMap;

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

@AutoLoad(false)
@Local
@RestService(name = "addr", comment = "地址服务")
@SuppressWarnings({"unused", "rawtypes"})
public class AddressRest extends BaseService {

    @Resource
    AddressService addressService;

    @RestMapping(name = "parseAddress", auth = true, sort = 1, logging = 1, comment = "地址解析", methods = {"POST"})
    @WebApiBean(type = ParseAreaResult.class, result = true)
    public CompletableFuture<RMap> parseAddress(
            @RestParam(name = "address", comment = "地址") String address,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    RetResult<List<ParseAreaResult>> infoResult = addressService.parseAddress(address, currentUser, dataSourceId, Long.parseLong(supplierCode));
                    if (infoResult.isSuccess()) {
                        return RMapUtils.successV2(infoResult.getResult(), null, null);
                    } else {
                        return RMapUtils.error(HttpCode.INTERNAL_SERVER_ERROR.value(), infoResult.getRetinfo());
                    }
                }, getExecutor());
    }

    @RestMapping(name = "parseAddressBaidu", auth = true, sort = 2, logging = 1, comment = "地址解析2", methods = {"POST"})
    @WebApiBean(type = ParseAreaResult.class, result = true)
    public CompletableFuture<RMap> parseAddressBaidu(
            @RestParam(name = "address", comment = "地址") String address,
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    RetResult<ParseAreaResult> infoResult = addressService.parseAddressBaidu(address, currentUser, dataSourceId, Long.parseLong(supplierCode));
                    if (infoResult.isSuccess()) {
                        return RMapUtils.successV2(infoResult.getResult(), null, null);
                    } else {
                        return RMapUtils.error(HttpCode.INTERNAL_SERVER_ERROR.value(), infoResult.getRetinfo());
                    }
                }, getExecutor());
    }

    @RestMapping(name = "mapAddressUpdate", auth = true, sort = 3, comment = "行政区划更新", methods = {"POST"})
    public CompletableFuture<RMap> mapAddressUpdate(
            @RestParam(name = "&", comment = "登录用户，无需传入") ERPTokenUser currentUser,
            @RestHeader(name = ERPHeader.HTTPHEADER_DATASOURCE) String dataSourceId,
            @RestHeader(name = ERPHeader.HTTPHEADER_SUPPLIER) String supplierCode) {
        return CompletableFuture.supplyAsync(
                () -> {
                    RetResult<ProcessStringItem> infoResult = addressService.mapAddressUpdate(currentUser, dataSourceId, Long.parseLong(supplierCode));
                    if (infoResult.isSuccess()) {
                        return RMapUtils.successV2(infoResult.getResult(), null, null);
                    } else {
                        return RMapUtils.error(HttpCode.INTERNAL_SERVER_ERROR.value(), infoResult.getRetinfo());
                    }
                }, getExecutor());
    }
}
