package com.ruoyi.basicData.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.basicData.mapper.TWarehouseMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TWarehouseUserMapper;
import com.ruoyi.system.domain.TWarehouseUser;
import com.ruoyi.basicData.service.ITWarehouseUserService;

/**
 * 仓库和用户关联Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-05
 */
@Service
public class TWarehouseUserServiceImpl implements ITWarehouseUserService {
    @Autowired
    private TWarehouseUserMapper tWarehouseUserMapper;

    @Autowired
    private TWarehouseMapper tWarehouseMapper;

    /**
     * 查询仓库和用户关联
     *
     * @param userId 仓库和用户关联ID
     * @return 仓库和用户关联
     */
    @Override
    public TWarehouseUser selectTWarehouseUserById(Long userId) {
        return tWarehouseUserMapper.selectTWarehouseUserById(userId);
    }

    /**
     * 查询仓库和用户关联列表
     *
     * @param tWarehouseUser 仓库和用户关联
     * @return 仓库和用户关联
     */
    @Override
    public List<TWarehouseUser> selectTWarehouseUserList(TWarehouseUser tWarehouseUser) {
        return tWarehouseUserMapper.selectTWarehouseUserList(tWarehouseUser);
    }

    /**
     * 根据用户ID获取仓库选择框列表
     *
     * @param userId 用户ID
     * @return 选中仓库ID列表
     */
    @Override
    public List<Long> selectWarehouseListByUserId(Long userId) {
        return tWarehouseUserMapper.selectWarehouseListByUserId(userId);
    }

    /**
     * 新增仓库和用户关联
     *
     * @param tWarehouseUser 仓库和用户关联
     * @return 结果
     */
    @Override
    public int insertTWarehouseUser(TWarehouseUser tWarehouseUser) {
        return tWarehouseUserMapper.insertTWarehouseUser(tWarehouseUser);
    }

    /**
     * 修改仓库和用户关联
     *
     * @param tWarehouseUser 仓库和用户关联
     * @return 结果
     */
    @Override
    public int updateTWarehouseUser(TWarehouseUser tWarehouseUser) {
        return tWarehouseUserMapper.updateTWarehouseUser(tWarehouseUser);
    }

    /**
     * 批量删除仓库和用户关联
     *
     * @param userIds 需要删除的仓库和用户关联ID
     * @return 结果
     */
    @Override
    public int deleteTWarehouseUserByIds(Long[] userIds) {
        return tWarehouseUserMapper.deleteTWarehouseUserByIds(userIds);
    }

    /**
     * 删除仓库和用户关联信息
     *
     * @param userId 仓库和用户关联ID
     * @return 结果
     */
    @Override
    public int deleteTWarehouseUserById(Long userId) {
        return tWarehouseUserMapper.deleteTWarehouseUserById(userId);
    }

    /**
     * bi大屏仓库列表
     *
     * @return 仓库集合
     */
    @Override
    public List<Map<String, Object>> biWarehouseList() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user.isAdmin()) {
            return tWarehouseMapper.biWarehouseList();
        }

        List<Map<String, Object>> warehouseList = tWarehouseUserMapper.biWarehouseList(user.getUserId());
        if (warehouseList.size() == 1) {
            Long warehouseId = (Long) warehouseList.get(0).get("warehouseId");
            if (warehouseId == 0) {
                return tWarehouseMapper.biWarehouseList();
            }
        }

        return warehouseList;
    }

}
