package com.example.bpa.app;

import com.example.bpa.Model.IRunT;
import com.example.bpa.Model.IThread;
import com.example.bpa.db.QueryDB;
import com.example.bpa.db.mode.BPA_SILOS;
import com.example.bpa.helper.MessageLog;
import com.example.bpa.helper.ThreadManager;
import com.example.bpa.view.mode.ResGoodsRecipe;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

public class BusinessServer {
    private static volatile BusinessServer _instance;

    public static BusinessServer get() {
        if (_instance == null)
            _instance = new BusinessServer();
        return _instance;
    }

    private BusinessServer() {
    }

    ConcurrentLinkedQueue<String> Commoditys = new ConcurrentLinkedQueue<String>();

    /**
     * 添加配方
     *
     * @param info 配方信息
     */
    public void AddCommodity(String info) {
        Commoditys.offer(info);
    }

    public void Start() {
        DeviceData.Get().TestInit();
        ThreadManager.Get().StartLong("配方执行业务流程", true, new IThread() {
            @Override
            public void Run() throws InterruptedException {
                while (Commoditys.size() > 0) {
                    String GoodId = Commoditys.poll();
                    ArrayList<ResGoodsRecipe> goods = QueryDB.GetGoodsSrecipeList(GoodId);
                    LinkedHashMap<Integer, List<ResGoodsRecipe>> rgrs = new LinkedHashMap<>();

                    //更具步骤分组
                    for (int i = 0; i < goods.size(); i++) {
                        if (!rgrs.containsKey(goods.get(i).sort))
                            rgrs.put(goods.get(i).sort, new ArrayList<>());
                        rgrs.get(goods.get(i).sort).add(goods.get(i));
                    }

//                    rgrs.forEach((x, y) -> {
//
//                    });


                    for (int i = 0; i < rgrs.size(); i++) {
                        List<Integer> ChNums = new ArrayList<>();
                        for (int m = 0; m < rgrs.get(i).size(); m++) {
                            //获取料仓信息
                            List<BPA_SILOS> bss = QueryDB.GetSolisByMaterialID(rgrs.get(i).get(m).materialID);
                            for (int x = 0; x < bss.size(); x++) {
//                                if (DeviceData.Get().getCallCurrentWeight(bss.get(x).num) >= (float) rgrs.get(i).get(m).value) {
                                MessageLog.ShowInfo("下发" + bss.get(x).num + "号料仓PLC控制命令,需求量:=" + rgrs.get(i).get(m).value);
                                ChNums.add(bss.get(x).num);
                                DeviceData.Get().testAddComm(bss.get(x).num);
//                                }
                            }
                        }

                        List<Integer> tempComplete = new ArrayList<>();
                        DeviceData.Get().OnChargeMixtureComNotPar = new IRunT() {
                            @Override
                            public void Run(Object o) {
                                tempComplete.add(Integer.parseInt(o.toString()));
                                MessageLog.ShowInfo("完成=" + o);
                            }
                        };

                        while (ChNums.size() < tempComplete.size()) {
                            Thread.sleep(100);
                        }
                        MessageLog.ShowInfo("结束=");
                    }

                }
                Thread.sleep(1000);
            }

            @Override
            public void RunComplete() throws InterruptedException {

            }
        });
    }

}
