package com.ruoyi.bst.hardwareVersion.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.bst.hardwareVersion.domain.HardwareVersion;
import com.ruoyi.bst.hardwareVersion.domain.HardwareVersionQuery;
import com.ruoyi.bst.hardwareVersion.domain.HardwareVersionVO;
import com.ruoyi.bst.hardwareVersion.service.HardwareVersionAssembler;
import com.ruoyi.bst.hardwareVersion.service.HardwareVersionService;
import com.ruoyi.common.utils.collection.CollectionUtils;

@Service
public class HardwareVersionAssemblerImpl implements HardwareVersionAssembler {

    @Autowired
    private HardwareVersionService hardwareVersionService;

    @Override
    public void assembleCount(List<HardwareVersionVO> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return;
        }
        // 子版本
        // 计算已绑定设备数量
        this.assembleBoundCount(list);
        // 计算未绑定设备数量
        this.assembleUnboundCount(list);
        // 计算已录入的设备数量
        this.assembleEnteredNum(list);
        // 父版本
        // 累加子版本的数据
        this.aggregateParentData(list);
        // 计算丢算的设备数量
        this.assembleLost(list);
    }


    // 拼接已绑定数量
    private void assembleBoundCount(List<HardwareVersionVO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        HardwareVersionQuery query = new HardwareVersionQuery();
        query.setHardwareVersionIds(list.stream().map(HardwareVersionVO::getId).collect(Collectors.toList()));
        List<HardwareVersionVO> hardwareVersionList = hardwareVersionService.selectBoundCount(query);
        Map<Long, Integer> idToBoundMap = hardwareVersionList.stream().collect(Collectors.toMap(HardwareVersionVO::getId, HardwareVersionVO::getBound));
        list.forEach(vo->{
            vo.setBound(idToBoundMap.getOrDefault(vo.getId(), 0));
        });
    }

    // 拼接未绑定数量
    private void assembleUnboundCount(List<HardwareVersionVO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        HardwareVersionQuery query = new HardwareVersionQuery();
        query.setHardwareVersionIds(list.stream().map(HardwareVersionVO::getId).collect(Collectors.toList()));
        List<HardwareVersionVO> hardwareVersionList = hardwareVersionService.selectUnBoundCount(query);
        Map<Long, Integer> idToBoundMap = hardwareVersionList.stream().collect(Collectors.toMap(HardwareVersionVO::getId, HardwareVersionVO::getUnBound));
        list.forEach(vo->{
            vo.setUnBound(idToBoundMap.getOrDefault(vo.getId(), 0));
        });
    }

    // 拼接已录入数量
    private void assembleEnteredNum(List<HardwareVersionVO> list) {
        list.forEach(vo->{
            int enteredNum = vo.getBound()+vo.getUnBound();
            vo.setEnteredNum(enteredNum);
        });
    }

    // 计算父版本数量
    private void aggregateParentData(List<HardwareVersionVO> list) {
        // 创建映射表
        Map<Long, List<HardwareVersionVO>> parentChildMap = list.stream()
                .filter(vo -> vo.getParentId() != 0)
                .collect(Collectors.groupingBy(HardwareVersionVO::getParentId));

        // 处理父版本数据
        list.stream()
                .filter(vo -> vo.getParentId() == 0)
                .forEach(parent -> {
                    List<HardwareVersionVO> children = parentChildMap.getOrDefault(parent.getId(), Collections.emptyList());

                    // 初始化父版本统计值
                    parent.setBound(0);
                    parent.setUnBound(0);
                    parent.setEnteredNum(0);

                    // 单次遍历累加子版本数据
                    for (HardwareVersionVO child : children) {
                        parent.setBound(parent.getBound() + child.getBound());
                        parent.setUnBound(parent.getUnBound() + child.getUnBound());
                        parent.setEnteredNum(parent.getEnteredNum() + child.getEnteredNum());
                    }
                });
    }

    // 拼接丢损的数量
    private void assembleLost(List<HardwareVersionVO> list) {
        list.stream()
                .filter(vo -> vo.getParentId() == 0) // 仅处理父版本
                .forEach(vo -> {
                    int lost = vo.getQuantity() - vo.getEnteredNum() - vo.getNotEnteredNum();
                    vo.setLost(lost);
                });
    }

}
