import com.sdtool.business.dao.SystemDao
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.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
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardOpenOption

import static com.sweetfish.net.Server.RESNAME_SERVER_ROOT

class BE_SiteContent_Update implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    private JsonConvert jsonConvert

    @Resource
    private RedisService redisService

    @Resource
    private NoSqlKeysService keysService

    @Resource
    private ConfigService configService

    @Resource
    private TunaService tunaService

    private SystemDao systemDao

    @Resource(name = RESNAME_SERVER_ROOT)
    String webRootStr

    @Override
    String scriptName() {
        return "网站内容修改"
    }

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

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

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

    @Override
    void start(long supplierCode) {
        systemDao = tunaService.generate(SystemDao.class)
    }

    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("无效的网站内容信息")
        }

        redisInfo.groupName = siteContent.groupName
        redisInfo.caption = siteContent.caption
        redisInfo.siteLabel = siteContent.siteLabel
        redisInfo.contentUrl = siteContent.contentUrl
        redisInfo.sortIndex = siteContent.sortIndex

        //内容存文件
        String fileName = webRootStr + File.separator +
                "siteContent" + File.separator +
                source.supplierCode + File.separator +
                siteContent.id + ".txt"

        String writeContent = ""
        if (StringUtils.isNotEmpty(siteContent.content)) {
            writeContent = siteContent.content
        }
        Files.write(Paths.get(fileName),
                Collections.singleton(new StringBuilder(writeContent)),
                StandardCharsets.UTF_8,
                StandardOpenOption.CREATE,
                StandardOpenOption.WRITE,
                StandardOpenOption.TRUNCATE_EXISTING)

        siteContent.content = ""
        redisService.hset(keysService.getRedisKey(RedisKeys.KEY_SITE_CONTENT, source.supplierCode), String.valueOf(redisInfo.getId()), redisInfo)

        configService.removeCacheSiteContentInfo(redisInfo.id, source.supplierCode)

        systemDao.updateSiteContent(redisInfo, source.dataSourceId, source.supplierCode)

        return RetResult.<InvokeCallResult> successT().result(
                InvokeCallResult.success().data("")
        )
    }
}
