package com.ruoyi.inventory.service.impl;

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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
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.inventory.domain.TbAssetInventory;
import com.ruoyi.inventory.domain.TbInventoryCompare;
import com.ruoyi.inventory.domain.enums.InventoryPlanResult;
import com.ruoyi.inventory.domain.enums.InventoryPlanStatus;
import com.ruoyi.inventory.domain.enums.InventoryResult;
import com.ruoyi.inventory.domain.enums.InventoryStatus;
import com.ruoyi.inventory.mapper.TbAssetInventoryMapper;
import com.ruoyi.utils.RecursionUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.inventory.mapper.TbInventoryDetailMapper;
import com.ruoyi.inventory.domain.TbInventoryDetail;
import com.ruoyi.inventory.service.ITbInventoryDetailService;

/**
 * 资产盘点明细Service业务层处理
 * 
 * @author ydl
 * @date 2023-06-01
 */
@Service
public class TbInventoryDetailServiceImpl implements ITbInventoryDetailService 
{
    @Autowired
    private TbInventoryDetailMapper tbInventoryDetailMapper;

    @Autowired
    private TbAssetInventoryMapper tbAssetInventoryMapper;

    @Autowired
    private TbAssetInformationMapper tbAssetInformationMapper;

    @Autowired
    private TbLocationMapper locationMapper;
    /**
     * 查询资产盘点明细
     * 
     * @param id 资产盘点明细主键
     * @return 资产盘点明细
     */
    @Override
    public TbInventoryDetail selectTbInventoryDetailById(Long id)
    {
        return tbInventoryDetailMapper.selectTbInventoryDetailById(id);
    }

    @Override
    public List<TbInventoryDetail> selectTbInventoryDetailByOrderNumber(String orderNumber) {
        return tbInventoryDetailMapper.selectTbInventoryDetailByOrderNumber(orderNumber);
    }

    @Override
    public List<TbAssetInventory> selectTbInventoryCompareList(String deptId){
        return tbAssetInventoryMapper.selectTbAssetInventoryListBy(deptId);
    }


    /**
     * 查询资产盘点明细列表
     * 
     * @param tbInventoryDetail 资产盘点明细
     * @return 资产盘点明细
     */
    @Override
    public List<TbInventoryDetail> selectTbInventoryDetailList(TbInventoryDetail tbInventoryDetail)
    {
        return tbInventoryDetailMapper.selectTbInventoryDetailList(tbInventoryDetail);
    }

    /**
     * 新增资产盘点明细
     * 
     * @param tbInventoryDetail 资产盘点明细
     * @return 结果
     */
    @Override
    public int insertTbInventoryDetail(TbInventoryDetail tbInventoryDetail)
    {
        return tbInventoryDetailMapper.insertTbInventoryDetail(tbInventoryDetail);
    }

