package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppRecordTaskMsgMapper;
import org.wcs.mapper.intf.AppRecordTaskMsgService;
import org.wcs.model.po.app.AppRecordTaskMag;

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

@Service
public class AppRecordTaskMsgDao extends ServiceImpl<AppRecordTaskMsgMapper, AppRecordTaskMag> implements AppRecordTaskMsgService {

    /**
     * 插入数据
     * @param appRecordTaskMag 待插入数据
     * @return 插入结果
     */
    @Override
    public boolean insert(AppRecordTaskMag appRecordTaskMag) {
        appRecordTaskMag.setCreateTime(LocalDateTime.now());
        try {
            return super.save(appRecordTaskMag);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 根据任务ID查询记录
     * @param taskId 任务ID
     * @return 记录列表
     */
    @Override
    public List<AppRecordTaskMag> queryWithTaskId(String taskId) {
        LambdaQueryWrapper<AppRecordTaskMag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppRecordTaskMag::getTaskId, taskId);
        queryWrapper.orderByAsc(AppRecordTaskMag::getCreateTime);
        try {
            return super.list(queryWrapper);
        } catch (Exception e){
            return null;
        }
    }

    /**
     * 删除指定天数前的数据
     * @param days 天数
     * @return 删除的行数
     */
    @Override
    public boolean deleteOldData(int days) {
        LambdaQueryWrapper<AppRecordTaskMag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.lt(AppRecordTaskMag::getCreateTime, LocalDateTime.now().minusDays(days));
        try {
            return super.remove(queryWrapper);
        } catch (Exception e){
            return false;
        }
    }
}
