package com.lsw.controller;

import com.alibaba.fastjson.JSONArray;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

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

public class BigScreenController extends BaseController {

    //首页
    public void index() {
        render("index.html");
    }

    //部门和产品线数据
    public void gd(){
        String ID = "ZMMMGm5mnGCE4WvWFeI";//部门ID
        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);
                }
            }

        }
        List<Record> prouname = Db.template("prouname").find();
        renderJson(Kv.by("prouname", prouname).set("department",list));
    }

    //试验次数
    public void cs() {
        List<Record> list = Db.template("cs", getParam()).find();
        renderJson(Kv.by("data", list));
    }

    //实验完成率
    public void finish_s() {
        List<Record> list = Db.template("finish_s", getParam()).find();
        renderJson(Kv.by("data", list));
    }

    //待完成试验
    public void no_examine() {
        List<Record> list = Db.template("no_examine", getParam()).find();
        renderJson(Kv.by("data", list));
    }

    //已完成试验
    public void examine() {
        List<Record> list = Db.template("examine", getParam()).find();
        renderJson(Kv.by("data", list));
    }

    //样机改装信息
    public void yj() {
        List<Record> list = Db.template("yj", getParam()).find();
        List<Record> logs = Db.find("select ITEM_TLOG,ITEM_KEY from tlk_myjcs where item_tlog is not null");
        for (Record record : list) {
            int i = 0;
            List<Record> records = logs.stream().filter(r -> r.get("ITEM_KEY").equals(record.get("ITEM_KEY"))).collect(Collectors.toList());
            if (!records.isEmpty()) {
                for (Record re : records) {
                    JSONArray array = JSONArray.parseArray(re.get("ITEM_TLOG"));
                    i += array.size() - 1;
                }
            }
            record.set("num", i);
        }
        renderJson(Kv.by("data", list));
    }

    //已完成试验
    public void yj_list() {
        List<Record> list = Db.template("yj_list", getParam()).find();
        renderJson(Kv.by("data", list));
    }

    //设备资产
    public void zc() {
        List<Record> zc_time = Db.template("zc_time", getParam()).find();
        List<Record> zc_pc = Db.template("zc_pc", getParam()).find();
        Record tj = Db.template("zc").findFirst();
        renderJson(Kv.by("tj", tj).set("zc_time", zc_time).set("zc_pc", zc_pc));
    }

    //样机信息
    public void yj_xx() {
        //:样机数量
        List<Record> yj_sl = Db.template("yj_sl", getParam()).find();
        //样机试验次数
        List<Record> yj_cs = Db.template("yj_cs", getParam()).find();
        renderJson(Kv.by("yj_sl", yj_sl).set("yj_cs", yj_cs));
    }
}