/**
 * 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.LikuDetail;
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.CompareHistoryService;
import com.jeeplus.modules.sg.balancedlibrary.materialReportDetails.service.MaterialReportDetailsService;
import com.jeeplus.modules.sg.balancedlibrary.materialReportDetails.utils.AdjustUtil;
import com.jeeplus.modules.sg.balancedlibrary.materialReportDetails.utils.ExportUtil;
import com.jeeplus.modules.sg.balancedlibrary.replaceMaterial.entity.ReplaceMaterial;
import com.jeeplus.modules.sg.balancedlibrary.replaceMaterial.service.ReplaceMaterialService;
import com.jeeplus.modules.sg.managementcenter.materialproject.entity.MaterialProject;
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.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

import static java.math.BigDecimal.ROUND_HALF_UP;

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

    @Autowired
    private MaterialReportDetailsService service;

    @Autowired
    private LiKuResourcePoolService poolService;

    @Autowired
    private ReplaceMaterialService replaceMaterialService;

    @Autowired
    private CompareHistoryService compareHistoryService;

    @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";
    }

    /**
     * 利库明细页面
     */
    @RequiresPermissions("sg:balancedlibrary:MaterialReportDetails:list")
    @RequestMapping(value = "likuResults")
    public String likuResults(MaterialReportDetails entity, Model model) {
        model.addAttribute("entity", entity);
        return "modules/sg/balancedlibrary/materialReportDetails/LikuResults";
    }

    /**
     * 利库明细数据
     */
    @ResponseBody
    @RequiresPermissions("sg:balancedlibrary:MaterialReportDetails:list")
    @RequestMapping(value = "likuResultsData")
    public Map<String, Object> likuResultsData(MaterialReportDetails entity, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<LikuDetail> list = service.getLikuDetails(entity);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", list);
        map.put("total", list.size());
        return map;
    }

    /**
     * 物资上报明细列表数据
     */
    @ResponseBody
    @RequiresPermissions("sg:balancedlibrary:MaterialReportDetails:list")
    @RequestMapping(value = "data")
    public Map<String, Object> data(MaterialReportDetails entity, HttpServletRequest request, HttpServletResponse response, Model model) {
        HttpSession session = request.getSession();
        session.setAttribute("materialReportDetails", entity);
        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 void exportFile(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        MaterialReportDetails entity = (MaterialReportDetails) session.getAttribute("materialReportDetails");
        entity.setPage(null);
        List<MaterialReportDetails> reportList = service.findList(entity);
        List<LikuDetail> likuList = service.getLikuDetails(entity);
        ExportUtil util = new ExportUtil();
        util.exportDetails(response,reportList,likuList);
    }

    /**
     * 物资上报明细-导入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> 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);
            /*****************************物资上报明细*********************************************/
            //根据段长非段长、区域 分到不同list，同时设置初始利库数量及申请数量
            for (MaterialReportDetails details : list) {
                details.preInsert(); //先给个Id
                //设置初始利库数量为0，初始申请数量为物资的表格数量
                details.setNumberOfNiches(0.0);
                details.setNumberOfApplications(details.getTotal());

                Integer theUnitMeter = LiKuUtils.isTheUnitMeter(details.getUnit());//判断是否段长
                Integer area = LiKuUtils.judgmentArea(details.getReportingArea());//判断区域
                if (theUnitMeter.equals(LiKuUtils.YES)) {
                    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);
                    }
                } else {
                    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 (bBSegmentList.size() > 0) {
                bBSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
            }
            //扬中段长数据
            if (yZSegmentList.size() > 0) {
                yZSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
            }
            //丹阳段长数据
            if (dYSegmentList.size() > 0) {
                dYSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
            }
            //句容段长数据
            if (jRSegmentList.size() > 0) {
                jRSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
            }

            //同地区上报表中先根据项目名称顺序排序匹配，然后依据表格顺序依次排序匹配
            //本部数据-非段长
            if (bBNonSegmentList.size() > 0) {
                bBNonSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
            }
            //扬中数据-非段长
            if (yZNonSegmentList.size() > 0) {
                yZNonSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
            }
            //丹阳数据-非段长
            if (dYNonSegmentList.size() > 0) {
                dYNonSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
            }
            //句容数据-非段长
            if (jRNonSegmentList.size() > 0) {
                jRNonSegmentList.sort(Comparator.comparing(MaterialReportDetails::getNameOfReserve));
            }

            //先根据
            /*****************************利库资源数据*********************************************/
            //利库资源池数据，根据本部>扬中>丹阳>句容分组
            List<LiKuResourcePool> poolList = poolService.findLiKuList(null,null);
            if(poolList==null||poolList.size()==0){
                j.setSuccess(false);
                j.setMsg("请先导入利库资源池！");
                return j;
            }
            //查询出所有本部资源数据-非段长
            List<LiKuResourcePool> bBNonPoolList = new ArrayList<>();
            //查询出所有扬中资源数据-非段长
            List<LiKuResourcePool> yZNonPoolList = new ArrayList<>();
            //查询出所有丹阳资源数据-非段长
            List<LiKuResourcePool> dYNonPoolList = new ArrayList<>();
            //查询出所有句容资源数据-非段长
            List<LiKuResourcePool> jRNonPoolList = new ArrayList<>();

            //查询出所有本部资源数据-段长
            List<LiKuResourcePool> bBPoolList = new ArrayList<>();
            //查询出所有扬中资源数据-段长
            List<LiKuResourcePool> yZPoolList = new ArrayList<>();
            //查询出所有丹阳资源数据-段长
            List<LiKuResourcePool> dYPoolList = new ArrayList<>();
            //查询出所有句容资源数据-段长
            List<LiKuResourcePool> jRPoolList = new ArrayList<>();
            //资源池分类同时每次设置初始利库数和剩余数
            for(LiKuResourcePool pool:poolList){
                pool.setNumberOfNiches(0.0);
                Integer isSectionLength = pool.getIsSectionLength();
                Integer whereFrom = pool.getWhereFrom();
                if(isSectionLength.equals(LiKuUtils.NO)){
                    pool.setRemaining(pool.getInventoryAmount());
                    if(whereFrom==LiKuUtils.BB_FLAG){
                        bBNonPoolList.add(pool);
                    }else if(whereFrom==LiKuUtils.YZ_FLAG){
                        yZNonPoolList.add(pool);
                    }else if(whereFrom==LiKuUtils.DY_FLAG){
                        dYNonPoolList.add(pool);
                    }else{
                        jRNonPoolList.add(pool);
                    }
                }else{
                    pool.setRemaining(pool.getLengthOfCable());
                    if(whereFrom==LiKuUtils.BB_FLAG){
                        bBPoolList.add(pool);
                    }else if(whereFrom==LiKuUtils.YZ_FLAG){
                        yZPoolList.add(pool);
                    }else if(whereFrom==LiKuUtils.DY_FLAG){
                        dYPoolList.add(pool);
                    }else{
                        jRPoolList.add(pool);
                    }
                }
            }

            //同地区库龄降序>数量大小降序>批次号升序
            //本部利库资源池数据-非段长
            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) {
                AdjustUtil.nonSegAdjust(bBNonSegmentList,bBNonPoolList,compareHistories,AdjustUtil.LOCAL_ADJUST);
            }
            //扬中-扬中
            if (yZNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(yZNonSegmentList,yZNonPoolList,compareHistories,AdjustUtil.LOCAL_ADJUST);
            }
            //丹阳-丹阳
            if (dYNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(dYNonSegmentList,dYNonPoolList,compareHistories,AdjustUtil.LOCAL_ADJUST);
            }
            //句容-句容
            if (jRNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(jRNonSegmentList,jRNonPoolList,compareHistories,AdjustUtil.LOCAL_ADJUST);
            }

            //本部-扬中
            if (bBNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(bBNonSegmentList,yZNonPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //本部-丹阳
            if (bBNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(bBNonSegmentList,dYNonPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //本部-句容
            if (bBNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(bBNonSegmentList,jRNonPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }

            //扬中-本部
            if (yZNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(yZNonSegmentList,bBNonPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //杨中-丹阳
            if (yZNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(yZNonSegmentList,dYNonPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //扬中-句容
            if (yZNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(yZNonSegmentList,jRNonPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //丹阳-本部
            if (dYNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(dYNonSegmentList,bBNonPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //丹阳-扬中
            if (dYNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(dYNonSegmentList,yZNonPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //丹阳-句容
            if (dYNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(dYNonSegmentList,jRNonPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //句容-本部
            if (jRNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(jRNonSegmentList,bBNonPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //句容-扬中
            if (jRNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(jRNonSegmentList,yZNonPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //句容-丹阳
            if (jRNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                AdjustUtil.nonSegAdjust(jRNonSegmentList,dYNonPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }

            /*************************************非段长以大代小******************************************************/
            //查找库中所有的以大代小的数据list
            List<ReplaceMaterial> replaceList = replaceMaterialService.findAll();
            if (replaceList.size() > 0) {
                //本部-本部本地以大代小
                if (bBNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(bBNonSegmentList,bBNonPoolList,compareHistories,AdjustUtil.REP_LOCAL_ADJUST,replaceList);
                }

                //扬中-扬中本地以大代小
                if (yZNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(yZNonSegmentList,yZNonPoolList,compareHistories,AdjustUtil.REP_LOCAL_ADJUST,replaceList);
                }
                //丹阳-丹阳本地以大代小
                if (dYNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(dYNonSegmentList,dYNonPoolList,compareHistories,AdjustUtil.REP_LOCAL_ADJUST,replaceList);
                }
                //句容-句容本地以大代小
                if (jRNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(jRNonSegmentList,jRNonPoolList,compareHistories,AdjustUtil.REP_LOCAL_ADJUST,replaceList);
                }


                //本部-扬中跨域以大代小
                if (bBNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(bBNonSegmentList,yZNonPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //本部-丹阳跨域以大代小
                if (bBNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(bBNonSegmentList,dYNonPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //本部-句容跨域以大代小
                if (bBNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(bBNonSegmentList,jRNonPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }

                //扬中-本部跨域以大代小
                if (yZNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(yZNonSegmentList,bBNonPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //扬中-丹阳跨域以大代小
                if (yZNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(yZNonSegmentList,dYNonPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //扬中-句容跨域以大代小
                if (yZNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(yZNonSegmentList,jRNonPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }

                //丹阳-本部跨域以大代小
                if (dYNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(dYNonSegmentList,bBNonPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //丹阳-扬中跨域以大代小
                if (dYNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(dYNonSegmentList,yZNonPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //丹阳-句容跨域以大代小
                if (dYNonSegmentList.size() > 0 && jRNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(dYNonSegmentList,jRNonPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }

                //句容-本部跨域以大代小
                if (jRNonSegmentList.size() > 0 && bBNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(jRNonSegmentList,bBNonPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }

                //句容-扬中跨域以大代小
                if (jRNonSegmentList.size() > 0 && yZNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(jRNonSegmentList,yZNonPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //句容-丹阳跨域以大代小
                if (jRNonSegmentList.size() > 0 && dYNonPoolList.size() > 0) {
                    AdjustUtil.nonSegRepAdjust(jRNonSegmentList,dYNonPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }

            }

            /****************************************段长比对*****************************************************/
            //本部-本部
            if (bBPoolList.size() > 0 && bBSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(bBSegmentList,bBPoolList,compareHistories,AdjustUtil.LOCAL_ADJUST);
                AdjustUtil.segAdjust(bBSegmentList,bBPoolList,compareHistories,AdjustUtil.LOCAL_ADJUST);
            }
            //扬中-扬中
            if (yZPoolList.size() > 0 && yZSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(yZSegmentList,yZPoolList,compareHistories,AdjustUtil.LOCAL_ADJUST);
                AdjustUtil.segAdjust(yZSegmentList,yZPoolList,compareHistories,AdjustUtil.LOCAL_ADJUST);
            }
            //丹阳-丹阳-段长
            if (dYPoolList.size() > 0 && dYSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(dYSegmentList,dYPoolList,compareHistories,AdjustUtil.LOCAL_ADJUST);
                AdjustUtil.segAdjust(dYSegmentList,dYPoolList,compareHistories,AdjustUtil.LOCAL_ADJUST);
            }
            //句容-句容-段长
            if (jRPoolList.size() > 0 && jRSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(jRSegmentList,jRPoolList,compareHistories,AdjustUtil.LOCAL_ADJUST);
                AdjustUtil.segAdjust(jRSegmentList,jRPoolList,compareHistories,AdjustUtil.LOCAL_ADJUST);
            }

            //本部-扬中-段长
            if (bBPoolList.size() > 0 && yZSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(yZSegmentList,bBPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
                AdjustUtil.segAdjust(yZSegmentList,bBPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //本部-丹阳-段长
            if (bBPoolList.size() > 0 && dYSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(dYSegmentList,bBPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
                AdjustUtil.segAdjust(dYSegmentList,bBPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //本部-句容-段长
            if (bBPoolList.size() > 0 && jRSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(jRSegmentList,bBPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
                AdjustUtil.segAdjust(jRSegmentList,bBPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }

            //杨中-本部-段长
            if (yZPoolList.size() > 0 && bBSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(bBSegmentList,yZPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
                AdjustUtil.segAdjust(bBSegmentList,yZPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //杨中-丹阳-段长
            if (yZPoolList.size() > 0 && dYSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(dYSegmentList,yZPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
                AdjustUtil.segAdjust(dYSegmentList,yZPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //杨中-句容-段长
            if (yZPoolList.size() > 0 && jRSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(jRSegmentList,yZPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
                AdjustUtil.segAdjust(jRSegmentList,yZPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }

            //丹阳-本部-段长
            if (dYPoolList.size() > 0 && bBSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(bBSegmentList,dYPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
                AdjustUtil.segAdjust(bBSegmentList,dYPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //丹阳-扬中-段长
            if (dYPoolList.size() > 0 && yZSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(yZSegmentList,dYPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
                AdjustUtil.segAdjust(yZSegmentList,dYPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //丹阳-句容-段长
            if (dYPoolList.size() > 0 && jRSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(jRSegmentList,dYPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
                AdjustUtil.segAdjust(jRSegmentList,dYPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }


            //句容-本部-段长
            if (jRPoolList.size() > 0 && bBSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(bBSegmentList,jRPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
                AdjustUtil.segAdjust(bBSegmentList,jRPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //句容-扬中-段长
            if (jRPoolList.size() > 0 && yZSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(yZSegmentList,jRPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
                AdjustUtil.segAdjust(yZSegmentList,jRPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }
            //句容-丹阳-段长
            if (jRPoolList.size() > 0 && dYSegmentList.size() > 0) {
                AdjustUtil.segAdjustReverse(dYSegmentList,jRPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
                AdjustUtil.segAdjust(dYSegmentList,jRPoolList,compareHistories,AdjustUtil.SPAN_ADJUST);
            }

            /******************************************合并利库************************************/
            //将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);
            }
            //将四个区域的段长利库数据合并
            List<LiKuResourcePool> fourLikuList = new ArrayList<>();
            if(bBPoolList.size()>0){
                fourLikuList.addAll(bBPoolList);
            }
            if(yZPoolList.size()>0){
                fourLikuList.addAll(yZPoolList);
            }
            if(dYPoolList.size()>0){
                fourLikuList.addAll(dYPoolList);
            }
            if(jRPoolList.size()>0){
                fourLikuList.addAll(jRPoolList);
            }

            //本部-所有
            if (bBSegmentList.size() > 0 && fourLikuList.size() > 0) {
                AdjustUtil.segAdjustReverse(bBSegmentList,fourLikuList,compareHistories,AdjustUtil.COMBINE_ADJUST);
            }
            //杨中-所有
            if (yZSegmentList.size() > 0 && fourLikuList.size() > 0) {
                AdjustUtil.segAdjustReverse(yZSegmentList,fourLikuList,compareHistories,AdjustUtil.COMBINE_ADJUST);
            }
            //丹阳-所有
            if (dYSegmentList.size() > 0 && fourLikuList.size() > 0) {
                AdjustUtil.segAdjustReverse(dYSegmentList,fourLikuList,compareHistories,AdjustUtil.COMBINE_ADJUST);
            }
            //句容-所有
            if (jRSegmentList.size() > 0 && fourLikuList.size() > 0) {
                AdjustUtil.segAdjustReverse(jRSegmentList,fourLikuList,compareHistories,AdjustUtil.COMBINE_ADJUST);
            }

            //本部-所有
            if (bBPoolList.size() > 0 && fourAreaList.size() > 0) {
                AdjustUtil.segAdjust(fourAreaList,bBPoolList,compareHistories,AdjustUtil.COMBINE_ADJUST);
            }
            //杨中-所有
            if (yZPoolList.size() > 0 && fourAreaList.size() > 0) {
                AdjustUtil.segAdjust(fourAreaList,yZPoolList,compareHistories,AdjustUtil.COMBINE_ADJUST);
            }
            //丹阳-所有
            if (dYPoolList.size() > 0 && fourAreaList.size() > 0) {
                AdjustUtil.segAdjust(fourAreaList,dYPoolList,compareHistories,AdjustUtil.COMBINE_ADJUST);
            }
            //句容-所有
            if (jRPoolList.size() > 0 && fourAreaList.size() > 0) {
                AdjustUtil.segAdjust(fourAreaList,jRPoolList,compareHistories,AdjustUtil.COMBINE_ADJUST);
            }

            //本部-扬中-丹阳-句容
            /****************************************段长以大代小********************************************/
            if (replaceList.size() > 0) {
                //本部-本部-段长以大代小
                if (bBPoolList.size() > 0 && bBSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(bBSegmentList,bBPoolList,compareHistories,AdjustUtil.REP_LOCAL_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(bBSegmentList,bBPoolList,compareHistories,AdjustUtil.REP_LOCAL_ADJUST,replaceList);
                }
                //扬中-扬中-段长以大代小
                if (yZPoolList.size() > 0 && yZSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(yZSegmentList,yZPoolList,compareHistories,AdjustUtil.REP_LOCAL_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(yZSegmentList,yZPoolList,compareHistories,AdjustUtil.REP_LOCAL_ADJUST,replaceList);
                }
                //丹阳-丹阳-段长以大代小
                if (dYPoolList.size() > 0 && dYSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(dYSegmentList,dYPoolList,compareHistories,AdjustUtil.REP_LOCAL_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(dYSegmentList,dYPoolList,compareHistories,AdjustUtil.REP_LOCAL_ADJUST,replaceList);
                }
                //句容-句容-段长以大代小
                if (jRPoolList.size() > 0 && jRSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(jRSegmentList,jRPoolList,compareHistories,AdjustUtil.REP_LOCAL_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(jRSegmentList,jRPoolList,compareHistories,AdjustUtil.REP_LOCAL_ADJUST,replaceList);
                }


                //本部-扬中-段长以大代小
                if (bBPoolList.size() > 0 && yZSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(yZSegmentList,bBPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(yZSegmentList,bBPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //本部-丹阳-段长以大代小
                if (bBPoolList.size() > 0 && dYSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(dYSegmentList,bBPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(dYSegmentList,bBPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //本部-句容-段长以大代小
                if (bBPoolList.size() > 0 && jRSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(jRSegmentList,bBPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(jRSegmentList,bBPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }

                //杨中-本部-段长以大代小
                if (yZPoolList.size() > 0 && bBSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(bBSegmentList,yZPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(bBSegmentList,yZPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //杨中-丹阳-段长以大代小
                if (yZPoolList.size() > 0 && dYSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(dYSegmentList,yZPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(dYSegmentList,yZPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //杨中-句容-段长以大代小
                if (yZPoolList.size() > 0 && jRSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(jRSegmentList,yZPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(jRSegmentList,yZPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }

                //丹阳-本部-段长以大代小
                if (dYPoolList.size() > 0 && bBSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(bBSegmentList,dYPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(bBSegmentList,dYPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //丹阳-扬中-段长以大代小
                if (dYPoolList.size() > 0 && yZSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(yZSegmentList,dYPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(yZSegmentList,dYPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //丹阳-句容-段长以大代小
                if (dYPoolList.size() > 0 && jRSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(jRSegmentList,dYPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(jRSegmentList,dYPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }

                //句容-本部-段长以大代小
                if (jRPoolList.size() > 0 && bBSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(bBSegmentList,jRPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(bBSegmentList,jRPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //句容-扬中-段长以大代小
                if (jRPoolList.size() > 0 && yZSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(yZSegmentList,jRPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(yZSegmentList,jRPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                //句容-丹阳-段长以大代小
                if (jRPoolList.size() > 0 && yZSegmentList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(yZSegmentList,jRPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                    AdjustUtil.segRepAdjust(yZSegmentList,jRPoolList,compareHistories,AdjustUtil.REP_SPAN_ADJUST,replaceList);
                }
                /******************************************以大代小合并利库************************************/
                //以大代小合并利库

                //本部-所有
                if (bBSegmentList.size() > 0 && fourLikuList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(bBSegmentList,fourLikuList,compareHistories,AdjustUtil.REP_COMBINE_ADJUST,replaceList);
                }
                //杨中-所有
                if (yZSegmentList.size() > 0 && fourLikuList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(yZSegmentList,fourLikuList,compareHistories,AdjustUtil.REP_COMBINE_ADJUST,replaceList);
                }
                //丹阳-所有
                if (dYSegmentList.size() > 0 && fourLikuList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(dYSegmentList,fourLikuList,compareHistories,AdjustUtil.REP_COMBINE_ADJUST,replaceList);
                }
                //句容-所有
                if (jRSegmentList.size() > 0 && fourLikuList.size() > 0) {
                    AdjustUtil.segRepAdjustReverse(jRSegmentList,fourLikuList,compareHistories,AdjustUtil.REP_COMBINE_ADJUST,replaceList);
                }

                //本部-所有
                if (bBPoolList.size() > 0 && fourAreaList.size() > 0) {
                    AdjustUtil.segRepAdjust(fourAreaList,bBPoolList,compareHistories,AdjustUtil.REP_COMBINE_ADJUST,replaceList);
                }
                //杨中-所有
                if (yZPoolList.size() > 0 && fourAreaList.size() > 0) {
                    AdjustUtil.segRepAdjust(fourAreaList,yZPoolList,compareHistories,AdjustUtil.REP_COMBINE_ADJUST,replaceList);
                }
                //丹阳-所有
                if (dYPoolList.size() > 0 && fourAreaList.size() > 0) {
                    AdjustUtil.segRepAdjust(fourAreaList,dYPoolList,compareHistories,AdjustUtil.REP_COMBINE_ADJUST,replaceList);
                }
                //句容-所有
                if (jRPoolList.size() > 0 && fourAreaList.size() > 0) {
                    AdjustUtil.segRepAdjust(fourAreaList,jRPoolList,compareHistories,AdjustUtil.REP_COMBINE_ADJUST,replaceList);
                }

            }


            /*****************************************调整记录保存**************************************************/
            if (compareHistories.size() > 0) {
                compareHistoryService.delAll();
                compareHistoryService.insertList(compareHistories);
            }
            /**********************************更新利库资源池，插入申请物资数据**************************************/

            //利库基础数据更新库存
            poolService.updateAllList(poolList);

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



    /**
     * 利库非段长排序方法，排序依据 库存日、库存数量、批次号
     * @param bBNonPoolList
     */
    private void poolSort(List<LiKuResourcePool> bBNonPoolList) {
        try {
            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));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 利库段长排序方法，排序依据 库存日、批次号、具体段长
     * @param bBNonPoolList
     */
    private void poolSegSort(List<LiKuResourcePool> bBNonPoolList) {
        try {
            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.stream().sorted(reversed.thenComparing(poolComparator).thenComparing(beComparator));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 下载物资上报明细导入模板
     */
    @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;
    }

}