JXMovies/CMS/JXCMS.CMS.Movie/Admin/Controllers/CollectionController.cs
2020-02-16 22:11:24 +08:00

124 lines
4.7 KiB
C#

using System;
using System.Collections.ObjectModel;
using System.Linq;
using FreeSql;
using JXCMS.CMS.Attribute;
using JXCMS.CMS.Movie.Entity;
using JXCMS.CMS.Movie.Jobs;
using JXCMS.CMS.Movie.Spider;
using JXCMS.CMS.Movie.Utils;
using JXCMS.Core.TimingTask;
using Microsoft.AspNetCore.Mvc;
using Quartz;
namespace JXCMS.CMS.Movie.Admin.Controllers
{
[Area("Admin")]
[AdminAuthentication]
public class CollectionController : Controller
{
public IActionResult Index(int pageNumber = 1, int pageSize = 20)
{
ViewBag.title = "采集地址";
var website = WebSiteEntity.Select.Count(out long count).Page(pageNumber, pageSize).ToList().Select(x =>
{
x.NextRunTime = x.IsEnable ? QuartzTask.GetCronSchedule(x.Cron, 1, DateTimeOffset.Now) : "已禁用";
return x;
}).ToList();
ViewBag.website = website;
ViewBag.count = count;
ViewBag.pageNumber = pageNumber;
ViewBag.totlePage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
return View();
}
[HttpPost]
public IActionResult AddWebSite(WebSiteEntity webSiteEntity)
{
if (!CronExpression.IsValidExpression(webSiteEntity.Cron))
{
return Redirect(Url.Action("Index"));
}
webSiteEntity.Save();
if (webSiteEntity.IsEnable)
{
QuartzTask.Instance().AddTask(typeof(CollectionJob), webSiteEntity.Cron, webSiteEntity.Id.ToString(), "default", ("id", webSiteEntity.Id));
}
return Redirect(Url.Action("Index"));
}
public IActionResult DeleteWebSite(int id)
{
WebSiteEntity.Find(id).Delete();
QuartzTask.Instance().DeleteTask(id.ToString());
return Redirect(Url.Action("Index"));
}
public IActionResult DeleteWebSiteBatch(int[] ids)
{
WebSiteEntity.Where(x => ids.Contains(x.Id)).ToDelete().ExecuteAffrows();
QuartzTask.Instance()
.DeleteTasks(
new ReadOnlyCollection<JobKey>(ids.Select(x => new JobKey(x.ToString(), "default")).ToList()));
return Redirect(Url.Action("Index"));
}
public IActionResult EnableWebSiteBatch(int[] ids)
{
var webSiteEntities = WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate()
.Set(y => y.IsEnable, true).ExecuteUpdated();
foreach (var webSiteEntity in webSiteEntities)
{
QuartzTask.Instance().AddTask(typeof(CollectionJob), webSiteEntity.Cron, webSiteEntity.Id.ToString(),
"default", ("id", webSiteEntity.Id));
}
return Redirect(Url.Action("Index"));
}
public IActionResult DisableWebSiteBatch(int[] ids)
{
WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate()
.Set(y => y.IsEnable, false).ExecuteAffrows();
QuartzTask.Instance().DeleteTasks(new ReadOnlyCollection<JobKey>(ids.Select(x => new JobKey(x.ToString(), "default")).ToList()));
return Redirect(Url.Action("Index"));
}
public IActionResult WebSiteDialog(int id)
{
WebSiteEntity webSiteEntity = null;
if (id == 0)
{
webSiteEntity = new WebSiteEntity();
ViewBag.title = "添加新网站";
}
else
{
webSiteEntity = WebSiteEntity.Find(id);
ViewBag.title = "修改" + webSiteEntity.WebSiteName;
}
return View(webSiteEntity);
}
public IActionResult BindingClassifyDialog(int id)
{
var classifyEntities = Classify.Structured(ClassifyEntity.Select.ToList());
var webSiteClassifyEntities = WebSiteClassifyEntity.Where(x => x.WebSiteId == id).ToList();
var webSiteEntity = WebSiteEntity.Find(id);
classifyEntities.Insert(0, new ClassifyEntity(){Id = 0, Name = "未绑定"});
ViewBag.classifyEntities = classifyEntities;
ClassifySpider.GetAllClassifyFromUrl(id, webSiteEntity.ApiUrl, webSiteClassifyEntities);
ViewBag.title = "修改" + webSiteEntity.WebSiteName + "的类型绑定";
return View(webSiteClassifyEntities);
}
public IActionResult UpdateBindingClassify(WebSiteClassifyEntity[] webSiteClassifyEntities)
{
foreach (var webSiteClassifyEntity in webSiteClassifyEntities)
{
webSiteClassifyEntity.Save();
}
return Redirect(Url.Action("Index"));
}
}
}