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.dySweetFishPlugin.sql.DBService
import com.dySweetFishPlugin.sql.TableIdService
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 javax.annotation.Resource
/**
 * @Author: xl Created on 2024-03-27
 * @Content:
 */
class BE_SaleTaskAddress_Add implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    TunaService tunaService

    @Resource
    JsonConvert jsonConvert

    @Resource
    TableIdService tableIdService

    @Resource
    DBService dbService

    private MallDao mallDao

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

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

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

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

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

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

    @Override
    RetResult<InvokeCallParams> checkExecute(InvokeCallParams source) {
        SelfTakeAddress selfTakeAddress = jsonConvert.convertFrom(SelfTakeAddress.class, source.params)
        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)

        selfTakeAddress.id = tableIdService.getTableIdMulti("maSelfTakeAddress.id", 1, dataSourceId, String.valueOf(supplierCode))
        SelfTakeAddress.create(selfTakeAddress,currentUser.id)

        int iData = mallDao.addSelfTakeAddress(selfTakeAddress,dataSourceId,supplierCode)

        if (iData > 0) {
            return RetResult.<InvokeCallResult> successT().result(InvokeCallResult
                    .success()
                    .data(jsonConvert.convertTo(selfTakeAddress))
            )
        } else RetResult.<InvokeCallResult> errorT().retinfo("新增自提地址失败")
    }
}
