package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
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.StudentInfo;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.TeacherInfoBo;
import org.dromara.system.domain.vo.TeacherInfoVo;
import org.dromara.system.domain.TeacherInfo;
import org.dromara.system.mapper.TeacherInfoMapper;
import org.dromara.system.service.ITeacherInfoService;

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

/**
 * 老师档案信息Service业务层处理
 *
 * @author boman
 * @date 2023-08-16
 */
@RequiredArgsConstructor
@Service
public class TeacherInfoServiceImpl implements ITeacherInfoService {

    private final TeacherInfoMapper baseMapper;

    /**
     * 查询老师档案信息
     */
    @Override
    public TeacherInfoVo queryById(Long id){
        return baseMapper.selectVoMapperById(id);
    }

    /**
     * 查询老师档案信息列表
     */
    @Override
    public TableDataInfo<TeacherInfoVo> queryPageList(TeacherInfoBo bo, PageQuery pageQuery) {

        LoginUser loginUser = LoginHelper.getLoginUser();
        Map<String, Object> params = bo.getParams();
        String role = "";
        if (params != null && params.size() > 0) {
            role = (String) params.get("role");
        }

        if("teacher".equals(role)){
            bo.setTeacherId(loginUser.getUserId());
        }

        QueryWrapper<TeacherInfo> lqw = buildQueryWrapper(bo);
        Page<TeacherInfoVo> result = baseMapper.selectVoMapperPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询老师档案信息列表
     */
    @Override
    public List<TeacherInfoVo> queryList(TeacherInfoBo bo) {
        QueryWrapper<TeacherInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoMapperList(lqw);
    }

    private QueryWrapper<TeacherInfo> buildQueryWrapper(TeacherInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<TeacherInfo> lqw = Wrappers.query();
        lqw.eq(bo.getTeacherId() != null && bo.getTeacherId() !=0L, "t.teacher_id", bo.getTeacherId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), "t.name", bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), "t.sex", bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getAge()), "t.age", bo.getAge());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), "t.phone", bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getProfessional()), "t.professional", bo.getProfessional());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCard()), "t.id_card", bo.getIdCard());
        lqw.eq(StringUtils.isNotBlank(bo.getHeight()), "t.height", bo.getHeight());
        lqw.eq(StringUtils.isNotBlank(bo.getWeight()), "t.weight", bo.getWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getBloodType()), "t.blood_type", bo.getBloodType());
        lqw.eq(StringUtils.isNotBlank(bo.getPoliticalStatus()), "t.political_status", bo.getPoliticalStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), "t.address", bo.getAddress());
        lqw.eq(StringUtils.isNotBlank((CharSequence) bo.getOnBoardTime()), "t.on_board_time", bo.getOnBoardTime());
        lqw.eq(StringUtils.isNotBlank((CharSequence) bo.getJobTime()), "t.job_time", bo.getJobTime());
        lqw.eq(bo.getSchoolId() != null && bo.getSchoolId() !=0L, "f.school_id", bo.getJobTime());
        lqw.groupBy("t.teacher_id");
        lqw.orderByDesc("ANY_VALUE (t.create_time)");
        return lqw;
    }

    /**
     * 新增老师档案信息
     */
    @Override
    public R<Void> insertByBo(TeacherInfoBo bo) {

        //查询数据库是否存在信息
        TeacherInfoBo teacher = new TeacherInfoBo();
        teacher.setTeacherId(bo.getTeacherId());
        QueryWrapper<TeacherInfo> lqw = buildQueryWrapper(teacher);
        List<TeacherInfoVo> teacherInfos = baseMapper.selectVoMapperList(lqw);
        if(teacherInfos!=null && teacherInfos.size()>0){
            return R.fail("老师信息已录入，请勿重复录入");
        }
        TeacherInfo add = MapstructUtils.convert(bo, TeacherInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 修改老师档案信息
     */
    @Override
    public Boolean updateByBo(TeacherInfoBo bo) {
        TeacherInfo update = MapstructUtils.convert(bo, TeacherInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除老师档案信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
