package com.zbf.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zbf.system.domain.TMiStock;
import com.zbf.system.mapper.TMiStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbf.common.annotation.DataScope;
import com.zbf.common.utils.DateUtils;
import com.zbf.system.domain.TBaseStorage;
import com.zbf.system.domain.TBaseStorageLocationRecord;
import com.zbf.system.mapper.TBaseStorageMapper;
import com.zbf.system.service.ITBaseStorageService;

/**
 * 仓库管理Service业务层处理
 *
 * @author tony
 * @date 2024-06-19
 */
@Service
public class TBaseStorageServiceImpl extends ServiceImpl<TBaseStorageMapper, TBaseStorage> implements ITBaseStorageService //修改
{
    @Resource
    private TBaseStorageMapper tBaseStorageMapper;
    @Autowired
    private TMiStockMapper tMiStockMapper;

    /**
     * 查询仓库管理
     *
     * @param id 仓库管理主键
     * @return 仓库管理
     */
    @Override
    public TBaseStorage selectTBaseStorageById(Long id) {
        return tBaseStorageMapper.selectTBaseStorageById(id);
    }

    /**
     * 查询仓库管理列表
     *
     * @param tBaseStorage 仓库管理
     * @return 仓库管理
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<TBaseStorage> selectTBaseStorageList(TBaseStorage tBaseStorage) {

        List<TBaseStorage> tBaseStorages = tBaseStorageMapper.selectTBaseStorageList(tBaseStorage);
        /*tBaseStorageMapper.callUpdateStorageIds();*/
        return tBaseStorages;
    }

    /**
     * 新增仓库管理
     *
     * @param tBaseStorage 仓库管理
     * @return 结果
     */
    @Override
    public int insertTBaseStorage(TBaseStorage tBaseStorage) {
        tBaseStorage.setCreateTime(DateUtils.getNowDate());
        int i = tBaseStorageMapper.insertTBaseStorage(tBaseStorage);

        return i;
    }

    /**
     * 修改仓库管理
     *
     * @param tBaseStorage 仓库管理
     * @return 结果
     */
    @Override
    public int updateTBaseStorage(TBaseStorage tBaseStorage) {
        tBaseStorage.setUpdateTime(DateUtils.getNowDate());
        int i = tBaseStorageMapper.updateTBaseStorage(tBaseStorage);
        return i;
    }

    /**
     * 批量删除仓库管理
     *
     * @param ids 需要删除的仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteTBaseStorageByIds(Long[] ids) {

        LambdaUpdateWrapper<TBaseStorage> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(TBaseStorage::getId, ids);
        List<TBaseStorage> storages = tBaseStorageMapper.selectList(wrapper);
        List<String> storageIds = storages.stream()
                .map(TBaseStorage::getStorageId)
                .collect(Collectors.toList());
        if (!storageIds.isEmpty()){

            LambdaQueryWrapper<TMiStock> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(TMiStock::getStorageId, storageIds);
            List<TMiStock> miStocks = tMiStockMapper.selectList(queryWrapper);
            if (miStocks.size() > 0) {
                throw new RuntimeException("存在库存，无法删除仓库！");
            }
        }

        return tBaseStorageMapper.deleteTBaseStorageByIds(ids);
    }

    /**
     * 删除仓库管理信息
     *
     * @param id 仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteTBaseStorageById(Long id) {
        Long[] ids = new Long[1];
        ids[0] = id;
        List<TBaseStorage> storageList = tBaseStorageMapper.selectStockByIds(ids);
        if (storageList.size() > 0) {
            throw new RuntimeException("存在库存，无法删除仓库！");
        }
        return tBaseStorageMapper.deleteTBaseStorageById(id);
    }


    /**
     * 批量禁用
     *
     * @param ids 仓库主键
     * @return 结果
     */
    @Override
    public int disabledTBaseStorageByIds(Long[] ids) {
        if (!judgeDisabled(ids)) {
            return 0;
        }
        return tBaseStorageMapper.disabledTBaseStorageByIds(ids);
    }

    /**
     * 判断库存是否可以停用的方法
     */
    private boolean judgeDisabled(Long[] ids) {
        List<TBaseStorage> storageList = tBaseStorageMapper.selectStockByIds(ids);
        if (storageList.size() > 0) {
            throw new RuntimeException("存在库存，无法停用仓库！");
        }
        return true;
    }

    /**
     * 批量启用
     *
     * @param ids 仓库管理主键
     * @return 结果
     */
    @Override
    public int enableTBaseStorageByIds(Long[] ids) {
        return tBaseStorageMapper.enableTBaseStorageByIds(ids);
    }

    /**
     * 修改状态
     *
     * @param tBaseStorage 工序管理主键
     * @return 结果
     */
    @Override
    public int updateStatus(TBaseStorage tBaseStorage) {
        if ("0".equals(tBaseStorage.getStatus())) {
            Long[] ids = new Long[1];
            ids[0] = tBaseStorage.getId();
            if (!judgeDisabled(ids)) {
                return 0;
            }
        }
        return tBaseStorageMapper.updateTBaseStorage(tBaseStorage);
    }

    /**
     *查询库存库区库位信息
     * @param tBaseStorageLocation
     * @return 结果
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<TBaseStorageLocationRecord> selectStorageAndAreaAndLocation(TBaseStorageLocationRecord tBaseStorageLocation) {
        List<TBaseStorageLocationRecord> tBaseStorageLocationRecords = tBaseStorageMapper.selectStorageAndAreaAndLocation(tBaseStorageLocation);
       /* tBaseStorageMapper.callUpdateStorageIds();*/
        return tBaseStorageLocationRecords;
    }

    @Override
    public String getIsAgvByStorageId(String storageId) {
        return tBaseStorageMapper.getIsAgvByStorageId(storageId);
    }

}
