using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using detect.gui.Models;
using detect.gui.Models.Entities;

namespace detect.gui.Services.Detect;

public class DetectTaskLogService : ServiceBase<DetectTaskLogService, DetectTaskLogEntity>
{
    public ApiResponse<DetectTaskLogEntity?> ListById(long id)
    {
        var item = Find(x => x.Id == id);
        return new ApiResponse<DetectTaskLogEntity?>(0, "success", item);
    }
    
    public ApiResponse<List<DetectTaskLogEntity>?> ListAll()
    {
        var items = FindList(x => true, "Id", true)
            .ToList();
        return new ApiResponse<List<DetectTaskLogEntity>?>(0, "success", items);
    }
    
    public ApiResponse<PagedResult<DetectTaskLogEntity>> Search(long? taskId = -1, int pageNum = 1, int pageSize = 10)
    {
        Expression<Func<DetectTaskLogEntity,bool>> filter = x => taskId == -1 || x.TaskId == taskId;
        var total = Count(filter);
        var items = FindList(filter, "CreateTime", false)
            .Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
        var pagedResult = new PagedResult<DetectTaskLogEntity>(pageNum, pageSize, total, items);
        return new ApiResponse<PagedResult<DetectTaskLogEntity>>(0, "success", pagedResult);
    }
    
    public ApiResponse<DetectTaskLogEntity?> AddData(DetectTaskLogEntity entity)
    {
        var data = Add(entity);
        return new ApiResponse<DetectTaskLogEntity?>(0, "success", data);
    }
    
    public ApiResponse<DetectTaskLogEntity?> UpdateData(DetectTaskLogEntity entity)
    {
        return new ApiResponse<DetectTaskLogEntity?>(Update(entity) ? 0 : -1, "success", null);
    }
    
    public ApiResponse<DetectTaskLogEntity?> DeleteById(long id)
    {
        Delete(x => x.Id == id);
        return new ApiResponse<DetectTaskLogEntity?>(0, "success", null);
    }
    
    public ApiResponse<DetectTaskLogEntity?> DeleteData(DetectTaskLogEntity entity)
    {
        Delete(entity);
        return new ApiResponse<DetectTaskLogEntity?>(0, "success", null);
    }
}