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.SelfTakeAddress
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 org.apache.commons.lang3.StringUtils
import org.rex.RMap

import javax.annotation.Resource
/**
 * @Author: xl Created on 2024-03-27
 * @Content:
 */
class BE_SaleTaskAddress_Edit implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    TunaService tunaService

    @Resource
    JsonConvert jsonConvert

    private MallDao mallDao

    @Override
    String scriptName() {
        return "商城后台-自提地址编辑"
    }

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

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

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

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

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

    @Override
    RetResult<InvokeCallParams> checkExecute(InvokeCallParams source) {
        SelfTakeAddress selfTakeAddress = jsonConvert.convertFrom(SelfTakeAddress.class, source.params)
        if (selfTakeAddress.id <= 0) return RetResult.<InvokeCallParams> errorT().retinfo("地址id不能为空")
        if (StringUtils.isBlank(selfTakeAddress.province)) return RetResult.<InvokeCallParams> errorT().retinfo("省份不能为空")
        if (StringUtils.isBlank(selfTakeAddress.city)) return RetResult.<InvokeCallParams> errorT().retinfo("市不能为空")
        if (StringUtils.isBlank(selfTakeAddress.area)) return RetResult.<InvokeCallParams> errorT().retinfo("区不能为空")
        if (StringUtils.isBlank(selfTakeAddress.address)) return RetResult.<InvokeCallParams> errorT().retinfo("发货地址不能为空")

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

    @Override
    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        String dataSourceId = source.dataSourceId
        long supplierCode = source.supplierCode
        ERPTokenUser currentUser = source.currentUser
        SelfTakeAddress selfTakeAddress = jsonConvert.convertFrom(SelfTakeAddress.class, source.params)

        RMap params = ERPUtils.ofRMap("id", selfTakeAddress.id)
        SelfTakeAddress dbAddress = mallDao.getSelfTakeAddress(params, source.dataSourceId, source.supplierCode)

        selfTakeAddress.createBy = dbAddress.createBy
        selfTakeAddress.createTime = dbAddress.createTime
        selfTakeAddress.createTimeLong = dbAddress.createTimeLong

        SelfTakeAddress.update(selfTakeAddress, currentUser.id)

        int iData = mallDao.editSelfTakeAddress(selfTakeAddress, dataSourceId, supplierCode)
        if (iData > 0) {
            return RetResult.<InvokeCallResult> successT().result(InvokeCallResult
                    .success()
                    .data(jsonConvert.convertTo(selfTakeAddress))
            )
        } else RetResult.<InvokeCallResult> errorT().retinfo("修改自提地址失败")
    }
}
