package org.dromara.system.service.impl;

import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.DateUtils;
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.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.vo.FormalParentsStudentVo;
import org.dromara.system.domain.vo.FormalTeacherClassListVo;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.FormalTeacherClassBo;
import org.dromara.system.domain.vo.FormalTeacherClassVo;
import org.dromara.system.domain.FormalTeacherClass;
import org.dromara.system.mapper.FormalTeacherClassMapper;
import org.dromara.system.service.IFormalTeacherClassService;

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

/**
 * 老师-班级（学科）(审核通过之后数据)Service业务层处理
 *
 * @author Lion Li
 * @date 2023-08-16
 */
@RequiredArgsConstructor
@Service
public class FormalTeacherClassServiceImpl implements IFormalTeacherClassService {

    private final FormalTeacherClassMapper baseMapper;

    /**
     * 查询老师-班级（学科）(审核通过之后数据)
     */
    @Override
    public FormalTeacherClassVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询老师-班级（学科）(审核通过之后数据)列表
     */
    @Override
    public TableDataInfo<FormalTeacherClassVo> queryPageList(FormalTeacherClassBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<FormalTeacherClass> lqw = buildQueryWrapper(bo);
        Page<FormalTeacherClassVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询老师-班级（学科）(审核通过之后数据)列表
     */
    @Override
    public List<FormalTeacherClassVo> queryList(FormalTeacherClassBo bo) {
        LambdaQueryWrapper<FormalTeacherClass> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FormalTeacherClass> buildQueryWrapper(FormalTeacherClassBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FormalTeacherClass> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTeacherId() != null, FormalTeacherClass::getTeacherId, bo.getTeacherId());
        lqw.eq(StringUtils.isNotBlank(bo.getSchoolId()), FormalTeacherClass::getSchoolId, bo.getSchoolId());
        lqw.like(StringUtils.isNotBlank(bo.getSchoolName()), FormalTeacherClass::getSchoolName, bo.getSchoolName());
        lqw.eq(bo.getClassId()!= null, FormalTeacherClass::getClassId, bo.getClassId());
        lqw.like(StringUtils.isNotBlank(bo.getClassName()), FormalTeacherClass::getClassName, bo.getClassName());
        lqw.eq(StringUtils.isNotBlank(bo.getDiscipline()), FormalTeacherClass::getDiscipline, bo.getDiscipline());
        lqw.like(StringUtils.isNotBlank(bo.getTeacherName()), FormalTeacherClass::getTeacherName, bo.getTeacherName());
        return lqw;
    }

    /**
     * 新增老师-班级（学科）(审核通过之后数据)
     */
    @Override
    public Boolean insertByBo(FormalTeacherClassBo bo) {
        LoginUser user = LoginHelper.getLoginUser();
        bo.setTeacherId(user.getUserId());
        bo.setTeacherName(user.getNickname());
        FormalTeacherClass add = MapstructUtils.convert(bo, FormalTeacherClass.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改老师-班级（学科）(审核通过之后数据)
     */
    @Override
    public Boolean updateByBo(FormalTeacherClassBo bo) {
        FormalTeacherClass update = MapstructUtils.convert(bo, FormalTeacherClass.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public R<Void> updateFormalTeacherClassList(FormalTeacherClassListVo formalTeacherClassListVo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<FormalTeacherClassBo> pormalTeacherClassList = formalTeacherClassListVo.getPormalTeacherClassList();
        if(pormalTeacherClassList==null || pormalTeacherClassList.size()<=0){
            return R.fail("参数错误");
        }
        //先删除
        baseMapper.deleteFormalTeacherClassByTeacherId(loginUser.getUserId());
        for (FormalTeacherClassBo formalTeacherClass : pormalTeacherClassList) {
            formalTeacherClass.setTeacherId(loginUser.getUserId());
            formalTeacherClass.setTeacherName(loginUser.getNickname());
            formalTeacherClass.setUpdateTime(DateUtils.getNowDate());
            formalTeacherClass.setCreateTime(DateUtils.getNowDate());
            baseMapper.insertFormalTeacherClass(formalTeacherClass);
        }
        return R.ok();
    }

    @Override
    public TableDataInfo<FormalTeacherClassVo> selectFormalTeacherClassListNoPage(FormalTeacherClassBo bo) {
        if(bo.getClassId()==null || bo.getClassId()==0L ){
            return TableDataInfo.build(new ArrayList<>());
        }
        LambdaQueryWrapper<FormalTeacherClass> lqw = buildQueryWrapper(bo);
        List<FormalTeacherClassVo> list = baseMapper.selectVoList(lqw);
        return TableDataInfo.build(list);
    }

    @Override
    public List<FormalTeacherClassVo> getTeacherClass(String teacherId) {
        return baseMapper.getTeacherClass(teacherId);
    }
}
