import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.api.mall.MallAdminService
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.MallUserLevel
import com.dySweetFishPlugin.sql.dao.OperatorWait
import com.sweetfish.convert.json.JsonConvert
import com.sweetfish.service.RetResult
import groovy.json.JsonSlurper

import javax.annotation.Resource

/**
 * @Author: xl Created on 2024-05-11
 * @Content:
 */
class BE_MallUserLevel_Get implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    JsonConvert jsonConvert

    @Resource
    MallAdminService mallAdminService

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

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

    @Override
    OperatorWait getAWait(InvokeCallParams s) {
        return OperatorWait.AWAIT
    }

    @Override
    boolean needLogin(InvokeCallParams source) {
        return true
    }

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

        MallUserLevel mallUserLevel = mallAdminService.getRedisMallUserLevel(id, source.supplierCode)
        if (mallUserLevel != null) {
            mallUserLevel.downgradeLevelName = ""
            if (mallUserLevel.idDowngradeLevel > 0L) {
                MallUserLevel downLevel = mallAdminService.getRedisMallUserLevel(mallUserLevel.idDowngradeLevel, source.supplierCode)
                if (downLevel != null) {
                    mallUserLevel.downgradeLevelName = downLevel.levelName
                }
            }
            return RetResult.<InvokeCallResult> successT().result(InvokeCallResult.success().data(jsonConvert.convertTo(mallUserLevel)))
        }

        return RetResult.<InvokeCallResult> errorT().retinfo("未找到此用户级别")
    }

}
