package com.template.services.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.model.pojo.SmartSemester;
import com.template.model.pojo.SmartSemester;
import com.template.mapper.SmartSemesterMapper;
import com.template.model.result.PageUtils;
import com.template.model.vo.SemesterVo;
import com.template.services.SmartSemesterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 应用管理 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2024-03-07
 */
@Service
public class SmartSemesterServiceImpl extends ServiceImpl<SmartSemesterMapper, SmartSemester> implements SmartSemesterService {
    @Autowired
    private SmartSemesterMapper smartSemesterMapper;

    @Override
    public int insertSmartSemester(SmartSemester sa) {
        int result = smartSemesterMapper.insert(sa);
        return sa.getId();
    }

    @Override
    public int updateSmartSemester(SmartSemester sa) {
        int result = smartSemesterMapper.updateById(sa);
        return result;
    }

    @Override
    public SmartSemester querySemesterByName(String name){
        QueryWrapper<SmartSemester> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        SmartSemester result = smartSemesterMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public PageUtils<SmartSemester> queryPageSmartSemesters(int currentPage, int pageCount, String name) {
        Page<SmartSemester> page = new Page<>(currentPage, pageCount);
        QueryWrapper<SmartSemester> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name), "name", name);
        IPage<SmartSemester> result = smartSemesterMapper.selectPage(page,queryWrapper);
        return new PageUtils<>(result);
    }

    @Override
    public int deleteSmartSemesterById(int id) {
        int result = smartSemesterMapper.deleteById(id);
        return result;
    }

    @Override
    public SmartSemester getSmartById(int id) {
        SmartSemester result = smartSemesterMapper.selectById(id);
        return result;
    }

    @Override
    public List<SmartSemester> queryAppletSemesters() {
        QueryWrapper<SmartSemester> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("category", "管理后台");
        List<SmartSemester> result = smartSemesterMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public SmartSemester getSemester(LocalDateTime now) {
        LambdaQueryWrapper<SmartSemester> wrapper=new LambdaQueryWrapper<>();
        wrapper.le(SmartSemester::getStartTime,now)
                .ge(SmartSemester::getEndTime,now);
        List<SmartSemester> list = this.list(wrapper);
        if (list.size()>0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<SmartSemester> getSemestersByTime(LocalDateTime now) {
        QueryWrapper<SmartSemester> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("start_time", now);
        queryWrapper.ge("end_time", now);
        List<SmartSemester> result = smartSemesterMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<SmartSemester> getSemesterTime(Date startTime, Date endTime) {
        LambdaQueryWrapper<SmartSemester> wrapper=new LambdaQueryWrapper<>();
        wrapper.and(query -> query.between(SmartSemester::getStartTime,startTime,endTime)
                .or()
                .between(SmartSemester::getEndTime,startTime,endTime));
        List<SmartSemester> list = this.list(wrapper);
        return list;
    }
}
