package org.dromara.system.service.impl;

import lombok.SneakyThrows;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.ClassUtils;
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.StudentInfo;
import org.dromara.system.domain.vo.StudentInfoVo;
import org.dromara.system.mapper.StudentInfoMapper;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.StudentMindOldBo;
import org.dromara.system.domain.vo.StudentMindOldVo;
import org.dromara.system.domain.StudentMindOld;
import org.dromara.system.mapper.StudentMindOldMapper;
import org.dromara.system.service.IStudentMindOldService;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 学生心理健康历史信息Service业务层处理
 *
 * @author Lion Li
 * @date 2023-08-16
 */
@RequiredArgsConstructor
@Service
public class StudentMindOldServiceImpl implements IStudentMindOldService {

    private final StudentMindOldMapper baseMapper;
    private final StudentInfoMapper studentInfoMapper;

    /**
     * 查询学生心理健康历史信息
     */
    @Override
    public StudentMindOldVo queryById(Long mindId){
        return baseMapper.selectVoMapperById(mindId);
    }

    /**
     * 查询学生心理健康历史信息列表
     */
    @Override
    public TableDataInfo<StudentMindOldVo> queryPageList(StudentMindOldBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StudentMindOld> lqw = buildQueryWrapper(bo);
        Page<StudentMindOldVo> result = baseMapper.selectVoMapperPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询学生心理健康历史信息列表
     */
    @Override
    public List<StudentMindOldVo> queryList(StudentMindOldBo bo) {
        LambdaQueryWrapper<StudentMindOld> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoMaooerList(lqw);
    }

    private LambdaQueryWrapper<StudentMindOld> buildQueryWrapper(StudentMindOldBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StudentMindOld> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStudentId() != null, StudentMindOld::getStudentId, bo.getStudentId());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), StudentMindOld::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getName()), StudentMindOld::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), StudentMindOld::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getAge()), StudentMindOld::getAge, bo.getAge());
        lqw.eq(StringUtils.isNotBlank(bo.getSchool()), StudentMindOld::getSchool, bo.getSchool());
        lqw.eq(StringUtils.isNotBlank(bo.getStudentNumber()), StudentMindOld::getStudentNumber, bo.getStudentNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCard()), StudentMindOld::getIdCard, bo.getIdCard());
        lqw.eq(StringUtils.isNotBlank(bo.getMind()), StudentMindOld::getMind, bo.getMind());
        lqw.eq(StringUtils.isNotBlank(bo.getPsychologicalDescription()), StudentMindOld::getPsychologicalDescription, bo.getPsychologicalDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateType()), StudentMindOld::getCreateType, bo.getCreateType());
        lqw.orderByDesc(StudentMindOld::getCreateTime);
        return lqw;
    }

    /**
     * 新增学生心理健康历史信息
     */
    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(StudentMindOldBo bo) {

        LoginUser user = LoginHelper.getLoginUser();
        StudentInfoVo student = studentInfoMapper.selectStudentInfoByStudentId(bo.getStudentId());
        if(student==null){
            throw new ServiceException("请先填写学生档案信息");
        }
        student.setMind(bo.getMind());
        student.setPsychologicalDescription(bo.getPsychologicalDescription());
        student.setUpdateTime(DateUtils.getNowDate());
        StudentInfo studentInfo = new StudentInfo();
        ClassUtils.copyProperties(student, studentInfo);
        studentInfoMapper.updateById(studentInfo);

        ClassUtils.copyProperties(student, bo);
        bo.setCreateTime(DateUtils.getNowDate());
        bo.setUpdateTime(DateUtils.getNowDate());
        bo.setCreateByName(user.getUsername());

        StudentMindOld add = MapstructUtils.convert(bo, StudentMindOld.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMindId(add.getMindId());
        }
        return flag;
    }

    /**
     * 修改学生心理健康历史信息
     */
    @Override
    public Boolean updateByBo(StudentMindOldBo bo) {
        StudentMindOld update = MapstructUtils.convert(bo, StudentMindOld.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除学生心理健康历史信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
