import com.sdtool.business.dao.MallDao
import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.datas.ERPModule
import com.sdtool.common.entity.base.InvokeCallParams
import com.sdtool.common.entity.base.InvokeCallResult
import com.sdtool.common.entity.mall.MallUserAddress
import com.sdtool.common.entity.site.ERPTokenUser
import com.sdtool.common.tool.ERPUtils
import com.alibaba.fastjson2.JSON
import com.dySweetFishPlugin.sql.dao.OperatorWait
import com.dySweetFishPlugin.sql.dao.TunaService
import com.sweetfish.convert.json.JsonConvert
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-02
 * @Content:
 */
class BE_MallUserAddress_Get implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    JsonConvert jsonConvert

    @Resource
    TunaService tunaService

    private MallDao mallDao

    @Override
    String scriptName() {
        return "购物商城-获取地址详情"
    }

    @Override
    ERPModule module() {
        ERPModule.MALL_FRONT
    }

    @Override
    String getPerms(ERPTokenUser currentUser) {
        return ""
    }

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

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

    @Override
    void start(long supplierCode) {
        mallDao = tunaService.generate(MallDao.class)
    }

    @Override
    RetResult<InvokeCallParams> checkExecute(InvokeCallParams source) {
        JsonSlurper jsonSlurper = new JsonSlurper()
        def invokeData = jsonSlurper.parseText(source.params)
        long id = invokeData["id"] as long

        if (id <= 0) return RetResult.error().retinfo("未传入地址id")

        return RetResult.<InvokeCallParams> successT().result(source)
    }

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

        RMap params = ERPUtils.ofRMap("id", invokeData["id"] as long)
        MallUserAddress mallUserAddress = mallDao.getMallUserAddress(params, source.dataSourceId, source.supplierCode)

        return RetResult.<InvokeCallResult> successT().result(InvokeCallResult.success().data(JSON.toJSONString(mallUserAddress)))
    }
}
