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.StudentLiveOldBo;
import org.dromara.system.domain.vo.StudentLiveOldVo;
import org.dromara.system.domain.StudentLiveOld;
import org.dromara.system.mapper.StudentLiveOldMapper;
import org.dromara.system.service.IStudentLiveOldService;
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 StudentLiveOldServiceImpl implements IStudentLiveOldService {

    private final StudentLiveOldMapper baseMapper;
    private final StudentInfoMapper studentInfoMapper;

    /**
     * 查询学生生活历史信息
     */
    @Override
    public StudentLiveOldVo queryById(Long liveId){
        return baseMapper.selectVoMapperById(liveId);
    }

    /**
     * 查询学生生活历史信息列表
     */
    @Override
    public TableDataInfo<StudentLiveOldVo> queryPageList(StudentLiveOldBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StudentLiveOld> lqw = buildQueryWrapper(bo);
        Page<StudentLiveOldVo> result = baseMapper.selectVoMapperPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询学生生活历史信息列表
     */
    @Override
    public List<StudentLiveOldVo> queryList(StudentLiveOldBo bo) {
        LambdaQueryWrapper<StudentLiveOld> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoMapperList(lqw);
    }

    private LambdaQueryWrapper<StudentLiveOld> buildQueryWrapper(StudentLiveOldBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StudentLiveOld> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStudentId() != null, StudentLiveOld::getStudentId, bo.getStudentId());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), StudentLiveOld::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getName()), StudentLiveOld::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), StudentLiveOld::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getAge()), StudentLiveOld::getAge, bo.getAge());
        lqw.eq(StringUtils.isNotBlank(bo.getSchool()), StudentLiveOld::getSchool, bo.getSchool());
        lqw.eq(StringUtils.isNotBlank(bo.getStudentNumber()), StudentLiveOld::getStudentNumber, bo.getStudentNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCard()), StudentLiveOld::getIdCard, bo.getIdCard());
        lqw.eq(StringUtils.isNotBlank(bo.getHeight()), StudentLiveOld::getHeight, bo.getHeight());
        lqw.eq(StringUtils.isNotBlank(bo.getWeight()), StudentLiveOld::getWeight, bo.getWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateType()), StudentLiveOld::getCreateType, bo.getCreateType());
        return lqw;
    }

    /**
     * 新增学生生活历史信息
     */
    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(StudentLiveOldBo bo) {
        LoginUser user = LoginHelper.getLoginUser();
        StudentInfoVo student = studentInfoMapper.selectStudentInfoByStudentId(bo.getStudentId());
        if(student==null){
            throw new ServiceException("请先填写学生档案信息");
        }
        student.setHeight(bo.getHeight());
        student.setWeight(bo.getWeight());
        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());
        StudentLiveOld add = MapstructUtils.convert(bo, StudentLiveOld.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLiveId(add.getLiveId());
        }
        return flag;
    }

    /**
     * 修改学生生活历史信息
     */
    @Override
    public Boolean updateByBo(StudentLiveOldBo bo) {
        StudentLiveOld update = MapstructUtils.convert(bo, StudentLiveOld.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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