package org.dromara.system.service.impl.record;

import com.google.api.client.util.SecurityUtils;
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.FormalParentsStudent;
import org.dromara.system.domain.FormalTeacherClass;
import org.dromara.system.domain.SysUser;
import org.dromara.system.domain.record.RecordLeave;
import org.dromara.system.domain.record.bo.RecordLeaveBo;
import org.dromara.system.domain.record.vo.RecordLeaveVo;
import org.dromara.system.domain.vo.FormalTeacherClassVo;
import org.dromara.system.domain.vo.SysClientVo;
import org.dromara.system.mapper.FormalParentsStudentMapper;
import org.dromara.system.mapper.FormalTeacherClassMapper;
import org.dromara.system.mapper.record.RecordLeaveMapper;
import org.dromara.system.service.record.IRecordLeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 请假记录信息Service业务层处理
 *
 * @author boman
 * @date 2023-08-16
 */
@RequiredArgsConstructor
@Service
public class RecordLeaveServiceImpl implements IRecordLeaveService {

    private final RecordLeaveMapper baseMapper;

    @Autowired
    private FormalTeacherClassMapper formalTeacherClassMapper;

    @Autowired
    private FormalParentsStudentMapper formalParentsStudentMapper;
    /**
     * 查询请假记录信息
     */
    @Override
    public RecordLeaveVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询请假记录信息列表
     */
    @Override
    public TableDataInfo<RecordLeaveVo> queryPageList(RecordLeaveBo bo, PageQuery pageQuery) {
        Page<RecordLeaveVo> result = new Page<>();
        StringBuilder classId = new StringBuilder("0,");
        Map<String, Object> params = bo.getParams();
        String role = "admin";
        if (params != null && params.size() > 0) {
            role = (String) params.get("role");
        }
        if ("teacher".equals(role)) {
            FormalTeacherClass formalTeacherClass = new FormalTeacherClass();
            formalTeacherClass.setTeacherId(LoginHelper.getUserId());
            List<FormalTeacherClassVo> formalTeacherClasses = formalTeacherClassMapper.selectFormalTeacherClassList(formalTeacherClass);
            if (formalTeacherClasses != null && formalTeacherClasses.size() > 0) {
                for (FormalTeacherClassVo teacherClass : formalTeacherClasses) {
                    classId.append(teacherClass.getClassId()).append(",");
                }
            }
            if (StringUtils.isNotBlank(classId.toString())) {
                classId = new StringBuilder(classId.substring(0, classId.length() - 1));
            }
            bo.setClassId(classId.toString());
            LambdaQueryWrapper<RecordLeave> lqw = buildQueryWrapper(bo);
            result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        } else if ("parents".equals(role)) {
            FormalParentsStudent formalParentsStudent = new FormalParentsStudent();
            formalParentsStudent.setParentsId(LoginHelper.getUserId());
            List<FormalParentsStudent> formalParentsStudents = formalParentsStudentMapper.selectFormalParentsStudentList(formalParentsStudent);
            for (FormalParentsStudent parentsStudent : formalParentsStudents) {
                classId.append(parentsStudent.getClassId()).append(",");
            }
            if (StringUtils.isNotBlank(classId.toString())) {
                classId = new StringBuilder(classId.substring(0, classId.length() - 1));
            }
            bo.setClassId(classId.toString());
            LambdaQueryWrapper<RecordLeave> lqw = buildQueryWrapper(bo);

            result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        } else if ("admin".equals(role)) {
            LambdaQueryWrapper<RecordLeave> lqw = buildQueryWrapper(bo);
            result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        }

        return TableDataInfo.build(result);

    }

    /**
     * 查询请假记录信息列表
     */
    @Override
    public List<RecordLeaveVo> queryList(RecordLeaveBo bo) {
        LambdaQueryWrapper<RecordLeave> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RecordLeave> buildQueryWrapper(RecordLeaveBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RecordLeave> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), RecordLeave::getType, bo.getType());
        lqw.eq(bo.getStartTime() != null, RecordLeave::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, RecordLeave::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getReason()), RecordLeave::getReason, bo.getReason());
        lqw.eq(bo.getClassId() != null, RecordLeave::getClassId, bo.getClassId());
        lqw.like(StringUtils.isNotBlank(bo.getClassName()), RecordLeave::getClassName, bo.getClassName());
        lqw.eq(StringUtils.isNotBlank(bo.getAbsenteeId()), RecordLeave::getAbsenteeId, bo.getAbsenteeId());
        lqw.like(StringUtils.isNotBlank(bo.getAbsenteeName()), RecordLeave::getAbsenteeName, bo.getAbsenteeName());
        lqw.eq(bo.getExaminersId() != null, RecordLeave::getExaminersId, bo.getExaminersId());
        lqw.like(StringUtils.isNotBlank(bo.getExaminersName()), RecordLeave::getExaminersName, bo.getExaminersName());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), RecordLeave::getCategory, bo.getCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPass()), RecordLeave::getIsPass, bo.getIsPass());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoto()), RecordLeave::getPhoto, bo.getPhoto());
        lqw.eq(bo.getSubmitTime() != null, RecordLeave::getSubmitTime, bo.getSubmitTime());
        return lqw;
    }

    /**
     * 新增请假记录信息
     */
    @Override
    public Boolean insertByBo(RecordLeaveBo bo) {
        RecordLeave add = MapstructUtils.convert(bo, RecordLeave.class);
        validEntityBeforeSave(add);
        add.setSubmitTime(DateUtils.getNowDate());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRecordId(add.getRecordId());
        }
        return flag;
    }

    /**
     * 修改请假记录信息
     */
    @Override
    public Boolean updateByBo(RecordLeaveBo bo) {
        RecordLeave update = MapstructUtils.convert(bo, RecordLeave.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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