package org.dromara.system.service.impl;

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.springframework.stereotype.Service;
import org.dromara.system.domain.bo.AskLeaveCourseBo;
import org.dromara.system.domain.vo.AskLeaveCourseVo;
import org.dromara.system.domain.AskLeaveCourse;
import org.dromara.system.mapper.AskLeaveCourseMapper;
import org.dromara.system.service.IAskLeaveCourseService;

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

/**
 * 请假课程信息Service业务层处理
 *
 * @author boman
 * @date 2024-11-04
 */
@RequiredArgsConstructor
@Service
public class AskLeaveCourseServiceImpl implements IAskLeaveCourseService {

    private final AskLeaveCourseMapper baseMapper;

    /**
     * 查询请假课程信息
     */
    @Override
    public AskLeaveCourseVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询请假课程信息列表
     */
    @Override
    public TableDataInfo<AskLeaveCourseVo> queryPageList(AskLeaveCourseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AskLeaveCourse> lqw = buildQueryWrapper(bo);
        Page<AskLeaveCourseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询请假课程信息列表
     */
    @Override
    public List<AskLeaveCourseVo> queryList(AskLeaveCourseBo bo) {
        LambdaQueryWrapper<AskLeaveCourse> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AskLeaveCourse> buildQueryWrapper(AskLeaveCourseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AskLeaveCourse> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCourseId() != null, AskLeaveCourse::getCourseId, bo.getCourseId());
        lqw.eq(bo.getLeaveId() != null, AskLeaveCourse::getLeaveId, bo.getLeaveId());
        lqw.eq(StringUtils.isNotBlank(bo.getCourseDate()), AskLeaveCourse::getCourseDate, bo.getCourseDate());
        lqw.eq(StringUtils.isNotBlank(bo.getWeek()), AskLeaveCourse::getWeek, bo.getWeek());
        lqw.eq(bo.getClassNumber() != null, AskLeaveCourse::getClassNumber, bo.getClassNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getTimeFrame()), AskLeaveCourse::getTimeFrame, bo.getTimeFrame());
        lqw.like(StringUtils.isNotBlank(bo.getCourseName()), AskLeaveCourse::getCourseName, bo.getCourseName());
        return lqw;
    }

    /**
     * 新增请假课程信息
     */
    @Override
    public Boolean insertByBo(AskLeaveCourseBo bo) {
        AskLeaveCourse add = MapstructUtils.convert(bo, AskLeaveCourse.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

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

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

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