You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
6.5 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Extensions;
2 years ago
using DS.Module.UserModule;
using DS.WMS.Common.Enum;
using DS.WMS.Core.System.Dtos;
using DS.WMS.Core.System.Entity;
using DS.WMS.Core.System.Interface;
using DS.WMS.Core.WmsModule.Entity;
2 years ago
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.System.Method;
/// <summary>
///
/// </summary>
public class SysNoticeService : ISysNoticeService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public SysNoticeService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
}
/// <summary>
/// 列表查询
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<NoticeViewModel>> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable<SysNotice>()
.Where(whereList)
.Select<NoticeViewModel>()
// .Where(x=>x.AuditStatus != AuditStatus.NoAudit.ToEnumInt())
.ToQueryPage(request.PageCondition);
return data;
}
/// <summary>
/// 获取信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<NoticeViewModel> GetInfo(string id)
{
var data = db.Queryable<SysNotice>()
.Where(x => x.Id == id)
.Select<NoticeViewModel>()
.Mapper(it =>
{
if (it.MsgType == "CLIENT")
{
var ids = it.ClientIds.Split(',');
var arr = db.Queryable<OP_YARD_COMPANY>().Where(x => ids.Contains(x.GID.ToString()))
.Select(x => x.SHORTNAME).ToArray();
//只能写在Select后面
it.ClientName = string.Join(",", arr);
}
})
.First();
return DataResult<NoticeViewModel>.Success(data);
}
/// <summary>
/// 编辑信息
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public DataResult EditNotice(NoticeInput model)
{
if (model.Id.IsNullOrEmpty())
{
var isExist = db.Queryable<SysNotice>().Where(x => x.Title == model.Title).First();
if (isExist != null)
{
return DataResult.Failed("公告标题已存在!");
}
2 years ago
var data = model.Adapt<SysNotice>();
db.Insertable(data).ExecuteCommand();
return DataResult.Successed("添加成功!");
}
else
{
var info = db.Queryable<SysNotice>().Where(x => x.Id == model.Id).First();
2 years ago
var data = model.Adapt(info);
2 years ago
db.Updateable(data).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
return DataResult.Successed("更新成功!");
}
}
/// <summary>
/// 发布公告
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult SendNotice(string id)
{
var notice = db.Queryable<SysNotice>().Where(x => x.Id == id).First();
if (notice.SendStatus == SendStatus.Send.ToEnumInt())
{
return DataResult.Failed("已发布!");
}
if (notice.MsgType == "CLIENT" && notice.ClientIds.IsNullOrEmpty())
{
return DataResult.Failed("请选择指定客户!");
}
var clients = db.Queryable<OP_YARD_COMPANY>().Where(x => x.AuditStatus == AuditStatus.Approve.ToEnumInt())
.ToList();
try
{
//开启事务
db.Ado.BeginTran();
notice.SendStatus = SendStatus.Send.ToEnumInt();
notice.SendTime = DateTime.Now;
db.Updateable(notice).ExecuteCommand();
var clientNotices = new List<SysNoticeClient>();
if (notice.MsgType == "CLIENT")
{
var ids = notice.ClientIds.Split(',');
foreach (var item in ids)
{
var info = new SysNoticeClient
{
NoticeId = id,
ClientId = item,
IsRead = false
};
clientNotices.Add(info);
}
}
else
{
foreach (var item in clients)
{
var info = new SysNoticeClient
{
NoticeId = id,
ClientId = item.GID.ToString(),
IsRead = false
};
clientNotices.Add(info);
}
}
if (clientNotices.Count > 0)
{
db.Insertable(clientNotices).ExecuteCommand();
}
db.Ado.CommitTran();
return DataResult.Successed("公告发布成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("公告发布失败!" + ex);
}
}
/// <summary>
/// 撤销公告
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult CancelNotice(string id)
{
var notice = db.Queryable<SysNotice>().Where(x => x.Id == id).First();
if (notice.SendStatus == SendStatus.Cancel.ToEnumInt())
{
return DataResult.Failed("已撤销!");
}
try
{
//开启事务
db.Ado.BeginTran();
notice.SendStatus = SendStatus.Cancel.ToEnumInt();
notice.CancelTime = DateTime.Now;
db.Updateable(notice).ExecuteCommand();
var cancelNotices = db.Queryable<SysNoticeClient>().Where(x => x.NoticeId == id).ToList();
db.Deleteable(cancelNotices).ExecuteCommand();
db.Ado.CommitTran();
return DataResult.Successed("公告撤销成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("公告撤销失败!" + ex);
}
}
}