package cn.edu.whu.boot.statistics.service;

import cn.edu.whu.boot.statistics.mapper.BasicStatMapper;
import cn.edu.whu.boot.xml.xmlreader.XmlTemplateReader;
import cn.edu.whu.boot.xml.xmlreader.bean.Config;
import cn.edu.whu.boot.xml.xmlreader.bean.Field;
import cn.edu.whu.boot.xml.xmlreader.bean.Table;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Map;

@Service
public class BasicStatService {

    @Value("#{'${XmlFilePath}' + '/'}")
    private String XML_FOLDER;

    private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("#.000");

    @Autowired
    BasicStatMapper basicStatMapper;

    public String getTasks() {
        JSONArray result = new JSONArray();
        result.addAll(basicStatMapper.getTasks());
        return result.toJSONString();
    }

    public String getTables(String tskName) {

        String fileName = basicStatMapper.getTaskXmlName(tskName);
        String path = XML_FOLDER + fileName + ".xml";
        Config config = XmlTemplateReader.read(path);
        Map<String, Table> tables =  config.getTables();

        JSONObject result = new JSONObject(true);
        tables.forEach((k, v)->{
            result.put(k, v.getName());
        });

        return result.toJSONString();

    }

    public String getFields(String tskName, String tblName) {

        String fileName = basicStatMapper.getTaskXmlName(tskName);
        String path = XML_FOLDER + fileName + ".xml";

        Config config = XmlTemplateReader.read(path);
        Map<String, Table> rootTbls =  config.getRootTables();

        Table table = rootTbls.get(tblName);

        Map<String, Field> fields = table.getFields();

        JSONObject result = new JSONObject(true);
        JSONArray statFields = new JSONArray();
        JSONArray groupFields = new JSONArray();

        fields.forEach((k, v)->{
            String dbType = v.getDbType();
            if ("integer".equals(dbType) || "long".equals(dbType) || "double".equals(dbType)) {
                if (null != v.getName()) {
                    JSONObject temp = new JSONObject();
                    temp.put(k, v.getName());
                    statFields.add(temp);
                }
            } else {
                if (null != v.getName()) {
                    JSONObject temp = new JSONObject();
                    temp.put(k, v.getName());
                    groupFields.add(temp);
                }
            }
        });

        result.put("可统计字段", statFields);
        result.put("分组字段", groupFields);
        return result.toJSONString();
    }

    public String stat(String tskName, String tblName, String statField, String groupField) {
        String taskDBName = basicStatMapper.getTaskDBName(tskName);
        tblName = '"' + taskDBName + '"' + "." + '"' + tblName + '"';
        statField = '"' + statField + '"';
        groupField = '"' + groupField + '"';
        return JSONObject.toJSONString(basicStatMapper.stat(tskName, tblName, statField, groupField));
    }
}
