package com.jeeplus.modules.sg.picking.activiti.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sg.picking.activiti.entity.PickIng;
import com.jeeplus.modules.sg.picking.activiti.entity.PickList;
import com.jeeplus.modules.sg.picking.activiti.entity.ShowList;
import com.jeeplus.modules.sg.picking.activiti.service.PickIngService;
import com.jeeplus.modules.sg.picking.activiti.service.ShowListService;
import net.sf.ehcache.util.SetAsList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping(value = "${adminPath}/picking/showlist")
public class ShowListController extends BaseController {
    @Autowired
    private ShowListService showListService;

    /**
     * 物料清单显示
     */
    @ResponseBody
    @RequestMapping(value = "data")
    public Map<String, Object> data(ShowList showList, String item, HttpServletRequest request, HttpServletResponse response, Model model) {
        ArrayList<String> objects = new ArrayList<>();
        ArrayList<String> findList = new ArrayList<>();

        //返回给页面的list
        List<ShowList> myList = new ArrayList<>();
        if (item != null && !"".equals(item)) {
            String[] split = item.split(";");
            //回显list
            List<ShowList> returnList = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                objects.add(split[i]);
            }
            for (String s : objects) {
                String[] split1 = s.split(":");
                ShowList myShow = new ShowList();
                myShow.setId(split1[0]);
                String s1 = split1[1];
                myShow.setNumber(Double.parseDouble(s1));
                findList.add(split1[0]);
                myList.add(myShow);
            }
            showList.setFindList(findList);
            Page<ShowList> page = showListService.findPage(new Page<ShowList>(request, response), showList);
            //查询出来所有的模块清单数据
            List<ShowList> list = page.getList();
            //前端传来选择的模块与数量
            for (ShowList my : myList) {
                //循环遍历
                for (ShowList showList1 : list) {
                    //选择的模块与查出来的一致
                    if (my.getId().equals(showList1.getBlockId())) {
                        Double mdSingleWeight = showList1.getMdSingleWeight();
                        Double number = my.getNumber();
                        Double mdCount = showList1.getMdCount();
                        Double i = mdSingleWeight * number;
                        BigDecimal b1 = new BigDecimal(Double.toString(number));
                        BigDecimal b2 = new BigDecimal(Double.toString(mdCount));
                        double v = b1.multiply(b2).doubleValue();
                        ShowList newShowList = new ShowList();
                        newShowList.setMaterialCode(showList1.getMaterialCode());
                        newShowList.setMaterialDescription(showList1.getMaterialDescription());
                        newShowList.setExtensionDescription(showList1.getExtensionDescription());
                        newShowList.setMdUnits(showList1.getMdUnits());
                        newShowList.setMdTheParty(showList1.getMdTheParty());
                        newShowList.setMdText(showList1.getMdText());
                        newShowList.setMdTotalAmount(v);
                        newShowList.setMdSingleWeight(showList1.getMdSingleWeight());
                        BigDecimal b = new BigDecimal(i);
                        BigDecimal a = new BigDecimal(1000);
                        BigDecimal divide = b.divide(a, 4, BigDecimal.ROUND_HALF_UP);
                        newShowList.setMdTotalWeight(Double.valueOf(divide.toString()));
                        newShowList.setBlockId(showList1.getBlockId());
                        returnList.add(newShowList);
                    }
                }
            }
            List<ShowList> finalList = new ArrayList<>();
            boolean flag;
            for (ShowList turn : returnList) {
                flag = true;
                for (ShowList list1 : finalList) {
                    if (turn.getMaterialCode().equals(list1.getMaterialCode()) && turn.getMaterialDescription().equals(list1.getMaterialDescription()) && turn.getExtensionDescription().equals(list1.getExtensionDescription())) {
                        Double mdTotalAmount = list1.getMdTotalAmount();
                        Double mdTotalAmount1 = turn.getMdTotalAmount();
                        BigDecimal b1 = new BigDecimal(Double.toString(mdTotalAmount));
                        BigDecimal b2 = new BigDecimal(Double.toString(mdTotalAmount1));
                        double v = b1.add(b2).doubleValue();
                        Double mdTotalWeight = list1.getMdTotalWeight();
                        Double mdTotalWeight1 = turn.getMdTotalWeight();
                        BigDecimal a1 = new BigDecimal(Double.toString(mdTotalWeight));
                        BigDecimal a2 = new BigDecimal(Double.toString(mdTotalWeight1));
                        double b = a1.add(a2).doubleValue();
                        list1.setMdTotalAmount(v);
                        list1.setMdTotalWeight(b);
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    showList = new ShowList();
                    showList.setMaterialCode(turn.getMaterialCode());
                    showList.setMaterialDescription(turn.getMaterialDescription());
                    showList.setExtensionDescription(turn.getExtensionDescription());
                    showList.setMdUnits(turn.getMdUnits());
                    showList.setMdTheParty(turn.getMdTheParty());
                    showList.setMdText(turn.getMdText());
                    showList.setMdTotalAmount(turn.getMdTotalAmount());
                    showList.setMdSingleWeight(turn.getMdSingleWeight());
                    showList.setMdTotalWeight(turn.getMdTotalWeight());
                    showList.setBlockId(turn.getBlockId());
                    finalList.add(showList);
                }
            }
            Collections.sort(finalList);
            page.setList(finalList);
            return getBootstrapData(page);
        } else {
            findList.add("没有数据");
            showList.setFindList(findList);
            Page<ShowList> page = showListService.findPage(new Page<ShowList>(request, response), showList);
            return getBootstrapData(page);
        }
    }


