package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.RegisterTeacher;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.RegisterTeacherClassBo;
import org.dromara.system.domain.vo.RegisterTeacherClassVo;
import org.dromara.system.domain.RegisterTeacherClass;
import org.dromara.system.mapper.RegisterTeacherClassMapper;
import org.dromara.system.service.IRegisterTeacherClassService;

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

/**
 * 注册-老师-班级（学科）Service业务层处理
 *
 * @author boman
 * @date 2023-08-16
 */
@RequiredArgsConstructor
@Service
public class RegisterTeacherClassServiceImpl implements IRegisterTeacherClassService {

    private final RegisterTeacherClassMapper baseMapper;

    /**
     * 查询注册-老师-班级（学科）
     */
    @Override
    public RegisterTeacherClassVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询注册-老师-班级（学科）列表
     */
    @Override
    public TableDataInfo<RegisterTeacherClassVo> queryPageList(RegisterTeacherClassBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RegisterTeacherClass> lqw = buildQueryWrapper(bo);
        Page<RegisterTeacherClassVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询注册-老师-班级（学科）列表
     */
    @Override
    public List<RegisterTeacherClassVo> queryList(RegisterTeacherClassBo bo) {
        LambdaQueryWrapper<RegisterTeacherClass> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoMapperList(lqw);
    }

    private LambdaQueryWrapper<RegisterTeacherClass> buildQueryWrapper(RegisterTeacherClassBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RegisterTeacherClass> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTenantId()), RegisterTeacherClass::getTenantId, bo.getTenantId());
        lqw.eq(bo.getTeacherId() != null, RegisterTeacherClass::getTeacherId, bo.getTeacherId());
        lqw.eq(bo.getClassId() != null, RegisterTeacherClass::getClassId, bo.getClassId());
        lqw.like(StringUtils.isNotBlank(bo.getClassName()), RegisterTeacherClass::getClassName, bo.getClassName());
        lqw.eq(StringUtils.isNotBlank(bo.getPeriod()), RegisterTeacherClass::getPeriod, bo.getPeriod());
        lqw.eq(StringUtils.isNotBlank(bo.getDiscipline()), RegisterTeacherClass::getDiscipline, bo.getDiscipline());
        return lqw;
    }

    /**
     * 新增注册-老师-班级（学科）
     */
    @Override
    public Boolean insertByBo(RegisterTeacherClassBo bo) {
        RegisterTeacherClass add = MapstructUtils.convert(bo, RegisterTeacherClass.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改注册-老师-班级（学科）
     */
    @Override
    public Boolean updateByBo(RegisterTeacherClassBo bo) {
        RegisterTeacherClass update = MapstructUtils.convert(bo, RegisterTeacherClass.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RegisterTeacherClass entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除注册-老师-班级（学科）
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
