package com.da.dangan.service.impl;

import com.da.common.core.domain.AjaxResult;
import com.da.common.core.domain.entity.DaCatalog;
import com.da.common.utils.DateUtils;
import com.da.dangan.domain.DaBookshelf;
import com.da.dangan.mapper.DaBookshelfMapper;
import com.da.dangan.service.IDaBookshelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 物理书架Service业务层处理
 * 
 * @author hs
 * @date 2024-05-11
 */
@Service
public class DaBookshelfServiceImpl implements IDaBookshelfService 
{
    @Autowired
    private DaBookshelfMapper daBookshelfMapper;

    /**
     * 查询物理书架
     * 
     * @param shelfId 物理书架主键
     * @return 物理书架
     */
    @Override
    public DaBookshelf selectDaBookshelfByShelfId(Long shelfId)
    {
        return daBookshelfMapper.selectDaBookshelfByShelfId(shelfId);
    }

    /**
     * 查询物理书架列表
     * 
     * @param daBookshelf 物理书架
     * @return 物理书架
     */
    @Override
    public List<DaBookshelf> selectDaBookshelfList(DaBookshelf daBookshelf)
    {
        return daBookshelfMapper.selectDaBookshelfList(daBookshelf);
    }

    /**
     * 新增物理书架
     * 
     * @param daBookshelf 物理书架
     * @return 结果
     */
    @Override
    public int insertDaBookshelf(DaBookshelf daBookshelf)
    {
        daBookshelf.setCreateTime(DateUtils.getNowDate());
        daBookshelf.setCreateTime(DateUtils.getNowDate());
        if(daBookshelf.getPid()==0){  //代表添加的是主目录
            daBookshelf.setAncestors(daBookshelf.getPid().toString());
        }else{
            DaBookshelf info = daBookshelfMapper.selectDaBookshelfByShelfId(daBookshelf.getPid());
            daBookshelf.setAncestors(info.getAncestors() + "," + daBookshelf.getPid());
        }
        return daBookshelfMapper.insertDaBookshelf(daBookshelf);
    }

    /**
     * 修改物理书架
     * 
     * @param daBookshelf 物理书架
     * @return 结果
     */
    @Override
    public int updateDaBookshelf(DaBookshelf daBookshelf)
    {
        daBookshelf.setUpdateTime(DateUtils.getNowDate());
        return daBookshelfMapper.updateDaBookshelf(daBookshelf);
    }

    /**
     * 批量删除物理书架
     * 
     * @param shelfIds 需要删除的物理书架主键
     * @return 结果
     */
    @Override
    public int deleteDaBookshelfByShelfIds(Long[] shelfIds)
    {
        return daBookshelfMapper.deleteDaBookshelfByShelfIds(shelfIds);
    }

    /**
     * 删除物理书架信息
     * 
     * @param shelfId 物理书架主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteDaBookshelfByShelfId(Long shelfId)
    {
        DaBookshelf daBookshelf = new DaBookshelf();
        daBookshelf.setPid(shelfId);
        //有子级目录的不能删除
        List<DaBookshelf> daBookshelfs = daBookshelfMapper.selectDaBookshelfList(daBookshelf);
        if(daBookshelfs!=null&&daBookshelfs.size()>0){
            return AjaxResult.error("有子级目录，不能删除");
        }
        int i= daBookshelfMapper.deleteDaBookshelfByShelfId(shelfId);
        if(i>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
