package com.ruoyi.dz.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.enums.UserTypeEnum;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dz.mapper.DzSchoolMapper;
import com.ruoyi.dz.domain.DzSchool;
import com.ruoyi.dz.service.IDzSchoolService;

/**
 * 机构校区Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-12
 */
@Service
public class DzSchoolServiceImpl implements IDzSchoolService
{
    @Autowired
    private DzSchoolMapper dzSchoolMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询机构校区
     *
     * @param id 机构校区主键
     * @return 机构校区
     */
    @Override
    public DzSchool selectDzSchoolById(Long id)
    {
        return dzSchoolMapper.selectDzSchoolById(id);
    }

    /**
     * 查询机构校区列表
     *
     * @param dzSchool 机构校区
     * @return 机构校区
     */
    @Override
    public List<DzSchool> selectDzSchoolList(DzSchool dzSchool)
    {
        return dzSchoolMapper.selectDzSchoolList(dzSchool);
    }

    @Override
    public List<DzSchool> selectDzSchoolListByIds(List<Long> schoolIds)
    {
        return dzSchoolMapper.selectDzSchoolListByIds(schoolIds);
    }

    /**
     * 新增机构校区
     *
     * @param dzSchool 机构校区
     * @return 结果
     */
    @Override
    public int insertDzSchool(DzSchool dzSchool)
    {
        dzSchool.setCreateTime(DateUtils.getNowDate());
        dzSchoolMapper.insertDzSchool(dzSchool);
        if(null != dzSchool.getDeptId() && dzSchool.getDeptId() > 0) {
            Long campusId = dzSchool.getId();
            Long userId = userService.insertRelateUser(UserTypeEnum.School, campusId, null, dzSchool.getDeptId(), dzSchool.getName(), dzSchool.getName());
            dzSchool.setUserId(userId);
            dzSchoolMapper.updateDzSchool(dzSchool);
        }
        return dzSchool.getId().intValue();
    }

    /**
     * 修改机构校区
     *
     * @param dzSchool 机构校区
     * @return 结果
     */
    @Override
    public int updateDzSchool(DzSchool dzSchool)
    {
        dzSchool.setUpdateTime(DateUtils.getNowDate());
        return dzSchoolMapper.updateDzSchool(dzSchool);
    }

    /**
     * 批量删除机构校区
     *
     * @param ids 需要删除的机构校区主键
     * @return 结果
     */
    @Override
    public int deleteDzSchoolByIds(Long[] ids)
    {
        return dzSchoolMapper.deleteDzSchoolByIds(ids);
    }

    /**
     * 删除机构校区信息
     *
     * @param id 机构校区主键
     * @return 结果
     */
    @Override
    public int deleteDzSchoolById(Long id)
    {
        return dzSchoolMapper.deleteDzSchoolById(id);
    }

    /**
     * 校验学校名称是否唯一
     *
     * @param name 学校名称
     * @param id 学校ID（修改时传入，新增时传null）
     * @return true-唯一，false-不唯一
     */
    @Override
    public boolean checkSchoolName(String name, Long id)
    {
        int count = dzSchoolMapper.checkSchoolName(name, id);
        return count == 0;
    }

    /**
     * 根据班级ID列表查询学校信息
     *
     * @param classIds 班级ID列表
     * @return 学校信息列表
     */
    @Override
    public List<DzSchool> getDzSchoolByClassIds(List<Long> classIds)
    {
        if (CollectionUtils.isEmpty(classIds)) {
            return new java.util.ArrayList<>();
        }
        return dzSchoolMapper.getDzSchoolByClassIds(classIds);
    }
}
