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

import javax.annotation.Resource

/**
 * @Author: xl Created on 2024-04-01
 * @Content:
 */
class BE_PosterLocation_Get implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    JsonConvert jsonConvert

    @Resource
    RedisService redisService

    @Resource
    NoSqlKeysService keysService

    @Resource
    SupplierInitService supplierService

    @Override
    String scriptName() {
        return "商城后台-广告位详情"
    }

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

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

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

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

        String redisStr = redisService.hget(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_POSTER_LOCATION, source.supplierCode),
                String.valueOf(id))

        if (StringUtils.isNotBlank(redisStr)) {
            PosterLocation posterLocation = jsonConvert.convertFrom(PosterLocation.class, redisStr)
            posterLocation.imageUrl = supplierService.getFileDownloadUrl(posterLocation.posterLocationImage,"",source.dataSourceId,source.getSupplierCode())
            posterLocation.imageWebUrl = supplierService.getFileWebDownloadUrl(posterLocation.posterLocationImage,"",source.dataSourceId,source.supplierCode)
            return RetResult.<InvokeCallResult> successT().result(InvokeCallResult
                    .success()
                    .data(jsonConvert.convertTo(posterLocation))
                    .page(null)
                    .collection(null)
            )
        } else return RetResult.error().retinfo("未找到此广告位置")
    }
}
