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.

386 lines
12 KiB
C#

using CommonModel;
using Dapr.Actors;
using Dapr.Actors.Client;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhongLianInterface;
using ZhongLianModel;
namespace ZhongLianService
{
public class InstallService : IInstallService
{
public InstallService(IServiceProvider services, ISqlSugarClient sqlSugarClient)
{
Services = services;
Db = sqlSugarClient;
Db.CodeFirst.SplitTables().InitTables(typeof(InstallDO));
Db.CodeFirst.SplitTables().InitTables(typeof(InstallStepDO));
}
private readonly IServiceProvider Services;
private readonly ISqlSugarClient Db;
#region 创建
/// <summary>
/// 创建
/// </summary>
/// <param name="install">安装单</param>
/// <returns></returns>
public async Task<InstallDO> Insert(InstallDO install)
{
#if DEBUG
return await InserDO(install);
#else
var _ActorProxy = Services.GetRequiredService<IActorProxyFactory>();
var _Proxy = _ActorProxy.CreateActorProxy<IInstallActor>(new ActorId(install.Id), "InstallActor");
return await _Proxy.Insert(install);
#endif
}
/// <summary>
/// 创建数据
/// </summary>
/// <param name="install">安装单</param>
/// <returns></returns>
public async Task<InstallDO> InserDO(InstallDO install)
{
try
{
Db.Ado.BeginTran();
await Db.Insertable<InstallDO>(install).SplitTable().ExecuteCommandAsync();
if (install.Steps != null && install.Steps.Length > 0)
{
await Db.Insertable<InstallStepDO>(install.Steps).SplitTable().ExecuteCommandAsync();
}
Db.Ado.CommitTran();
}
catch
{
Db.Ado.RollbackTran();
throw;
}
return install;
}
/// <summary>
/// 创建校验
/// </summary>
/// <param name="install">安装单</param>
/// <returns></returns>
public string InsertCheck(InstallDO install)
{
var _MaxId = Db.Queryable<InstallDO>().SplitTable(DateTime.Today, DateTime.Today.AddDays(1)).Max(x => x.Id);
if (string.IsNullOrEmpty(_MaxId))
{
install.Id = $"IS{DateTime.Now.ToString("yyyyMMdd")}000001";
}
else
{
install.Id = $"IS{DateTime.Now.ToString("yyyyMMdd")}{(Convert.ToInt32(_MaxId.Substring(10, 6)) + 1).ToString().PadLeft(6, '0')}";
}
install.State = 0;
install.CreateDate = DateTime.Now;
if (install.Steps != null && install.Steps.Length > 0)
{
foreach (var step in install.Steps)
{
step.InstallId = install.Id;
step.CreateDate = install.CreateDate;
step.State = 0;
}
}
return "";
}
#endregion
#region 更新
/// <summary>
/// 更新
/// </summary>
/// <param name="install">安装单</param>
/// <returns></returns>
public async Task<InstallDO> Update(InstallDO install)
{
#if DEBUG
return await UpdateDO(install);
#else
var _ActorProxy = Services.GetRequiredService<IActorProxyFactory>();
var _Proxy = _ActorProxy.CreateActorProxy<IInstallActor>(new ActorId(install.Id), "InstallActor");
return await _Proxy.Update(install);
#endif
}
/// <summary>
/// 更新数据
/// </summary>
/// <param name="install">安装单</param>
/// <returns></returns>
public async Task<InstallDO> UpdateDO(InstallDO install)
{
try
{
Db.Ado.BeginTran();
await Db.Updateable<InstallDO>(install).IgnoreColumns(ignoreAllNullColumns: true).Where(x => x.Id == install.Id).SplitTable().ExecuteCommandAsync();
if (install.Steps != null && install.Steps.Length > 0)
{
SplitDateId SplitDateId = new SplitDateId(install.Id!, 2);
foreach (var step in install.Steps)
{
if (Db.Queryable<InstallStepDO>().SplitTable(SplitDateId.StartDate, SplitDateId.EndDate).Any(x => x.InstallId == install.Id && x.Key == step.Key))
{
await Db.Updateable<InstallStepDO>(step).IgnoreColumns(ignoreAllNullColumns: true).Where(x => x.InstallId == install.Id && x.Key == step.Key).SplitTable().ExecuteCommandAsync();
}
else
{
await Db.Insertable<InstallStepDO>(step).SplitTable().ExecuteCommandAsync();
}
}
}
Db.Ado.CommitTran();
}
catch
{
Db.Ado.RollbackTran();
throw;
}
return install;
}
/// <summary>
/// 更新校验
/// </summary>
/// <param name="install">安装单</param>
/// <returns></returns>
public string UpdateCheck(InstallDO install)
{
if (string.IsNullOrEmpty(install.Id))
{
return "安装单编号不存在";
}
SplitDateId SplitDateId = new SplitDateId(install.Id!, 2);
if (!Db.Queryable<InstallDO>().SplitTable(SplitDateId.StartDate, SplitDateId.EndDate).Any(x => x.Id == install.Id)) {
return "安装单编号不存在";
}
var _Install = Db.Queryable<InstallDO>().SplitTable(SplitDateId.StartDate, SplitDateId.EndDate).Where(x => x.Id == install.Id).Single();
if (install.Steps != null && install.Steps.Length > 0)
{
foreach (var step in install.Steps)
{
step.InstallId = install.Id;
if (step.State == null)
{
step.State = 0;
}
if (step.State == 1)
{
step.Comment = "";
}
if (install.State == 3 && step.State == 2)
{
install.State = 2;
}
step.CreateDate = _Install.CreateDate;
step.LastDate = DateTime.Now;
}
}
if (install.State == 1)
{
install.InstallDate = DateTime.Now;
}
if (install.State == 2 || install.State == 3)
{
install.PatrolDate = DateTime.Now;
}
install.LastDate = DateTime.Now;
return "";
}
#endregion
#region 删除
/// <summary>
/// 删除
/// </summary>
/// <param name="ids">编号数组</param>
/// <returns></returns>
public async Task Delete(string[] ids)
{
#if DEBUG
await DeleteDO(ids);
#else
var _ActorProxy = Services.GetRequiredService<IActorProxyFactory>();
var _Proxy = _ActorProxy.CreateActorProxy<IInstallActor>(new ActorId(Guid.NewGuid().ToString()), "InstallActor");
await _Proxy.Delete(ids);
#endif
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
public async Task DeleteDO(string[] ids)
{
try
{
Db.Ado.BeginTran();
foreach(var id in ids)
{
InstallDO InstallDO = new InstallDO() {
Id = id,
State = -1
};
await Db.Updateable<InstallDO>(InstallDO).IgnoreColumns(ignoreAllNullColumns: true).Where(x => x.Id == InstallDO.Id).SplitTable().ExecuteCommandAsync();
}
Db.Ado.CommitTran();
}
catch
{
Db.Ado.RollbackTran();
throw;
}
}
#endregion
#region 列表
/// <summary>
/// 查询列表
/// </summary>
/// <param name="pageSearch">查询条件</param>
/// <returns></returns>
public async Task<PageData<InstallDO>> List(PageYearSearch<InstallDO> pageSearch)
{
return await ListDO(pageSearch);
}
/// <summary>
/// 查询列表数据
/// </summary>
/// <param name="pageSearch">查询条件</param>
/// <returns></returns>
public async Task<PageData<InstallDO>> ListDO(PageYearSearch<InstallDO> pageSearch)
{
RefAsync<int> _Total = 0;
var _Result = await Db.Queryable<InstallDO>()
.Where(x => x.State != -1)
.WhereIF(!string.IsNullOrEmpty(pageSearch.Search), x =>
x.Id!.Contains(pageSearch.Search!) ||
x.Address!.Contains(pageSearch.Search!) ||
SqlFunc.JsonLike(x.InstallUser, pageSearch.Search!) ||
SqlFunc.JsonLike(x.PatrolUser, pageSearch.Search!)
)
.WhereIF(pageSearch.Mode != null && pageSearch.Mode.State != null, x => x.State == pageSearch.Mode!.State)
.WhereIF(pageSearch.Mode != null && !string.IsNullOrEmpty(pageSearch.Mode.InstallUid), x =>
x.InstallUid == pageSearch.Mode!.InstallUid
)
.WhereIF(pageSearch.Mode != null && !string.IsNullOrEmpty(pageSearch.Mode.PatrolUid), x =>
x.PatrolUid == pageSearch.Mode!.PatrolUid
)
.OrderByDescending(x => x.Id)
.SplitTable((DateTime)pageSearch.StartDate!, (DateTime)pageSearch.EndDate!)
.ToPageListAsync(pageSearch.Index, pageSearch.Size, _Total);
return new PageData<InstallDO>()
{
Index = pageSearch.Index,
Size = pageSearch.Size,
Data = _Result,
Total = _Total.Value
};
}
#endregion
#region 查询
/// <summary>
/// 查询详情
/// </summary>
/// <param name="id">编号</param>
/// <returns></returns>
public async Task<InstallDO> Get(string id)
{
return await GetDO(id);
}
/// <summary>
/// 查询详情数据
/// </summary>
/// <param name="id">编号</param>
/// <returns></returns>
public async Task<InstallDO> GetDO(string id)
{
SplitDateId SplitDateId = new SplitDateId(id, 2);
return await Db.Queryable<InstallDO>().Where(x => x.Id == id).SplitTable(SplitDateId.StartDate, SplitDateId.EndDate)
.Mapper((x) =>
{
x.Steps = Db.Queryable<InstallStepDO>()
.Where(y => y.InstallId == id)
.OrderBy(y => y.Index)
.SplitTable(SplitDateId.StartDate, SplitDateId.EndDate)
.ToArray();
})
.SingleAsync();
}
#endregion
#region 查询步骤
/// <summary>
/// 查询步骤
/// </summary>
/// <param name="installId">编号</param>
/// <returns></returns>
public async Task<InstallStepDO[]> GetSteps(string installId)
{
return await GetStepsDO(installId);
}
/// <summary>
/// 查询步骤数据
/// </summary>
/// <param name="installId">编号</param>
/// <returns></returns>
public async Task<InstallStepDO[]> GetStepsDO(string installId)
{
SplitDateId SplitDateId = new SplitDateId(installId, 2);
return await Db.Queryable<InstallStepDO>().Where(x => x.InstallId == installId).OrderBy(x => x.Index).SplitTable(SplitDateId.StartDate, SplitDateId.EndDate)
.ToArrayAsync();
}
#endregion
}
}