package com.ruoyi.info.management.service.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.info.management.domain.TeacherDepartmentRel;
import com.ruoyi.info.management.domain.bo.TeacherDepartmentRelBo;
import com.ruoyi.info.management.domain.vo.TeacherDepartmentRelVo;
import com.ruoyi.info.management.exception.TeacherDepartmentRelExceptionEnum;
import com.ruoyi.info.management.mapper.TeacherDepartmentRelMapper;
import com.ruoyi.info.management.service.ITeacherDepartmentRelService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;

/**
 * 管理老师科室关联Service业务层处理
 *
 * @author baifc
 * @date 2025-09-30
 */
@RequiredArgsConstructor
@Service
public class TeacherDepartmentRelServiceImpl implements ITeacherDepartmentRelService {

    private final TeacherDepartmentRelMapper baseMapper;

    /**
     * 查询管理老师科室关联分页
     *
     * @param bo 管理老师科室关联
     * @return 管理老师科室关联
     */
    @Override
    public TableDataInfo<TeacherDepartmentRelVo> queryPageList(TeacherDepartmentRelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TeacherDepartmentRel> lqw = buildQueryWrapper(bo);
        Page<TeacherDepartmentRelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询管理老师科室关联列表
     *
     * @param bo 管理老师科室关联
     * @return 管理老师科室关联
     */
    @Override
    public List<TeacherDepartmentRelVo> queryList(TeacherDepartmentRelBo bo) {
        LambdaQueryWrapper<TeacherDepartmentRel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TeacherDepartmentRel> buildQueryWrapper(TeacherDepartmentRelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TeacherDepartmentRel> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTeacherId() != null, TeacherDepartmentRel::getTeacherId, bo.getTeacherId());
        lqw.eq(bo.getHospitalId() != null, TeacherDepartmentRel::getHospitalId, bo.getHospitalId());
        lqw.eq(bo.getDepartmentId() != null, TeacherDepartmentRel::getDepartmentId, bo.getDepartmentId());
        return lqw;
    }

    /**
     * 查询管理老师科室关联
     *
     * @param id 管理老师科室关联主键
     * @return 管理老师科室关联
     */
    @Override
    public TeacherDepartmentRelVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 详情管理老师科室关联
     *
     * @param id 管理老师科室关联主键
     * @return 管理老师科室关联
     */
    @Override
    public TeacherDepartmentRel loadById(Long id, Boolean tw){
        TeacherDepartmentRel info = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(TeacherDepartmentRelExceptionEnum.TeacherDepartmentRel_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增管理老师科室关联
     *
     * @param bo 管理老师科室关联
     * @return 结果
     */
    @Override
    public Boolean insertByBo(TeacherDepartmentRelBo bo) {
        TeacherDepartmentRel add = BeanUtil.toBean(bo, TeacherDepartmentRel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改管理老师科室关联
     *
     * @param bo 管理老师科室关联
     * @return 结果
     */
    @Override
    public Boolean updateByBo(TeacherDepartmentRelBo bo) {
        TeacherDepartmentRel teacherDepartmentRel = baseMapper.selectById(bo.getId());
        TeacherDepartmentRel update = BeanCopyUtils.copy(bo, teacherDepartmentRel);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TeacherDepartmentRel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除管理老师科室关联
     *
     * @param ids 需要删除的管理老师科室关联主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<TeacherDepartmentRel> queryByDepartmentIds(Collection<Long> departmentIds) {
        LambdaQueryWrapper<TeacherDepartmentRel> lqw = Wrappers.lambdaQuery();
        lqw.in(TeacherDepartmentRel::getDepartmentId, departmentIds);
        return baseMapper.selectList(lqw);
    }

    @Override
    public List<TeacherDepartmentRel> queryByTeacherId(Long teacherId) {
        LambdaQueryWrapper<TeacherDepartmentRel> lqw = Wrappers.lambdaQuery();
        lqw.eq(TeacherDepartmentRel::getTeacherId, teacherId);
        return baseMapper.selectList(lqw);
    }

}
