package com.ruoyi.order.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import cn.hutool.core.lang.func.LambdaUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.asset.domain.TbAssetInformation;
import com.ruoyi.asset.mapper.TbAssetInformationMapper;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.order.domain.TbOrderDetail;
import com.ruoyi.order.domain.dto.OrderTypeEnum;
import com.ruoyi.order.mapper.TbOrderDetailMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.TbOrderMapper;
import com.ruoyi.order.domain.TbOrder;
import com.ruoyi.order.service.ITbOrderService;

import javax.annotation.Resource;

/**
 * 资产单据Service业务层处理
 * 
 * @author 原动力
 * @date 2023-05-24
 */
@Service
public class TbOrderServiceImpl implements ITbOrderService 
{
    @Resource
    private TbOrderMapper tbOrderMapper;

    @Resource
    private TbOrderDetailMapper tbOrderDetailMapper;

    @Resource
    private TbAssetInformationMapper tbAssetInformationMapper;

    /**
     * 查询资产单据
     * 
     * @param id 资产单据主键
     * @return 资产单据
     */
    @Override
    public TbOrder selectTbOrderById(Long id)
    {
        return tbOrderMapper.selectTbOrderById(id);
    }

    /**
     * 查询资产单据列表
     * 
     * @param tbOrder 资产单据
     * @return 资产单据
     */
    @Override
    public List<TbOrder> selectTbOrderList(TbOrder tbOrder)
    {
        return tbOrderMapper.selectTbOrderList(tbOrder);
    }

    @Override
    public List<TbOrder> selectTbOrderByIds(Long[] ids) {
        return tbOrderMapper.selectTbOrderByIDs(ids);
    }

    /**
     * 新增资产单据
     * 
     * @param tbOrder 资产单据
     * @return 结果
     */
    @Override
    public int insertTbOrder(TbOrder tbOrder)
    {
        return tbOrderMapper.insertTbOrder(tbOrder);
    }

    /**
     * 修改资产单据
     * 
     * @param tbOrder 资产单据
     * @return 结果
     */
    @Override
    public int updateTbOrder(TbOrder tbOrder)
    {
        return tbOrderMapper.updateTbOrder(tbOrder);
    }

    /**
     * 批量删除资产单据
     * 
     * @param ids 需要删除的资产单据主键
     * @return 结果
     */
    @Override
    public int deleteTbOrderByIds(Long[] ids)
    {
        List<TbOrder> tbOrders = selectTbOrderByIds(ids);
        ids = tbOrders.stream().filter(tbOrder -> tbOrder.getRecordStatus() == 0).map(TbOrder::getId).toArray(Long[]::new);
        List<String> orderNumbers = tbOrders.stream().filter(tbOrder -> tbOrder.getRecordStatus() == 0).map(TbOrder::getOrderNumber).collect(Collectors.toList());
        if (!orderNumbers.isEmpty()) {
            tbOrderDetailMapper.deleteTbOrderDetailByOrderNumbers(orderNumbers);
        }
        return tbOrderMapper.deleteTbOrderByIds(ids);
    }

    /**
     * 删除资产单据信息
     * 
     * @param id 资产单据主键
     * @return 结果
     */
    @Override
    public int deleteTbOrderById(Long id)
    {
        TbOrder tbOrder = selectTbOrderById(id);
        if (tbOrder == null) {
            return 0;
        }
        tbOrderDetailMapper.deleteTbOrderDetailByOrderNumber(tbOrder.getOrderNumber());
        return tbOrderMapper.deleteTbOrderById(id);
    }

    @Override
    public TbOrder selectTbOrderByOrderNumber(String orderNumber) {
        return tbOrderMapper.selectTbOrderByOrderNumber(orderNumber);
    }

    @Override
    public Map<String, Double> accounting(String orderNumber) {
        List<TbOrderDetail> tbOrderDetails = tbOrderDetailMapper.selectTbOrderDetailByOrderNumber(orderNumber);

        double originalValue = 0;
        double netValue = 0;

        for (TbOrderDetail tbOrderDetail : tbOrderDetails) {
            String assetNumber = tbOrderDetail.getAssetNumber();
            TbAssetInformation tbAssetInformation = tbAssetInformationMapper.selectTbAssetInformationByAssetNumber(assetNumber);
            Double assetOriginalValue = tbAssetInformation.getOriginalValue();
            Double assetNetValue = tbAssetInformation.getNetValue();
            if (assetOriginalValue != null) {
                originalValue += assetOriginalValue;
            }
            if (assetNetValue != null) {
                netValue += assetNetValue;
            }
        }

        HashMap<String, Double> hashMap = new HashMap<>();
        hashMap.put("assetTotalOriginalValue", originalValue);
        hashMap.put("assetTotalNetValue", netValue);

        return hashMap;
    }
}
