package com.ruoyi.inventory.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.inventory.domain.HealthcareInventoryStoreroom;
import com.ruoyi.inventory.mapper.HealthcareInventoryStoreroomMapper;
import com.ruoyi.inventory.service.IHealthcareInventoryStoreroomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.inventory.mapper.HealthcareInventoryWarehouseSelectionMapper;
import com.ruoyi.inventory.domain.HealthcareInventoryWarehouseSelection;
import com.ruoyi.inventory.service.IHealthcareInventoryWarehouseSelectionService;

/**
 * 库房选择Service业务层处理
 *
 * @author 何顺
 * @date 2024-12-17
 */
@Service
public class HealthcareInventoryWarehouseSelectionServiceImpl implements IHealthcareInventoryWarehouseSelectionService
{
    @Autowired
    private HealthcareInventoryWarehouseSelectionMapper healthcareInventoryWarehouseSelectionMapper;

    @Autowired
    private HealthcareInventoryStoreroomMapper healthcareInventoryStoreroomMapper;

    /**
     * 查询库房选择
     *
     * @param id 库房选择主键
     * @return 库房选择
     */
    @Override
    public HealthcareInventoryWarehouseSelection selectHealthcareInventoryWarehouseSelectionByUserId(Long id)
    {
        HealthcareInventoryWarehouseSelection warehouseSelection = healthcareInventoryWarehouseSelectionMapper.selectHealthcareInventoryWarehouseSelectionByUserId(id);
        // 判断 是否 这个 用户 的 库房选择 信息  如何 没有 进行 创建
        if( warehouseSelection == null) {
            // 进行 创建 库房选择 信息
            HealthcareInventoryWarehouseSelection healthcareInventoryWarehouseSelection = new HealthcareInventoryWarehouseSelection();
            // 用户 名称
            LoginUser loginUser = SecurityUtils.getLoginUser();
            healthcareInventoryWarehouseSelection.setUserName(loginUser.getUsername());
            // 创建 者
            healthcareInventoryWarehouseSelection.setCreateBy(loginUser.getUsername());
            // 创建 时间
            healthcareInventoryWarehouseSelection.setCreateTime(DateUtils.getNowDate());

            // 用户 ID
            healthcareInventoryWarehouseSelection.setUserId(id);
            // 获取 仓库 名称
            List<HealthcareInventoryStoreroom> inventoryStoreroom = healthcareInventoryStoreroomMapper.selectHealthcareInventoryStoreroomList(null);
            healthcareInventoryWarehouseSelection.setStoreroomName(inventoryStoreroom.get(0).getStoreroomName());
            // 赋值 仓库 id
            healthcareInventoryWarehouseSelection.setStoreroomId(inventoryStoreroom.get(0).getId());
            // 进行 写入 数据库
            healthcareInventoryWarehouseSelectionMapper.insertHealthcareInventoryWarehouseSelection(healthcareInventoryWarehouseSelection);
            // 获取 最新的 库房选择 信息
            warehouseSelection = healthcareInventoryWarehouseSelectionMapper.selectHealthcareInventoryWarehouseSelectionByUserId(id);
        }
        return warehouseSelection;
    }

    /**
     * 查询库房选择列表
     *
     * @param healthcareInventoryStoreroom 库房选择
     * @return 库房选择
     */
    @Override
    public List<HealthcareInventoryStoreroom> selectHealthcareInventoryWarehouseList(HealthcareInventoryStoreroom healthcareInventoryStoreroom)
    {
        // 获取 库房 healthcareInventoryStoreroomMapper
        return healthcareInventoryStoreroomMapper.selectHealthcareInventoryStoreroomList(healthcareInventoryStoreroom);
    }

    /**
     * 新增库房选择
     *
     * @param healthcareInventoryWarehouseSelection 库房选择
     * @return 结果
     */
    @Override
    public int insertHealthcareInventoryWarehouseSelection(HealthcareInventoryWarehouseSelection healthcareInventoryWarehouseSelection)
    {
        healthcareInventoryWarehouseSelection.setCreateTime(DateUtils.getNowDate());
        return healthcareInventoryWarehouseSelectionMapper.insertHealthcareInventoryWarehouseSelection(healthcareInventoryWarehouseSelection);
    }

    /**
     * 修改库房选择
     *
     * @param healthcareInventoryWarehouseSelection 库房选择
     * @return 结果
     */
    @Override
    public int updateHealthcareInventoryWarehouseSelection(HealthcareInventoryWarehouseSelection healthcareInventoryWarehouseSelection)
    {
        healthcareInventoryWarehouseSelection.setUpdateTime(DateUtils.getNowDate());
        return healthcareInventoryWarehouseSelectionMapper.updateHealthcareInventoryWarehouseSelection(healthcareInventoryWarehouseSelection);
    }

    /**
     * 批量删除库房选择
     *
     * @param ids 需要删除的库房选择主键
     * @return 结果
     */
    @Override
    public int deleteHealthcareInventoryWarehouseSelectionByIds(Long[] ids)
    {
        return healthcareInventoryWarehouseSelectionMapper.deleteHealthcareInventoryWarehouseSelectionByIds(ids);
    }

    /**
     * 删除库房选择信息
     *
     * @param id 库房选择主键
     * @return 结果
     */
    @Override
    public int deleteHealthcareInventoryWarehouseSelectionById(Long id)
    {
        return healthcareInventoryWarehouseSelectionMapper.deleteHealthcareInventoryWarehouseSelectionById(id);
    }
}
