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.

163 lines
5.6 KiB
C#

using CommonModel;
using Google.Api;
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 MaintenanceService: IMaintenanceService
{
public MaintenanceService(IServiceProvider services, ISqlSugarClient sqlSugarClient)
{
Services = services;
Db = sqlSugarClient;
Db.CodeFirst.SplitTables().InitTables(typeof(MaintenanceDO));
}
private readonly IServiceProvider Services;
private readonly ISqlSugarClient Db;
public async Task<MaintenanceDO> InserDO(MaintenanceDO maintenance)
{
await Db.Insertable<MaintenanceDO>(maintenance).SplitTable().ExecuteCommandAsync();
return maintenance;
}
public string InsertCheck(MaintenanceDO maintenance)
{
var _MaxId = Db.Queryable<MaintenanceDO>().SplitTable(DateTime.Today, DateTime.Today.AddDays(1)).Max(x => x.Id);
if (string.IsNullOrEmpty(_MaxId))
{
maintenance.Id = $"MTC{DateTime.Now.ToString("yyyyMMdd")}000001";
}
else
{
maintenance.Id = $"MTC{DateTime.Now.ToString("yyyyMMdd")}{(Convert.ToInt32(_MaxId.Substring(11, 6)) + 1).ToString().PadLeft(6, '0')}";
}
maintenance.State = 0;
maintenance.CreateDate = DateTime.Now;
return "";
}
public async Task<MaintenanceDO> UpdateDO(MaintenanceDO maintenance)
{
var paramService = Services.GetRequiredService<IParamService>();
if (maintenance.BomCodes != null && maintenance.BomCodes.Length > 0)
{
foreach(var code in maintenance.BomCodes)
{
await paramService.Use(code);
}
}
if (!string.IsNullOrEmpty(maintenance.ErrorCodeCode))
{
await paramService.Use(maintenance.ErrorCodeCode);
}
if (maintenance.State > 1) {
maintenance.BomCodes = null;
maintenance.Boms = null;
maintenance.ErrorRemarkPhoto = null;
maintenance.ProcessResultPhoto = null;
}
await Db.Updateable<MaintenanceDO>(maintenance).IgnoreColumns(ignoreAllNullColumns: true).Where(x => x.Id == maintenance.Id).SplitTable().ExecuteCommandAsync();
return maintenance;
}
public string UpdateCheck(MaintenanceDO maintenance)
{
if (string.IsNullOrEmpty(maintenance.Id))
{
return "维保单编号不存在";
}
SplitDateId SplitDateId = new SplitDateId(maintenance.Id!, 3);
if (!Db.Queryable<MaintenanceDO>().SplitTable(SplitDateId.StartDate, SplitDateId.EndDate).Any(x => x.Id == maintenance.Id))
{
return "维保单编号不存在";
}
if (maintenance.State == 1)
{
maintenance.MaintenanceDate = DateTime.Now;
}
return "";
}
public async Task DeleteDO(string[] ids)
{
try
{
Db.Ado.BeginTran();
foreach (var id in ids)
{
MaintenanceDO Maintenance = new MaintenanceDO()
{
Id = id,
State = -1
};
await Db.Updateable<MaintenanceDO>(Maintenance).IgnoreColumns(ignoreAllNullColumns: true).Where(x => x.Id == Maintenance.Id).SplitTable().ExecuteCommandAsync();
}
Db.Ado.CommitTran();
}
catch
{
Db.Ado.RollbackTran();
throw;
}
}
public async Task<MaintenanceDO> GetDO(string id)
{
SplitDateId SplitDateId = new SplitDateId(id, 3);
return await Db.Queryable<MaintenanceDO>().Where(x => x.Id == id).SplitTable(SplitDateId.StartDate, SplitDateId.EndDate)
.SingleAsync();
}
public async Task<PageData<MaintenanceDO>> ListDO(PageYearSearch<MaintenanceDO> pageSearch)
{
RefAsync<int> _Total = 0;
var _Result = await Db.Queryable<MaintenanceDO>()
.Where(x => x.State != -1)
.WhereIF(!string.IsNullOrEmpty(pageSearch.Search), x =>
x.Id!.Contains(pageSearch.Search!) ||
x.Title!.Contains(pageSearch.Search!) ||
SqlFunc.JsonLike(x.MaintenanceUser, pageSearch.Search!)
)
.WhereIF(pageSearch.Mode != null && pageSearch.Mode.State != null, x => x.State == pageSearch.Mode!.State)
.WhereIF(pageSearch.Mode != null && !string.IsNullOrEmpty(pageSearch.Mode.MaintenanceUid), x =>
x.MaintenanceUid == pageSearch.Mode!.MaintenanceUid
)
.OrderBy("(CASE State WHEN 2 THEN 0 ELSE State END) ASC")
.OrderByDescending(x => x.Id)
.SplitTable((DateTime)pageSearch.StartDate!, (DateTime)pageSearch.EndDate!)
.ToPageListAsync(pageSearch.Index, pageSearch.Size, _Total);
return new PageData<MaintenanceDO>()
{
Index = pageSearch.Index,
Size = pageSearch.Size,
Data = _Result,
Total = _Total.Value
};
}
}
}