using ClockSnowFlake;
using dy.net.dto;
using dy.net.model;
using dy.net.service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using static Dm.net.buffer.ByteArrayBuffer;
namespace dy.net.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ConfigController : ControllerBase
{
private readonly DyCookieService dyCookieService;
private readonly CommonService commonService;
private readonly QuartzJobService quartzJobService;
public ConfigController(DyCookieService dyCookieService, CommonService commonService,QuartzJobService quartzJobService)
{
this.dyCookieService = dyCookieService;
this.commonService = commonService;
this.quartzJobService = quartzJobService;
}
///
/// 分页查询
///
/// 分页结果(视频列表和总数)
[HttpPost("paged")]
public async Task GetPagedAsync(
PageRequestDto dto)
{
var (list, totalCount) = await dyCookieService.GetPagedAsync(dto.PageIndex, dto.PageSize);
return Ok(new
{
code = 0,
data = new
{
data = list,
total=totalCount,
pageIndex= dto.PageIndex,
pageSize= dto.PageSize
}
}
);
}
///
/// 新增用户Cookie
///
[HttpPost("add")]
public async Task AddAsync([FromBody] DyUserCookies dyUserCookies)
{
if(dyUserCookies.UpSecUserIdsJson!=null)
{
dyUserCookies.UpSecUserIds = JsonConvert.SerializeObject( dyUserCookies.UpSecUserIdsJson);
}
var result = await dyCookieService.Add(dyUserCookies);
if (result)
{
await ReStartJob();
return Ok(new { code = 0 });
}
return BadRequest(new { code=-1, message = "添加失败" });
}
///
/// 更新用户Cookie
///
[HttpPost("update")]
public async Task UpdateAsync([FromBody] DyUserCookies dyUserCookies)
{
if (dyUserCookies.UpSecUserIdsJson != null )
{
dyUserCookies.UpSecUserIds = JsonConvert.SerializeObject(dyUserCookies.UpSecUserIdsJson);
}
if (dyUserCookies.Id == "0")
{
dyUserCookies.Id=IdGener.GetLong().ToString();
var result = await dyCookieService.Add(dyUserCookies);
if (result)
{
await ReStartJob();
return Ok(new { code = 0 });
}
return BadRequest(new { code = -1, message = "添加失败" });
}
else {
var result = await dyCookieService.UpdateAsync(dyUserCookies);
if (result)
{
await ReStartJob();
return Ok(new { code = 0 });
}
return BadRequest(new { code = -1, message = "更新失败" });
}
}
///
/// 批量删除用户Cookie
///
[HttpGet("delete")]
public async Task DeleteAsync(string id)
{
var count = await dyCookieService.DeleteByIdsAsync(new List { id});
if (count > 0) {
await ReStartJob();
}
return Ok(new { code = 0, deletedCount = count });
}
[HttpGet("GetConfig")]
public IActionResult GetConfig()
{
var data = commonService.GetConfig();
return Ok(new { code = 0, data = data });
}
[HttpPost("UpdateConfig")]
public async Task UpdateConfig(AppConfig config)
{
var data = await commonService.UpdateConfig(config);
if (data) {
await ReStartJob();
}
return Ok(new { code = 0, data = data });
}
///
/// 立即执行同步任务
///
///
[HttpGet("ExecuteJobNow")]
[Authorize]
public async Task ExecuteJobNow()
{
var config = commonService.GetConfig();
await quartzJobService.StartJob(config.Cron);
return Ok(new { code = 0 , error = "" });
}
///
/// 重置指定Cookie的同步状态,强制重新同步
///
/// Cookie ID
/// 重置类型:collect=收藏, favorite=喜欢, all=全部
///
[Authorize]
[HttpPost("ResetSyncStatus")]
public async Task ResetSyncStatus(string id, string type = "all")
{
try
{
var cookie = await dyCookieService.GetByIdAsync(id);
if (cookie == null)
{
return BadRequest(new { code = -1, error = "Cookie不存在" });
}
if (type == "collect" || type == "all")
{
cookie.CollHasSyncd = 0;
}
if (type == "favorite" || type == "all")
{
cookie.FavHasSyncd = 0;
}
if (type == "all")
{
cookie.UperSyncd = 0;
}
var result = await dyCookieService.UpdateAsync(cookie);
if (result)
{
Serilog.Log.Information($"重置Cookie[{cookie.UserName}]的同步状态,类型: {type}");
return Ok(new { code = 0, message = "同步状态已重置" });
}
else
{
return BadRequest(new { code = -1, error = "重置失败" });
}
}
catch (Exception ex)
{
Serilog.Log.Error(ex, "重置同步状态失败");
return StatusCode(500, new { code = -1, error = "重置失败:" + ex.Message });
}
}
private async Task ReStartJob() {
var config= commonService.GetConfig();
//var cookies = await dyCookieService.GetAllCookies();
//重置同步状态
//foreach (var cookie in cookies)
//{
// cookie.CollHasSyncd = 0;
// cookie.FavHasSyncd = 0;
// cookie.UperSyncd = 0;
// await dyCookieService.UpdateAsync(cookie);
//}
if (config!=null)
quartzJobService.StartJob(config.Cron);
//避免前端等待
}
}
}