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.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 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(StringUtils.isNotBlank(bo.getOneStartTime()), CourseTableTime::getOneStartTime, bo.getOneStartTime());
        lqw.eq(StringUtils.isNotBlank(bo.getOneEndTime()), CourseTableTime::getOneEndTime, bo.getOneEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getTwoStartTime()), CourseTableTime::getTwoStartTime, bo.getTwoStartTime());
        lqw.eq(StringUtils.isNotBlank(bo.getTwoEndTime()), CourseTableTime::getTwoEndTime, bo.getTwoEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getThreeStartTime()), CourseTableTime::getThreeStartTime, bo.getThreeStartTime());
        lqw.eq(StringUtils.isNotBlank(bo.getThreeEndTime()), CourseTableTime::getThreeEndTime, bo.getThreeEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getFourStartTime()), CourseTableTime::getFourStartTime, bo.getFourStartTime());
        lqw.eq(StringUtils.isNotBlank(bo.getFourEndTime()), CourseTableTime::getFourEndTime, bo.getFourEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getFiveStartTime()), CourseTableTime::getFiveStartTime, bo.getFiveStartTime());
        lqw.eq(StringUtils.isNotBlank(bo.getFiveEndTime()), CourseTableTime::getFiveEndTime, bo.getFiveEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSixStartTime()), CourseTableTime::getSixStartTime, bo.getSixStartTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSixEndTime()), CourseTableTime::getSixEndTime, bo.getSixEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSevenStartTime()), CourseTableTime::getSevenStartTime, bo.getSevenStartTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSevenEndTime()), CourseTableTime::getSevenEndTime, bo.getSevenEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getEightStartTime()), CourseTableTime::getEightStartTime, bo.getEightStartTime());
        lqw.eq(StringUtils.isNotBlank(bo.getEightEndTime()), CourseTableTime::getEightEndTime, bo.getEightEndTime());
        return lqw;
    }

    /**
     * 新增课程-时间
     */
    @Override
    public Boolean insertByBo(CourseTableTimeBo bo) {
        CourseTableTime add = MapstructUtils.convert(bo, CourseTableTime.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改课程-时间
     */
    @Override
    public Boolean updateByBo(CourseTableTimeBo bo) {
        CourseTableTime update = MapstructUtils.convert(bo, CourseTableTime.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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