package com.zbf.system.service.impl;

import com.zbf.common.annotation.DataSource;
import com.zbf.common.enums.DataSourceType;
import com.zbf.system.domain.SafetyStockVO;
import com.zbf.system.mapper.SafetyStockMapper;
import com.zbf.system.service.ITSafetyStockService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TSafetyStockServiceImpl implements ITSafetyStockService {

    @Resource
    SafetyStockMapper safetyStockMapper;

    @Override
    @DataSource(value = DataSourceType.MASTER)
    public List<SafetyStockVO> selectList(SafetyStockVO safetyStockVO) {
        List<SafetyStockVO> safetyStockVOS = safetyStockMapper.selectList(safetyStockVO);
       // 收集 shelvesNum > warnUp 或 shelvesNum < warnDown 的 SafetyStockVO 列表
        List<SafetyStockVO> filteredList = safetyStockVOS.stream()
                .filter(stock -> {
                    BigDecimal shelvesNum = stock.getShelvesNum() != null ? BigDecimal.valueOf(stock.getShelvesNum()) : null;
                    BigDecimal warnUp = stock.getWarnUp();
                    BigDecimal warnDown = stock.getWarnDown();

                    return (shelvesNum != null && warnUp != null && shelvesNum.compareTo(warnUp) > 0) ||
                            (shelvesNum != null && warnDown != null && shelvesNum.compareTo(warnDown) < 0);
                })
                .collect(Collectors.toList());

        return filteredList;
    }


    public List<SafetyStockVO> selectLists(SafetyStockVO safetyStockVO) {
        List<SafetyStockVO> safetyStockVOS = safetyStockMapper.selectList(safetyStockVO);
        // 收集 shelvesNum > warnUp 或 shelvesNum < warnDown 的 SafetyStockVO 列表
        List<SafetyStockVO> filteredList = safetyStockVOS.stream()
                .filter(stock -> {
                    BigDecimal shelvesNum = BigDecimal.valueOf(stock.getShelvesNum()); // 安全，因为 long 不能是 null
                    BigDecimal warnUp = stock.getWarnUp() != null ? stock.getWarnUp() : BigDecimal.ZERO;
                    BigDecimal warnDown = stock.getWarnDown() != null ? stock.getWarnDown() : BigDecimal.ZERO;

                    return shelvesNum.compareTo(warnUp) > 0 || shelvesNum.compareTo(warnDown) < 0;
                })
                .collect(Collectors.toList());

        return filteredList;
    }
}
