package com.openhis.workflow.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.openhis.workflow.dto.AdjustPriceDto;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.openhis.common.enums.DelFlag;
import com.openhis.common.enums.PublicationStatus;
import com.openhis.workflow.domain.InventoryItem;
import com.openhis.workflow.mapper.InventoryItemMapper;
import com.openhis.workflow.service.IInventoryItemService;

/**
 * 库存项目管理Service业务层处理
 *
 * @author system
 * @date 2025-02-20
 */
@Service
public class InventoryItemServiceImpl extends ServiceImpl<InventoryItemMapper, InventoryItem>
    implements IInventoryItemService {

    /**
     * 入库
     *
     * @param inventoryItemList 入库项目
     */
    @Override
    public void stockIn(List<InventoryItem> inventoryItemList) {
        for (InventoryItem inventoryItem : inventoryItemList) {
            baseMapper.insert(inventoryItem);
        }
    }

    /**
     * 更新库房数量
     *
     * @param id 主键
     * @param quantity 最小单位库存数量
     * @param now 当前时间
     * @return 更新件数
     */
    @Override
    public Boolean updateInventoryQuantity(Long id, BigDecimal quantity, Date now) {
        int updateCount = baseMapper.update(null, new LambdaUpdateWrapper<InventoryItem>().eq(InventoryItem::getId, id)
            .set(InventoryItem::getUpdateTime, now).set(InventoryItem::getQuantity, quantity));
        return updateCount > 0;

    }

    /**
     * 查询
     *
     * @param itemId 项目编号
     * @param lotNumber 产品批号
     * @param locationId 仓库
     * @param tenantId 租户id
     * @return 单据详情
     */
    @Override
    public List<InventoryItem> selectInventoryByItemId(Long itemId, String lotNumber, Long locationId,
        Integer tenantId) {
        LambdaQueryWrapper<InventoryItem> queryWrapper = new LambdaQueryWrapper<InventoryItem>()
            .eq(InventoryItem::getItemId, itemId).eq(InventoryItem::getLotNumber, lotNumber)
            .eq(InventoryItem::getTenantId, tenantId).eq(InventoryItem::getDeleteFlag, DelFlag.NO.getValue())
            .eq(InventoryItem::getInventoryStatusEnum, PublicationStatus.ACTIVE.getValue());
        if (locationId != null) {
            queryWrapper.eq(InventoryItem::getLocationId, locationId);
        }
        // 查询取库房信息
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 查询
     *
     * @param itemIdList 项目编号列表
     * @param tenantId 租户id
     * @return 单据详情
     */
    @Override
    public List<InventoryItem> selectInventoryByItemIdList(List<Long> itemIdList, Integer tenantId) {
        LambdaQueryWrapper<InventoryItem> queryWrapper =
            new LambdaQueryWrapper<InventoryItem>().in(InventoryItem::getItemId, itemIdList)
                .eq(InventoryItem::getTenantId, tenantId).eq(InventoryItem::getDeleteFlag, DelFlag.NO.getValue())
                .eq(InventoryItem::getInventoryStatusEnum, PublicationStatus.ACTIVE.getValue());
        // 查询取库房信息
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 查询
     *
     * @return 单据详情
     */
    @Override
    public List<InventoryItem> selectAllInventory() {
        LambdaQueryWrapper<InventoryItem> queryWrapper =
            new LambdaQueryWrapper<InventoryItem>().eq(InventoryItem::getDeleteFlag, DelFlag.NO.getValue());
        // 查询取库房信息
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<AdjustPriceDto> searchResultDataList(List<Long> productIds) {

        return baseMapper.searchResultDataList(productIds);
    }

    @Override
    public List<AdjustPriceDto> searchResultDataListByGroup(List<Long> productIds) {
        return baseMapper.searchResultDataListByGroup(productIds);
    }
}
