package com.lsw.controller;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.lsw.utils.StringUtil;
import com.lsw.utils.TimeUtil;

import java.util.List;
import java.util.stream.Collectors;

public class ReportController extends BaseController {

    public void index() {
        String op = getPara("op", "finish");
        String ID = getPara("ID", "ZMMMGm5mnGCE4WvWFeI");//部门ID
        set("look", getPara("look"));
        set("ip", getPara("ip", "192.168.0.251"));
        set("DOMAINID", getPara("DOMAINID", "uumsxrgm1KEEqSZIoHs"));
        set("ID", ID); //部门ID
        set("time_name", getPara("time_name", "CLGF220叉装车"));

        set("begin", "2021-06-01");//TimeUtil.getDay("firstDay")
        set("end", TimeUtil.getDay("lastDay"));//TimeUtil.getDay("lastDay")
        List<Record> department_list = Db.template("department", Kv.by("ID", ID)).find();
        List<Record> list = department_list.stream().filter(r -> r.get("ID").equals(ID)).collect(Collectors.toList());
        for (Record record : list) {
            set("value", record.getStr("ID"));
            //二级
            List<Record> list2 = department_list.stream().filter(r -> r.get("SUPERIOR") != null && r.get("SUPERIOR").equals(record.get("ID"))).collect(Collectors.toList());
            record.set("children", list2);
            for (Record record2 : list2) {
                //三级
                List<Record> list3 = department_list.stream().filter(r -> r.get("SUPERIOR") != null && r.get("SUPERIOR").equals(record2.get("ID"))).collect(Collectors.toList());
                record2.set("children", list3);
                for (Record record3 : list3) {
                    //三级
                    List<Record> list4 = department_list.stream().filter(r -> r.get("SUPERIOR") != null && r.get("SUPERIOR").equals(record3.get("ID"))).collect(Collectors.toList());
                    record3.set("children", list4);
                }
            }

        }
        if (op.equals("data") || op.equals("change")||op.equals("forms")||op.equals("finish")||op.equals("type")) {
            List<Record> prouname = Db.template("prouname").find();
            set("prouname", JsonKit.toJson(prouname));
        }
        if (op.equals("forms")) {
            List<Record> prouname = Db.template("prouname").find();

            set("prouname", JsonKit.toJson(prouname));
        }
        set("department", JsonKit.toJson(list));
        render(op + ".html");
    }

    //实验完成率
    public void finish() {
        long start = System.currentTimeMillis();
        List<Record> list = Db.template("finish", getParam()).find();
        long end = System.currentTimeMillis();
        renderJson(Kv.by("data", list).set("time", StringUtil.mul((end - start), (float) 0.001) + "s"));
    }

    //实验完成率
    public void finish_list() {
        long start = System.currentTimeMillis();
        List<Record> list = Db.template("finish_list", getParam()).find();
        long end = System.currentTimeMillis();
        renderJson(Kv.by("data", list).set("time", StringUtil.mul((end - start), (float) 0.001) + "s"));
    }

    //各流程耗时
    public void process() {
        long start = System.currentTimeMillis();
        List<Record> list = Db.template("process", getParam()).find();
        long end = System.currentTimeMillis();
        renderJson(Kv.by("data", list).set("time", StringUtil.mul((end - start), (float) 0.001) + "s"));
    }

    //试验类型统计
    public void type() {
        long start = System.currentTimeMillis();
        List<Record> list = Db.template("type", getParam()).find();
        long end = System.currentTimeMillis();
        renderJson(Kv.by("data", list).set("time", StringUtil.mul((end - start), (float) 0.001) + "s"));
    }

    //试验类型统计列表
    public void type_list() {
        long start = System.currentTimeMillis();
        List<Record> list = Db.template("type_list", getParam()).find();
        long end = System.currentTimeMillis();
        renderJson(Kv.by("data", list).set("time", StringUtil.mul((end - start), (float) 0.001) + "秒"));
    }

    //样机试验统计
    public void model() {
        long start = System.currentTimeMillis();
        List<Record> list = Db.template("model", getParam()).find();
        long end = System.currentTimeMillis();
        renderJson(Kv.by("data", list).set("time", StringUtil.mul((end - start), (float) 0.001) + "s"));
    }

    //样机试验统计列表
    public void model_list() {
        long start = System.currentTimeMillis();
        List<Record> list = Db.template("model_list", getParam()).find();
        long end = System.currentTimeMillis();
        renderJson(Kv.by("data", list).set("time", StringUtil.mul((end - start), (float) 0.001) + "秒"));
    }

    //产品线统计
    public void product() {
        long start = System.currentTimeMillis();
        List<Record> list = Db.template("product", getParam()).find();
        long end = System.currentTimeMillis();
        renderJson(Kv.by("data", list).set("time", StringUtil.mul((end - start), (float) 0.001) + "s"));
    }

    //产品线统计列表
    public void product_list() {
        long start = System.currentTimeMillis();
        List<Record> list = Db.template("product_list", getParam()).find();
        long end = System.currentTimeMillis();
        renderJson(Kv.by("data", list).set("time", StringUtil.mul((end - start), (float) 0.001) + "s"));
    }

    //数据查询与统计
    public void data() {
        long start = System.currentTimeMillis();
        List<Record> list = Db.template("data", getParam()).find();
        long end = System.currentTimeMillis();
        renderJson(Kv.by("data", list).set("time", StringUtil.mul((end - start), (float) 0.001) + "s"));
    }

    //时间线统计
    public void time() {
        long start = System.currentTimeMillis();
        List<Record> list = Db.template("time", getParam()).find();
        List<Record> times = Db.template("times", getParam()).find();
        Kv kv = getParam();
        for (Record record : list) {
            record.set("times", times.stream().filter(r -> r.get("ITEM_VERSION").equals(record.getStr("ITEM_VERSION"))).collect(Collectors.toList()));
            kv.set("item_key", record.getStr("ITEM_KEY"));
            List<Record> list2 = Db.template("time_list", kv).find();
            for (Record record2 : list2) {
                record2.set("ITEM_VERSION", record.getStr("ITEM_VERSION"));
            }
            record.set("children", list2);
        }
        long end = System.currentTimeMillis();
        renderJson(Kv.by("data", list).set("time", StringUtil.mul((end - start), (float) 0.001) + "s"));
    }

    //数据查询与统计
    public void change() {
        long start = System.currentTimeMillis();
        List<Record> list = Db.template("change", getParam()).find();
        long end = System.currentTimeMillis();
        renderJson(Kv.by("data", list).set("time", StringUtil.mul((end - start), (float) 0.001) + "s"));
    }

    //样机选择
    public void prou() {
        long start = System.currentTimeMillis();
        List<Record> list = Db.template("prou", getParam()).find();
        long end = System.currentTimeMillis();
        renderJson(Kv.by("data", list).set("time", StringUtil.mul((end - start), (float) 0.001) + "s"));
    }

    //样机报表
    public void getYj() {
        long start = System.currentTimeMillis();
        //样机
        Record yj = Db.template("getYj", getParam()).findFirst();
        //委托单
        List<Record> wtd = Db.template("getWtt", getParam()).find();
        long end = System.currentTimeMillis();
        Record r = new Record();
        r.set("yj", yj);
        r.set("wtd", wtd);
        r.set("time", StringUtil.mul((end - start), (float) 0.001) + "s");
        renderJson(r);
    }

    //委托单点击联动
    public void getWtd() {
        List<Record> list = Db.template("getWttld", getParam()).find();
        renderJson(Kv.by("data", list));
    }
}