package com.template.services.impl;

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.enumModel.eAttendanceStatu;
import com.template.model.pojo.SmartAttendance;
import com.template.mapper.SmartAttendanceMapper;
import com.template.model.pojo.SmartVisitor;
import com.template.model.result.PageUtils;
import com.template.model.vo.AskForLeaveScreenVo;
import com.template.model.vo.AskForLeaveVo;
import com.template.model.vo.ClasAttendanceVo;
import com.template.model.vo.SmartAttendanceVo;
import com.template.services.SmartAttendanceService;
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.util.Date;
import java.util.List;

/**
 * <p>
 * 楼栋表 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-12-25
 */
@Service
public class SmartAttendanceServiceImpl extends ServiceImpl<SmartAttendanceMapper, SmartAttendance> implements SmartAttendanceService {

    @Autowired
    private SmartAttendanceMapper smartAttendanceMapper;

    @Override
    public List<SmartAttendance> queryAttendances(String startDate, String endDate) {

        QueryWrapper<SmartAttendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge(StringUtils.hasText(startDate), "attend_time", startDate);//大于等于
        queryWrapper.le(StringUtils.hasText(endDate), "attend_time", endDate);//小于等于

        List<SmartAttendance> result = smartAttendanceMapper.selectList(queryWrapper);
        return result;
    }


    @Override
    public List<SmartAttendance> queryExistAttendances(String startDate, String endDate) {
        QueryWrapper<SmartAttendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge(StringUtils.hasText(startDate), "start_time", startDate);//大于等于
        queryWrapper.le(StringUtils.hasText(endDate), "start_time", endDate);//小于等于
        queryWrapper.eq("status", eAttendanceStatu.AskLeave.getValue());//等于

        List<SmartAttendance> result = smartAttendanceMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public boolean insertAttendanceBatch(List<SmartAttendance> attendances) {
        boolean result = this.saveBatch(attendances);
        return result;
    }

    @Override
    public List<SmartAttendance> queryAttendanceList(Date startMonth, Date endMonth) {
        QueryWrapper<SmartAttendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("start_time", startMonth);//大于等于
        queryWrapper.le("start_time", endMonth);//小于等于

        List<SmartAttendance> result = smartAttendanceMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<SmartAttendance> getHistoricalAttendance(Integer userId,Integer status,Date startTime, Date endTime) {
        LambdaQueryWrapper<SmartAttendance> queryWrapper = new LambdaQueryWrapper<>();
        if (6==status) {
            queryWrapper.between(SmartAttendance::getInitiateTime,startTime,endTime);
        }else {
            queryWrapper.between(SmartAttendance::getAttendTime,startTime,endTime);
        }
        queryWrapper.eq(SmartAttendance::getStatus,status);
        queryWrapper.eq(SmartAttendance::getUserId,userId);
        List<SmartAttendance> result = smartAttendanceMapper.selectList(queryWrapper);

        return result;
    }

    @Override
    public List<Integer> getGroup() {
        return smartAttendanceMapper.getGroup();
    }

    @Override
    public List<SmartAttendance> track(String startTime, String endTime, int userId) {
        LambdaQueryWrapper<SmartAttendance> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SmartAttendance::getUserId,userId)
                .between(SmartAttendance::getAttendTime,startTime,endTime)
                .eq(SmartAttendance::getStatus,0)
                .orderByDesc(SmartAttendance::getAttendTime);
        List<SmartAttendance> list = this.list(wrapper);

        return list;
    }

    @Override
    public PageUtils<AskForLeaveVo> askForLeavePage(int currentPage, int pageCount,String name, String cardNo, String startTime, String endTime,String ifVerification,String classId) {
        Page<AskForLeaveVo> page = new Page<>(currentPage,pageCount);
        IPage<AskForLeaveVo> datas = smartAttendanceMapper.askForLeavePage(page, name,cardNo,startTime,endTime,ifVerification,classId);
        return new PageUtils(datas);
    }

    @Override
    public PageUtils<SmartAttendanceVo> queryPage(int currentPage, int pageCount, Integer gradeId, Integer classId, String status, String studentNo, String name, String startTime, String endTime) {
        Page<SmartAttendanceVo> page = new Page<>(currentPage,pageCount);
        IPage<SmartAttendanceVo> datas = smartAttendanceMapper.queryPage(page,gradeId,classId,status,studentNo,name,startTime,endTime);
        return new PageUtils(datas);
    }

    @Override
    public List<SmartAttendance> duplicatesList(Date startTime, Date endTime, Date initiateTime) {
        LambdaQueryWrapper<SmartAttendance> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SmartAttendance::getStartTime,startTime)
                .eq(SmartAttendance::getEndTime,endTime)
                .eq(SmartAttendance::getInitiateTime,initiateTime);
        List<SmartAttendance> list = this.list(wrapper);

        return list;
    }

    @Override
    public List<SmartAttendanceVo> queryExpor(Integer gradeId, Integer classId, String status, String studentNo, String name, String startTime, String endTime) {
        List<SmartAttendanceVo> vos =smartAttendanceMapper.queryExpor(gradeId,classId,status,studentNo,name,startTime,endTime);
        return vos;
    }

    @Override
    public PageUtils<AskForLeaveScreenVo> askForLeaveScreenPage(int currentPage, int pageCount, String cardNo, String ifVerification) {
        Page<AskForLeaveScreenVo> page = new Page<>(currentPage,pageCount);
        IPage<AskForLeaveScreenVo> datas = smartAttendanceMapper.askForLeaveScreenPage(page,cardNo,ifVerification);
        return new PageUtils(datas);
    }

    @Override
    public Integer getStudentHistoricalAttendance(Integer status, Date startTime, Date endTime) {
        LambdaQueryWrapper<SmartAttendance> queryWrapper = new LambdaQueryWrapper<>();
        if (6==status) {
            queryWrapper.between(SmartAttendance::getInitiateTime,startTime,endTime);
        }else {
            queryWrapper.between(SmartAttendance::getAttendTime,startTime,endTime);
        }
        queryWrapper.eq(SmartAttendance::getStatus,status);
        Integer count = this.count(queryWrapper);
        return count;
    }

    @Override
    public  List<ClasAttendanceVo> getClassHistoricalAttendance(Date startTime, Date endTime, Integer id) {
        return smartAttendanceMapper.getClassHistoricalAttendance(startTime,endTime,id);
    }

    @Override
    public Integer getClassAckHistoricalAttendance(int i, Date startTime, Date endTime, Integer id) {
        LambdaQueryWrapper<SmartAttendance> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(SmartAttendance::getInitiateTime,startTime,endTime);
        queryWrapper.eq(SmartAttendance::getStatus,i);
        queryWrapper.eq(SmartAttendance::getClassId,id);
        Integer count = this.count(queryWrapper);
        return count;
    }
}
