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.site.ERPTokenUser
import com.sdtool.common.tool.ERPUtils
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.rex.RMap

import javax.annotation.Resource

/**
 * @Author: xl Created on 2024-03-27
 * @Content:
 */
class BE_MallUserAddress_Delete implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    TunaService tunaService

    @Resource
    JsonConvert jsonConvert

    private MallDao mallDao

    @Override
    String scriptName() {
        return "购物商城-用户地址删除"
    }

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

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

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

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

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

    @Override
    RetResult<InvokeCallParams> checkExecute(InvokeCallParams source) {
        def jsonSlurper = new JsonSlurper()
        def invokeData = jsonSlurper.parseText(source.params)
        long id = invokeData["id"] as long
        if (id <= 0) return RetResult.<InvokeCallParams> errorT().retinfo("地址id不能为空")
        return RetResult.<InvokeCallParams> successT().result(source)
    }

    @Override
    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        long supplierCode = source.supplierCode
        String dataSourceId = source.dataSourceId
        JsonSlurper jsonSlurper = new JsonSlurper()
        def invokeData = jsonSlurper.parseText(source.params)
        RMap params = ERPUtils.ofRMap("id", invokeData["id"] as long)
        int iData = mallDao.deleteMallUserAddress(params, dataSourceId, supplierCode)
        if (iData > 0) return RetResult.successT()
        else RetResult.<InvokeCallResult> errorT().retinfo("修改用户发货地址失败")
    }
}
