package com.ruoyi.inventory.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.mchange.lang.DoubleUtils;
import com.ruoyi.asset.domain.TbAssetInformation;
import com.ruoyi.asset.domain.TbLocation;
import com.ruoyi.asset.mapper.TbAssetInformationMapper;
import com.ruoyi.asset.mapper.TbLocationMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.inventory.domain.TbInventoryDetail;
import com.ruoyi.inventory.domain.dto.*;
import com.ruoyi.inventory.mapper.TbInventoryDetailMapper;
import com.ruoyi.utils.IDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.inventory.mapper.TbAssetInventoryMapper;
import com.ruoyi.inventory.domain.TbAssetInventory;
import com.ruoyi.inventory.service.ITbAssetInventoryService;
import org.springframework.web.bind.annotation.RequestMapping;

import static com.ruoyi.inventory.domain.enums.InventoryPlanResult.*;
import static com.ruoyi.inventory.domain.enums.InventoryPlanStatus.PLAN_FINISH;
import static com.ruoyi.inventory.domain.enums.InventoryResult.*;
import static com.ruoyi.inventory.domain.enums.InventoryStatus.COUNTED;
import static com.ruoyi.inventory.domain.enums.InventoryStatus.UNCOUNTED;

/**
 * 资产盘点记录Service业务层处理
 *
 * @author ydl
 * @date 2023-06-01
 */
@Service
public class TbAssetInventoryServiceImpl implements ITbAssetInventoryService
{
    @Autowired
    private TbAssetInventoryMapper tbAssetInventoryMapper;

    @Autowired
    private TbAssetInformationMapper informationMapper;

    @Autowired
    private TbInventoryDetailMapper tbInventoryDetailMapper;

    @Autowired
    private TbLocationMapper locationMapper;

    /**
     * 查询资产盘点记录
     *
     * @param id 资产盘点记录主键
     * @return 资产盘点记录
     */
    @Override
    public TbAssetInventory selectTbAssetInventoryById(Long id)
    {
        TbAssetInventory tbAssetInventory = tbAssetInventoryMapper.selectTbAssetInventoryById(id);
        String inventoryLocation = tbAssetInventory.getInventoryLocation();
        String locationName = getLocationName(inventoryLocation);
        tbAssetInventory.setInventoryLocationName(locationName);
        return tbAssetInventory;
    }

    @Override
    public TbAssetInventory selectTbAssetInventoryByNumber(String num) {
        TbAssetInventory tbAssetInventory = tbAssetInventoryMapper.selectTbAssetInventoryByNumber(num);
        String inventoryLocation = tbAssetInventory.getInventoryLocation();
        String locationName = getLocationName(inventoryLocation);
        tbAssetInventory.setInventoryLocationName(locationName);
        return tbAssetInventory;
    }

    /**
     * 查询资产盘点记录列表
     *
     * @param tbAssetInventory 资产盘点记录
     * @return 资产盘点记录
     */
    @Override
    public List<TbAssetInventory> selectTbAssetInventoryList(TbAssetInventory tbAssetInventory)
    {
        List<TbAssetInventory> tbAssetInventories = tbAssetInventoryMapper.selectTbAssetInventoryList(tbAssetInventory);
        for (TbAssetInventory assetInventory : tbAssetInventories) {
            String inventoryLocation = assetInventory.getInventoryLocation();
            String locationName = getLocationName(inventoryLocation);
            assetInventory.setInventoryLocationName(locationName);
        }
        return tbAssetInventories;
    }

