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.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_MallUserAddress_Edit 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
    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) {
        MallUserAddress mallUserAddress = jsonConvert.convertFromO(MallUserAddress.class, source.params)
        if (mallUserAddress.id <= 0) return RetResult.<InvokeCallParams> errorT().retinfo("地址id不能为空")
        if (StringUtils.isBlank(mallUserAddress.receiveMan)) return RetResult.<InvokeCallParams> errorT().retinfo("收货人不能为空")
        if (StringUtils.isBlank(mallUserAddress.receivePhone)) return RetResult.<InvokeCallParams> errorT().retinfo("联系电话不能为空")
        if (StringUtils.isBlank(mallUserAddress.province)) return RetResult.<InvokeCallParams> errorT().retinfo("省份不能为空")
        if (StringUtils.isBlank(mallUserAddress.city)) return RetResult.<InvokeCallParams> errorT().retinfo("市不能为空")
        if (StringUtils.isBlank(mallUserAddress.area)) return RetResult.<InvokeCallParams> errorT().retinfo("区不能为空")
        if (StringUtils.isBlank(mallUserAddress.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
        MallUserAddress mallUserAddress = jsonConvert.convertFromO(MallUserAddress.class, source.params)

        RMap params = ERPUtils.ofRMap("id", mallUserAddress.id)
        MallUserAddress dbAddress = mallDao.getMallUserAddress(params, source.dataSourceId, source.supplierCode)

        mallUserAddress.userId = currentUser.id
        if (dbAddress.createBy <= 0) {
            MallUserAddress.create(mallUserAddress,currentUser.id)
        } else {
            mallUserAddress.createBy = dbAddress.createBy
            mallUserAddress.createTime = dbAddress.createTime
        }

        MallUserAddress.update(mallUserAddress, currentUser.id)

        int iData = mallDao.editMallUserAddress(mallUserAddress, dataSourceId, supplierCode)
        if (iData > 0) {
            return RetResult.<InvokeCallResult> successT().result(InvokeCallResult
                    .success()
                    .data(jsonConvert.convertTo(mallUserAddress))
                    .page(null)
                    .collection(null)
            )
        } else RetResult.<InvokeCallResult> errorT().retinfo("修改用户发货地址失败")
    }
}
