import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.api.ConfigService
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.site.SiteContent
import com.dySweetFishPlugin.redis.RedisService
import com.dySweetFishPlugin.sql.dao.OperatorWait
import com.sweetfish.convert.json.JsonConvert
import com.sweetfish.service.RetResult
import org.apache.commons.lang3.StringUtils

import javax.annotation.Resource

class BE_SiteContent_Get implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    private JsonConvert jsonConvert

    @Resource
    private RedisService redisService

    @Resource
    private NoSqlKeysService keysService

    @Resource
    private ConfigService configService

    @Override
    String scriptName() {
        return "网站内容获取详情"
    }

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

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

    private <T> T getRedisDocInfo(String redisKey, String field, Class<T> clazz) {
        String docJson = redisService.hget(redisKey, field)
        if (StringUtils.isNotBlank(docJson)) {
            return jsonConvert.convertFrom(clazz, docJson)
        } else return null
    }

    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        SiteContent siteContent = jsonConvert.convertFrom(SiteContent.class, source.params)

        SiteContent redisInfo = getRedisDocInfo(keysService.getRedisKey(RedisKeys.KEY_SITE_CONTENT, source.supplierCode), String.valueOf(siteContent.id), SiteContent.class)
        if (redisInfo == null) {
            return RetResult.<InvokeCallResult> errorT().retinfo("无效的网站内容信息")
        }

        SiteContent cacheContent = configService.getCacheSiteContentInfo(redisInfo.id, source.supplierCode)
        if (cacheContent != null) {
            redisInfo.content = cacheContent.content
        }

        return RetResult.<InvokeCallResult> successT().result(
                InvokeCallResult.success().data(jsonConvert.convertTo(redisInfo))
        )
    }
}
