import com.sdtool.business.dao.MallDao
import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.api.NoSqlKeysService
import com.sdtool.common.api.mall.MallAdminService
import com.sdtool.common.datas.ERPModule
import com.sdtool.common.datas.RedisKeys
import com.sdtool.common.entity.base.InvokeCallParams
import com.sdtool.common.entity.base.InvokeCallResult
import com.sdtool.common.entity.mall.MallUser
import com.dySweetFishPlugin.redis.RedisService
import com.dySweetFishPlugin.sql.dao.OperatorWait
import com.dySweetFishPlugin.sql.dao.TunaService
import com.dySweetFishPlugin.tool.crypto.EncryptUtil
import com.sweetfish.convert.json.JsonConvert
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_MallAdmin_ModifyUserName implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {
    @Resource
    TunaService tunaService

    @Resource
    JsonConvert jsonConvert

    @Resource
    MallAdminService mallAdminService

    @Resource
    RedisService redisService

    @Resource
    NoSqlKeysService keysService

    private MallDao mallDao

    @Override
    String scriptName() {
        return "商城后台8080-修改注册用户昵称"
    }

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

    @Override
    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        def jsonSlurper = new JsonSlurper()
        def invokeData = jsonSlurper.parseText(source.params)
        long idMallUser = invokeData["id"] as long
        String userName = invokeData["userName"] as String

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

        MallUser mallUser = mallAdminService.getRedisMallUser(idMallUser, source.supplierCode)
        if (mallUser == null) {
            return RetResult.<InvokeCallResult> errorT().retinfo("无效的用户信息")
        }

        mallUser.userName = userName

        redisService.hset(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_User, source.supplierCode), String.valueOf(mallUser.getId()), mallUser)

        RMap paramMap = new RMap()
        paramMap.put("id", idMallUser)
        paramMap.put("username", userName)
        mallDao.editMallLoginUserName(paramMap, source.dataSourceId, source.supplierCode)

        return RetResult.<InvokeCallResult> successT().result(InvokeCallResult.success())
    }
}
