package com.template.services.impl;

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.mapper.SmartFreezeRecordMapper;
import com.template.model.pojo.SmartFreezeRecord;
import com.template.model.pojo.SmartFreezeRecord;
import com.template.mapper.SmartFreezeRecordMapper;
import com.template.model.result.PageUtils;
import com.template.model.vo.ScreenshotRecordPageVo;
import com.template.model.vo.SmartFreezeRecordPageVo;
import com.template.services.SmartFreezeRecordService;
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-04
 */
@Service
public class SmartFreezeRecordServiceImpl extends ServiceImpl<SmartFreezeRecordMapper, SmartFreezeRecord> implements SmartFreezeRecordService {
    @Autowired
    private SmartFreezeRecordMapper smartFreezeRecordMapper;

    @Override
    public int insertSmartFreezeRecord(SmartFreezeRecord sa) {
        int result = smartFreezeRecordMapper.insert(sa);
        return result;
    }

    @Override
    public int updateSmartFreezeRecord(SmartFreezeRecord sa) {
        int result = smartFreezeRecordMapper.updateById(sa);
        return result;
    }

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

    @Override
    public int deleteSmartFreezeRecordById(int id) {
        int result = smartFreezeRecordMapper.deleteById(id);
        return result;
    }

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

    @Override
    public int getSmartByUserId(int userId) {
        QueryWrapper<SmartFreezeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt(userId > 0 ,"deadline",new Date());
        int result = smartFreezeRecordMapper.selectCount(queryWrapper);
        return result;
    }

    @Override
    public List<SmartFreezeRecord> getSmartByUserIds(List<Integer> userIds) {
        QueryWrapper<SmartFreezeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(userIds.size() > 0 ,"user_id",userIds);
        queryWrapper.gt(userIds.size() > 0 ,"deadline",new Date());
        List<SmartFreezeRecord> result = smartFreezeRecordMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public PageUtils<SmartFreezeRecordPageVo> queryFreezeRecordPageList(int currentPage, int pageCount) {
        Page<SmartFreezeRecordPageVo> page = new Page<>(currentPage, pageCount);
        IPage<SmartFreezeRecordPageVo> result = smartFreezeRecordMapper.querySmartFreezeRecordPageList(page);
        return new PageUtils<>(result);
    }
}
