package com.cnd3b.restcontroller.system.system;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.db.DBConnectPool;
import com.cnd3b.common.parameter.parameter;
import com.cnd3b.common.Controller;
import p2.p2server.DBConnProvider;
import p2.p2server.P2Server;
import p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class getCallMethodStatistics extends Controller {
    public getCallMethodStatistics(JSONObject content) {
        super(content);
    }

    public String query() {
        JSONObject result = new JSONObject();
        result.put("当前数据库链接统计", getDBConnectPool());
        result.put("方法请求统计", getCallMethodStatistics());
        return result.toString();
    }

    public JSONArray getCallMethodStatistics() {
        JSONArray array = new JSONArray();
        for (String key : parameter.callmethodTimes.keySet()) {
            JSONObject object = new JSONObject();
            object.put("方法名", key);
            long callmethodTimes = parameter.callmethodTimes.get(key);
            object.put("请求总数", callmethodTimes);
            long callmethod_fromcacheTimes = parameter.callmethod_fromcacheTimes.containsKey(key) ? parameter.callmethod_fromcacheTimes.get(key) : 0L;
            object.put("数据库获取次数", callmethodTimes - callmethod_fromcacheTimes);
            object.put("缓存获取次数", callmethod_fromcacheTimes);
            object.put("数据库查询平均耗时", parameter.callmethodTimeLong.get(key));
            object.put("综合耗时", (parameter.callmethodTimeLong.get(key) * (callmethodTimes - callmethod_fromcacheTimes)) / callmethodTimes);
            object.put("数据库查询最新耗时", parameter.callmethodLastTimeLong.get(key));
            object.put("最新查询时间", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(parameter.lastcallmethodtime.get(key)));
            array.add(object);
        }
        return array;
    }

    public JSONArray getDBConnectPool() {
        JSONArray array = new JSONArray();
        for (String key : DBConnectPool.POOLS_MAP.keySet()) {
            JSONObject object = new JSONObject();
            object.put("数据库", key);
            object.put("链接数", DBConnectPool.POOLS_MAP.get(key).size());
            array.add(object);
        }
        DBConnProvider dbConnProvider = P2Server.getP2Server().getDBConnProvider();
        JSONObject object = new JSONObject();
        object.put("数据库", dbConnProvider.getDatabaseProductName());
        object.put("链接总数", dbConnProvider.getDBConnTotal());
        object.put("正在使用链接", dbConnProvider.getDBConnUsed());
        object.put("空闲链接", dbConnProvider.getDBConnFree());
        array.add(object);
        return array;
    }


    public String addRequestTimes() throws P2Exception {
        if (!"".equalsIgnoreCase(siteid)) {
            String fappname = content.getString("fappname");
            Rows rows = dbConnect.runSqlQuery("select siteid from tapprequest where siteid='" + siteid + "' and fappname='" + fappname + "' and convert(varchar(10),fdate,120)=convert(varchar(10),getdate(),120)");
            if (rows.isEmpty()) {
                PaoSetRemote tapprequestSet = getP2ServerSystemPaoSet("tapprequest");
                PaoRemote tapprequest = tapprequestSet.addAtEnd();
                tapprequest.setValue("SITEID", siteid, 11L);
                tapprequest.setValue("fappname", fappname, 11L);
                tapprequest.setValue("fdate", Calendar.getInstance().getTime(), 11L);
                tapprequest.setValue("ftimes", 1, 11L);
                tapprequestSet.save();
            } else {
                dbConnect.runSqlUpdate("update tapprequest set ftimes=ftimes+1 where siteid='" + siteid + "' and fappname='" + fappname + "' and convert(varchar(10),fdate,120)=convert(varchar(10),getdate(),120)");
            }
        }
        return getSucReturnObject().toString();
    }
}