    private String getLocationName(String locationIds) {
        String[] ids = locationIds.split(",");
        if (ids.length < 1) {
            return "";
        }
        List<TbLocation> tbLocations = locationMapper.selectTbLocationByIds(List.of(ids));
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < tbLocations.size(); i++) {
            if (i == tbLocations.size() - 1) {
                stringBuilder.append(tbLocations.get(i).getName());
                continue;
            }
            stringBuilder.append(tbLocations.get(i).getName());
            stringBuilder.append(",");
        }
        return stringBuilder.toString();
    }

    /**
     * 新增资产盘点记录
     *
     * @param tbAssetInventory 资产盘点记录
     * @return 结果
     */
    @Override
    public int insertTbAssetInventory(TbAssetInventory tbAssetInventory)
    {
        String orderNumber = IDUtil.getUUID1();
        tbAssetInventory.setOrderNumber(orderNumber);

        Integer recordStatus = tbAssetInventory.getRecordStatus();
        if (recordStatus > 0) {
            tbAssetInventory.setInventoryStatus(1);
        } else {
            tbAssetInventory.setInventoryStatus(0);
        }

        String inventoryLocation = tbAssetInventory.getInventoryLocation();
        List<TbInventoryDetail> inventoryDetails = getInventoryDetails(orderNumber, inventoryLocation);

        int insertTbAssetInventory = tbAssetInventoryMapper.insertTbAssetInventory(tbAssetInventory);
        if (insertTbAssetInventory == 0) {
            return 0;
        }

        for (TbInventoryDetail tbInventoryDetail : inventoryDetails) {
            tbInventoryDetailMapper.insertTbInventoryDetail(tbInventoryDetail);
        }

        return 1;
    }

    /**
     * 手持机盘点
     *
     * @param takeStockDTOList
     * @return
     */
    @Override
    public int handheldInventory(List<TakeStockDTO> takeStockDTOList) {
        int result = 0;
        for (TakeStockDTO takeStockDTO : takeStockDTOList) {
            // 获取单据编码
            String orderNumber = takeStockDTO.getOrderNumber();
            // 获取手持机的epc
            Set<String> epcSet = takeStockDTO.getAssetList()
                    .stream()
                    .map(tagInfo -> {
                        String epc = tagInfo.getEpc();
                        // 删除字符串中前导的零
                        epc = epc.replaceFirst("^0+", "");
                        return epc;
                    })
                    .collect(Collectors.toSet());
            if (StrUtil.isNotBlank(orderNumber)) { // 存在盘点计划
                TbAssetInventory tbAssetInventory = tbAssetInventoryMapper.selectTbAssetInventoryByNumber(orderNumber);
                if (tbAssetInventory == null) {
                    continue;
                }

                Integer stockResult = startStock(tbAssetInventory,  epcSet);
                if (stockResult > 0) {
                    result++;
                }
            } else { // 未存在盘点计划
                // 获取盘点位置编码
                String inventoryLocation = takeStockDTO.getInventoryLocation();
                List<String> locationNumbers = List.of(inventoryLocation.split(","));

                // 根据编号获取有效位置ID
                List<Long> locationIds = locationMapper.selectTbLocationIdByNumbers(locationNumbers);
                if (locationIds.isEmpty()) {
                    continue;
                }

                TbAssetInventory inventory = new TbAssetInventory();
                Date now = new Date();
                String nowDateTime = DateUtil.format(now, "yyyy年MM月dd日 HH:mm:ss");
                inventory.setName(nowDateTime + "临时盘点任务");
                inventory.setInventoryDepartment("100");
                inventory.setInventoryBy("admin"); // TODO 这里是否非admin登录，无法看到该条数据
                inventory.setInventoryDate(now);

                StringBuilder locationStrIds = new StringBuilder();
                for (int i = 0; i < locationIds.size(); i++) {
                    locationStrIds.append(locationIds.get(i));
                    if (i < locationIds.size() - 1) {
                        locationStrIds.append(",");
                    }
                }
                inventory.setInventoryLocation(locationStrIds.toString());
                inventory.setRecordStatus(1);

                // 新增盘点计划
                int insertResult = insertTbAssetInventory(inventory);
                if (insertResult < 1) { // 新增失败
                    continue;
                }

                // 获取单点单号
                String newOrderNumber = inventory.getOrderNumber();
                // 获取盘点计划
                TbAssetInventory tbAssetInventory = tbAssetInventoryMapper.selectTbAssetInventoryByNumber(newOrderNumber);

                Integer stockResult = startStock(tbAssetInventory, epcSet);
                if (stockResult > 0) {
                    result++;
                }
            }

        }
        return result > 0 ? 1 : 0;
    }

    /**
     * 开始盘点
     *
     * @param tbAssetInventory 盘点计划信息
     * @param epcSet 盘点epc集合
     */
    private Integer startStock(TbAssetInventory tbAssetInventory, Set<String> epcSet) {
        // 获取盘点单号
        String orderNumber = tbAssetInventory.getOrderNumber();

        int result = 0;

        // 根据盘点单号获取盘点资产详细
        List<TbInventoryDetail> tbInventoryDetails = tbInventoryDetailMapper.selectTbInventoryDetailByOrderNumber(orderNumber);
        for (TbInventoryDetail tbInventoryDetail : tbInventoryDetails) {
            // 获取资产元数据
            String inventoryMetadata = tbInventoryDetail.getInventoryMetadata();
            // 将元数据转换为资产信息
            TbAssetInformation assetInformation = JSONUtil.toBean(inventoryMetadata, TbAssetInformation.class);
            Integer assetQuantity = assetInformation.getQuantity();

            // 实际盘点数量统计
            int inventoryQuantity;

            // 获取该资产的所有epc
            Set<String> assetEpcSet = epcSet
                    .stream()
                    .filter(epc -> TbAssetInformation.encodeChildEpc2Epc(epc).equals(assetInformation.getEpc()))
                    .collect(Collectors.toSet());

            inventoryQuantity = assetEpcSet.size();
            // 删除已盘点epc
            epcSet.removeAll(assetEpcSet);

            if (inventoryQuantity > 0) {
                // 设置盘点更新状态：已盘点
                tbInventoryDetail.setInventoryStatus(COUNTED.getCode());
                // 设置盘点数量
                tbInventoryDetail.setInventoryQuantity(inventoryQuantity);

                if (inventoryQuantity > assetQuantity) { // 盘盈: 实际盘点数量 大于 资产账面数量
                    tbInventoryDetail.setInventoryResult(INVENTORY_PROFIT.getCode());
                } else if (inventoryQuantity == assetQuantity) { // 正常: 实际盘点数量 等于 资产账面数量
                    tbInventoryDetail.setInventoryResult(INVENTORY_NORMAL.getCode());
                } else {  // 盘亏: 实际盘点数量 小于 资产账面数量
                    tbInventoryDetail.setInventoryResult(INVENTORY_SHORTAGES.getCode());
                }
            } else {
                // 设置盘点更新状态：未盘点
                tbInventoryDetail.setInventoryStatus(UNCOUNTED.getCode());
            }

            int update = tbInventoryDetailMapper.updateTbInventoryDetail(tbInventoryDetail);
            if (update > 0) {
                if (result < 1) {
                    result = 1;
                }
            }
        }

        // 统计未盘点
        int uncountedCount = tbInventoryDetailMapper.countUncounted(orderNumber);

        if (uncountedCount > 0) { // 存在未盘点 盘亏
            tbAssetInventory.setInventoryResult(PLAN_INVENTORY_SHORTAGES.getCode());
        } else { // 不存在未盘点
            // 统计盘亏
            int inventoryShortagesCount = tbInventoryDetailMapper.countInventoryShortages(orderNumber);
            // 是否存在盘亏
            if (inventoryShortagesCount > 0) {
                tbAssetInventory.setInventoryResult(PLAN_INVENTORY_SHORTAGES.getCode());
            } else {
                // 统计盘盈
                int inventoryProfitCount = tbInventoryDetailMapper.countInventoryProfit(orderNumber);
                // 是否存在盘盈
                if (inventoryProfitCount > 0) {
                    tbAssetInventory.setInventoryResult(PLAN_INVENTORY_PROFIT.getCode());
                } else {
                    tbAssetInventory.setInventoryResult(PLAN_INVENTORY_NORMAL.getCode());
                }
            }
        }

        // 意外盘点资产EPC
        if (!epcSet.isEmpty()) {
            Set<String> unCountEpcSet = new HashSet<>();

            for (String epc : epcSet) {
                String unCountEpc = TbAssetInformation.encodeChildEpc2Epc(epc);
                // 存入未盘点资产epc中
                unCountEpcSet.add(unCountEpc);
            }

            // 根据意外资产epc获取资产信息
            List<TbAssetInformation> tbAssetInformationList = informationMapper.selectTbAssetInformationByEpcList(unCountEpcSet);

            for (TbAssetInformation tbAssetInformation : tbAssetInformationList) { // 盘点异常
                // 盘点意外的资产信息
                TbInventoryDetail tbInventoryDetail = TbInventoryDetail.errCounted(orderNumber, tbAssetInformation);

                Integer assetQuantity = tbAssetInformation.getQuantity();

                // 实际盘点数量统计
                int inventoryQuantity;

                // 获取该资产的所有epc
                Set<String> assetEpcSet = epcSet
                        .stream()
                        .filter(epc -> TbAssetInformation.encodeChildEpc2Epc(epc).equals(tbAssetInformation.getEpc()))
                        .collect(Collectors.toSet());

                inventoryQuantity = assetEpcSet.size();
                // 删除已盘点epc
                epcSet.removeAll(assetEpcSet);

                // 设置盘点数量
                tbInventoryDetail.setInventoryQuantity(inventoryQuantity);
                if (inventoryQuantity > assetQuantity) { // 盘盈: 实际盘点数量 大于 资产账面数量
                    tbInventoryDetail.setInventoryResult(INVENTORY_PROFIT.getCode());
                } else if (inventoryQuantity == assetQuantity) { // 正常: 实际盘点数量 等于 资产账面数量
                    tbInventoryDetail.setInventoryResult(INVENTORY_NORMAL.getCode());
                } else {  // 盘亏: 实际盘点数量 小于 资产账面数量
                    tbInventoryDetail.setInventoryResult(INVENTORY_SHORTAGES.getCode());
                }

                tbInventoryDetailMapper.insertTbInventoryDetail(tbInventoryDetail);
            }
        }

        // 设置盘点状态：盘点完毕
        tbAssetInventory.setInventoryStatus(PLAN_FINISH.getCode());
        tbAssetInventoryMapper.updateTbAssetInventory(tbAssetInventory);
        return result;
    }

    /**
     * 修改资产盘点记录
     *
     * @param tbAssetInventory 资产盘点记录
     * @return 结果
     */
    @Override
    public int updateTbAssetInventory(TbAssetInventory tbAssetInventory)
    {
        Integer recordStatus = tbAssetInventory.getRecordStatus();
        if (recordStatus > 0) {
            tbAssetInventory.setInventoryStatus(1);
        } else {
            tbAssetInventory.setInventoryStatus(0);
        }

        int update = tbAssetInventoryMapper.updateTbAssetInventory(tbAssetInventory);
        if (update < 1) {
            return 0;
        }

        String orderNumber = tbAssetInventory.getOrderNumber();
        tbInventoryDetailMapper.deleteTbInventoryDetailByOrderNumber(orderNumber);

        String inventoryLocation = tbAssetInventory.getInventoryLocation();
        List<TbInventoryDetail> inventoryDetails = getInventoryDetails(orderNumber, inventoryLocation);

        for (TbInventoryDetail tbInventoryDetail : inventoryDetails) {
            tbInventoryDetailMapper.insertTbInventoryDetail(tbInventoryDetail);
        }

        return 1;
    }

    private List<TbInventoryDetail> getInventoryDetails(String orderNumber, String inventoryLocation) {
        String[] locationIds = inventoryLocation.split(",");

        Set<String> locationNumberSet = new HashSet<>();
        for (String locationId : locationIds) {
            List<String> locationNumberList = locationMapper.selectChildTbLocationById(Long.valueOf(locationId));
            locationNumberSet.addAll(locationNumberList);
        }
        List<TbAssetInformation> tbAssetInformationList = informationMapper.selectAssetInformationByLocations(locationNumberSet);

        List<TbInventoryDetail> tbInventoryDetails = new ArrayList<>();
        for (TbAssetInformation tbAssetInformation : tbAssetInformationList) {
            // 未盘点
            TbInventoryDetail uncounted = TbInventoryDetail.uncounted(orderNumber, tbAssetInformation);
            tbInventoryDetails.add(uncounted);
        }

        return tbInventoryDetails;
    }

    /**
     * 批量删除资产盘点记录
     *
     * @param ids 需要删除的资产盘点记录主键
     * @return 结果
     */
    @Override
    public int deleteTbAssetInventoryByIds(Long[] ids)
    {
        List<String> orderNumbers = tbAssetInventoryMapper.selectOrderNumberByIds(List.of(ids));
        tbInventoryDetailMapper.deleteTbInventoryDetailByOrderNumbers(orderNumbers);
        return tbAssetInventoryMapper.deleteTbAssetInventoryByIds(ids);
    }

    /**
     * 删除资产盘点记录信息
     *
     * @param id 资产盘点记录主键
     * @return 结果
     */
    @Override
    public int deleteTbAssetInventoryById(Long id)
    {
        TbAssetInventory tbAssetInventory = tbAssetInventoryMapper.selectTbAssetInventoryById(id);
        if (tbAssetInventory == null) {
            return 0;
        }
        String orderNumber = tbAssetInventory.getOrderNumber();
        tbInventoryDetailMapper.deleteTbInventoryDetailByOrderNumbers(Collections.singletonList(orderNumber));
        return tbAssetInventoryMapper.deleteTbAssetInventoryById(id);
    }

    @Override
    public List<TbAssetInventoryDTO> selectTbAssetInventoryDTOList(TbAssetInventoryDTO dto) {
        List<TbAssetInventoryDTO> tbAssetInventoryDTOS = tbAssetInventoryMapper.selectTbAssetInventoryDTOList(dto);
        for (TbAssetInventoryDTO tbAssetInventoryDTO : tbAssetInventoryDTOS) {
            setLocationNameDTO(tbAssetInventoryDTO);
        }
        return tbAssetInventoryDTOS;
    }

    private void setLocationNameDTO(TbAssetInventoryDTO tbAssetInventoryDTO) {
        String inventoryLocation = tbAssetInventoryDTO.getInventoryLocation();
        String[] strLocationIds = inventoryLocation.split(",");
        List<TbLocation> tbLocations = locationMapper.selectTbLocationByIds(List.of(strLocationIds));
        String locationName = Arrays.toString(tbLocations.stream().map(TbLocation::getName).toArray());
        tbAssetInventoryDTO.setInventoryLocationName(locationName);
    }

    /**
     * 根据部门id统计最近两次盘点间的部门资产损耗情况
     * @param depId
     * @return
     */
    public List<DepAssetLossResultDto> inventoryDepAssetById(String depId) throws Exception {

        /**
           在for循环中查询数据库，数据量多的时候时间花销大
         */

        List<String> childDeptIdList = tbAssetInventoryMapper.getChildDeptId(depId);
        if(childDeptIdList == null || childDeptIdList.size()==0){
            throw new RuntimeException("不存在该部门id");
        }
        List<DepAssetLossResultDto> resultDtoList = new ArrayList<>();
        //历史盘点计划编码和日期
        List<AssetInvOrderDto> invs;
        for (String ch_depId : childDeptIdList) {
            invs = tbAssetInventoryMapper.getOrderNumberByDepId(ch_depId);
            if (ObjectUtil.isEmpty(invs) || invs.size()==0){
                continue;
            };

            AssetInvOrderDto new_inv = invs.get(0);
//        System.out.println("new-盘点计划："+new_inv);
            AssetInvOrderDto old_inv = null;
            if (invs.size()>1) old_inv = invs.get(1);

            DepAssetLossResultDto resultDto = null;

            if (!ObjectUtil.isNull(new_inv)) {
                resultDto = compareResult(new_inv,old_inv);
                resultDto.setDeptName(new_inv.getDeptName());
            }
            resultDtoList.add(resultDto);
        }


        //前前一次盘点计划编码和日期
//        TbAssetInventoryDTO old_inv = tbAssetInventoryMapper.getOrderNumberByDepId(depId);

        return resultDtoList;
    }

    /**
     * 处理资产信息，统计每一样资产的损耗情况
     * @param assetInfos
     * @param assetMap
     */
    private void handleAssetInfo(List<TbAssetInformation> assetInfos,
                                 Map<String, DepAssetLossDto> assetMap,
                                 Map<String, Integer> invQuantity_map){
        for (TbAssetInformation asset : assetInfos) {
            //资产位置
            // String loc = asset.getAncestorLocationName()+asset.getParentLocationName()+asset.getLocationName();
            String loc = asset.getLocationName();

            if (!assetMap.containsKey(asset.getName()+"-"+loc)){

                DepAssetLossDto lossDto = new DepAssetLossDto();
                lossDto.setAssetName(asset.getName());
                lossDto.setCategory(asset.getCategoryName());
                lossDto.setUnits(asset.getUnits());
                lossDto.setLocation(loc);
                //账面数量
                lossDto.setNowQuantity(asset.getQuantity());
                //实点数量
                lossDto.setInvQuantity(invQuantity_map.get(asset.getName()+"-"+loc));
                //损耗数量 = 账面数量 - 实点数量
                lossDto.setLossQuantity(lossDto.getNowQuantity()-lossDto.getInvQuantity());
                //损耗值
                Double a = asset.getOriginalValue(); //原值
                Double b = asset.getNetValue();      //净值
                Double c = asset.getResidualValue(); //残值
                if (lossDto.getLossQuantity()>0){
                    if (ObjectUtil.isNull(a) || ObjectUtil.isNull(b) || ObjectUtil.isNull(c)) {
                        lossDto.setLossVal(0);
                    } else {
                        lossDto.setLossVal((a-b)*invQuantity_map.get(asset.getName()+"-"+loc)
                                + (a-c)*lossDto.getLossQuantity());
                    }
                } else {
                    if (ObjectUtil.isNull(a) || ObjectUtil.isNull(b)){
                        lossDto.setLossVal(0);
                    } else {
                        lossDto.setLossVal((a-b)*invQuantity_map.get(asset.getName()+"-"+loc));
                    }
                }
                assetMap.put(asset.getName()+"-"+loc,lossDto);
            } else {
                DepAssetLossDto lossDto = assetMap.get(asset.getName()+"-"+loc);
                //账面数量
                lossDto.setNowQuantity(lossDto.getNowQuantity()+asset.getQuantity());
                //损耗数量
                lossDto.setLossQuantity(lossDto.getNowQuantity()-lossDto.getInvQuantity());
                //损耗值
                Double a = asset.getOriginalValue(); //原值
                Double b = asset.getNetValue();      //净值
                Double c = asset.getResidualValue(); //残值
                if (lossDto.getLossQuantity()>0){
                    if (ObjectUtil.isNull(a) || ObjectUtil.isNull(b) || ObjectUtil.isNull(c)) {
                        lossDto.setLossVal(0);
                    } else {
                        lossDto.setLossVal((a-b)*invQuantity_map.get(asset.getName()+"-"+loc)
                                + (a-c)*lossDto.getLossQuantity());
                    }
                } else {
                    if (ObjectUtil.isNull(a) || ObjectUtil.isNull(b)){
                        lossDto.setLossVal(0);
                    } else {
                        lossDto.setLossVal((a-b)*invQuantity_map.get(asset.getName()+"-"+loc));
                    }
                }
            }
        }
    }

    /**
     * 统计并对比两次盘点的资产数据
     * @param new_invPlan
     * @return
     */
    private DepAssetLossResultDto compareResult(AssetInvOrderDto new_invPlan, AssetInvOrderDto old_invPlan){

        //----------------------最近一次---------------------------
        //资产统计
        Map<String, DepAssetLossDto> new_assetMap = new HashMap<>();
        //记录实点数量
        Map<String, Integer> new_invQuantity_map = new HashMap<>();

        getInvResult(new_invPlan, new_assetMap, new_invQuantity_map);

        //---------------------前前一次----------------------------
        //资产统计
        Map<String, DepAssetLossDto> old_assetMap = new HashMap<>();
        //记录实点数量
        Map<String, Integer> old_invQuantity_map = new HashMap<>();

        boolean result = getInvResult(old_invPlan, old_assetMap, old_invQuantity_map);
        //如果只有一次盘点，则直接返回资产数据
        if (!result){
            ArrayList<DepAssetLossDto> resultList = new ArrayList<>();
            Set<Map.Entry<String, DepAssetLossDto>> assets = new_assetMap.entrySet();
            for (Map.Entry<String, DepAssetLossDto> asset : assets) {
                resultList.add(asset.getValue());
            }
            DepAssetLossResultDto resultDto = new DepAssetLossResultDto();
            resultDto.setCompareResultList(resultList);
            resultDto.setLastFirstDate(new_invPlan.getInventDate());
            resultDto.setLastSecondDate(null);
            return resultDto;
        }
        //比较两次盘点(最近一次的减去前前次的)，并将比较结果转到list
        ArrayList<DepAssetLossDto> compareResultList = new ArrayList<>();
        Set<Map.Entry<String, DepAssetLossDto>> assets = new_assetMap.entrySet();
        for (Map.Entry<String, DepAssetLossDto> asset : assets) {

            DepAssetLossDto new_ = asset.getValue();
            if (!old_assetMap.containsKey(asset.getKey())) {
                compareResultList.add(new_);
                continue;
            }
            DepAssetLossDto old_ = old_assetMap.get(asset.getKey());
            new_.setNowQuantity(new_.getNowQuantity()-old_.getNowQuantity());
            new_.setInvQuantity(new_.getInvQuantity()-old_.getInvQuantity());
            new_.setLossQuantity(new_.getLossQuantity()-old_.getLossQuantity());
            new_.setLossVal(new_.getLossVal()-old_.getLossVal());

            compareResultList.add(new_);
        }
        //封装结果
        DepAssetLossResultDto resultDto = new DepAssetLossResultDto();
//        resultDto.setTotalLossVal(lossVal);
        resultDto.setCompareResultList(compareResultList);
        resultDto.setLastFirstDate(new_invPlan.getInventDate());
        resultDto.setLastSecondDate(old_invPlan.getInventDate());

        return resultDto;
    }

    //统计
    private boolean getInvResult(AssetInvOrderDto invPlan,
                              Map<String, DepAssetLossDto> assetMap,
                              Map<String, Integer> invQuantity_map){

        if (ObjectUtil.isNull(invPlan)) return false;

        List<DepAssetDetailDto> asset_list = tbAssetInventoryMapper.getDepAssetByOrderNumber(invPlan.getOrderNumber());
        //盘点资产详细信息-元数据
        List<TbAssetInformation> assetInfos = new ArrayList<>();

        //json数据 --> java对象
        for (DepAssetDetailDto asset : asset_list) {
            TbAssetInformation info = JSONUtil.toBean(asset.getAssetData(), TbAssetInformation.class);
            assetInfos.add(info);
            //位置
            // String loc = info.getAncestorLocationName()+info.getParentLocationName()+info.getLocationName();
            String loc = info.getLocationName();

            if (!invQuantity_map.containsKey(info.getName()+"-"+loc)) {
                invQuantity_map.put(info.getName() + "-" + loc, asset.getInventoryQuantity());
            } else {
                invQuantity_map.put(
                        info.getName() + "-" + loc,
                        invQuantity_map.get(info.getName()+"-"+loc) + asset.getInventoryQuantity()
                );
            }
        }

        //统计盘点计划每一样资产（同资产名且同位置）的相关信息
        handleAssetInfo(assetInfos,assetMap,invQuantity_map);
        return true;
    }

}

