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.dySweetFishPlugin.sql.dao.OperatorWait
import com.dySweetFishPlugin.sql.dao.TunaService
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-08-26
 * @Content:
 */
class BE_MallUser_ModifyUserName implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    TunaService tunaService

    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
    OperatorWait getAWait(InvokeCallParams s) {
        return OperatorWait.SYNC
    }

    @Override
    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        ERPTokenUser currentUser = source.currentUser

        if (currentUser == null) {
            return RetResult.<InvokeCallResult> errorT().retinfo("未登录")
        }

        def jsonSlurper = new JsonSlurper()
        def invokeData = jsonSlurper.parseText(source.params)
        String userName = invokeData["userName"] as String

        if (StringUtils.isBlank(userName)) {
            return RetResult.<InvokeCallResult> errorT().retinfo("用户名不能为空")
        }

        RMap paramMap = new RMap()
        paramMap.put("id", currentUser.id)
        paramMap.put("username", userName)
        mallDao.editMallLoginUserName(paramMap, source.dataSourceId, source.supplierCode)
        return RetResult.<InvokeCallResult> successT().result(InvokeCallResult.success())
    }
}