    /**
     * 修改资产盘点明细
     * 
     * @param tbInventoryDetail 资产盘点明细
     * @return 结果
     */
    @Override
    public int updateTbInventoryDetail(TbInventoryDetail tbInventoryDetail)
    {
        // 获取盘点计划单号
        String orderNumber = tbInventoryDetail.getOrderNumber();
        // 根据单号获取盘点计划
        TbAssetInventory tbAssetInventory = tbAssetInventoryMapper.selectTbAssetInventoryByNumber(orderNumber);

        TbAssetInformation assetInformation = JSONUtil.toBean(tbInventoryDetail.getInventoryMetadata(), TbAssetInformation.class);
        // 获取账面数量
        Integer quantity = assetInformation.getQuantity();
        // 获取盘点数量
        Integer inventoryQuantity = tbInventoryDetail.getInventoryQuantity();

        // 盘点数量为null
        if (inventoryQuantity == null) {
            tbInventoryDetail.setInventoryResult(null);
        } else {
            if (quantity > inventoryQuantity) { // 账面大于盘点 盘亏
                tbInventoryDetail.setInventoryResult(InventoryResult.INVENTORY_SHORTAGES.getCode());
            }
            if (quantity.equals(inventoryQuantity)) { // 账面等于盘点 正常
                tbInventoryDetail.setInventoryResult(InventoryResult.INVENTORY_NORMAL.getCode());
            }
            if (inventoryQuantity > quantity ) { // 盘点大于账面 盘盈
                tbInventoryDetail.setInventoryResult(InventoryResult.INVENTORY_PROFIT.getCode());
            }
        }

        int updated = tbInventoryDetailMapper.updateTbInventoryDetail(tbInventoryDetail);
        if (updated < 1) {
            throw new RuntimeException("操作失败");
        }

        // 查看盘点计划是否已执行
        Integer inventoryStatus = tbAssetInventory.getInventoryStatus();
        if (inventoryStatus.equals(InventoryPlanStatus.PLAN_FINISH.getCode())) {
            // 根据计划单号获取盘点详细
            List<TbInventoryDetail> tbInventoryDetails = tbInventoryDetailMapper.selectTbInventoryDetailByOrderNumber(orderNumber);

            // 获取盘点计划中的盘点详细
            List<TbInventoryDetail> inventoryDetailList = tbInventoryDetails.stream()
                    .filter(item -> !InventoryStatus.ERROR.getCode().equals(item.getInventoryStatus()))
                    .collect(Collectors.toList());

            // 获取盘点详细中未盘
            List<TbInventoryDetail> uncountedInventoryDetailList = inventoryDetailList.stream()
                    .filter(item -> InventoryStatus.UNCOUNTED.getCode().equals(item.getInventoryStatus()))
                    .collect(Collectors.toList());

            // 获取盘点详细中盘亏
            List<TbInventoryDetail> shortagesInventoryDetailList = inventoryDetailList.stream()
                    .filter(item -> InventoryResult.INVENTORY_SHORTAGES.getCode().equals(item.getInventoryResult()))
                    .collect(Collectors.toList());

            // 获取盘点详细中盘盈
            List<TbInventoryDetail> normalInventoryDetailList = inventoryDetailList.stream()
                    .filter(item -> InventoryResult.INVENTORY_PROFIT.getCode().equals(item.getInventoryResult()))
                    .collect(Collectors.toList());

            // 未盘数量
            int uncounted = uncountedInventoryDetailList.size();
            // 盘亏数量
            int shortages = shortagesInventoryDetailList.size();
            // 盘盈数量
            int normal = normalInventoryDetailList.size();
            if (uncounted > 0 || shortages > 0) { // 存在盘亏 或者 存在未盘
                tbAssetInventory.setInventoryResult(InventoryPlanResult.PLAN_INVENTORY_SHORTAGES.getCode());
            } else {
                if (normal > 0) { // 不存在盘亏，存在盘盈
                    tbAssetInventory.setInventoryResult(InventoryPlanResult.PLAN_INVENTORY_PROFIT.getCode());
                } else { // 不存在盘亏盘盈
                    tbAssetInventory.setInventoryResult(InventoryPlanResult.PLAN_INVENTORY_NORMAL.getCode());
                }
            }

            int flag = tbAssetInventoryMapper.updateTbAssetInventory(tbAssetInventory);
            if (flag < 1) {
                throw new RuntimeException("操作失败");
            }
        }

        return 1;
    }

    /**
     * 批量删除资产盘点明细
     * 
     * @param ids 需要删除的资产盘点明细主键
     * @return 结果
     */
    @Override
    public int deleteTbInventoryDetailByIds(Long[] ids)
    {
        return tbInventoryDetailMapper.deleteTbInventoryDetailByIds(ids);
    }

    /**
     * 删除资产盘点明细信息
     * 
     * @param id 资产盘点明细主键
     * @return 结果
     */
    @Override
    public int deleteTbInventoryDetailById(Long id)
    {
        return tbInventoryDetailMapper.deleteTbInventoryDetailById(id);
    }
}
