/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sg.balancedlibrary.materialReportDetails.web;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sg.balancedlibrary.liKuResourcePool.entity.LiKuResourcePool;
import com.jeeplus.modules.sg.balancedlibrary.liKuResourcePool.service.LiKuResourcePoolService;
import com.jeeplus.modules.sg.balancedlibrary.liKuResourcePool.utils.LiKuUtils;
import com.jeeplus.modules.sg.balancedlibrary.liKuResourcePool.utils.OptimalUtil;
import com.jeeplus.modules.sg.balancedlibrary.materialReportDetails.entity.CompareHistory;
import com.jeeplus.modules.sg.balancedlibrary.materialReportDetails.entity.MaterialDetails;
import com.jeeplus.modules.sg.balancedlibrary.materialReportDetails.entity.MaterialReportDetails;
import com.jeeplus.modules.sg.balancedlibrary.materialReportDetails.service.MaterialReportDetailsService;
import com.jeeplus.modules.sg.balancedlibrary.replaceMaterial.entity.ReplaceMaterial;
import com.jeeplus.modules.sg.balancedlibrary.replaceMaterial.service.ReplaceMaterialService;
import com.jeeplus.modules.sg.picking.activiti.entity.MaterialReport;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import static java.math.BigDecimal.ROUND_HALF_UP;

/**
 * 物资上报明细Controller
 *
 * @author enford
 * @version 1.0
 */
//@Controller
//@RequestMapping(value = "${adminPath}/MaterialReportDetails")
public class MaterialReportDetailsController extends BaseController {

    @Autowired
    private MaterialReportDetailsService service;

    @Autowired
    private LiKuResourcePoolService poolService;

    @Autowired
    private ReplaceMaterialService replaceMaterialService;