    /**
     * 将模块清单的数据获取计算，保存入库
     */
    @ResponseBody
    @RequestMapping(value = "saveData")
    public AjaxJson saveData(ShowList showList, String item,String projectName, Model model) throws Exception {
        ArrayList<String> objects = new ArrayList<>();
        ArrayList<String> findList = new ArrayList<>();
        AjaxJson j = new AjaxJson();
        //返回给页面的list
        List<ShowList> myList = new ArrayList<>();
        if (item != null && !"".equals(item)) {
            String[] split = item.split(";");
            //回显list
            List<ShowList> returnList = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                objects.add(split[i]);
            }
            for (String s : objects) {
                String[] split1 = s.split(":");
                ShowList myShow = new ShowList();
                myShow.setId(split1[0]);
                String s1 = split1[1];
                myShow.setNumber(Double.parseDouble(s1));
                findList.add(split1[0]);
                myList.add(myShow);
            }
            showList.setFindList(findList);
            //查询出来所有的模块清单数据
            List<ShowList> list = showListService.findList(showList);
            //前端传来选择的模块与数量
            for (ShowList my : myList) {
                //循环遍历
                for (ShowList showList1 : list) {
                    //选择的模块与查出来的一致
                    if (my.getId().equals(showList1.getBlockId())) {
                        Double mdSingleWeight = showList1.getMdSingleWeight();
                        Double number = my.getNumber();
                        Double mdCount = showList1.getMdCount();
                        Double i = mdSingleWeight * number;
                        BigDecimal b1 = new BigDecimal(Double.toString(number));
                        BigDecimal b2 = new BigDecimal(Double.toString(mdCount));
                        double v = b1.multiply(b2).doubleValue();
                        ShowList newShowList = new ShowList();
                        newShowList.setMaterialCode(showList1.getMaterialCode());
                        newShowList.setMaterialDescription(showList1.getMaterialDescription());
                        newShowList.setExtensionDescription(showList1.getExtensionDescription());
                        newShowList.setMdUnits(showList1.getMdUnits());
                        newShowList.setMdTheParty(showList1.getMdTheParty());
                        newShowList.setMdText(showList1.getMdText());
                        newShowList.setMdTotalAmount(v);
                        newShowList.setMdSingleWeight(showList1.getMdSingleWeight());
                        BigDecimal b = new BigDecimal(i);
                        BigDecimal a = new BigDecimal(1000);
                        BigDecimal divide = b.divide(a, 4, BigDecimal.ROUND_HALF_UP);
                        newShowList.setMdTotalWeight(Double.valueOf(divide.toString()));
                        newShowList.setBlockId(showList1.getBlockId());
                        returnList.add(newShowList);
                    }
                }
            }
            List<ShowList> finalList = new ArrayList<>();
            boolean flag;
            for (ShowList turn : returnList) {
                flag = true;
                for (ShowList list1 : finalList) {
                    if (turn.getMaterialCode().equals(list1.getMaterialCode()) && turn.getMaterialDescription().equals(list1.getMaterialDescription()) && turn.getExtensionDescription().equals(list1.getExtensionDescription())) {
                        Double mdTotalAmount = list1.getMdTotalAmount();
                        Double mdTotalAmount1 = turn.getMdTotalAmount();
                        BigDecimal b1 = new BigDecimal(Double.toString(mdTotalAmount));
                        BigDecimal b2 = new BigDecimal(Double.toString(mdTotalAmount1));
                        double v = b1.add(b2).doubleValue();
                        Double mdTotalWeight = list1.getMdTotalWeight();
                        Double mdTotalWeight1 = turn.getMdTotalWeight();
                        BigDecimal a1 = new BigDecimal(Double.toString(mdTotalWeight));
                        BigDecimal a2 = new BigDecimal(Double.toString(mdTotalWeight1));
                        double b = a1.add(a2).doubleValue();
                        list1.setMdTotalAmount(v);
                        list1.setMdTotalWeight(b);
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    showList = new ShowList();
                    showList.setMaterialCode(turn.getMaterialCode());
                    showList.setMaterialDescription(turn.getMaterialDescription());
                    showList.setExtensionDescription(turn.getExtensionDescription());
                    showList.setMdUnits(turn.getMdUnits());
                    showList.setMdTheParty(turn.getMdTheParty());
                    showList.setMdText(turn.getMdText());
                    showList.setMdTotalAmount(turn.getMdTotalAmount());
                    showList.setMdSingleWeight(turn.getMdSingleWeight());
                    showList.setMdTotalWeight(turn.getMdTotalWeight());
                    showList.setBlockId(turn.getBlockId());
                    finalList.add(showList);
                }
            }
            for (ShowList l :
                    finalList) {
                l.setProjectName(projectName);
            }
            showListService.trunCate(projectName);
            //将数据存入提资领料表
            showListService.replaceData(finalList);
        }
        j.setSuccess(true);
        return j;
    }


