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.dromara.system.domain.CourseTable;
import org.dromara.system.domain.list.CourseTableTimeListVo;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.CourseTableTimeBo;
import org.dromara.system.domain.vo.CourseTableTimeVo;
import org.dromara.system.domain.CourseTableTime;
import org.dromara.system.mapper.CourseTableTimeMapper;
import org.dromara.system.service.ICourseTableTimeService;
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 CourseTableTimeServiceImpl implements ICourseTableTimeService {

    private final CourseTableTimeMapper baseMapper;

    /**
     * 查询课程-时间
     */
    @Override
    public CourseTableTimeVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询课程-时间列表
     */
    @Override
    public TableDataInfo<CourseTableTimeVo> queryPageList(CourseTableTimeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CourseTableTime> lqw = buildQueryWrapper(bo);
        Page<CourseTableTimeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询课程-时间列表
     */
    @Override
    public List<CourseTableTimeVo> queryList(CourseTableTimeBo bo) {
        LambdaQueryWrapper<CourseTableTime> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CourseTableTime> buildQueryWrapper(CourseTableTimeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CourseTableTime> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSchoolId()), CourseTableTime::getSchoolId, bo.getSchoolId());
        lqw.like(StringUtils.isNotBlank(bo.getSchoolName()), CourseTableTime::getSchoolName, bo.getSchoolName());
        lqw.eq(bo.getClassNumber() != null, CourseTableTime::getClassNumber, bo.getClassNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getTimeFrame()), CourseTableTime::getTimeFrame, bo.getTimeFrame());
        lqw.eq(StringUtils.isNotBlank(bo.getStartTime()), CourseTableTime::getStartTime, bo.getStartTime());
        lqw.eq(StringUtils.isNotBlank(bo.getEndTime()), CourseTableTime::getEndTime, bo.getEndTime());
        lqw.orderByAsc(CourseTableTime::getTimeFrame);
        lqw.orderByAsc(CourseTableTime::getClassNumber);
        return lqw;
    }

    /**
     * 新增课程-时间
     */
    @Override
    @Transactional
    public Boolean insertByBo(CourseTableTimeListVo boList) {
        List<CourseTableTimeBo> CourseTableTimeBoList = boList.getCourseTableTimeBoList();
        boolean flag = true;
        for (CourseTableTimeBo courseTableTimeBo : CourseTableTimeBoList) {
            courseTableTimeBo.setSchoolId(boList.getSchoolId());
            courseTableTimeBo.setSchoolName(boList.getSchoolName());
            CourseTableTime add = MapstructUtils.convert(courseTableTimeBo, CourseTableTime.class);
            validEntityBeforeSave(add);
            flag = baseMapper.insert(add) > 0;
            if (flag) {
                courseTableTimeBo.setId(add.getId());
            }
        }

        return flag;
    }

    /**
     * 修改课程-时间
     */
    @Override
    @Transactional
    public Boolean updateByBo(CourseTableTimeListVo boList) {
        //删除
        CourseTableTimeBo bo = new CourseTableTimeBo();
        bo.setSchoolId(boList.getSchoolId());
        LambdaQueryWrapper<CourseTableTime> lqw = buildQueryWrapper(bo);
        baseMapper.delete(lqw);
        List<CourseTableTimeBo> CourseTableTimeBoList = boList.getCourseTableTimeBoList();
        int index = 0;
        for (CourseTableTimeBo courseTableTimeBo : CourseTableTimeBoList) {
            courseTableTimeBo.setSchoolId(boList.getSchoolId());
            courseTableTimeBo.setSchoolName(boList.getSchoolName());
            //新增
            CourseTableTime add = MapstructUtils.convert(courseTableTimeBo, CourseTableTime.class);
            validEntityBeforeSave(add);
            index = index + baseMapper.insert(add);
        }
        return index > 0;
    }

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

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

    @Override
    public int deleteWithValidBySchoolId(String schoolId) {
        //删除
        CourseTableTimeBo bo = new CourseTableTimeBo();
        bo.setSchoolId(schoolId);
        LambdaQueryWrapper<CourseTableTime> lqw = buildQueryWrapper(bo);
        return baseMapper.delete(lqw);
    }
}