    @ModelAttribute
    public MaterialReportDetails get(@RequestParam(required = false) String id) {
        MaterialReportDetails entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = service.get(id);
        }
        if (entity == null) {
            entity = new MaterialReportDetails();
        }
        return entity;
    }

    /**
     * 物资上报明细列表页面
     */
    @RequiresPermissions("sg:balancedlibrary:MaterialReportDetails:list")
    @RequestMapping(value = "list")
    public String list(MaterialReportDetails entity, Model model) {
        model.addAttribute("entity", entity);
        return "modules/sg/balancedlibrary/materialReportDetails/MaterialReportDetailslList";
    }

    /**
     * 物资上报明细列表数据
     */
    @ResponseBody
    @RequiresPermissions("sg:balancedlibrary:MaterialReportDetails:list")
    @RequestMapping(value = "data")
    public Map<String, Object> data(MaterialReportDetails entity, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<MaterialReportDetails> page = service.findPage(new Page<MaterialReportDetails>(request, response), entity);
        return getBootstrapData(page);
    }

    /**
     * 查看，增加，编辑物资上报明细表单页面
     */
    @RequiresPermissions(value = {"sg:balancedlibrary:MaterialReportDetails:view", "sg:balancedlibrary:MaterialReportDetails:add", "sg:balancedlibrary:MaterialReportDetails:edit"}, logical = Logical.OR)
    @RequestMapping(value = "form")
    public String form(MaterialReportDetails entity, Model model) {
        model.addAttribute("entity", entity);
        return "modules/test/one/dialog/MaterialReportDetailsForm";
    }

    /**
     * 保存物资上报明细
     */
    @ResponseBody
    @RequiresPermissions(value = {"sg:balancedlibrary:MaterialReportDetails:add", "sg:balancedlibrary:MaterialReportDetails:edit"}, logical = Logical.OR)
    @RequestMapping(value = "save")
    public AjaxJson save(MaterialReportDetails entity, Model model) throws Exception {
        AjaxJson j = new AjaxJson();
        /**
         * 后台hibernate-validation插件校验
         */
        String errMsg = beanValidator(entity);
        if (StringUtils.isNotBlank(errMsg)) {
            j.setSuccess(false);
            j.setMsg(errMsg);
            return j;
        }
        //新增或编辑表单保存
        service.save(entity);
        j.setSuccess(true);
        j.setMsg("保存物资上报明细成功");
        return j;
    }

    /**
     * 删除物资上报明细
     */
    @ResponseBody
    @RequiresPermissions("sg:balancedlibrary:MaterialReportDetails:del")
    @RequestMapping(value = "delete")
    public AjaxJson delete(MaterialReportDetails entity) {
        AjaxJson j = new AjaxJson();
        service.delete(entity);
        j.setMsg("删除物资上报明细成功");
        return j;
    }

    /**
     * 批量删除物资上报明细
     */
    @ResponseBody
    @RequiresPermissions("sg:balancedlibrary:MaterialReportDetails:del")
    @RequestMapping(value = "deleteAll")
    public AjaxJson deleteAll(String ids) {
        AjaxJson j = new AjaxJson();
        String idArray[] = ids.split(",");
        for (String id : idArray) {
            service.delete(service.get(id));
        }
        j.setMsg("删除物资上报明细成功");
        return j;
    }

    /**
     * 导出excel文件
     */
    @ResponseBody
    @RequestMapping(value = "export")
    public AjaxJson exportFile(MaterialReportDetails entity, HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            String fileName = "物资上报利库明细表" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            Page<MaterialReportDetails> page = service.findPage(new Page<MaterialReportDetails>(request, response, -1), entity);
            new ExportExcel("物料信息表", MaterialReportDetails.class).setDataList(page.getList()).write(response, fileName).dispose();
            j.setSuccess(true);
            j.setMsg("导出成功！");
            return j;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导出物资上报利库明细表失败！失败信息：" + e.getMessage());
        }
        return j;
    }

    /**
     * 物资上报明细-导入Excel数据
     */
    @ResponseBody
    @RequestMapping(value = "import")
    public AjaxJson importFile(@RequestParam("file") MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        //记录物资上报明细比对利库资源明细详细情况
        List<CompareHistory> compareHistories = new ArrayList<>();
        //物资上报明细数据-段长
        List<MaterialReportDetails> segmentList = new ArrayList<>();
        //物资上报明细数据-非段长
        List<MaterialReportDetails> NonSegmentList = new ArrayList<>();
        //本部-段长
        List<MaterialReportDetails> bBSegmentList = new ArrayList<>();
        //扬中-段长
        List<MaterialReportDetails> yZSegmentList = new ArrayList<>();
        //丹阳-段长
        List<MaterialReportDetails> dYSegmentList = new ArrayList<>();
        //句容-段长
        List<MaterialReportDetails> jRSegmentList = new ArrayList<>();
        //本部-非段长
        List<MaterialReportDetails> bBNonSegmentList = new ArrayList<>();
        //扬中-非段长
        List<MaterialReportDetails> yZNonSegmentList = new ArrayList<>();
        //丹阳-非段长
        List<MaterialReportDetails> dYNonSegmentList = new ArrayList<>();
        //句容-非段长
        List<MaterialReportDetails> jRNonSegmentList = new ArrayList<>();
        try {
            ImportExcel ei = new ImportExcel(file, 1, 0);
            //物资上报明细数据列表
            List<MaterialReportDetails> list = ei.getDataList(MaterialReportDetails.class);
            /*****************************物资上报明细*********************************************/
            //判断物资上报明细单位是否为米或千米，将数据分为非段长与段长
            for (MaterialReportDetails details : list) {
                details.preInsert();
                Integer theUnitMeter = LiKuUtils.isTheUnitMeter(details.getUnit());
                if (theUnitMeter.equals(LiKuUtils.YES)) {
                    segmentList.add(details);
                } else {
                    NonSegmentList.add(details);
                }
            }

            //将段长数据根据本部-扬中-丹阳-句容分组
            if (segmentList.size() > 0) {
                for (MaterialReportDetails details :
                        segmentList) {
                    Integer area = LiKuUtils.judgmentArea(details.getReportingArea());
                    if (area.equals(LiKuUtils.BB_FLAG)) {
                        bBSegmentList.add(details);
                    } else if (area.equals(LiKuUtils.YZ_FLAG)) {
                        yZSegmentList.add(details);
                    } else if (area.equals(LiKuUtils.DY_FLAG)) {
                        dYSegmentList.add(details);
                    } else if (area.equals(LiKuUtils.JR_FLAG)) {
                        jRSegmentList.add(details);
                    }
                }
            }

            //同地区上报表中先根据项目名称顺序排序匹配，然后依据表格顺序依次排序匹配
            //本部段长数据
            if (bBSegmentList.size() > 0) {
                bBSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
                for (MaterialReportDetails material :
                        bBSegmentList) {
                    //设置初始利库数量为0，初始申请数量为物资的表格数量
                    material.setNumberOfNiches(0.0);
                    material.setNumberOfApplications(material.getTotal());
                }
            }
            //扬中段长数据
            if (yZSegmentList.size() > 0) {
                yZSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
                for (MaterialReportDetails material :
                        yZSegmentList) {
                    material.setNumberOfNiches(0.0);
                    material.setNumberOfApplications(material.getTotal());
                }
            }
            //丹阳段长数据
            if (dYSegmentList.size() > 0) {
                dYSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
                for (MaterialReportDetails material :
                        dYSegmentList) {
                    material.setNumberOfNiches(0.0);
                    material.setNumberOfApplications(material.getTotal());
                }
            }
            //句容段长数据
            if (jRSegmentList.size() > 0) {
                jRSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
                for (MaterialReportDetails material :
                        jRSegmentList) {
                    material.setNumberOfNiches(0.0);
                    material.setNumberOfApplications(material.getTotal());
                }
            }
            //将非段长数据根据本部-扬中-丹阳-句容分组
            if (NonSegmentList.size() > 0) {
                for (MaterialReportDetails details :
                        NonSegmentList) {
                    Integer area = LiKuUtils.judgmentArea(details.getReportingArea());
                    if (area.equals(LiKuUtils.BB_FLAG)) {
                        bBNonSegmentList.add(details);
                    } else if (area.equals(LiKuUtils.YZ_FLAG)) {
                        yZNonSegmentList.add(details);
                    } else if (area.equals(LiKuUtils.DY_FLAG)) {
                        dYNonSegmentList.add(details);
                    } else if (area.equals(LiKuUtils.JR_FLAG)) {
                        jRNonSegmentList.add(details);
                    }
                }
            }

            //同地区上报表中先根据项目名称顺序排序匹配，然后依据表格顺序依次排序匹配
            //本部数据-非段长
            if (bBNonSegmentList.size() > 0) {
                bBNonSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
                for (MaterialReportDetails material :
                        bBNonSegmentList) {
                    material.setNumberOfNiches(0.0);
                    material.setNumberOfApplications(material.getTotal());
                }
            }
            //扬中数据-非段长
            if (yZNonSegmentList.size() > 0) {
                yZNonSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
                for (MaterialReportDetails material :
                        yZNonSegmentList) {
                    material.setNumberOfNiches(0.0);
                    material.setNumberOfApplications(material.getTotal());
                }
            }
            //丹阳数据-非段长
            if (dYNonSegmentList.size() > 0) {
                dYNonSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
                for (MaterialReportDetails material :
                        dYNonSegmentList) {
                    material.setNumberOfNiches(0.0);
                    material.setNumberOfApplications(material.getTotal());
                }
            }
            //句容数据-非段长
            if (jRNonSegmentList.size() > 0) {
                jRNonSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
                for (MaterialReportDetails material :
                        jRNonSegmentList) {
                    material.setNumberOfNiches(0.0);
                    material.setNumberOfApplications(material.getTotal());
                }
            }
            //先根据
            /*****************************利库资源数据*********************************************/
            //利库资源池数据，根据本部>扬中>丹阳>句容分组
            //查询出所有本部资源数据-非段长
            List<LiKuResourcePool> bBNonPoolList = poolService.findLiKuList(LiKuUtils.NO, LiKuUtils.BB_FLAG);
            //查询出所有扬中资源数据-非段长
            List<LiKuResourcePool> yZNonPoolList = poolService.findLiKuList(LiKuUtils.NO, LiKuUtils.YZ_FLAG);
            //查询出所有丹阳资源数据-非段长
            List<LiKuResourcePool> dYNonPoolList = poolService.findLiKuList(LiKuUtils.NO, LiKuUtils.DY_FLAG);
            //查询出所有句容资源数据-非段长
            List<LiKuResourcePool> jRNonPoolList = poolService.findLiKuList(LiKuUtils.NO, LiKuUtils.JR_FLAG);

            //查询出所有本部资源数据-段长
            List<LiKuResourcePool> bBPoolList = poolService.findLiKuList(LiKuUtils.YES, LiKuUtils.BB_FLAG);
            //查询出所有扬中资源数据-段长
            List<LiKuResourcePool> yZPoolList = poolService.findLiKuList(LiKuUtils.YES, LiKuUtils.YZ_FLAG);
            //查询出所有丹阳资源数据-段长
            List<LiKuResourcePool> dYPoolList = poolService.findLiKuList(LiKuUtils.YES, LiKuUtils.DY_FLAG);
            //查询出所有句容资源数据-段长
            List<LiKuResourcePool> jRPoolList = poolService.findLiKuList(LiKuUtils.YES, LiKuUtils.JR_FLAG);

            //同地区库龄降序>数量大小降序>批次号升序
            //本部利库资源池数据-非段长
            if (bBNonPoolList.size() > 0) {
                poolSort(bBNonPoolList);
            }
            //扬中利库资源池数据-非段长
            if (yZNonPoolList.size() > 0) {
                poolSort(yZNonPoolList);
            }
            //丹阳利库资源池数据-非段长
            if (dYNonPoolList.size() > 0) {
                poolSort(dYNonPoolList);
            }
            //句容利库资源池数据-非段长
            if (jRNonPoolList.size() > 0) {
                poolSort(jRNonPoolList);
            }

            //本部利库资源池数据-段长
            if (bBPoolList.size() > 0) {
                for (LiKuResourcePool pool :
                        bBPoolList) {
                    //设置标识利库中段长状态为 未完成利库 状态，单独设置状态因为无法根据数量判断，段长利库可能有剩余
                    pool.setCompleteLiKu(0);
                }
                poolSegSort(bBPoolList);
            }
            //扬中利库资源池数据-段长
            if (yZPoolList.size() > 0) {
                for (LiKuResourcePool pool :
                        yZPoolList) {
                    pool.setCompleteLiKu(0);
                }
                poolSegSort(yZPoolList);
            }
            //丹阳利库资源池数据-段长
            if (dYPoolList.size() > 0) {
                for (LiKuResourcePool pool :
                        dYPoolList) {
                    pool.setCompleteLiKu(0);
                }
                poolSegSort(dYPoolList);
            }
            //句容利库资源池数据-段长
            if (jRPoolList.size() > 0) {
                for (LiKuResourcePool pool :
                        jRPoolList) {
                    pool.setCompleteLiKu(0);
                }
                poolSegSort(jRPoolList);
            }

            /*****************************************非段长比对*****************************************************/
            //非段长比对顺序，由物资上报数据比对利库资源池数据
            //本部-本部
            if (bBNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        bBNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    //如果总需求数量等于利库数量则不利库
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            bBNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("本地利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("本地利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("本地利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    details.setLocalList(detailsList);
                }
            }
            //扬中-扬中
            if (yZNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        yZNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            yZNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("本地利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("本地利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("本地利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    details.setLocalList(detailsList);
                }
            }
            //丹阳-丹阳
            if (dYNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        dYNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            dYNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("本地利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("本地利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("本地利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    details.setLocalList(detailsList);
                }
            }
            //句容-句容
            if (jRNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        jRNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            jRNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("本地利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("本地利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("本地利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    details.setLocalList(detailsList);
                }
            }

            //本部-扬中
            if (bBNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        bBNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            yZNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    if (null != regionalList && regionalList.size() > 0) {
                        regionalList.addAll(detailsList);
                        details.setRegionalList(regionalList);
                    } else {
                        regionalList = new ArrayList<>();
                        regionalList.addAll(detailsList);
                    }
                    details.setRegionalList(regionalList);
                }
            }
            //本部-丹阳
            if (bBNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        bBNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            dYNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    if (null != regionalList && regionalList.size() > 0) {
                        regionalList.addAll(detailsList);
                        details.setRegionalList(regionalList);
                    } else {
                        regionalList = new ArrayList<>();
                        regionalList.addAll(detailsList);
                    }
                    details.setRegionalList(regionalList);
                }
            }
            //本部-句容
            if (bBNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        bBNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            jRNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    if (null != regionalList && regionalList.size() > 0) {
                        regionalList.addAll(detailsList);
                        details.setRegionalList(regionalList);
                    } else {
                        regionalList = new ArrayList<>();
                        regionalList.addAll(detailsList);
                    }
                    details.setRegionalList(regionalList);
                }
            }

            //扬中-本部
            if (yZNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        yZNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            bBNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    if (null != regionalList && regionalList.size() > 0) {
                        regionalList.addAll(detailsList);
                        details.setRegionalList(regionalList);
                    } else {
                        regionalList = new ArrayList<>();
                        regionalList.addAll(detailsList);
                    }
                    details.setRegionalList(regionalList);
                }
            }
            //杨中-丹阳
            if (yZNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        yZNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            dYNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    if (null != regionalList && regionalList.size() > 0) {
                        regionalList.addAll(detailsList);
                        details.setRegionalList(regionalList);
                    } else {
                        regionalList = new ArrayList<>();
                        regionalList.addAll(detailsList);
                    }
                    details.setRegionalList(regionalList);
                }
            }
            //扬中-句容
            if (yZNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        yZNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            jRNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    if (null != regionalList && regionalList.size() > 0) {
                        regionalList.addAll(detailsList);
                        details.setRegionalList(regionalList);
                    } else {
                        regionalList = new ArrayList<>();
                        regionalList.addAll(detailsList);
                    }
                    details.setRegionalList(regionalList);
                }
            }

            //丹阳-本部
            if (dYNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        dYNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            bBNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    if (null != regionalList && regionalList.size() > 0) {
                        regionalList.addAll(detailsList);
                        details.setRegionalList(regionalList);
                    } else {
                        regionalList = new ArrayList<>();
                        regionalList.addAll(detailsList);
                    }
                    details.setRegionalList(regionalList);
                }
            }
            //丹阳-扬中
            if (dYNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        dYNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            yZNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    if (null != regionalList && regionalList.size() > 0) {
                        regionalList.addAll(detailsList);
                        details.setRegionalList(regionalList);
                    } else {
                        regionalList = new ArrayList<>();
                        regionalList.addAll(detailsList);
                    }
                    details.setRegionalList(regionalList);
                }
            }
            //丹阳-句容
            if (dYNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        dYNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            jRNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    if (null != regionalList && regionalList.size() > 0) {
                        regionalList.addAll(detailsList);
                        details.setRegionalList(regionalList);
                    } else {
                        regionalList = new ArrayList<>();
                        regionalList.addAll(detailsList);
                    }
                    details.setRegionalList(regionalList);
                }
            }

            //句容-本部
            if (jRNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        jRNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            bBNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    if (null != regionalList && regionalList.size() > 0) {
                        regionalList.addAll(detailsList);
                        details.setRegionalList(regionalList);
                    } else {
                        regionalList = new ArrayList<>();
                        regionalList.addAll(detailsList);
                    }
                    details.setRegionalList(regionalList);
                }
            }
            //句容-扬中
            if (jRNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        jRNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            yZNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    if (null != regionalList && regionalList.size() > 0) {
                        regionalList.addAll(detailsList);
                        details.setRegionalList(regionalList);
                    } else {
                        regionalList = new ArrayList<>();
                        regionalList.addAll(detailsList);
                    }
                    details.setRegionalList(regionalList);
                }
            }
            //句容-丹阳
            if (jRNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                for (MaterialReportDetails details :
                        jRNonSegmentList) {
                    List<MaterialDetails> detailsList = new ArrayList<>();
                    String code = details.getMaterialCode();
                    String exCode = details.getExtendedCoding();
                    //物资需求数量
                    BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                    //总需求数量
                    BigDecimal all = BigDecimal.valueOf(details.getTotal());
                    //利库数量
                    BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                    if (all.compareTo(numberOfNiches) == 0) {
                        continue;
                    }
                    for (LiKuResourcePool pool :
                            dYNonPoolList) {
                        MaterialDetails materialDetails = new MaterialDetails();
                        CompareHistory compareHistory = new CompareHistory();
                        String poolCode = pool.getMaterialCode();
                        String poExCode = pool.getExtendedCoding();
                        //利库库存数量
                        BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                        if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                            //需求数量>库存数量
                            if (total.compareTo(amount) > 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(pool.getInventoryAmount());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) == 0) {
                                total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(amount.doubleValue());
                                pool.setRemaining(0.0);
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(total.doubleValue());
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(amount.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(amount.toString());
                            } else if (total.compareTo(amount) < 0) {
                                BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                pool.setNumberOfNiches(bigDecimal.doubleValue());
                                pool.setRemaining(surplus.doubleValue());
                                BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                details.setNumberOfNiches(niches.doubleValue());
                                details.setNumberOfApplications(0.0);
                                compareHistory.setLiKuId(pool.getId());
                                compareHistory.setMaterialId(details.getId());
                                compareHistory.setComparisonType("跨区利库");
                                compareHistory.setNumberNiches(total.doubleValue());
                                materialDetails.setLiKuBatch(pool.getBatch());
                                materialDetails.setWarehouseLocation(pool.getInvLocation());
                                materialDetails.setNumberOfNiches(total.toString());
                            }
                            compareHistories.add(compareHistory);
                            detailsList.add(materialDetails);
                            if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                break;
                            }
                        }
                    }
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    if (null != regionalList && regionalList.size() > 0) {
                        regionalList.addAll(detailsList);
                        details.setRegionalList(regionalList);
                    } else {
                        regionalList = new ArrayList<>();
                        regionalList.addAll(detailsList);
                    }
                    details.setRegionalList(regionalList);
                }
            }

            /*************************************非段长以大代小******************************************************/
            //查找库中所有的以大代小的数据list
            List<ReplaceMaterial> allList = replaceMaterialService.findAll();
            if (allList.size() > 0) {
                for (ReplaceMaterial a :
                        allList) {
                    String code = a.getMaterialCode();
                    String exCode = a.getExtendedCoding();
                    String codeRep = a.getMaterialCodeRep();
                    String exCodeRep = a.getExtendedCodingRep();
                    for (MaterialReportDetails material :
                            bBNonSegmentList) {
                        if (material.getMaterialCode().equals(code) && material.getExtendedCoding().equals(exCode)) {
                            material.setReplaceCode(codeRep);
                            material.setReplaceExCode(exCodeRep);
                        }
                    }
                    for (MaterialReportDetails material :
                            yZNonSegmentList) {
                        if (material.getMaterialCode().equals(code) && material.getExtendedCoding().equals(exCode)) {
                            material.setReplaceCode(codeRep);
                            material.setReplaceExCode(exCodeRep);
                        }
                    }
                    for (MaterialReportDetails material :
                            dYNonSegmentList) {
                        if (material.getMaterialCode().equals(code) && material.getExtendedCoding().equals(exCode)) {
                            material.setReplaceCode(codeRep);
                            material.setReplaceExCode(exCodeRep);
                        }
                    }
                    for (MaterialReportDetails material :
                            jRNonSegmentList) {
                        if (material.getMaterialCode().equals(code) && material.getExtendedCoding().equals(exCode)) {
                            material.setReplaceCode(codeRep);
                            material.setReplaceExCode(exCodeRep);
                        }
                    }
                }
                //本部-本部本地以大代小
                if (bBNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            bBNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                bBNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        details.setLocalRepList(detailsList);
                    }
                }

                //扬中-扬中本地以大代小
                if (yZNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            yZNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                yZNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        details.setLocalRepList(detailsList);
                    }
                }
                //丹阳-丹阳本地以大代小
                if (dYNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            dYNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                dYNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        details.setLocalRepList(detailsList);
                    }
                }
                //句容-句容本地以大代小
                if (jRNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            jRNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                jRNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        details.setLocalRepList(detailsList);
                    }
                }


                //本部-扬中跨域以大代小
                if (bBNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            bBNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                yZNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                        if (null != regionalRepList && regionalRepList.size() > 0) {
                            regionalRepList.addAll(detailsList);
                            details.setRegionalRepList(regionalRepList);
                        } else {
                            regionalRepList = new ArrayList<>();
                            regionalRepList.addAll(detailsList);
                        }
                        details.setRegionalRepList(regionalRepList);
                    }
                }
                //本部-丹阳跨域以大代小
                if (bBNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            bBNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                dYNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                        if (null != regionalRepList && regionalRepList.size() > 0) {
                            regionalRepList.addAll(detailsList);
                            details.setRegionalRepList(regionalRepList);
                        } else {
                            regionalRepList = new ArrayList<>();
                            regionalRepList.addAll(detailsList);
                        }
                        details.setRegionalRepList(regionalRepList);
                    }
                }
                //本部-句容跨域以大代小
                if (bBNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            bBNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                jRNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                        if (null != regionalRepList && regionalRepList.size() > 0) {
                            regionalRepList.addAll(detailsList);
                            details.setRegionalRepList(regionalRepList);
                        } else {
                            regionalRepList = new ArrayList<>();
                            regionalRepList.addAll(detailsList);
                        }
                        details.setRegionalRepList(regionalRepList);
                    }
                }

                //扬中-本部跨域以大代小
                if (yZNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            yZNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                bBNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                        if (null != regionalRepList && regionalRepList.size() > 0) {
                            regionalRepList.addAll(detailsList);
                            details.setRegionalRepList(regionalRepList);
                        } else {
                            regionalRepList = new ArrayList<>();
                            regionalRepList.addAll(detailsList);
                        }
                        details.setRegionalRepList(regionalRepList);
                    }
                }
                //扬中-丹阳跨域以大代小
                if (yZNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            yZNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                dYNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                        if (null != regionalRepList && regionalRepList.size() > 0) {
                            regionalRepList.addAll(detailsList);
                            details.setRegionalRepList(regionalRepList);
                        } else {
                            regionalRepList = new ArrayList<>();
                            regionalRepList.addAll(detailsList);
                        }
                        details.setRegionalRepList(regionalRepList);
                    }
                }
                //扬中-句容跨域以大代小
                if (yZNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            yZNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                jRNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                        if (null != regionalRepList && regionalRepList.size() > 0) {
                            regionalRepList.addAll(detailsList);
                            details.setRegionalRepList(regionalRepList);
                        } else {
                            regionalRepList = new ArrayList<>();
                            regionalRepList.addAll(detailsList);
                        }
                        details.setRegionalRepList(regionalRepList);
                    }
                }

                //丹阳-本部跨域以大代小
                if (dYNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            dYNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                bBNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                        if (null != regionalRepList && regionalRepList.size() > 0) {
                            regionalRepList.addAll(detailsList);
                            details.setRegionalRepList(regionalRepList);
                        } else {
                            regionalRepList = new ArrayList<>();
                            regionalRepList.addAll(detailsList);
                        }
                        details.setRegionalRepList(regionalRepList);
                    }
                }
                //丹阳-扬中跨域以大代小
                if (dYNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            dYNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                yZNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                        if (null != regionalRepList && regionalRepList.size() > 0) {
                            regionalRepList.addAll(detailsList);
                            details.setRegionalRepList(regionalRepList);
                        } else {
                            regionalRepList = new ArrayList<>();
                            regionalRepList.addAll(detailsList);
                        }
                        details.setRegionalRepList(regionalRepList);
                    }
                }
                //丹阳-句容跨域以大代小
                if (dYNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            dYNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                jRNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                        if (null != regionalRepList && regionalRepList.size() > 0) {
                            regionalRepList.addAll(detailsList);
                            details.setRegionalRepList(regionalRepList);
                        } else {
                            regionalRepList = new ArrayList<>();
                            regionalRepList.addAll(detailsList);
                        }
                        details.setRegionalRepList(regionalRepList);
                    }
                }

                //句容-本部跨域以大代小
                if (jRNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            jRNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                bBNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                        if (null != regionalRepList && regionalRepList.size() > 0) {
                            regionalRepList.addAll(detailsList);
                            details.setRegionalRepList(regionalRepList);
                        } else {
                            regionalRepList = new ArrayList<>();
                            regionalRepList.addAll(detailsList);
                        }
                        details.setRegionalRepList(regionalRepList);
                    }
                }

                //句容-扬中跨域以大代小
                if (jRNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            jRNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                yZNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                        if (null != regionalRepList && regionalRepList.size() > 0) {
                            regionalRepList.addAll(detailsList);
                            details.setRegionalRepList(regionalRepList);
                        } else {
                            regionalRepList = new ArrayList<>();
                            regionalRepList.addAll(detailsList);
                        }
                        details.setRegionalRepList(regionalRepList);
                    }
                }
                //句容-丹阳跨域以大代小
                if (jRNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                    for (MaterialReportDetails details :
                            jRNonSegmentList) {
                        List<MaterialDetails> detailsList = new ArrayList<>();
                        String code = details.getReplaceCode();
                        String exCode = details.getReplaceExCode();
                        if (null == code || "".equals(code) || null == exCode || "".equals(exCode)) {
                            continue;
                        }
                        //物资需求数量
                        BigDecimal total = BigDecimal.valueOf(details.getNumberOfApplications());
                        //总需求数量
                        BigDecimal all = BigDecimal.valueOf(details.getTotal());
                        //利库数量
                        BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
                        if (all.compareTo(numberOfNiches) == 0) {
                            continue;
                        }
                        for (LiKuResourcePool pool :
                                dYNonPoolList) {
                            MaterialDetails materialDetails = new MaterialDetails();
                            CompareHistory compareHistory = new CompareHistory();
                            String poolCode = pool.getMaterialCode();
                            String poExCode = pool.getExtendedCoding();
                            //利库库存数量
                            BigDecimal amount = BigDecimal.valueOf(pool.getRemaining());
                            if (code.equals(poolCode) && exCode.equals(poExCode) && amount.compareTo(BigDecimal.ZERO) != 0) {
                                //需求数量>库存数量
                                if (total.compareTo(amount) > 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(pool.getInventoryAmount());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) == 0) {
                                    total = total.subtract(amount).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(amount.doubleValue());
                                    pool.setRemaining(0.0);
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(amount).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(total.doubleValue());
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(amount.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(amount.toString());
                                } else if (total.compareTo(amount) < 0) {
                                    BigDecimal surplus = amount.subtract(total).setScale(2, ROUND_HALF_UP);
                                    BigDecimal bigDecimal = BigDecimal.valueOf(pool.getNumberOfNiches());
                                    bigDecimal = bigDecimal.add(total).setScale(2, ROUND_HALF_UP);
                                    pool.setNumberOfNiches(bigDecimal.doubleValue());
                                    pool.setRemaining(surplus.doubleValue());
                                    BigDecimal niches = BigDecimal.valueOf(details.getNumberOfNiches());
                                    niches = niches.add(total).setScale(2, ROUND_HALF_UP);
                                    details.setNumberOfNiches(niches.doubleValue());
                                    details.setNumberOfApplications(0.0);
                                    compareHistory.setLiKuId(pool.getId());
                                    compareHistory.setMaterialId(details.getId());
                                    compareHistory.setComparisonType("本地利库");
                                    compareHistory.setNumberNiches(total.doubleValue());
                                    materialDetails.setLiKuBatch(pool.getBatch());
                                    materialDetails.setWarehouseLocation(pool.getInvLocation());
                                    materialDetails.setNumberOfNiches(total.toString());
                                }
                                compareHistories.add(compareHistory);
                                detailsList.add(materialDetails);
                                if (details.getNumberOfNiches().compareTo(details.getTotal()) == 0) {
                                    break;
                                }
                            }
                        }
                        List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                        if (null != regionalRepList && regionalRepList.size() > 0) {
                            regionalRepList.addAll(detailsList);
                            details.setRegionalRepList(regionalRepList);
                        } else {
                            regionalRepList = new ArrayList<>();
                            regionalRepList.addAll(detailsList);
                        }
                        details.setRegionalRepList(regionalRepList);
                    }
                }
            }

            /****************************************段长比对*****************************************************/
            //本部-本部
            if (bBPoolList.size() > 0 && bBSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        bBPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(bBSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    bBSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setLocalWareBatch(pool.getBatch());
                                    b.setLocalWareHouse(pool.getInvLocation());
                                    b.setNumberOfLocal(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //扬中-扬中
            if (yZPoolList.size() > 0 && yZSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        yZPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(yZSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    yZSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setLocalWareBatch(pool.getBatch());
                                    b.setLocalWareHouse(pool.getInvLocation());
                                    b.setNumberOfLocal(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //丹阳-丹阳-段长
            if (dYPoolList.size() > 0 && dYSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        dYPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(dYSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    dYSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setLocalWareBatch(pool.getBatch());
                                    b.setLocalWareHouse(pool.getInvLocation());
                                    b.setNumberOfLocal(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //句容-句容-段长
            if (jRPoolList.size() > 0 && jRSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        jRPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(jRSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    jRSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setLocalWareBatch(pool.getBatch());
                                    b.setLocalWareHouse(pool.getInvLocation());
                                    b.setNumberOfLocal(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }

            //本部-扬中-段长
            if (bBPoolList.size() > 0 && yZSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        bBPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(yZSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    yZSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionalBatch(pool.getBatch());
                                    b.setRegionalHouse(pool.getInvLocation());
                                    b.setNumberOfRegional(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //本部-丹阳-段长
            if (bBPoolList.size() > 0 && dYSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        bBPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(dYSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    dYSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionalBatch(pool.getBatch());
                                    b.setRegionalHouse(pool.getInvLocation());
                                    b.setNumberOfRegional(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //本部-句容-段长
            if (bBPoolList.size() > 0 && jRSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        bBPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(jRSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    jRSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionalBatch(pool.getBatch());
                                    b.setRegionalHouse(pool.getInvLocation());
                                    b.setNumberOfRegional(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }

            //杨中-本部-段长
            if (yZPoolList.size() > 0 && bBSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        yZPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(bBSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    bBSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionalBatch(pool.getBatch());
                                    b.setRegionalHouse(pool.getInvLocation());
                                    b.setNumberOfRegional(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //杨中-丹阳-段长
            if (yZPoolList.size() > 0 && dYSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        yZPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(dYSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    dYSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionalBatch(pool.getBatch());
                                    b.setRegionalHouse(pool.getInvLocation());
                                    b.setNumberOfRegional(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //杨中-句容-段长
            if (yZPoolList.size() > 0 && jRSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        yZPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(jRSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    jRSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionalBatch(pool.getBatch());
                                    b.setRegionalHouse(pool.getInvLocation());
                                    b.setNumberOfRegional(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }

            //丹阳-本部-段长
            if (dYPoolList.size() > 0 && bBSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        dYPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(bBSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    bBSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionalBatch(pool.getBatch());
                                    b.setRegionalHouse(pool.getInvLocation());
                                    b.setNumberOfRegional(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //丹阳-扬中-段长
            if (dYPoolList.size() > 0 && yZSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        dYPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(yZSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    yZSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionalBatch(pool.getBatch());
                                    b.setRegionalHouse(pool.getInvLocation());
                                    b.setNumberOfRegional(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //丹阳-句容-段长
            if (dYPoolList.size() > 0 && jRSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        dYPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(jRSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    jRSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionalBatch(pool.getBatch());
                                    b.setRegionalHouse(pool.getInvLocation());
                                    b.setNumberOfRegional(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }


            //句容-本部-段长
            if (jRPoolList.size() > 0 && bBSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        jRPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(bBSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    bBSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionalBatch(pool.getBatch());
                                    b.setRegionalHouse(pool.getInvLocation());
                                    b.setNumberOfRegional(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //句容-扬中-段长
            if (jRPoolList.size() > 0 && yZSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        jRPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(yZSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    yZSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionalBatch(pool.getBatch());
                                    b.setRegionalHouse(pool.getInvLocation());
                                    b.setNumberOfRegional(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //句容-丹阳-段长
            if (jRPoolList.size() > 0 && dYSegmentList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        jRPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(dYSegmentList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    dYSegmentList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionalBatch(pool.getBatch());
                                    b.setRegionalHouse(pool.getInvLocation());
                                    b.setNumberOfRegional(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }

            /******************************************合并利库************************************/
            //将4个区域的段长数据合并，使用各个区域的
            List<MaterialReportDetails> fourAreaList = new ArrayList<>();
            if (bBSegmentList.size() > 0) {
                fourAreaList.addAll(bBSegmentList);
            }
            if (yZSegmentList.size() > 0) {
                fourAreaList.addAll(yZSegmentList);
            }
            if (dYSegmentList.size() > 0) {
                fourAreaList.addAll(dYSegmentList);
            }
            if (jRSegmentList.size() > 0) {
                fourAreaList.addAll(jRSegmentList);
            }
            //本部-所有
            if (bBPoolList.size() > 0 && fourAreaList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        bBPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(fourAreaList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    fourAreaList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setConsolidatedBatch(pool.getBatch());
                                    b.setConsolidatedWareHouse(pool.getInvLocation());
                                    b.setNumberOfConsolidated(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //杨中-所有
            if (yZPoolList.size() > 0 && fourAreaList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        yZPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(fourAreaList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    fourAreaList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setConsolidatedBatch(pool.getBatch());
                                    b.setConsolidatedWareHouse(pool.getInvLocation());
                                    b.setNumberOfConsolidated(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //丹阳-所有
            if (dYPoolList.size() > 0 && fourAreaList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        dYPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(fourAreaList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    fourAreaList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setConsolidatedBatch(pool.getBatch());
                                    b.setConsolidatedWareHouse(pool.getInvLocation());
                                    b.setNumberOfConsolidated(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //句容-所有
            if (jRPoolList.size() > 0 && fourAreaList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        jRPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = Calculation(fourAreaList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    fourAreaList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setConsolidatedBatch(pool.getBatch());
                                    b.setConsolidatedWareHouse(pool.getInvLocation());
                                    b.setNumberOfConsolidated(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //将合并为的数据，根据区域拆分为4个list
            List<MaterialReportDetails> bBList = new ArrayList<>();
            List<MaterialReportDetails> yZList = new ArrayList<>();
            List<MaterialReportDetails> dYList = new ArrayList<>();
            List<MaterialReportDetails> jRList = new ArrayList<>();
            for (MaterialReportDetails a :
                    fourAreaList) {
                String reportingArea = a.getReportingArea();
                if (reportingArea.equals(LiKuUtils.BB_PLACE)) {
                    bBList.add(a);
                } else if (reportingArea.equals(LiKuUtils.YZ_PLACE)) {
                    yZList.add(a);
                }else if (reportingArea.equals(LiKuUtils.DY_PLACE)) {
                    dYList.add(a);
                }else if (reportingArea.equals(LiKuUtils.JR_PLACE)) {
                    jRList.add(a);
                }
            }
            //本部-扬中-丹阳-句容
            /****************************************段长以大代小********************************************/
            //获取所有以大代小的替换规则数据
            if (allList.size() > 0) {
                for (ReplaceMaterial a :
                        allList) {
                    String code = a.getMaterialCode();
                    String exCode = a.getExtendedCoding();
                    String codeRep = a.getMaterialCodeRep();
                    String exCodeRep = a.getExtendedCodingRep();
                    for (MaterialReportDetails b :
                            bBList) {
                        if (b.getMaterialCode().equals(code) && b.getExtendedCoding().equals(exCode)) {
                            b.setReplaceCode(codeRep);
                            b.setReplaceExCode(exCodeRep);
                        }
                    }
                    for (MaterialReportDetails b :
                            yZList) {
                        if (b.getMaterialCode().equals(code) && b.getExtendedCoding().equals(exCode)) {
                            b.setReplaceCode(codeRep);
                            b.setReplaceExCode(exCodeRep);
                        }
                    }
                    for (MaterialReportDetails b :
                            dYList) {
                        if (b.getMaterialCode().equals(code) && b.getExtendedCoding().equals(exCode)) {
                            b.setReplaceCode(codeRep);
                            b.setReplaceExCode(exCodeRep);
                        }
                    }
                    for (MaterialReportDetails b :
                            jRList) {
                        if (b.getMaterialCode().equals(code) && b.getExtendedCoding().equals(exCode)) {
                            b.setReplaceCode(codeRep);
                            b.setReplaceExCode(exCodeRep);
                        }
                    }
                }
            }

            //本部-本部-段长以大代小
            if (bBPoolList.size() > 0 && bBList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        bBPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(bBList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    bBList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setLocalLiKuBatch(pool.getBatch());
                                    b.setLocalLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfLocalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //扬中-扬中-段长以大代小
            if (yZPoolList.size() > 0 && yZList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        yZPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(yZList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    yZList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setLocalLiKuBatch(pool.getBatch());
                                    b.setLocalLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfLocalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //丹阳-丹阳-段长以大代小
            if (dYPoolList.size() > 0 && dYList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        dYPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(dYList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    dYList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setLocalLiKuBatch(pool.getBatch());
                                    b.setLocalLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfLocalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //句容-句容-段长以大代小
            if (jRPoolList.size() > 0 && jRList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        jRPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(jRList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    jRList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setLocalLiKuBatch(pool.getBatch());
                                    b.setLocalLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfLocalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }

            //本部-扬中-段长以大代小
            if (bBPoolList.size() > 0 && yZList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        bBPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(yZList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    yZList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionLiKuBatch(pool.getBatch());
                                    b.setRegionLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfRegionalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //本部-丹阳-段长以大代小
            if (bBPoolList.size() > 0 && dYList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        bBPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(dYList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    dYList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionLiKuBatch(pool.getBatch());
                                    b.setRegionLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfRegionalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //本部-句容-段长以大代小
            if (bBPoolList.size() > 0 && jRList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        bBPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(jRList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    jRList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionLiKuBatch(pool.getBatch());
                                    b.setRegionLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfRegionalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }

            //杨中-本部-段长以大代小
            if (yZPoolList.size() > 0 && bBList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        yZPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(bBList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    bBList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionLiKuBatch(pool.getBatch());
                                    b.setRegionLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfRegionalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //杨中-丹阳-段长以大代小
            if (yZPoolList.size() > 0 && dYList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        yZPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(dYList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    dYList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionLiKuBatch(pool.getBatch());
                                    b.setRegionLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfRegionalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //杨中-句容-段长以大代小
            if (yZPoolList.size() > 0 && jRList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        yZPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(jRList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    jRList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionLiKuBatch(pool.getBatch());
                                    b.setRegionLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfRegionalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }

            //丹阳-本部-段长以大代小
            if (dYPoolList.size() > 0 && bBList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        dYPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(bBList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    bBList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionLiKuBatch(pool.getBatch());
                                    b.setRegionLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfRegionalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //丹阳-扬中-段长以大代小
            if (dYPoolList.size() > 0 && yZList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        dYPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(yZList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    yZList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionLiKuBatch(pool.getBatch());
                                    b.setRegionLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfRegionalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //丹阳-句容-段长以大代小
            if (dYPoolList.size() > 0 && jRList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        dYPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(jRList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    jRList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionLiKuBatch(pool.getBatch());
                                    b.setRegionLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfRegionalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }

            //句容-本部-段长以大代小
            if (jRPoolList.size() > 0 && bBList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        jRPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(bBList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    bBList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionLiKuBatch(pool.getBatch());
                                    b.setRegionLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfRegionalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //句容-扬中-段长以大代小
            if (jRPoolList.size() > 0 && yZList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        jRPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(yZList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    yZList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionLiKuBatch(pool.getBatch());
                                    b.setRegionLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfRegionalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //句容-丹阳-段长以大代小
            if (jRPoolList.size() > 0 && dYList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        jRPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(dYList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    dYList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setRegionLiKuBatch(pool.getBatch());
                                    b.setRegionLiKuWareHouse(pool.getInvLocation());
                                    b.setNumberOfRegionalLiKu(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }

            //以大代小合并利库
            List<MaterialReportDetails> fourRepList = new ArrayList<>();
            if (bBSegmentList.size() > 0) {
                fourRepList.addAll(bBSegmentList);
            }
            if (yZSegmentList.size() > 0) {
                fourRepList.addAll(yZSegmentList);
            }
            if (dYSegmentList.size() > 0) {
                fourRepList.addAll(dYSegmentList);
            }
            if (jRSegmentList.size() > 0) {
                fourRepList.addAll(jRSegmentList);
            }
            //本部-所有
            if (bBPoolList.size() > 0 && fourRepList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        bBPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(fourRepList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    fourRepList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setConLocalBatch(pool.getBatch());
                                    b.setConLocalWareHouse(pool.getInvLocation());
                                    b.setNumberOfConLocal(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //杨中-所有
            if (yZPoolList.size() > 0 && fourRepList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        yZPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(fourRepList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    fourRepList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setConLocalBatch(pool.getBatch());
                                    b.setConLocalWareHouse(pool.getInvLocation());
                                    b.setNumberOfConLocal(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //丹阳-所有
            if (dYPoolList.size() > 0 && fourRepList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        dYPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(fourRepList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    fourRepList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setConLocalBatch(pool.getBatch());
                                    b.setConLocalWareHouse(pool.getInvLocation());
                                    b.setNumberOfConLocal(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            //句容-所有
            if (jRPoolList.size() > 0 && fourRepList.size() > 0) {
                //计算最优解的集合
                List<MaterialReportDetails> calculation = new ArrayList<>();
                //最优解情况
                List<MaterialReportDetails> bestChose = new ArrayList<>();
                for (LiKuResourcePool pool :
                        jRPoolList) {
                    OptimalUtil optimalUtil = new OptimalUtil();
                    if (pool.getCompleteLiKu() == 1) {
                        continue;
                    }
                    String poolCode = pool.getMaterialCode();
                    String poExCode = pool.getExtendedCoding();
                    //利库数量
                    BigDecimal niches = BigDecimal.valueOf(pool.getNumberOfNiches());
                    //库存总量
                    BigDecimal amount = BigDecimal.valueOf(pool.getLengthOfCable());
                    if (amount.compareTo(niches) == 0) {
                        continue;
                    }
                    //通过物料编码，扩展编码，是否完成利库，获取符合当前利库线段数据的项目集合数据
                    calculation = CalculationRep(fourRepList, poolCode, poExCode);
                    //返回最优解
                    bestChose = optimalUtil.getOptimal(pool.getLengthOfCable(),calculation);;;
                    //计算最优解
                    if (bestChose.size() > 0) {
                        for (MaterialReportDetails a :
                                bestChose) {
                            for (MaterialReportDetails b :
                                    fourRepList) {
                                if (a.getId().equals(b.getId())) {
                                    b.setNumberOfApplications(0.0);
                                    b.setNumberOfNiches(b.getTotal());
                                    b.setConLocalBatch(pool.getBatch());
                                    b.setConLocalWareHouse(pool.getInvLocation());
                                    b.setNumberOfConLocal(b.getTotal().toString() + "第" + pool.getSegBelongs() + "段");
                                }
                            }
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal begin = BigDecimal.valueOf(0.0);
                        BigDecimal bigDecimal = BigDecimal.valueOf(pool.getLengthOfCable());
                        for (MaterialReportDetails a :
                                bestChose) {
                            begin  = begin.add(BigDecimal.valueOf(a.getTotal()));
                        }
                        //存在最优解，当前利库信息使用，变更状态
                        BigDecimal decimal = bigDecimal.subtract(begin).setScale(2, ROUND_HALF_UP);
                        pool.setNumberOfNiches(begin.doubleValue());
                        pool.setRemaining(decimal.doubleValue());
                        pool.setCompleteLiKu(1);
                    }
                }
            }
            /*****************************************历史记录保存**************************************************/
            if (compareHistories.size() > 0) {

            }
            /**********************************数据导出合并，生成文件返回**************************************/
            List<MaterialReportDetails> exportList = new ArrayList<>();
            if (fourRepList.size() > 0) {
                exportList.addAll(fourRepList);
            }
            if (bBNonSegmentList.size() > 0) {
                for (MaterialReportDetails details :
                        bBNonSegmentList) {
                    String localWareBatch = "";
                    String localWareHouse = "";
                    String numberOfLocal = "";
                    String regionalBatch = "";
                    String regionalHouse = "";
                    String numberOfRegional = "";
                    String localLiKuModel = "";
                    String localLiKuBatch = "";
                    String localLiKuWareHouse = "";
                    String numberOfLocalLiKu = "";
                    String regionLiKuModel = "";
                    String regionLiKuBatch = "";
                    String regionLiKuWareHouse = "";
                    String numberOfRegionalLiKu = "";
                    List<MaterialDetails> localList = details.getLocalList();
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    List<MaterialDetails> localRepList = details.getLocalRepList();
                    List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                    if (null != localList && localList.size() > 0) {
                        for (MaterialDetails a :
                                localList) {
                            localWareBatch += a.getLiKuBatch() + ",";
                            localWareHouse += a.getWarehouseLocation() + ",";
                            numberOfLocal += a.getNumberOfNiches() + ",";
                        }
                        details.setLocalWareBatch(localWareBatch);
                        details.setLocalWareHouse(localWareHouse);
                        details.setNumberOfLocal(numberOfLocal);
                    }
                    if (null != regionalList && regionalList.size() > 0) {
                        for (MaterialDetails a :
                                regionalList) {
                            regionalBatch += a.getLiKuBatch() + ",";
                            regionalHouse += a.getWarehouseLocation() + ",";
                            numberOfRegional += a.getNumberOfNiches() + ",";
                        }
                        details.setRegionalBatch(regionalBatch);
                        details.setRegionalHouse(regionalHouse);
                        details.setNumberOfRegional(numberOfRegional);
                    }
                    if (null != localRepList && localRepList.size() > 0) {
                        for (MaterialDetails a :
                                localRepList) {
                            localLiKuBatch += a.getLiKuBatch() + ",";
                            localLiKuWareHouse += a.getWarehouseLocation() + ",";
                            numberOfLocalLiKu += a.getNumberOfNiches() + ",";
                            ;
                        }
                        details.setLocalLiKuBatch(localLiKuBatch);
                        details.setLocalLiKuWareHouse(localLiKuWareHouse);
                        details.setNumberOfLocalLiKu(numberOfLocalLiKu);
                    }
                    if (null != regionalRepList && regionalRepList.size() > 0) {
                        for (MaterialDetails a :
                                regionalRepList) {
                            regionLiKuBatch += a.getLiKuBatch() + ",";
                            regionLiKuWareHouse += a.getWarehouseLocation() + ",";
                            numberOfRegionalLiKu += a.getNumberOfNiches() + ",";
                        }
                        details.setRegionLiKuBatch(regionLiKuBatch);
                        details.setRegionLiKuWareHouse(regionLiKuWareHouse);
                        details.setNumberOfRegionalLiKu(numberOfRegionalLiKu);
                    }
                    exportList.add(details);
                }
            }
            if (yZNonSegmentList.size() > 0) {
                for (MaterialReportDetails details :
                        yZNonSegmentList) {
                    String localWareBatch = "";
                    String localWareHouse = "";
                    String numberOfLocal = "";
                    String regionalBatch = "";
                    String regionalHouse = "";
                    String numberOfRegional = "";
                    String localLiKuModel = "";
                    String localLiKuBatch = "";
                    String localLiKuWareHouse = "";
                    String numberOfLocalLiKu = "";
                    String regionLiKuModel = "";
                    String regionLiKuBatch = "";
                    String regionLiKuWareHouse = "";
                    String numberOfRegionalLiKu = "";
                    List<MaterialDetails> localList = details.getLocalList();
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    List<MaterialDetails> localRepList = details.getLocalRepList();
                    List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                    if (null != localList && localList.size() > 0) {
                        for (MaterialDetails a :
                                localList) {
                            localWareBatch += a.getLiKuBatch() + ",";
                            localWareHouse += a.getWarehouseLocation() + ",";
                            numberOfLocal += a.getNumberOfNiches() + ",";
                        }
                        details.setLocalWareBatch(localWareBatch);
                        details.setLocalWareHouse(localWareHouse);
                        details.setNumberOfLocal(numberOfLocal);
                    }
                    if (null != regionalList && regionalList.size() > 0) {
                        for (MaterialDetails a :
                                regionalList) {
                            regionalBatch += a.getLiKuBatch() + ",";
                            regionalHouse += a.getWarehouseLocation() + ",";
                            numberOfRegional += a.getNumberOfNiches() + ",";
                        }
                        details.setRegionalBatch(regionalBatch);
                        details.setRegionalHouse(regionalHouse);
                        details.setNumberOfRegional(numberOfRegional);
                    }
                    if (null != localRepList && localRepList.size() > 0) {
                        for (MaterialDetails a :
                                localRepList) {
                            localLiKuBatch += a.getLiKuBatch() + ",";
                            localLiKuWareHouse += a.getWarehouseLocation() + ",";
                            numberOfLocalLiKu += a.getNumberOfNiches() + ",";
                            ;
                        }
                        details.setLocalLiKuBatch(localLiKuBatch);
                        details.setLocalLiKuWareHouse(localLiKuWareHouse);
                        details.setNumberOfLocalLiKu(numberOfLocalLiKu);
                    }
                    if (null != regionalRepList && regionalRepList.size() > 0) {
                        for (MaterialDetails a :
                                regionalRepList) {
                            regionLiKuBatch += a.getLiKuBatch() + ",";
                            regionLiKuWareHouse += a.getWarehouseLocation() + ",";
                            numberOfRegionalLiKu += a.getNumberOfNiches() + ",";
                        }
                        details.setRegionLiKuBatch(regionLiKuBatch);
                        details.setRegionLiKuWareHouse(regionLiKuWareHouse);
                        details.setNumberOfRegionalLiKu(numberOfRegionalLiKu);
                    }
                    exportList.add(details);
                }
            }
            if (dYNonSegmentList.size() > 0) {
                for (MaterialReportDetails details :
                        dYNonSegmentList) {
                    String localWareBatch = "";
                    String localWareHouse = "";
                    String numberOfLocal = "";
                    String regionalBatch = "";
                    String regionalHouse = "";
                    String numberOfRegional = "";
                    String localLiKuModel = "";
                    String localLiKuBatch = "";
                    String localLiKuWareHouse = "";
                    String numberOfLocalLiKu = "";
                    String regionLiKuModel = "";
                    String regionLiKuBatch = "";
                    String regionLiKuWareHouse = "";
                    String numberOfRegionalLiKu = "";
                    List<MaterialDetails> localList = details.getLocalList();
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    List<MaterialDetails> localRepList = details.getLocalRepList();
                    List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                    if (null != localList && localList.size() > 0) {
                        for (MaterialDetails a :
                                localList) {
                            localWareBatch += a.getLiKuBatch() + ",";
                            localWareHouse += a.getWarehouseLocation() + ",";
                            numberOfLocal += a.getNumberOfNiches() + ",";
                        }
                        details.setLocalWareBatch(localWareBatch);
                        details.setLocalWareHouse(localWareHouse);
                        details.setNumberOfLocal(numberOfLocal);
                    }
                    if (null != regionalList && regionalList.size() > 0) {
                        for (MaterialDetails a :
                                regionalList) {
                            regionalBatch += a.getLiKuBatch() + ",";
                            regionalHouse += a.getWarehouseLocation() + ",";
                            numberOfRegional += a.getNumberOfNiches() + ",";
                        }
                        details.setRegionalBatch(regionalBatch);
                        details.setRegionalHouse(regionalHouse);
                        details.setNumberOfRegional(numberOfRegional);
                    }
                    if (null != localRepList && localRepList.size() > 0) {
                        for (MaterialDetails a :
                                localRepList) {
                            localLiKuBatch += a.getLiKuBatch() + ",";
                            localLiKuWareHouse += a.getWarehouseLocation() + ",";
                            numberOfLocalLiKu += a.getNumberOfNiches() + ",";
                            ;
                        }
                        details.setLocalLiKuBatch(localLiKuBatch);
                        details.setLocalLiKuWareHouse(localLiKuWareHouse);
                        details.setNumberOfLocalLiKu(numberOfLocalLiKu);
                    }
                    if (null != regionalRepList && regionalRepList.size() > 0) {
                        for (MaterialDetails a :
                                regionalRepList) {
                            regionLiKuBatch += a.getLiKuBatch() + ",";
                            regionLiKuWareHouse += a.getWarehouseLocation() + ",";
                            numberOfRegionalLiKu += a.getNumberOfNiches() + ",";
                        }
                        details.setRegionLiKuBatch(regionLiKuBatch);
                        details.setRegionLiKuWareHouse(regionLiKuWareHouse);
                        details.setNumberOfRegionalLiKu(numberOfRegionalLiKu);
                    }
                    exportList.add(details);
                }
            }
            if (jRNonSegmentList.size() > 0) {
                for (MaterialReportDetails details :
                        jRNonSegmentList) {
                    String localWareBatch = "";
                    String localWareHouse = "";
                    String numberOfLocal = "";
                    String regionalBatch = "";
                    String regionalHouse = "";
                    String numberOfRegional = "";
                    String localLiKuModel = "";
                    String localLiKuBatch = "";
                    String localLiKuWareHouse = "";
                    String numberOfLocalLiKu = "";
                    String regionLiKuModel = "";
                    String regionLiKuBatch = "";
                    String regionLiKuWareHouse = "";
                    String numberOfRegionalLiKu = "";
                    List<MaterialDetails> localList = details.getLocalList();
                    List<MaterialDetails> regionalList = details.getRegionalList();
                    List<MaterialDetails> localRepList = details.getLocalRepList();
                    List<MaterialDetails> regionalRepList = details.getRegionalRepList();
                    if (null != localList && localList.size() > 0) {
                        for (MaterialDetails a :
                                localList) {
                            localWareBatch += a.getLiKuBatch() + ",";
                            localWareHouse += a.getWarehouseLocation() + ",";
                            numberOfLocal += a.getNumberOfNiches() + ",";
                        }
                        details.setLocalWareBatch(localWareBatch);
                        details.setLocalWareHouse(localWareHouse);
                        details.setNumberOfLocal(numberOfLocal);
                    }
                    if (null != regionalList && regionalList.size() > 0) {
                        for (MaterialDetails a :
                                regionalList) {
                            regionalBatch += a.getLiKuBatch() + ",";
                            regionalHouse += a.getWarehouseLocation() + ",";
                            numberOfRegional += a.getNumberOfNiches() + ",";
                        }
                        details.setRegionalBatch(regionalBatch);
                        details.setRegionalHouse(regionalHouse);
                        details.setNumberOfRegional(numberOfRegional);
                    }
                    if (null != localRepList && localRepList.size() > 0) {
                        for (MaterialDetails a :
                                localRepList) {
                            localLiKuBatch += a.getLiKuBatch() + ",";
                            localLiKuWareHouse += a.getWarehouseLocation() + ",";
                            numberOfLocalLiKu += a.getNumberOfNiches() + ",";
                        }
                        details.setLocalLiKuBatch(localLiKuBatch);
                        details.setLocalLiKuWareHouse(localLiKuWareHouse);
                        details.setNumberOfLocalLiKu(numberOfLocalLiKu);
                    }
                    if (null != regionalRepList && regionalRepList.size() > 0) {
                        for (MaterialDetails a :
                                regionalRepList) {
                            regionLiKuBatch += a.getLiKuBatch() + ",";
                            regionLiKuWareHouse += a.getWarehouseLocation() + ",";
                            numberOfRegionalLiKu += a.getNumberOfNiches() + ",";
                        }
                        details.setRegionLiKuBatch(regionLiKuBatch);
                        details.setRegionLiKuWareHouse(regionLiKuWareHouse);
                        details.setNumberOfRegionalLiKu(numberOfRegionalLiKu);
                    }
                    exportList.add(details);
                }
            }
            //利库基础数据更新库存
            List<LiKuResourcePool> replaceList = new ArrayList<>();
            if (bBNonPoolList.size() > 0) {
                replaceList.addAll(bBNonPoolList);
            }
            if (yZNonPoolList.size() > 0) {
                replaceList.addAll(yZNonPoolList);
            }
            if (dYNonPoolList.size() > 0) {
                replaceList.addAll(dYNonPoolList);
            }
            if (dYNonPoolList.size() > 0) {
                replaceList.addAll(dYNonPoolList);
            }
            if (bBPoolList.size() > 0) {
                replaceList.addAll(bBPoolList);
            }
            if (yZPoolList.size() > 0) {
                replaceList.addAll(yZPoolList);
            }
            if (dYPoolList.size() > 0) {
                replaceList.addAll(dYPoolList);
            }
            if (jRPoolList.size() > 0) {
                replaceList.addAll(jRPoolList);
            }
            if (bBList.size() > 0) {
                replaceList.addAll(bBPoolList);
            }
            if (yZList.size() > 0) {
                replaceList.addAll(yZPoolList);
            }
            if (dYList.size() > 0) {
                replaceList.addAll(dYPoolList);
            }
            if (jRList.size() > 0) {
                replaceList.addAll(jRPoolList);
            }
            if (replaceList.size() > 0) {
                poolService.updateAllList(replaceList);
            }

            if (exportList.size() > 0) {
                service.delAll();
                service.insertList(exportList);
            }
            j.setSuccess(true);
            j.setMsg("已成功导入 " + list.size() + " 条物资上报明细记录");
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入物资上报明细失败！失败信息：" + e.getMessage());
        }
        return j;
    }

    /***
     * 通过项目编码，扩展编码，查询在物资上报中，符合利库数据集合，计算后续最优解
     * */
    private List<MaterialReportDetails> Calculation(List<MaterialReportDetails> bBSegmentList, String poolCode, String poExCode) {
        List<MaterialReportDetails> returnList = new ArrayList<>();
        for (MaterialReportDetails details :
                bBSegmentList) {
            String code = details.getMaterialCode();
            String exCode = details.getExtendedCoding();
            //物资需求数量
            BigDecimal total = BigDecimal.valueOf(details.getTotal());
            //利库数量
            BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
            if (poolCode.equals(code) && poExCode.equals(exCode) && total.compareTo(numberOfNiches) != 0) {
                returnList.add(details);
            }
        }
        return returnList;
    }

    /***
     * 通过项目编码，扩展编码，查询在物资上报中，符合利库数据集合，计算后续最优解-以大代小
     * */
    private List<MaterialReportDetails> CalculationRep(List<MaterialReportDetails> bBSegmentList, String poolCode, String poExCode) {
        List<MaterialReportDetails> returnList = new ArrayList<>();
        for (MaterialReportDetails details :
                bBSegmentList) {
            String code = details.getReplaceCode();
            String exCode = details.getReplaceExCode();
            //物资需求数量
            BigDecimal total = BigDecimal.valueOf(details.getTotal());
            //利库数量
            BigDecimal numberOfNiches = BigDecimal.valueOf(details.getNumberOfNiches());
            if (poolCode.equals(code) && poExCode.equals(exCode) && total.compareTo(numberOfNiches) != 0) {
                returnList.add(details);
            }
        }
        return returnList;
    }

    /**
     * 利库非段长排序方法，排序依据 库存日、库存数量、批次号
     * @param bBNonPoolList
     */
    private void poolSort(List<LiKuResourcePool> bBNonPoolList) {
        Comparator<LiKuResourcePool> reversed = Comparator.comparing(LiKuResourcePool::getDaysStorage).reversed();
        Comparator<LiKuResourcePool> comparator = Comparator.comparing(LiKuResourcePool::getInventoryAmount).reversed();
        Comparator<LiKuResourcePool> poolComparator = Comparator.comparing(LiKuResourcePool::getBatch);
        bBNonPoolList.stream().sorted(reversed.thenComparing(comparator).thenComparing(poolComparator));
    }

    /**
     * 利库段长排序方法，排序依据 库存日、库存数量、批次号、具体段长
     * @param bBNonPoolList
     */
    private void poolSegSort(List<LiKuResourcePool> bBNonPoolList) {
        Comparator<LiKuResourcePool> reversed = Comparator.comparing(LiKuResourcePool::getDaysStorage).reversed();
        Comparator<LiKuResourcePool> comparator = Comparator.comparing(LiKuResourcePool::getInventoryAmount).reversed();
        Comparator<LiKuResourcePool> poolComparator = Comparator.comparing(LiKuResourcePool::getBatch);
        Comparator<LiKuResourcePool> beComparator = Comparator.comparing(LiKuResourcePool::getLengthOfCable);
        bBNonPoolList.sort(reversed.thenComparing(comparator).thenComparing(poolComparator).thenComparing(beComparator));
    }

    /**
     * 下载物资上报明细导入模板
     */
    @ResponseBody
    @RequestMapping(value = "import/template")
    public AjaxJson importFileTemplate(HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            String fileName = "物料信息.xlsx";
            List<MaterialReportDetails> list = Lists.newArrayList();
            new ExportExcel("物料信息表", MaterialReportDetails.class, 1).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入模板下载失败！失败信息：" + e.getMessage());
        }
        return j;
    }

}