import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.datas.ERPModule
import com.sdtool.common.datas.LocalTypeToken
import com.sdtool.common.datas.TokenUserFrom
import com.sdtool.common.entity.base.InvokeCallParams
import com.sdtool.common.entity.base.InvokeCallResult
import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.JSONObject
import com.dySweetFishPlugin.sql.dao.OperatorWait
import com.sweetfish.service.RetResult
import groovy.json.JsonSlurper

import javax.annotation.Resource

class BE_SiteConfig_Read implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource(name = "APP_HOME")
    private String appHome

    JSONObject siteConfigData

    @Override
    String scriptName() {
        return "读取网站配置"
    }

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

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

    @Override
    void start(long supplierCode) {
        def siteConfigFile = new File(appHome + File.separator +
                "conf" + File.separator +
                "script" + File.separator +
                supplierCode + File.separator +
                "config" + File.separator +
                "default_SiteConfig.json")

        if (siteConfigFile.exists()) {
            String siteContent = siteConfigFile.getText("UTF-8")
            siteConfigData = JSON.parseObject(siteContent)
        } else {
            siteConfigData = new JSONObject()
        }

        //tt-2024-03-06 这里不太好清缓存，放redis里面不太舒服，有种办法是在保存的时候重载这个read脚本估计可行，待测试
    }

    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        Map<String, String> invokeData = JSON.parseObject(source.params, LocalTypeToken.fastJsonMapString)

        if ((invokeData != null) && (invokeData.containsKey("siteHost"))) {
            String fileName = invokeData["siteHost"] + "_SiteConfig.json"

            def userSiteConfigFile = new File(appHome + File.separator +
                    "conf" + File.separator +
                    "script" + File.separator +
                    source.supplierCode + File.separator +
                    "config" + File.separator +
                    fileName)
            if (userSiteConfigFile.exists()) {
                String siteContent = userSiteConfigFile.getText("UTF-8")
                JSONObject userSteConfigData = JSON.parseObject(siteContent)
                return RetResult.<InvokeCallResult> successT().result(
                        InvokeCallResult.success().data(userSteConfigData.toJSONString())
                )
            }
        }

        if ((source.currentUser != null) && (source.currentUser.userFrom == TokenUserFrom.PC_MALL_FRONT.value)) {
            String fileName = source.currentUser.id + "_SiteConfig.json"

            def userSiteConfigFile = new File(appHome + File.separator +
                    "conf" + File.separator +
                    "script" + File.separator +
                    source.supplierCode + File.separator +
                    "config" + File.separator +
                    fileName)
            if (userSiteConfigFile.exists()) {
                String siteContent = userSiteConfigFile.getText("UTF-8")
                JSONObject userSteConfigData = JSON.parseObject(siteContent)
                return RetResult.<InvokeCallResult> successT().result(
                        InvokeCallResult.success().data(userSteConfigData.toJSONString())
                )
            }
        }

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


