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.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_Edit implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    TunaService tunaService

    @Resource
    JsonConvert jsonConvert

    @Resource
    TableIdService tableIdService

    @Resource
    RedisService redisService

    @Resource
    NoSqlKeysService keysService

    @Resource
    MallAdminService mallAdminService

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

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

        MallUserLevel redisLevel = mallAdminService.getRedisMallUserLevel(srcLevel.id,source.supplierCode)
        if (redisLevel == null) {
            return RetResult.<InvokeCallParams>errorT().retinfo("未找到此级别")
        }

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

    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        MallUserLevel userLevel = jsonConvert.convertFrom(MallUserLevel.class, source.params)
        if (userLevel.levelValue == null || userLevel.levelValue <= 0) {
            userLevel.levelValue = BigDecimal.ONE
        }

        // 如果级别不多，就没必要判断重名了
        MallUserLevel redisLevel = mallAdminService.getRedisMallUserLevel(userLevel.id,source.supplierCode)
        redisLevel.levelName = userLevel.levelName
        redisLevel.levelValue = userLevel.levelValue
        redisLevel.checkFileFlag = userLevel.checkFileFlag
        redisLevel.viewPriceDetailFlag = userLevel.viewPriceDetailFlag
        redisLevel.expireDays = userLevel.expireDays
        redisLevel.idDowngradeLevel = userLevel.idDowngradeLevel
        MallUserLevel.update(redisLevel,source.currentUser.id)

        int iData = mallDao.updateMallUserLevel(redisLevel,source.dataSourceId,source.supplierCode)
        if (iData > 0) {
            redisService.hset(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_USER_LEVEL,source.supplierCode), String.valueOf(redisLevel.id),jsonConvert.convertTo(redisLevel))
            return RetResult.<InvokeCallResult>successT()
        }
        return RetResult.<InvokeCallResult>errorT().retinfo(" 修改用户级别失败")
    }

}
