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

import java.util.Collections;
import java.util.List;

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.domain.vo.HardwareVersionNameVO;
import com.ruoyi.bst.hardwareVersion.mapper.HardwareVersionMapper;
import com.ruoyi.bst.hardwareVersion.service.HardwareVersionService;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.collection.CollectionUtils;

/**
 * 硬件版本Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@Service
public class HardwareVersionServiceImpl implements HardwareVersionService
{
    @Autowired
    private HardwareVersionMapper hardwareVersionMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询硬件版本
     *
     * @param id 硬件版本主键
     * @return 硬件版本
     */
    @Override
    public HardwareVersionVO selectHardwareVersionById(Long id)
    {
        return hardwareVersionMapper.selectHardwareVersionById(id);
    }

    /**
     * 查询硬件版本列表
     *
     * @param hardwareVersion 硬件版本
     * @return 硬件版本
     */
    @Override
    public List<HardwareVersionVO> selectHardwareVersionList(HardwareVersionQuery hardwareVersion)
    {
        return hardwareVersionMapper.selectHardwareVersionList(hardwareVersion);
    }

    /**
     * 新增硬件版本
     *
     * @param hardwareVersion 硬件版本
     * @return 结果
     */
    @Override
    public int insertHardwareVersion(HardwareVersion hardwareVersion)
    {
        hardwareVersion.setCreateTime(DateUtils.getNowDate());
        int rows = hardwareVersionMapper.insertHardwareVersion(hardwareVersion);
        if (rows > 0) {
            this.clearCache();
        }
        return rows;
    }

    /**
     * 修改硬件版本
     *
     * @param hardwareVersion 硬件版本
     * @return 结果
     */
    @Override
    public int updateHardwareVersion(HardwareVersion hardwareVersion)
    {
        int rows = hardwareVersionMapper.updateHardwareVersion(hardwareVersion);
        if (rows > 0) {
            this.clearCache();
        }
        return rows;
    }

    /**
     * 批量删除硬件版本
     *
     * @param ids 需要删除的硬件版本主键
     * @return 结果
     */
    @Override
    public int deleteHardwareVersionByIds(Long[] ids)
    {
        int rows = hardwareVersionMapper.deleteHardwareVersionByIds(ids);
        if (rows > 0) {
            this.clearCache();
        }
        return rows;
    }


    @Override
    public List<HardwareVersionVO> computeNumber(HardwareVersionQuery query) {
        List<HardwareVersionVO> list = hardwareVersionMapper.selectHardwareVersionList(query);
        for (HardwareVersionVO hardwareVersionVO : list) {
            if (hardwareVersionVO.getParentId() != 0){
                int boundNum = hardwareVersionMapper.countBoundNum(hardwareVersionVO.getId());
                hardwareVersionVO.setBound(boundNum);
                int unBoundNum = hardwareVersionMapper.countUnBoundNum(hardwareVersionVO.getId());
                hardwareVersionVO.setUnBound(unBoundNum);
                int enteredNum =boundNum + unBoundNum;
                hardwareVersionVO.setEnteredNum(enteredNum);
            }
        }

        //统计父版本的各个特征数量
        for (HardwareVersionVO parentVersion : list) {
            if (parentVersion.getParentId() == 0){
                int totalBound = 0;
                int totalUnBound = 0;
                int totalEntered = 0;

                for (HardwareVersionVO childVersion : list) {
                    if (childVersion.getParentId().equals(parentVersion.getId())){
                        totalBound += childVersion.getBound();
                        totalUnBound += childVersion.getUnBound();
                        totalEntered += childVersion.getEnteredNum();
                    }
                }
                parentVersion.setBound(totalBound);
                parentVersion.setUnBound(totalUnBound);
                parentVersion.setEnteredNum(totalEntered);
            }
        }
        for (HardwareVersionVO parentVersion : list) {
            if (parentVersion.getParentId() == 0){
                parentVersion.setLost(parentVersion.getQuantity()-parentVersion.getEnteredNum()-parentVersion.getNotEnteredNum());
            }
        }
        return list;
    }

    @Override
    public List<HardwareVersionNameVO> selectAllNames() {
        String key = CacheConstants.ALL_HARDWARE_VERSION_NAME;
        List<HardwareVersionNameVO> list = redisCache.getCacheObject(key);
        if (CollectionUtils.isEmptyElement(list)){
            list = hardwareVersionMapper.selectNameList(new HardwareVersionQuery());
            if (CollectionUtils.isNotEmptyElement(list)){
                redisCache.setCacheObject(key, list);
            }
        }
        return list;
    }

    private void clearCache() {
        String key = CacheConstants.ALL_HARDWARE_VERSION_NAME;
        redisCache.deleteObject(key);
    }

    @Override
    public List<HardwareVersionVO> selectBoundCount(HardwareVersionQuery query) {
        return hardwareVersionMapper.selectBoundCount(query);
    }

    @Override
    public List<HardwareVersionVO> selectUnBoundCount(HardwareVersionQuery query) {
        return hardwareVersionMapper.selectUnBoundCount(query);
    }
}