    @ResponseBody
    @RequestMapping(value = "raiseData")
    public Map<String, Object> raiseData(ShowList showList, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ShowList> page = showListService.findRaisePage(new Page<ShowList>(request, response), showList);
        return getBootstrapData(page);
    }

    /**
     * 模块清单显示页面
     */
    @ResponseBody
    @RequestMapping(value = "mkData")
    public Map<String, Object> mkData(ShowList showList, String item, HttpServletRequest request, HttpServletResponse response, Model model) {
        ArrayList<String> objects = new ArrayList<>();
        ArrayList<String> findList = new ArrayList<>();

        //返回给页面的list
        List<ShowList> myList = new ArrayList<>();
        if (item != null && !"".equals(item)) {
            String[] split = item.split(";");
            //回显list
            List<ShowList> returnList = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                objects.add(split[i]);
            }
            for (String s : objects) {
                String[] split1 = s.split(":");
                ShowList myShow = new ShowList();
                myShow.setId(split1[0]);
                String s1 = split1[1];
                myShow.setNumber(Double.parseDouble(s1));
                findList.add(split1[0]);
                myList.add(myShow);
            }
            showList.setFindList(findList);
            Page<ShowList> page = showListService.findPage(new Page<ShowList>(request, response), showList);
            //查询出来所有的模块清单数据
            List<ShowList> list = page.getList();
            //前端传来选择的模块与数量
            for (ShowList my : myList) {
                //循环遍历
                for (ShowList showList1 : list) {
                    //选择的模块与查出来的一致
                    if (my.getId().equals(showList1.getBlockId())) {
                        Double mdSingleWeight = showList1.getMdSingleWeight();
                        Double number = my.getNumber();
                        Double mdCount = showList1.getMdCount();
                        Double i = mdSingleWeight * number;
                        BigDecimal b1 = new BigDecimal(Double.toString(number));
                        BigDecimal b2 = new BigDecimal(Double.toString(mdCount));
                        double v = b1.multiply(b2).doubleValue();
                        ShowList newShowList = new ShowList();
                        newShowList.setMdBigClass(showList1.getMdBigClass());
                        newShowList.setMdTheClass(showList1.getMdTheClass());
                        newShowList.setMdName(showList1.getMdName());
                        newShowList.setMaterialCode(showList1.getMaterialCode());
                        newShowList.setMaterialDescription(showList1.getMaterialDescription());
                        newShowList.setExtensionDescription(showList1.getExtensionDescription());
                        newShowList.setMdUnits(showList1.getMdUnits());
                        newShowList.setMdTheParty(showList1.getMdTheParty());
                        newShowList.setMdText(showList1.getMdText());
                        newShowList.setMdTotalAmount(v);
                        newShowList.setMdSingleWeight(showList1.getMdSingleWeight());
                        BigDecimal b = new BigDecimal(i);
                        BigDecimal a = new BigDecimal(1000);
                        BigDecimal divide = b.divide(a, 4, BigDecimal.ROUND_HALF_UP);
                        newShowList.setMdTotalWeight(Double.valueOf(divide.toString()));
                        newShowList.setBlockId(showList1.getBlockId());
                        returnList.add(newShowList);
                    }
                }
            }
            Collections.sort(returnList);
            page.setList(returnList);
            return getBootstrapData(page);
        } else {
            findList.add("没有数据");
            showList.setFindList(findList);
            Page<ShowList> page = showListService.findPage(new Page<ShowList>(request, response), showList);
            return getBootstrapData(page);
        }
    }

    @ResponseBody
    @RequestMapping(value = "afterData")
    public Map<String, Object> afterData(ShowList showList, HttpServletRequest request, HttpServletResponse response, Model model) {
        ArrayList<String> objects = new ArrayList<>();
        ArrayList<String> findList = new ArrayList<>();
        String procInsId = showList.getProcInsId();
        String item = showListService.itemQuery(procInsId);
        //返回给页面的list
        List<ShowList> myList = new ArrayList<>();
        if (item != null && !"".equals(item)) {
            String[] split = item.split(";");
            //回显list
            List<ShowList> returnList = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                objects.add(split[i]);
            }
            for (String s : objects) {
                String[] split1 = s.split(":");
                ShowList myShow = new ShowList();
                myShow.setId(split1[0]);
                String s1 = split1[1];
                myShow.setNumber(Double.parseDouble(s1));
                findList.add(split1[0]);
                myList.add(myShow);
            }
            showList.setFindList(findList);
            Page<ShowList> page = showListService.findPage(new Page<ShowList>(request, response), showList);
            //查询出来所有的模块清单数据
            List<ShowList> list = page.getList();
            //前端传来选择的模块与数量
            for (ShowList my : myList) {
                //循环遍历
                for (ShowList showList1 : list) {
                    //选择的模块与查出来的一致
                    if (my.getId().equals(showList1.getBlockId())) {
                        Double mdSingleWeight = showList1.getMdSingleWeight();
                        Double number = my.getNumber();
                        Double mdCount = showList1.getMdCount();
                        Double i = mdSingleWeight * number;
                        BigDecimal b1 = new BigDecimal(Double.toString(number));
                        BigDecimal b2 = new BigDecimal(Double.toString(mdCount));
                        double v = b1.multiply(b2).doubleValue();
                        ShowList newShowList = new ShowList();
                        newShowList.setMdBigClass(showList1.getMdBigClass());
                        newShowList.setMdTheClass(showList1.getMdTheClass());
                        newShowList.setMdName(showList1.getMdName());
                        newShowList.setMaterialCode(showList1.getMaterialCode());
                        newShowList.setMaterialDescription(showList1.getMaterialDescription());
                        newShowList.setExtensionDescription(showList1.getExtensionDescription());
                        newShowList.setMdUnits(showList1.getMdUnits());
                        newShowList.setMdTheParty(showList1.getMdTheParty());
                        newShowList.setMdText(showList1.getMdText());
                        newShowList.setMdTotalAmount(v);
                        newShowList.setMdSingleWeight(showList1.getMdSingleWeight());
                        BigDecimal b = new BigDecimal(i);
                        BigDecimal a = new BigDecimal(1000);
                        BigDecimal divide = b.divide(a, 4, BigDecimal.ROUND_HALF_UP);
                        newShowList.setMdTotalWeight(Double.valueOf(divide.toString()));
                        newShowList.setBlockId(showList1.getBlockId());
                        returnList.add(newShowList);
                    }
                }
            }
            Collections.sort(returnList);
            page.setList(returnList);
            return getBootstrapData(page);
        } else {
            findList.add("没有数据");
            showList.setFindList(findList);
            Page<ShowList> page = showListService.findPage(new Page<ShowList>(request, response), showList);
            return getBootstrapData(page);
        }

    }
}
