package com.vctgo.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.ServletUtils;
import com.vctgo.common.mybatisplus.constant.MybatisPageConstants;
import com.vctgo.common.security.utils.SecurityUtils;
import com.vctgo.system.api.domain.SysClassSetting;
import com.vctgo.system.api.domain.dto.ReLocationDto;
import com.vctgo.system.api.domain.SysReLocationClass;
import com.vctgo.system.mapper.SysClassSettingMapper;
import com.vctgo.system.mapper.SysReLocationClassMapper;
import com.vctgo.system.service.ISysReLocationClassService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 位置与班级关联Service业务层处理
 *
 * @author caesar
 * @date 2023-05-30
 */
@Slf4j
@Service
public class SysReLocationClassServiceImpl extends ServiceImpl<SysReLocationClassMapper, SysReLocationClass> implements ISysReLocationClassService {
    @Autowired
    private SysReLocationClassMapper sysReLocationClassMapper;

    @Autowired
    private SysClassSettingMapper sysClassSettingMapper;

    /**
     * 查询位置与班级关联
     *
     * @param id 位置与班级关联主键
     * @return 位置与班级关联
     */
    @Override
    public SysReLocationClass selectSysReLocationClassById(Long id) {
        return sysReLocationClassMapper.selectById(id);
    }

    /**
     * 查询位置与班级关联列表-分页
     *
     * @param sysReLocationClass 位置与班级关联
     * @return 位置与班级关联
     */
    @Override
    public IPage<SysReLocationClass> selectSysReLocationClassPage(SysReLocationClass sysReLocationClass) {
        Page mpPage = new Page(Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM), 1L)
                , Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE), 10L));
        return sysReLocationClassMapper.selectSysReLocationClassList(mpPage, sysReLocationClass);
    }

    /**
     * 查询位置与班级关联列表
     *
     * @param sysReLocationClass 位置与班级关联
     * @return 位置与班级关联
     */
    @Override
    public List<SysReLocationClass> selectSysReLocationClassList(SysReLocationClass sysReLocationClass) {
        return sysReLocationClassMapper.selectSysReLocationClassList(sysReLocationClass);
    }

    /**
     * 查询位置相关信息列表
     *
     * @param reLocationDto 位置相关信息
     * @return 位置相关信息集合
     */
    @Override
    public List<ReLocationDto> selectLocationInfoByParam(ReLocationDto reLocationDto) {
        if(StringUtils.isNotEmpty(reLocationDto.getGradeIn()) || StringUtils.isNotEmpty(reLocationDto.getClassNumber())){
            SysClassSetting sysClassSetting = new SysClassSetting();
            sysClassSetting.setGradeIn(reLocationDto.getGradeIn());
            sysClassSetting.setClassNumber(reLocationDto.getClassNumber());
            List<SysClassSetting> sysClassSettingList = sysClassSettingMapper.selectSysClassSettingList(sysClassSetting);
            List<Long> classIdList = sysClassSettingList.stream().map(SysClassSetting::getId).collect(Collectors.toList());
            reLocationDto.setClassIdList(classIdList);
        }
        reLocationDto.setTenantId(SecurityUtils.getTenantId());
        return sysReLocationClassMapper.selectLocationInfoByParam(reLocationDto);
    }

    @Override
    public Long staticsReLocationByClassId(Long classId) {
        return sysReLocationClassMapper.staticsReLocationByClassId(classId);
    }

    /**
     * 新增位置与班级关联
     *
     * @param sysReLocationClass 位置与班级关联
     * @return 结果
     */

    @Override
    public int insertSysReLocationClass(SysReLocationClass sysReLocationClass) {
        return sysReLocationClassMapper.insert(sysReLocationClass);
    }

    /**
     * 修改位置与班级关联
     *
     * @param sysReLocationClass 位置与班级关联
     * @return 结果
     */
    @Override
    public int updateSysReLocationClass(SysReLocationClass sysReLocationClass) {
        return sysReLocationClassMapper.updateById(sysReLocationClass);
    }

    /**
     * 批量删除位置与班级关联
     *
     * @param ids 需要删除的位置与班级关联主键
     * @return 结果
     */
    @Override
    public int deleteSysReLocationClassByIds(Long[] ids) {
        return sysReLocationClassMapper.deleteSysReLocationClassByIds(ids);
    }

    /**
     * 删除位置与班级关联信息
     *
     * @param id 位置与班级关联主键
     * @return 结果
     */
    @Override
    public int deleteSysReLocationClassById(Long id) {
        return sysReLocationClassMapper.deleteById(id);
    }

    /**
     * 通过位置Id和班级Id批量删除
     *
     * @param locationId 位置id
     * @param areaIds    需要删除的数据主键集合
     * @return
     */
    @Override
    public int updateSysReLocationClassBylocationIdAndAreaIds(Long locationId, Long[] areaIds) {
        return sysReLocationClassMapper.updateSysReLocationClassBylocationIdAndAreaIds(locationId, areaIds);
    }
}
