package com.auth.service.base.service.impl;

import com.auth.common.model.common.result.PageResult;
import com.auth.dao.base.entity.DeptEntity;
import com.auth.dao.base.mapper.v1.DeptMapper;
import com.auth.model.base.dto.DeptDto;
import com.auth.model.base.vo.DeptVo;
import com.auth.service.base.service.DeptService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author Bunny
 * @since 2025-07-22 21:43:42
 */
@Service
@Transactional
public class DeptServiceImpl extends ServiceImpl<DeptMapper, DeptEntity> implements DeptService {

    /**
     * 部门表 服务实现类
     *
     * @param pageParams 部门表分页查询page对象
     * @param dto        部门表分页查询对象
     * @return 查询分页部门表返回对象
     */
    @Override
    public PageResult<DeptVo> getDeptPage(Page<DeptEntity> pageParams, DeptDto dto) {
        IPage<DeptVo> page = baseMapper.selectListByPage(pageParams, dto);

        return PageResult.<DeptVo>builder()
                .list(page.getRecords())
                .pageNo(page.getCurrent())
                .pageSize(page.getSize())
                .total(page.getTotal())
                .build();
    }

    /**
     * 根据id查询部门表详情
     *
     * @param id 主键
     * @return 部门表详情 DeptVo}
     */
    public DeptVo getById(Long id) {
        DeptEntity deptEntity = super.getById(id);

        DeptVo deptVo = new DeptVo();
        BeanUtils.copyProperties(deptEntity, deptVo);

        return deptVo;
    }

    /**
     * 添加部门表
     *
     * @param dto 部门表添加
     */
    @Override
    public void create(DeptDto dto) {
        DeptEntity dept = new DeptEntity();
        BeanUtils.copyProperties(dto, dept);

        save(dept);
    }

    /**
     * 更新部门表
     *
     * @param dto 部门表更新
     */
    @Override
    public void update(DeptDto dto) {
        DeptEntity dept = new DeptEntity();
        BeanUtils.copyProperties(dto, dept);

        updateById(dept);
    }

    /**
     * 删除|批量删除部门表
     *
     * @param ids 删除id列表
     */
    @Override
    public void batchDelete(List<Long> ids) {
        removeByIds(ids);
    }
}