package com.jeethink.dataCenter.service.impl;

import com.jeethink.common.constant.UserConstants;
import com.jeethink.common.exception.CustomException;
import com.jeethink.common.utils.DateUtils;
import com.jeethink.common.utils.StringUtils;
import com.jeethink.dataCenter.domain.Warehouse;
import com.jeethink.dataCenter.mapper.WarehouseMapper;
import com.jeethink.dataCenter.service.IWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 库房管理 服务层
 *
 * @author wms
 */
@Service
public class WarehouseServiceImpl implements IWarehouseService
{

    @Autowired
    private WarehouseMapper warehouseMapper;

    /**
     * 查询库房管理数据
     *
     * @param warehouse 库房信息
     * @return 库房信息合集
     */
    @Override
    public List<Warehouse> selectWarehouseList(Warehouse warehouse)
    {
        List<Warehouse> warehouseList=warehouseMapper.selectWarehouseList(warehouse);
        for (int i=0;i<warehouseList.size();i++){
            String [] ids=warehouseList.get(i).getAncestors().split(",");
            String parentsName="";
            for (int j=0;j<ids.length;j++){
                Warehouse warehouse1=warehouseMapper.selectWarehouseById(Long.valueOf(ids[j]));
                if(warehouse1!=null){
                    parentsName+=warehouse1.getName()+",";
                }
            }
            if (parentsName.length()>0){
                warehouseList.get(i).setParentsName(parentsName.substring(0,parentsName.length()-1));
            }else {
                warehouseList.get(i).setParentsName(parentsName);
            }
        }
        return warehouseList;
    }

    /**
     * 根据ID查询信息
     *
     * @param id 库房ID
     * @return 库房信息
     */
    @Override
    public Warehouse selectWarehouseById(Long id)
    {
        return warehouseMapper.selectWarehouseById(id);
    }

    /**
     * 校验库房名称是否唯一
     *
     * @param warehouse 库房信息
     * @return 结果
     */
    @Override
    public String checkNameUnique(Warehouse warehouse)
    {
        Long id = StringUtils.isNull(warehouse.getId()) ? -1L : warehouse.getId();
        Warehouse info = warehouseMapper.checkNameUnique(warehouse.getName(), warehouse.getParentId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增库房信息
     *
     * @param warehouse 库房信息
     * @return 结果
     */
    @Override
    public int insertWarehouse(Warehouse warehouse)
    {
        Warehouse info = warehouseMapper.selectWarehouseById(warehouse.getParentId());
        if (!UserConstants.DEPT_NORMAL.equals(warehouse.getStatus()))
        {
            throw new CustomException("库房停用， 不允许新增");
        }
        return warehouseMapper.insertWarehouse(warehouse);
    }

    /**
     * 根据ID查询所有正常状态的子库房数
     *
     * @param id 库房ID
     * @return 子库房数
     */
    @Override
    public int selectNormalChildrenById(Long id)
    {

        return warehouseMapper.selectNormalChildrenById(id);
    }

    /**
     * 修改库房信息
     *
     * @param warehouse 库房信息
     * @return 结果
     */
    @Override
    public int updateWarehouse(Warehouse warehouse)
    {
        return warehouseMapper.updateWarehouse(warehouse);
    }

    /**
     * 是否存在子库房
     *
     * @param id 库房ID
     * @return 结果
     */
    @Override
    public boolean hasChildById(Long id)
    {
        return warehouseMapper.getChildList(id).size() > 0 ? true : false;
    }

    /**
     * 删除库房信息
     *
     * @param id 库房ID
     * @return 结果
     */
    @Override
    public int deleteWarehouseById(Long id) {
        return warehouseMapper.deleteWarehouseById(id);
    }

    /**
     * 根据关联人员查找库房
     *
     * @param relaUser 关联人员
     * @return 库房合集 （原则上只有1个）
     */
    @Override
    public List<Warehouse> selectWarehouseByRelaUser(Long relaUser) {

        return warehouseMapper.selectWarehouseByRelaUser(relaUser);
    }

    @Override
    public void deleteArchives(Long id) {
        warehouseMapper.deleteArchives(id);
    }

    @Override
    public String selectArchivesById(String archivesId) {
        return warehouseMapper.selectArchivesById(archivesId);
    }

    @Override
    public List<String> selectArchivesByParentId(Long parentId) {
        return warehouseMapper.selectArchivesByParentId(parentId);
    }
}
