import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.datas.ERPModule
import com.sdtool.common.entity.base.InvokeCallParams
import com.sdtool.common.entity.base.InvokeCallResult
import com.alibaba.fastjson.JSON
import com.dySweetFishPlugin.entity.system.SQLRunData
import com.dySweetFishPlugin.sql.dao.OperatorWait
import com.dySweetFishPlugin.sql.dao.TunaDaoService
import com.sweetfish.service.RetResult
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger

import javax.annotation.Resource

@SuppressWarnings("unused")
class BE_System_SqlErrorList implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    protected final Logger logger = LogManager.getLogger(this.getClass().getSimpleName())


    @Resource
    private TunaDaoService daoService

    @Override
    String scriptName() {
        return "获取错误的SQL列表"
    }

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

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

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

    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        Map<String, SQLRunData> errorData = daoService.getErrorRunMap()

        def errorList = errorData.collect {
            return [
                    errorKey    : it.key,
                    dataSourceId: it.value.dataSource,
                    sqlExample  : it.value.successList.collect { item -> item.sql }.join("\r\n").take(800) + "..."
            ]
        }

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