import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.api.SystemService
import com.sdtool.common.datas.ERPModule
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.base.ProcessMapItem
import com.alibaba.fastjson2.JSON
import com.dySweetFishPlugin.sql.dao.OperatorWait
import com.sweetfish.service.RetResult
import groovy.json.JsonSlurper
import org.apache.commons.lang3.StringUtils
import org.rex.RMap

import javax.annotation.Resource

/**
 * @Author: xl Created on 2024-04-07
 * @Content:
 */
class BE_MallParseAddress implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    SystemService systemService

    @Override
    String scriptName(){
        return "购物商城-地址识别"
    }

    @Override
    ERPModule module() {
        return ERPModule.MALL_FRONT
    }

    @Override
    boolean needLogin(InvokeCallParams source) {
        return true
    }

    @Override
    OperatorWait getAWait(InvokeCallParams s) {
        return OperatorWait.SYNC
    }

    @Override
    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        JsonSlurper jsonSlurper = new JsonSlurper()
        def invokeData = jsonSlurper.parseText(source.params)
        String inputAddress = invokeData["address"]

        if (StringUtils.isBlank(inputAddress)) return RetResult.error().retinfo("地址不能为空")

        RMap addressMap = systemService.parseAddress(inputAddress,source.currentUser,source.dataSourceId,source.supplierCode)
        if (addressMap.getInt("httpCode") == HttpCode.INTERNAL_SERVER_ERROR.value()) {
            return RetResult.error().retinfo("地址识别失败")
        } else {
            return RetResult.<InvokeCallResult> successT().result(InvokeCallResult
                    .success()
                    .data(JSON.toJSONString(addressMap.get("data")))
                    .page(null)
            )
        }
    }

}
