package com.ruoyi.project.business.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.enums.DelFlag;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.business.domain.TcProjectEquipment;
import com.ruoyi.project.business.domain.TcProjectEquipmentHistory;
import com.ruoyi.project.business.domain.vo.lead.LeadRes;
import com.ruoyi.project.business.enums.DelFlagEnum;
import com.ruoyi.project.business.mapper.TbAgentMapper;
import com.ruoyi.project.business.service.ITcProjectEquipmentHistoryService;
import com.ruoyi.project.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.business.mapper.TcProjectHistoryMapper;
import com.ruoyi.project.business.domain.TcProjectHistory;
import com.ruoyi.project.business.service.ITcProjectHistoryService;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-22
 */
@Service
public class TcProjectHistoryServiceImpl implements ITcProjectHistoryService 
{
    @Autowired
    private TcProjectHistoryMapper tcProjectHistoryMapper;

    @Resource
    private ITcProjectEquipmentHistoryService tcProjectEquipmentHistoryService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private TbAgentMapper tbAgentMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param projectId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TcProjectHistory selectTcProjectHistoryByProjectId(Long projectId, Long version)
    {
        TcProjectHistory result = tcProjectHistoryMapper.selectTcProjectHistoryByProjectId(projectId, version);
        result.setMachineList(tcProjectEquipmentHistoryService.list(Wrappers.<TcProjectEquipmentHistory>lambdaQuery()
                .eq(TcProjectEquipmentHistory::getProjectId, projectId)
                .eq(TcProjectEquipmentHistory::getVersion, version)
                .eq(TcProjectEquipmentHistory::getDelFlag, DelFlagEnum.NORMAL.getValue())
        ));
        // 查询负责人
        Optional.ofNullable(sysUserMapper.selectUserById(result.getOwner()))
                .ifPresent(user -> result.setOwnerName(user.getNickName()));


        // 查询所属代理商tbAgentMapper
        if (result.getOwnerAgent() == 0) {
            // 总部
            result.setOwnerAgentName(MessageUtils.message("agent.headquarters"));
        } else {
            Optional.ofNullable(tbAgentMapper.selectById(result.getOwnerAgent()))
                    .ifPresent(agent -> result.setOwnerAgentName(agent.getAgentName()));
        }
        // ProjectEquipmentReq param = new ProjectEquipmentReq().setProjectId(tcProject.getProjectId());
        // tcProject.setMachineList(tcProjectEquipmentMapper.selectTcProjectEquipmentList(param));
        List<TcProjectEquipmentHistory> equipmentList = result.getMachineList();
        // 总价=单价×数量 保留两位小数
        result.setPriceTotal(equipmentList.stream().map(item -> {
            BigDecimal amount = item.getAmount();
            Long equipmentQuantity = item.getEquipmentQuantity();
            return amount == null || equipmentQuantity == null ? BigDecimal.ZERO
                    : amount.multiply(new BigDecimal(equipmentQuantity));
        }).reduce(BigDecimal.ZERO, BigDecimal::add));
        result.setDeviceTotal(
                Math.toIntExact(equipmentList.stream().mapToLong(TcProjectEquipmentHistory::getEquipmentQuantity).sum()));
        return result;
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param tcProjectHistory 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TcProjectHistory> selectTcProjectHistoryList(TcProjectHistory tcProjectHistory)
    {
        return tcProjectHistoryMapper.selectTcProjectHistoryList(tcProjectHistory);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param tcProjectHistory 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTcProjectHistory(TcProjectHistory tcProjectHistory)
    {
        tcProjectHistory.setCreateTime(DateUtils.getNowDate());
        tcProjectHistory.setCreateBy(SecurityUtils.getUserId().toString());
        return tcProjectHistoryMapper.insertTcProjectHistory(tcProjectHistory);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param tcProjectHistory 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTcProjectHistory(TcProjectHistory tcProjectHistory)
    {
        tcProjectHistory.setUpdateTime(DateUtils.getNowDate());
        return tcProjectHistoryMapper.updateTcProjectHistory(tcProjectHistory);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param projectIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTcProjectHistoryByProjectIds(Long[] projectIds)
    {
        return tcProjectHistoryMapper.deleteTcProjectHistoryByProjectIds(projectIds);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param projectId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTcProjectHistoryByProjectId(Long projectId)
    {
        return tcProjectHistoryMapper.deleteTcProjectHistoryByProjectId(projectId);
    }
}
