package com.bonus.sgzb.machine.service.impl;

import com.bonus.sgzb.common.core.constant.UserConstants;
import com.bonus.sgzb.common.core.exception.ServiceException;
import com.bonus.sgzb.common.core.utils.StringUtils;
import com.bonus.sgzb.machine.damain.MaHouse;
import com.bonus.sgzb.machine.mapper.MaHouseMapper;
import com.bonus.sgzb.machine.service.IhouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 仓库信息 服务层处理
 *
 * @author ruoyi
 */
@Service
public class MaHouseServiceImpl implements IhouseService {

    @Autowired
    private MaHouseMapper houseMapper;
    /**
     * 校验工程项目名称
     *
     * @param house 仓库信息
     * @return 结果
     */
    @Override
    public boolean checkHouseNameUnique(MaHouse house) {
        Long houseId = StringUtils.isNull(house.getHouseId()) ? -1L : house.getHouseId();
        MaHouse info = houseMapper.checkHouseNameUnique(house.getHouseName());
        if (StringUtils.isNotNull(info) && info.getHouseId().longValue() != houseId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增保存仓库信息
     *
     * @param house 仓库信息
     * @return 结果
     */
    @Override
    public int insertHouse(MaHouse house) {
        MaHouse info = houseMapper.selectHouseById(house.getParentId());
        // 如果父节点不为正常状态,则不允许新增子节点
        if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
        {
            throw new ServiceException("仓库货架停用，不允许新增");
        }
        return houseMapper.insertHouse(house);
    }

    /**
     * 修改仓库信息
     *
     * @param house 仓库信息
     * @return 结果
     */
    @Override
    public int updatetHouse(MaHouse house) {
        return houseMapper.updateHouse(house);
    }

    /**
     * 删除仓库货架信息
     * @param houseId
     * @return
     */
    @Override
    public int deleteHouseById(Long houseId) {
        return houseMapper.deleteHouseById(houseId);
    }

    /**
     * 查询仓库列表
     * @param house
     * @return
     */
    @Override
    public List<MaHouse> selectHouseList(MaHouse house) {
        return houseMapper.selectHouseList(house);
    }

    /**
     * 是否存在子仓库
     * @param houseId
     * @return
     */
    @Override
    public boolean hasChildByHouseId(Long houseId) {
        int result = houseMapper.hasChildByHouseId(houseId);
        return result > 0;
    }
}
