package gcbatch.admin.executors;

import gcbatch.base.RuntimeContext;
import gcbatch.base.cmd.CmdType;
import gcbatch.base.cmd.annotation.GCCmd;
import gcbatch.base.cmd.annotation.GCCmdGroup;
import gcbatch.base.cmd.annotation.GCParam;
import gcbatch.base.cmd.executor.widgets.sql.SqlChartWidget;
import gcbatch.base.cmd.executor.widgets.sql.annotation.*;
import gcbatch.base.cmd.model.chart.ClassicChart;
import gcbatch.base.cmd.model.chart.common.CurrChartDrillParam;
import gcbatch.base.cmd.model.table.GCPagedTable;
import gcbatch.base.exception.BusinessException;
import gcbatch.utils.DateUtil;
import gcbatch.utils.JsonUtil;

import java.util.Map;


@GCSqlChart(
        labelItems = {
                @GCSqlChartLabel(name = "用户", text = "userid||'/'||(select name from gcbatch_user gu where gu.userid=guor.userid)", groupBy = "userId"),
                @GCSqlChartLabel(name = "应用", text = "appName", groupBy = "appName"),
                @GCSqlChartLabel(name = "服务", text = "svcname", groupBy = "svcname"),
                @GCSqlChartLabel(name = "命令", text = "command", groupBy = "command")
        },
        dataItems = {
                @GCSqlChartData(name = "成功次数", text = "sum(CASE retcode WHEN '0' THEN 1 ELSE 0 END)", stackName = "操作次数"),
                @GCSqlChartData(name = "失败次数", text = "sum(CASE retcode WHEN '0' THEN 0 ELSE 1 END)", stackName = "操作次数"),
                @GCSqlChartData(name = "平均耗时", text = "avg(cause)", gridNo = 2)
        },
        tableName = "gcbatch_user_operation_record guor",
        where = "userid is not null and userid!='system'",
        queryParams = @GCSqlQueryParam(
                name = "dateDuration", where = "recordtime >= ?",
                options = {"30", "180", "365"},
                optionDisplayNames = {"月", "半年", "年"},
                defaultValue = "30"
        ),
        chartContextMenuCmds = @GCChartCmd(name = "detail")
)
    public class UserStats extends SqlChartWidget {
    public UserStats(RuntimeContext ctx, String baseUrl, String tableConnName) throws BusinessException {
        super(ctx, baseUrl, tableConnName);
        userOperationRecordTable = new UserOperationRecordTable(ctx, baseUrl + "." + userOperationRecordTable);
    }

    @GCCmdGroup
    public UserOperationRecordTable userOperationRecordTable;

    @GCCmd(type = CmdType.info)
    public final ClassicChart main(
            @GCParam(options = {"30", "180", "365"}, optionDisplayNames = {"月", "半年", "年"}, defaultValue = "30") String dateDuration,
            @GCParam(name = "/dynamicParams") Map<String, Object> inputParams) throws BusinessException {
        inputParams.put("dateDuration", DateUtil.getAddDate(Integer.parseInt(dateDuration) * -1, "yyyy-MM-dd") + " 00:00:00");
        return main(inputParams);
    }

    @GCCmd(displayName = "明细记录", type = CmdType.info)
    public GCPagedTable detail(CurrChartDrillParam _currChartDrillParam, String _selectedLabel, String _selectedName) throws BusinessException {
        log.info(JsonUtil.toJson(_currChartDrillParam, true));
        int idx = _currChartDrillParam.getCurrLevelIdx();
        if(idx == 0) {
            return userOperationRecordTable.main(_selectedLabel.substring(0, _selectedLabel.indexOf("/")), null, null,  null, null);
        }

        if(idx == 1) {
            String userText = _currChartDrillParam.getCurrValues()[0];
            return userOperationRecordTable.main(userText.substring(0, userText.indexOf("/")), null, _selectedLabel,  null, null);
        }
        if(idx == 2) {
            String userText = _currChartDrillParam.getCurrValues()[0];
            return userOperationRecordTable.main(userText.substring(0, userText.indexOf("/")), null, _currChartDrillParam.getCurrValues()[1],  _selectedLabel, null);
        }
        if(idx == 3) {
            String userText = _currChartDrillParam.getCurrValues()[0];
            return userOperationRecordTable.main(userText.substring(0, userText.indexOf("/")), null, _currChartDrillParam.getCurrValues()[1],  _currChartDrillParam.getCurrValues()[2], _selectedLabel);
        }

        throw new BusinessException("12419", "参数异常！");
    }
}