import com.sdtool.business.dao.MallDao
import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.api.NoSqlKeysService
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.MallUserLevel
import com.dySweetFishPlugin.redis.RedisService
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-05-11
 * @Content:
 */
class BE_MallUserLevel_Add implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    TunaService tunaService

    @Resource
    JsonConvert jsonConvert

    @Resource
    TableIdService tableIdService

    @Resource
    RedisService redisService

    @Resource
    NoSqlKeysService keysService

    private MallDao mallDao

    @Override
    String scriptName() {
        return "用户级别-增加"
    }

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

    @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) {
        MallUserLevel mallUserLevel = jsonConvert.convertFrom(MallUserLevel.class, source.params)

        if (StringUtils.isBlank(mallUserLevel.levelName)) {
            return RetResult.<InvokeCallParams>errorT().retinfo("级别名称不能为空")
        }

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

    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        MallUserLevel userLevel = jsonConvert.convertFrom(MallUserLevel.class, source.params)
        userLevel.id = tableIdService.getTableIdMulti("maMallUserLevel.id", 1, source.dataSourceId, String.valueOf(source.supplierCode))
        userLevel.voidFlag = 0
        if (userLevel.levelValue == null || userLevel.levelValue <= 0) {
            userLevel.levelValue = BigDecimal.ONE
        }
        MallUserLevel.create(userLevel,source.currentUser.id)

        int iData = mallDao.addMallUserLevel(userLevel, source.dataSourceId, source.supplierCode)
        if (iData > 0) {
            redisService.hset(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_USER_LEVEL, source.supplierCode), String.valueOf(userLevel.id), jsonConvert.convertTo(userLevel))
            return RetResult.<InvokeCallResult> successT()
        }

        RetResult.<InvokeCallResult>errorT().retinfo("增加用户级别失败")
    }

}
