using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NCC.Common.Core.Manager;
using NCC.Common.Filter;
using NCC.DynamicApiController;
using NCC.Extend.Entitys.Dto.Changelog;
using NCC.Extend.Entitys.lq_changelog;
using NCC.Dependency;
using SqlSugar;
using Yitter.IdGenerator;
namespace NCC.Extend
{
///
/// 系统更新日志服务
///
[ApiDescriptionSettings(Tag = "Common", Name = "Changelog", Order = 170)]
[Route("api/Extend/[controller]")]
public class ChangelogService : IDynamicApiController, ITransient
{
private readonly ISqlSugarClient _db;
private readonly IUserManager _userManager;
public ChangelogService(ISqlSugarClient db, IUserManager userManager)
{
_db = db;
_userManager = userManager;
_db.CodeFirst.InitTables(typeof(LqChangelogEntity));
}
///
/// 获取更新日志列表(分页)
///
[HttpGet("GetList")]
public async Task GetList([FromQuery] ChangelogListQueryInput input)
{
var data = await _db.Queryable()
.WhereIF(input.isPublished.HasValue, x => x.IsPublished == input.isPublished.Value)
.WhereIF(input.type.HasValue, x => x.Type == input.type.Value)
.WhereIF(!string.IsNullOrWhiteSpace(input.keyword),
x => x.Title.Contains(input.keyword) || x.Version.Contains(input.keyword))
.OrderBy(x => x.PublishedTime, OrderByType.Desc)
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.Select(x => new ChangelogListOutput
{
id = x.Id,
version = x.Version,
title = x.Title,
content = x.Content,
type = x.Type,
isPublished = x.IsPublished,
publishedTime = x.PublishedTime,
createTime = x.CreateTime,
modifyTime = x.ModifyTime
})
.ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
///
/// 获取已发布的更新日志列表(前端用)
///
[HttpGet("GetPublishedList")]
public async Task GetPublishedList([FromQuery] int currentPage = 1, int pageSize = 20)
{
var data = await _db.Queryable()
.Where(x => x.IsPublished)
.OrderBy(x => x.PublishedTime, OrderByType.Desc)
.Select(x => new ChangelogListOutput
{
id = x.Id,
version = x.Version,
title = x.Title,
content = x.Content,
type = x.Type,
isPublished = x.IsPublished,
publishedTime = x.PublishedTime,
createTime = x.CreateTime,
modifyTime = x.ModifyTime
})
.ToPagedListAsync(currentPage, pageSize);
return PageResult.SqlSugarPageResult(data);
}
///
/// 获取最新一条已发布更新日志(右上角红点提示用)
///
[HttpGet("GetLatest")]
public async Task GetLatest()
{
var latest = await _db.Queryable()
.Where(x => x.IsPublished)
.OrderBy(x => x.PublishedTime, OrderByType.Desc)
.FirstAsync();
return latest;
}
///
/// 获取更新日志详情
///
[HttpGet("GetDetail/{id}")]
public async Task GetDetail(string id)
{
var entity = await _db.Queryable()
.Where(x => x.Id == id)
.FirstAsync();
if (entity == null)
throw new Exception("更新日志不存在");
return entity;
}
///
/// 添加更新日志
///
[HttpPost("Add")]
public async Task Add([FromBody] ChangelogAddInput input)
{
var entity = new LqChangelogEntity
{
Id = YitIdHelper.NextId().ToString(),
Version = input.Version,
Title = input.Title,
Content = input.Content,
Type = input.Type,
IsPublished = input.IsPublished,
PublishedTime = input.IsPublished ? DateTime.Now : null,
CreateUserId = _userManager?.UserId,
CreateTime = DateTime.Now
};
await _db.Insertable(entity).ExecuteCommandAsync();
return new { success = true, id = entity.Id };
}
///
/// 修改更新日志
///
[HttpPost("Update")]
public async Task Update([FromBody] ChangelogUpdateInput input)
{
var entity = await _db.Queryable()
.Where(x => x.Id == input.Id)
.FirstAsync();
if (entity == null)
throw new Exception("更新日志不存在");
entity.Version = input.Version ?? entity.Version;
entity.Title = input.Title ?? entity.Title;
entity.Content = input.Content ?? entity.Content;
entity.Type = input.Type;
entity.ModifyTime = DateTime.Now;
if (input.IsPublished && !entity.IsPublished)
{
entity.IsPublished = true;
entity.PublishedTime = DateTime.Now;
}
else if (!input.IsPublished)
{
entity.IsPublished = false;
entity.PublishedTime = null;
}
await _db.Updateable(entity).ExecuteCommandAsync();
return new { success = true };
}
///
/// 删除更新日志
///
[HttpPost("Delete/{id}")]
public async Task Delete(string id)
{
await _db.Deleteable(id).ExecuteCommandAsync();
return new { success = true };
}
///
/// 切换发布状态
///
[HttpPost("TogglePublish/{id}")]
public async Task TogglePublish(string id)
{
var entity = await _db.Queryable()
.Where(x => x.Id == id)
.FirstAsync();
if (entity == null)
throw new Exception("更新日志不存在");
entity.IsPublished = !entity.IsPublished;
entity.PublishedTime = entity.IsPublished ? DateTime.Now : null;
entity.ModifyTime = DateTime.Now;
await _db.Updateable(entity)
.UpdateColumns(x => new { x.IsPublished, x.PublishedTime, x.ModifyTime })
.ExecuteCommandAsync();
return new { success = true, isPublished = entity.IsPublished };
}
}
}