2024-05-28 14:36:38 +00:00
using DeviceRepair.Models ;
2024-07-17 02:32:45 +00:00
using DeviceRepair.Models.Device ;
2024-05-28 14:36:38 +00:00
using DeviceRepair.Models.History ;
using DeviceRepair.Models.Logs ;
2024-07-17 02:32:45 +00:00
using DeviceRepair.Models.Plan ;
using DeviceRepair.Models.Plan.View ;
using DeviceRepair.Models.Preserve.JsonData ;
using DeviceRepair.Models.Record ;
2024-05-28 14:36:38 +00:00
using DeviceRepair.Utils ;
2024-07-17 02:32:45 +00:00
using Newtonsoft.Json ;
2024-07-22 07:50:10 +00:00
using NLog ;
2024-05-28 14:36:38 +00:00
using SqlSugar ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Runtime.Serialization.Formatters.Binary ;
namespace DeviceRepair.DataAccess
{
public class PreserveAccess : DbContext < MaintenanceRecordInfo >
{
2024-07-22 07:50:10 +00:00
private static readonly Logger log = LogManager . GetCurrentClassLogger ( ) ;
2024-05-28 14:36:38 +00:00
private static PreserveAccess manager ;
public static PreserveAccess Instance
{
get
{
if ( manager = = null )
manager = new PreserveAccess ( ) ;
return manager ;
}
}
/// <summary>
/// 获取保养计划及对应的点检表信息
/// </summary>
/// <param name="PrimaryKey"></param>
public APIResponseData GetPlanAndFormInfo ( int PrimaryKey )
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = $"获取数据失败!" } ;
if ( PrimaryKey = = 0 )
return new APIResponseData { Code = - 1 , Message = $"参数【主键编号】不能为空!" } ;
try
{
db . ChangeDatabase ( "main" ) ;
2024-07-01 16:52:48 +00:00
2024-07-17 02:32:45 +00:00
if ( db . Queryable < MaintenanceRecordInfo , DeviceInformationInfo > ( ( x1 , x2 ) = > new object [ ] { JoinType . Inner , x1 . PlanPrimaryID = = PrimaryKey & & x1 . EquipmentPrimaryID = = x2 . AutoID & & x1 . FormPrimaryID = = x2 . MaintenanceFormVersion } ) . Any ( ) )
2024-07-01 16:52:48 +00:00
{
2024-07-22 07:50:10 +00:00
var datas = db . Queryable < DriveMaintencePlanInfo , DeviceInformationInfo , MaintenanceRecordInfo , MaintenanceFormVersionInfo > ( ( maintencePlan , dev , record , form ) = >
2024-07-01 16:52:48 +00:00
new object [ ] {
2024-07-17 02:32:45 +00:00
JoinType . Inner , maintencePlan . EquipmentID = = dev . AutoID ,
JoinType . Left , maintencePlan . AutoID = = record . PlanPrimaryID & & record . FormPrimaryID = = dev . MaintenanceFormVersion & & record . EquipmentPrimaryID = = dev . AutoID ,
2024-07-01 16:52:48 +00:00
JoinType . Left , record . FormPrimaryID = = form . AutoID
2024-07-17 02:32:45 +00:00
} ) . Where ( ( maintencePlan , dev , record , form ) = > maintencePlan . AutoID = = PrimaryKey ) . Select ( ( maintencePlan , dev , record , form ) = > new { maintencePlan , form } ) . First ( ) ;
2024-07-01 16:52:48 +00:00
if ( datas = = null )
return new APIResponseData { Code = - 1 , Message = $"获取数据失败,数据库不存在【主键编号】为【{PrimaryKey}】的数据!" } ;
else
return new APIResponseData { Code = 1 , Data = datas } ;
}
else
{
2024-07-22 07:50:10 +00:00
var datas = db . Queryable < DriveMaintencePlanInfo , MaintenanceRecordInfo , MaintenanceFormVersionInfo > ( ( maintencePlan , record , form ) = >
2024-07-01 16:52:48 +00:00
new object [ ] {
2024-07-22 07:50:10 +00:00
JoinType . Left , maintencePlan . EquipmentID = = record . EquipmentPrimaryID & & maintencePlan . AutoID = = record . PlanPrimaryID ,
JoinType . Left , record . FormPrimaryID = = form . AutoID
2024-07-01 16:52:48 +00:00
} ) . Where ( ( maintencePlan , device , form ) = > maintencePlan . AutoID = = PrimaryKey ) . Select ( ( maintencePlan , device , form ) = > new { maintencePlan , form } ) . First ( ) ;
2024-05-28 14:36:38 +00:00
2024-07-01 16:52:48 +00:00
if ( datas = = null )
return new APIResponseData { Code = - 1 , Message = $"获取数据失败,数据库不存在【主键编号】为【{PrimaryKey}】的数据!" } ;
else
return new APIResponseData { Code = 1 , Data = datas } ;
}
2024-05-28 14:36:38 +00:00
}
catch ( SqlSugarException e )
{
2024-07-22 07:50:10 +00:00
log . Error ( e ) ;
2024-05-28 14:36:38 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = e . Message ;
}
catch ( Exception ex )
{
2024-07-22 07:50:10 +00:00
log . Error ( ex ) ;
2024-05-28 14:36:38 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = ex . Message ;
}
return apiResponseData ;
}
/// <summary>
/// 获取设备保养信息记录
/// </summary>
/// <param name="PrimaryKey"></param>
/// <returns></returns>
public APIResponseData GetPreserveRecordItemByPrimaryKey ( int PrimaryKey )
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = "没有查询到数据!" } ;
try
{
db . ChangeDatabase ( "main" ) ;
MaintenanceRecordInfo entity = base . GetSingle ( PrimaryKey ) ;
if ( entity = = null )
return apiResponseData ;
return new APIResponseData { Code = 1 , Data = entity } ;
}
catch ( SqlSugarException e )
{
2024-07-22 07:50:10 +00:00
log . Error ( e ) ;
2024-05-28 14:36:38 +00:00
return new APIResponseData { Code = - 1 , Message = e . Message } ;
}
catch ( Exception e )
{
2024-07-22 07:50:10 +00:00
log . Error ( e ) ;
2024-05-28 14:36:38 +00:00
return new APIResponseData { Code = - 1 , Message = e . Message } ;
}
}
/// <summary>
/// 获取点检表表单信息
/// </summary>
/// <param name="PrimaryKey"></param>
/// <returns></returns>
public MaintenanceFormVersionInfo GetMaintenanceFormVersionSingle ( int PrimaryKey )
{
MaintenanceFormVersionInfo entity = null ;
try
{
db . ChangeDatabase ( "main" ) ;
entity = db . Queryable < MaintenanceFormVersionInfo > ( ) . First ( x = > x . AutoID = = PrimaryKey ) ;
}
catch ( SqlSugarException e )
{
throw e ;
}
catch ( Exception e )
{
throw e ;
}
return entity ;
}
/// <summary>
/// 获取上传的附件
/// </summary>
/// <param name="PrimaryKey"></param>
/// <returns></returns>
public AttachmentInfo GetAttachmentInfo ( int PrimaryKey )
{
try
{
db . ChangeDatabase ( "main" ) ;
return db . Queryable < AttachmentInfo > ( ) . First ( x = > x . AutoID = = PrimaryKey & & x . Status ) ;
}
catch ( SqlSugarException e )
{
throw e ;
}
catch ( Exception e )
{
throw e ;
}
}
/// <summary>
/// 通过设备主键编号及点检表主键编号获取单条保养信息记录
/// </summary>
/// <param name="EquipmentPrimaryID"></param>
/// <param name="FormPrimaryID"></param>
/// <returns></returns>
2024-07-17 02:32:45 +00:00
public APIResponseData GetSingleByEquipmentPrimaryIDAndFormPrimaryID ( int PlanAutoID , int EquipmentPrimaryID , int FormPrimaryID )
2024-05-28 14:36:38 +00:00
{
if ( EquipmentPrimaryID < = 0 )
return new APIResponseData { Code = - 1 , Message = $"参数【设备表主键编号】不能为空!" } ;
if ( FormPrimaryID < = 0 )
return new APIResponseData { Code = - 1 , Message = $"参数【保养点检表主键编号】不能为空!" } ;
try
{
db . ChangeDatabase ( "main" ) ;
2024-07-17 02:32:45 +00:00
DriveMaintencePlanInfo plan = db . Queryable < DriveMaintencePlanInfo > ( ) . First ( x = > x . AutoID = = PlanAutoID ) ;
if ( plan = = null )
return new APIResponseData { Code = - 1 , Message = $"当前计划不存在,请刷新后再试!" } ;
List < MaintenanceRecordInfo > entity = CurrentDb . AsQueryable ( )
. OrderBy ( x = > x . AutoID , OrderByType . Asc )
. Where ( x = > x . EquipmentPrimaryID = = EquipmentPrimaryID & & x . FormPrimaryID = = FormPrimaryID
2024-07-22 07:50:10 +00:00
& & x . MYear = = plan . MaintenanceYear & & x . PlanPrimaryID = = plan . AutoID ) . ToList ( ) ;
2024-05-28 14:36:38 +00:00
2024-07-22 07:50:10 +00:00
if ( entity = = null | | entity . Count = = 0 )
2024-05-28 14:36:38 +00:00
return new APIResponseData { Code = 1 , Data = null } ;
2024-07-17 02:32:45 +00:00
MaintenanceRecordInfo Item = entity [ 0 ] ;
Models . Preserve . JsonData . ContentData content = JsonConvert . DeserializeObject < Models . Preserve . JsonData . ContentData > ( Item . ContentData ) ;
for ( int i = 1 ; i < entity . Count ; i + + )
{
MaintenanceRecordInfo m = entity [ i ] ;
Models . Preserve . JsonData . ContentData data = JsonConvert . DeserializeObject < Models . Preserve . JsonData . ContentData > ( m . ContentData ) ;
foreach ( SheetDataItem item in data . InstallationSite )
{
SheetDataItem j = content . InstallationSite . FirstOrDefault ( x = > x . ColumnIndex = = item . ColumnIndex & & x . RowIndex = = item . RowIndex ) ;
if ( j ! = null )
j . Value = item . Value ;
else
content . InstallationSite . Add ( item ) ;
}
foreach ( SheetDataItem item in data . EquipmentName )
{
SheetDataItem j = content . EquipmentName . FirstOrDefault ( x = > x . ColumnIndex = = item . ColumnIndex & & x . RowIndex = = item . RowIndex ) ;
if ( j ! = null )
j . Value = item . Value ;
else
content . EquipmentName . Add ( item ) ;
}
foreach ( SheetDataItem item in data . DeviceSpecification )
{
SheetDataItem j = content . DeviceSpecification . FirstOrDefault ( x = > x . ColumnIndex = = item . ColumnIndex & & x . RowIndex = = item . RowIndex ) ;
if ( j ! = null )
j . Value = item . Value ;
else
content . DeviceSpecification . Add ( item ) ;
}
foreach ( SheetDataItem item in data . EquipmentID )
{
SheetDataItem j = content . EquipmentID . FirstOrDefault ( x = > x . ColumnIndex = = item . ColumnIndex & & x . RowIndex = = item . RowIndex ) ;
if ( j ! = null )
j . Value = item . Value ;
else
content . EquipmentID . Add ( item ) ;
}
foreach ( SheetDataItem item in data . Year )
{
SheetDataItem j = content . Year . FirstOrDefault ( x = > x . ColumnIndex = = item . ColumnIndex & & x . RowIndex = = item . RowIndex ) ;
if ( j ! = null )
j . Value = item . Value ;
else
content . Year . Add ( item ) ;
}
foreach ( SheetDataItem item in data . Content )
{
SheetDataItem j = content . Content . FirstOrDefault ( x = > x . ColumnIndex = = item . ColumnIndex & & x . RowIndex = = item . RowIndex ) ;
if ( j ! = null )
j . Value = item . Value ;
else
content . Content . Add ( item ) ;
}
foreach ( SheetDataItem item in data . ExceptionDescription )
{
SheetDataItem j = content . ExceptionDescription . FirstOrDefault ( x = > x . ColumnIndex = = item . ColumnIndex & & x . RowIndex = = item . RowIndex ) ;
if ( j ! = null )
j . Value + = Environment . NewLine + item . Value ;
else
content . ExceptionDescription . Add ( item ) ;
}
foreach ( SheetDataItem item in data . Operation )
{
SheetDataItem j = content . Operation . FirstOrDefault ( x = > x . ColumnIndex = = item . ColumnIndex & & x . RowIndex = = item . RowIndex ) ;
if ( j ! = null )
j . Value = item . Value ;
else
content . Operation . Add ( item ) ;
}
foreach ( SheetDataItem item in data . OperationDate )
{
SheetDataItem j = content . OperationDate . FirstOrDefault ( x = > x . ColumnIndex = = item . ColumnIndex & & x . RowIndex = = item . RowIndex ) ;
if ( j ! = null )
j . Value = item . Value ;
else
content . OperationDate . Add ( item ) ;
}
foreach ( SheetDataItem item in data . YearAndMonth )
{
SheetDataItem j = content . YearAndMonth . FirstOrDefault ( x = > x . ColumnIndex = = item . ColumnIndex & & x . RowIndex = = item . RowIndex ) ;
if ( j ! = null )
j . Value = item . Value ;
else
content . YearAndMonth . Add ( item ) ;
}
}
Item . ContentData = JsonConvert . SerializeObject ( content ) ;
return new APIResponseData { Code = 1 , Data = Item } ;
2024-05-28 14:36:38 +00:00
}
catch ( SqlSugarException e )
{
2024-07-22 07:50:10 +00:00
log . Error ( e ) ;
2024-05-28 14:36:38 +00:00
return new APIResponseData { Code = - 1 , Message = e . Message } ;
}
catch ( Exception ex )
{
2024-07-22 07:50:10 +00:00
log . Error ( ex ) ;
2024-05-28 14:36:38 +00:00
return new APIResponseData { Code = - 1 , Message = ex . Message } ;
}
}
/// <summary>
/// 获取所有的保养记录数据
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
2024-07-22 07:50:10 +00:00
public APIResponseData GetPreserveDatas ( string filter , string LoginCode )
2024-05-28 14:36:38 +00:00
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = "获取数据失败" } ;
List < View_MaintenanceRecordList > lst = null ;
try
{
2024-07-22 07:50:10 +00:00
List < DeviceInformationInfo > Devs = DeviceAccess . Instance . GetDevsByLoginAuths ( LoginCode ) ;
if ( Devs . Count = = 0 )
return new APIResponseData { Code = - 1 , Message = $"没有查询到数据!" } ;
int [ ] ids = Devs . Select ( x = > x . AutoID ) . ToArray ( ) ;
2024-05-28 14:36:38 +00:00
db . ChangeDatabase ( "main" ) ;
2024-06-10 17:33:11 +00:00
2024-07-22 07:50:10 +00:00
lst = db . Queryable < MaintenanceRecordInfo , UserInfoModel , DeviceInformationInfo , MaintenanceFormVersionInfo , DriveMaintencePlanInfo > ( ( t1 , t2 , t3 , t4 , t5 ) = > new object [ ] {
2024-05-28 14:36:38 +00:00
JoinType . Inner , t1 . CreateBy = = t2 . AutoID ,
JoinType . Inner , t1 . EquipmentPrimaryID = = t3 . AutoID ,
2024-07-22 07:50:10 +00:00
JoinType . Inner , t1 . FormPrimaryID = = t4 . AutoID ,
JoinType . Inner , t1 . PlanPrimaryID = = t5 . AutoID
} ) . Where ( ( t1 , t2 , t3 , t4 , t5 ) = > SqlFunc . ContainsArray ( ids , t3 . AutoID ) ) . Select ( ( t1 , t2 , t3 , t4 , t5 ) = > new View_MaintenanceRecordList
2024-05-28 14:36:38 +00:00
{
AutoID = t1 . AutoID ,
EquipmentID = t3 . AutoID ,
EquipmentNo = t3 . EquipmentID ,
EquipmenName = t3 . EquipmentName ,
FormVersionID = t4 . AutoID ,
FormVersionName = t4 . FormName ,
FormVersionCode = t4 . VersionCode ,
FormVersionRev = t4 . VersionRev ,
PlanPrimaryID = t1 . PlanPrimaryID ,
PlanType = t1 . PlanType ,
2024-07-22 07:50:10 +00:00
MaintenanceYear = t5 . MaintenanceYear ,
MaintenanceMonth = t5 . MaintenanceMonth ,
2024-05-28 14:36:38 +00:00
CreateDate = t1 . CreateDate ,
CreateUser = t1 . CreateBy ,
CreateUserName = t2 . RealName
} ) ? . ToList ( ) ;
if ( ! string . IsNullOrWhiteSpace ( filter ) )
{
filter = filter . ToLower ( ) ;
lst = lst . Where ( x = >
x . EquipmentID . ToString ( ) . Contains ( filter )
| | x . EquipmentNo . ToLower ( ) . Contains ( filter )
| | x . EquipmenName . ToLower ( ) . Contains ( filter )
| | x . FormVersionName . ToLower ( ) . Contains ( filter )
| | x . FormVersionCode . ToLower ( ) . Contains ( filter )
| | x . FormVersionRev . ToLower ( ) . Contains ( filter )
| | x . CreateUserName . ToLower ( ) . Contains ( filter )
) . ToList ( ) ;
}
apiResponseData . Code = 1 ;
apiResponseData . Data = lst ;
apiResponseData . Message = "" ;
}
catch ( SqlSugarException e )
{
2024-07-22 07:50:10 +00:00
log . Error ( e ) ;
2024-05-28 14:36:38 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = e . Message ;
}
catch ( Exception ex )
{
2024-07-22 07:50:10 +00:00
log . Error ( ex ) ;
2024-05-28 14:36:38 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = ex . Message ;
}
return apiResponseData ;
}
/// <summary>
/// 获取保养数据
/// </summary>
/// <param name="PrimaryKey"></param>
/// <returns></returns>
public APIResponseData GetPreserveDetailByID ( int PrimaryKey )
{
if ( PrimaryKey = = 0 )
return new APIResponseData { Code = - 1 , Message = "未能获取到查询条件:保养记录主键ID,或ID等于0。" } ;
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = $"获取数据失败!" } ;
try
{
db . ChangeDatabase ( "main" ) ;
MaintenanceRecordSubmit model = new MaintenanceRecordSubmit ( ) ;
MaintenanceRecordInfo query = db . Queryable < MaintenanceRecordInfo > ( ) . Single ( x = > x . AutoID = = PrimaryKey ) ;
List < AttachmentInfo > att = db . Queryable < AttachmentInfo > ( ) . Where ( x = > x . TableName = = "MaintenanceRecord"
& & x . PrimaryKey = = "AutoID" & & x . PrimaryValue = = ( query . AutoID + "" ) & & x . Status ) . ToList ( ) ;
model . Record = query ;
if ( att . Count > 0 )
{
List < AttachmentInfo > fs = att . Where ( x = > x . Module = = "Attachment" ) . ToList ( ) ;
foreach ( AttachmentInfo item in fs )
{
model . Files . Add ( new AttachmentSubmitModel
{
Datas = File . ReadAllBytes ( item . FilePath ) ,
Extension = item . Extension ,
FileName = item . FileName ,
Module = item . Module
} ) ;
}
List < AttachmentInfo > im = att . Where ( x = > x . Module = = "Image" ) . ToList ( ) ;
foreach ( AttachmentInfo item in im )
{
model . Imgs . Add ( new AttachmentSubmitModel
{
Datas = File . ReadAllBytes ( item . FilePath ) ,
Extension = item . Extension ,
FileName = item . FileName ,
Module = item . Module
} ) ;
}
}
byte [ ] byteArray ;
BinaryFormatter formatter = new BinaryFormatter ( ) ;
using ( MemoryStream stream = new MemoryStream ( ) )
{
formatter . Serialize ( stream , model ) ;
byteArray = stream . ToArray ( ) ;
}
//数据压缩
byteArray = byteArray . Compress ( ) ;
return new APIResponseData { Code = 1 , Data = byteArray } ;
}
catch ( SqlSugarException e )
{
2024-07-22 07:50:10 +00:00
log . Error ( e ) ;
2024-05-28 14:36:38 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = e . Message ;
}
catch ( Exception ex )
{
2024-07-22 07:50:10 +00:00
log . Error ( ex ) ;
2024-05-28 14:36:38 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = ex . Message ;
}
return apiResponseData ;
}
2024-07-17 02:32:45 +00:00
/// <summary>
/// 获取保养计划的全部保养记录
/// </summary>
/// <param name="PlanPrimaryKey"></param>
/// <returns></returns>
public APIResponseData GetPreserveDetailAll ( int PlanPrimaryKey )
{
if ( PlanPrimaryKey = = 0 )
return new APIResponseData { Code = - 1 , Message = "未能获取到查询条件:保养计划主键ID等于0的数据" } ;
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = "获取数据失败!" } ;
try
{
db . ChangeDatabase ( "main" ) ;
MaintenanceRecordSubmit model = new MaintenanceRecordSubmit ( ) ;
2024-07-22 07:50:10 +00:00
//List<MaintenanceRecordInfo> Datas = db.Queryable<MaintenanceRecordInfo, DeviceInformationInfo>((x, d) => new object[] {
//JoinType.Inner,x.EquipmentPrimaryID == d.AutoID && x.FormPrimaryID == d.MaintenanceFormVersion
//}).Where((x, d) => x.PlanPrimaryID == PlanPrimaryKey).Select((x, d) => x).ToList();
2024-07-17 02:32:45 +00:00
List < MaintenanceRecordInfo > Datas = db . Queryable < MaintenanceRecordInfo > ( )
2024-07-22 07:50:10 +00:00
. Where ( x = > x . PlanPrimaryID = = PlanPrimaryKey ) . ToList ( ) ;
2024-07-17 02:32:45 +00:00
if ( Datas ! = null & & Datas . Count > 0 )
{
2024-07-22 07:50:10 +00:00
MaintenanceRecordInfo last = Datas . OrderByDescending ( x = > x . CreateDate ) . Take ( 1 ) . FirstOrDefault ( ) ;
Datas = Datas . Where ( x = > x . FormPrimaryID = = last . FormPrimaryID ) . ToList ( ) ;
2024-07-17 02:32:45 +00:00
//if (Datas[0].PlanType == EnumMaintenanceType.Daily.ToString())
//{
// /* 其他保养 */
// Datas = db.Queryable<MaintenanceRecordInfo>()
// .Where(x => x.EquipmentPrimaryID == Datas[0].EquipmentPrimaryID
// && x.FormPrimaryID == Datas[0].FormPrimaryID
// && x.FormVersionCode == Datas[0].FormVersionCode
// && x.FormVersionRev == Datas[0].FormVersionRev
// && x.MYear == Datas[0].MYear).ToList();
//}
ContentData Content = null ;
foreach ( MaintenanceRecordInfo item in Datas )
{
ContentData News = JsonConvert . DeserializeObject < ContentData > ( item . ContentData ) ;
if ( Content = = null )
{
Content = News ;
continue ;
}
Content . MergeData ( News ) ;
}
model . Record = new MaintenanceRecordInfo
{
AutoID = Datas [ 0 ] . AutoID ,
ContentData = JsonConvert . SerializeObject ( Content ) ,
CreateBy = Datas [ 0 ] . CreateBy ,
CreateDate = Datas [ 0 ] . CreateDate ,
Description = Datas [ 0 ] . Description ,
EquipmentID = Datas [ 0 ] . EquipmentID ,
EquipmentName = Datas [ 0 ] . EquipmentName ,
EquipmentPrimaryID = Datas [ 0 ] . EquipmentPrimaryID ,
InstallationSite = Datas [ 0 ] . InstallationSite ,
FormName = Datas [ 0 ] . FormName ,
FormPrimaryID = Datas [ 0 ] . FormPrimaryID ,
FormVersionCode = Datas [ 0 ] . FormVersionCode ,
FormVersionRev = Datas [ 0 ] . FormVersionRev ,
Guid = Datas [ 0 ] . Guid ,
ModifyBy = Datas [ 0 ] . ModifyBy ,
ModifyDate = Datas [ 0 ] . ModifyDate ,
MYear = Datas [ 0 ] . MYear ,
PlanPrimaryID = Datas [ 0 ] . PlanPrimaryID ,
PlanType = Datas [ 0 ] . PlanType ,
Specification = Datas [ 0 ] . Specification
} ;
string [ ] recordIds = Datas . Select ( x = > x . AutoID . ToString ( ) ) . ToArray ( ) ;
List < AttachmentInfo > att = db . Queryable < AttachmentInfo > ( )
. Where ( x = > x . TableName = = "MaintenanceRecord"
& & x . PrimaryKey = = "AutoID" & & SqlFunc . ContainsArray ( recordIds , x . PrimaryValue ) & & x . Status ) . ToList ( ) ;
if ( att . Count > 0 )
{
List < AttachmentInfo > fs = att . Where ( x = > x . Module = = "Attachment" ) . ToList ( ) ;
foreach ( AttachmentInfo item in fs )
{
model . Files . Add ( new AttachmentSubmitModel
{
Datas = File . ReadAllBytes ( item . FilePath ) ,
Extension = item . Extension ,
FileName = item . FileName ,
Module = item . Module
} ) ;
}
List < AttachmentInfo > im = att . Where ( x = > x . Module = = "Image" ) . ToList ( ) ;
foreach ( AttachmentInfo item in im )
{
model . Imgs . Add ( new AttachmentSubmitModel
{
Datas = File . ReadAllBytes ( item . FilePath ) ,
Extension = item . Extension ,
FileName = item . FileName ,
Module = item . Module
} ) ;
}
}
byte [ ] byteArray ;
BinaryFormatter formatter = new BinaryFormatter ( ) ;
using ( MemoryStream stream = new MemoryStream ( ) )
{
formatter . Serialize ( stream , model ) ;
byteArray = stream . ToArray ( ) ;
}
//数据压缩
byteArray = byteArray . Compress ( ) ;
apiResponseData = new APIResponseData { Code = 1 , Data = byteArray } ;
}
}
catch ( SqlSugarException e )
{
db . RollbackTran ( ) ;
2024-07-22 07:50:10 +00:00
log . Error ( e ) ;
2024-07-17 02:32:45 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = e . Message ;
}
catch ( Exception ex )
{
db . RollbackTran ( ) ;
2024-07-22 07:50:10 +00:00
log . Error ( ex ) ;
2024-07-17 02:32:45 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = ex . Message ;
}
return apiResponseData ;
}
2024-05-28 14:36:38 +00:00
/// <summary>
/// 新增数据
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public APIResponseData Insertable ( MaintenanceRecordSubmit entity , HeaderModel Operation )
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = $"获取数据失败!" } ;
if ( entity = = null )
return new APIResponseData { Code = - 1 , Message = "数据获取失败,传入的数据为空!" } ;
try
{
db . ChangeDatabase ( "main" ) ;
db . BeginTran ( ) ;
string OperationType = "新增" ;
DateTime CurrentDate = DateTime . Now ;
int Identity = 0 ;
if ( entity . Record . AutoID = = 0 )
{
2024-07-17 02:32:45 +00:00
if ( entity . Record . PlanType = = "Daily" )
{
DailyPlanDetail DetailView = db . Ado . SqlQuery < DailyPlanDetail > ( "SELECT * FROM dbo.func_GetDailyPlanDetail(@AutoID)" , new { AutoID = entity . Record . PlanPrimaryID } )
. FirstOrDefault ( x = > x . MaintenanceDate = = entity . Daily_Date & & x . Banci = = entity . Banci ) ;
if ( DetailView ! = null & & DetailView . IsComplate = = 1 )
{
throw new Exception ( "当前数据已被其他用户录入 " ) ;
}
}
else if ( db . Queryable < MaintenanceRecordInfo > ( ) . Any ( x = > x . PlanPrimaryID = = entity . Record . PlanPrimaryID ) )
2024-06-10 17:33:11 +00:00
{
throw new Exception ( "当前数据已被其他用户录入 " ) ;
}
2024-07-17 02:32:45 +00:00
2024-05-28 14:36:38 +00:00
entity . Record . ModifyDate = CurrentDate ;
entity . Record . ModifyBy = Operation . Operator ;
entity . Record . CreateDate = CurrentDate ;
Identity = db . Insertable ( entity . Record ) . ExecuteReturnIdentity ( ) ;
}
else
{
OperationType = "修改" ;
MaintenanceRecordHisInfo his = new MaintenanceRecordHisInfo
{
HisID = entity . Record . AutoID ,
OperationBy = Operation . Operator ,
OperationDate = CurrentDate ,
EquipmentPrimaryID = entity . Record . EquipmentPrimaryID ,
EquipmentID = entity . Record . EquipmentID ,
EquipmentName = entity . Record . EquipmentName ,
Specification = entity . Record . Specification ,
PlanPrimaryID = entity . Record . PlanPrimaryID ,
PlanType = entity . Record . PlanType ,
FormPrimaryID = entity . Record . FormPrimaryID ,
FormVersionCode = entity . Record . FormVersionCode ,
FormVersionRev = entity . Record . FormVersionRev ,
FormName = entity . Record . FormName ,
MYear = entity . Record . MYear ,
ContentData = entity . Record . ContentData ,
CreateBy = entity . Record . CreateBy ,
CreateDate = entity . Record . CreateDate ,
ModifyBy = entity . Record . ModifyBy ,
ModifyDate = entity . Record . ModifyDate ,
Description = entity . Record . Description
} ;
int count = db . Insertable ( his ) . ExecuteCommand ( ) ;
if ( count > 0 )
{
entity . Record . ModifyDate = CurrentDate ;
entity . Record . ModifyBy = Operation . Operator ;
count = db . Updateable ( entity . Record )
. UpdateColumns ( x = > new { x . ContentData , x . ModifyDate , x . ModifyBy } )
. ExecuteCommand ( ) ;
if ( count > 0 )
{
Identity = entity . Record . AutoID ;
}
else
{
db . RollbackTran ( ) ;
return new APIResponseData { Code = - 1 , Message = "操作失败!" } ;
}
}
else
{
db . RollbackTran ( ) ;
return new APIResponseData { Code = - 1 , Message = "操作失败!" } ;
}
}
if ( Identity > 0 )
{
//附件存放路径
2024-07-27 01:44:19 +00:00
string AttachmentDirectory = Path . Combine ( DeviceRepair . Utils . Config . Configurations . Properties . AttachmentDirectory , DateTime . Today . ToString ( "yyyyMMdd" ) ) ;
2024-05-28 14:36:38 +00:00
if ( ! Directory . Exists ( AttachmentDirectory ) )
Directory . CreateDirectory ( AttachmentDirectory ) ;
List < AttachmentInfo > Attas = new List < AttachmentInfo > ( ) ;
#region 附 件 写 入
//附件写入
2024-06-10 17:33:11 +00:00
if ( entity . Files = = null | | entity . Files . Count = = 0 )
{
int cCount = db . Updateable < AttachmentInfo > ( ) . UpdateColumns ( x = > new { x . Status } )
. SetColumns ( x = > x . Status = = false )
. Where ( x = > x . Module = = "Attachment" & & x . TableName = = "MaintenanceRecord" & & x . PrimaryKey = = "AutoID" & & x . PrimaryValue = = Identity + "" ) . ExecuteCommand ( ) ;
}
2024-05-28 14:36:38 +00:00
foreach ( var item in entity . Files )
{
2024-07-01 16:52:48 +00:00
if ( item . AutoID ! = 0 & & item . Datas = = null )
2024-05-28 14:36:38 +00:00
{
2024-07-01 16:52:48 +00:00
continue ;
// int cCount = db.Updateable<AttachmentInfo>().UpdateColumns(x => new { x.Status })
//.SetColumns(x => x.Status == false)
//.Where(x => x.AutoID == item.AutoID).ExecuteCommand();
2024-05-28 14:36:38 +00:00
}
AttachmentInfo atta = new AttachmentInfo
{
CreateBy = Operation . Operator ,
FileName = item . FileName ,
Extension = item . Extension ,
Module = "Attachment" ,
TableName = "MaintenanceRecord" ,
PrimaryValue = Identity + "" ,
PrimaryKey = "AutoID" ,
ValueType = "int" ,
FilePath = Path . Combine ( AttachmentDirectory , $"{Guid.NewGuid()}{item.Extension}" ) ,
CreateOn = CurrentDate ,
Status = true
} ;
Attas . Add ( atta ) ;
File . WriteAllBytes ( atta . FilePath , item . Datas ) ;
}
#endregion
#region 图 片 写 入
int [ ] NoneImg = entity . Imgs . Select ( x = > x . AutoID ) . Distinct ( ) . ToArray ( ) ;
string PrimaryValue = entity . Record . AutoID . ToString ( ) ;
var exp = Expressionable . Create < AttachmentInfo > ( )
. AndIF ( NoneImg . Length > 0 , x = > ! SqlFunc . ContainsArray ( NoneImg , x . AutoID ) )
. And ( x = > x . Module = = "Image" )
. And ( x = > x . TableName = = "MaintenanceRecord" )
. And ( x = > x . PrimaryKey = = "AutoID" )
. And ( x = > x . PrimaryValue = = PrimaryValue ) . ToExpression ( ) ;
db . Updateable < AttachmentInfo > ( ) . UpdateColumns ( x = > new { x . Status } )
. SetColumns ( x = > x . Status = = false )
. Where ( exp ) . ExecuteCommand ( ) ;
//if (NoneImg.Length > 0)
//{
// int aCount = db.Updateable<AttachmentInfo>().UpdateColumns(x => new { x.Status })
// .SetColumns(x => x.Status == false)
// .Where(x => !SqlFunc.ContainsArray(NoneImg, x.AutoID) && x.Module == "Image" && x.TableName == "MaintenanceRecord" && x.PrimaryKey == "AutoID" && x.PrimaryValue == PrimaryValue).ExecuteCommand();
//}
//else
//{
// int aCount = db.Updateable<AttachmentInfo>().UpdateColumns(x => new { x.Status })
// .SetColumns(x => x.Status == false)
// .Where(x => x.Module == "Image" && x.TableName == "MaintenanceRecord" && x.PrimaryKey == "AutoID" && x.PrimaryValue == PrimaryValue).ExecuteCommand();
//}
List < int > ImgIds = new List < int > ( ) ;
foreach ( var item in entity . Imgs )
{
if ( item . Datas = = null & & item . AutoID ! = 0 )
{
ImgIds . Add ( item . AutoID ) ;
continue ;
}
string FileName = $"{Guid.NewGuid()}.Jpeg" ;
AttachmentInfo atta = new AttachmentInfo
{
CreateBy = Operation . Operator ,
FileName = FileName ,
Extension = ".Jpeg" ,
Module = "Image" ,
TableName = "MaintenanceRecord" ,
PrimaryValue = Identity + "" ,
PrimaryKey = "AutoID" ,
ValueType = "int" ,
FilePath = Path . Combine ( AttachmentDirectory , $"{Guid.NewGuid()}.Jpeg" ) ,
CreateOn = CurrentDate ,
Status = true
} ;
Attas . Add ( atta ) ;
File . WriteAllBytes ( atta . FilePath , item . Datas ) ;
}
#endregion
//附件插入
if ( Attas . Count > 0 )
{
bool result = db . Insertable ( Attas ) . ExecuteCommand ( ) > 0 ;
if ( ! result )
{
db . RollbackTran ( ) ;
return new APIResponseData { Code = - 1 , Message = "附件写入失败!" } ;
}
}
db . CommitTran ( ) ;
apiResponseData . Code = 1 ;
apiResponseData . Message = "" ;
MaintenanceRecordInfo model = db . Queryable < MaintenanceRecordInfo > ( ) . First ( x = > x . AutoID = = Identity ) ;
db . ChangeDatabase ( "log" ) ;
DeviceMaintenanceLogInfo log = new DeviceMaintenanceLogInfo
{
EquipmentAutoID = model . EquipmentPrimaryID ,
EquipmentID = model . EquipmentID ,
EquipmentName = model . EquipmentName ,
PlanID = model . PlanPrimaryID ,
PlanType = model . PlanType ,
OperationComputer = Operation . ClientName ,
OperationUserName = Operation . OperatorName ,
OperationDate = CurrentDate ,
OperationIP = Operation . IPAddress ,
OperationType = OperationType ,
OperationUser = Operation . Operator
} ;
db . Insertable ( log ) . ExecuteCommand ( ) ;
return apiResponseData ;
}
db . RollbackTran ( ) ;
return new APIResponseData { Code = - 1 } ;
}
catch ( SqlSugarException e )
{
db . RollbackTran ( ) ;
2024-07-22 07:50:10 +00:00
log . Error ( e ) ;
2024-05-28 14:36:38 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = e . Message ;
}
catch ( Exception ex )
{
db . RollbackTran ( ) ;
2024-07-22 07:50:10 +00:00
log . Error ( ex ) ;
2024-05-28 14:36:38 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = ex . Message ;
}
return apiResponseData ;
}
2024-07-01 16:52:48 +00:00
#region AM
2024-07-17 02:32:45 +00:00
/// <summary>
/// 获取设备保养记录信息
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
public APIResponseData GetRecordHisView ( MaintenanceFilterModel filter )
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = $"数据库通信失败!" } ;
try
{
db . ChangeDatabase ( "main" ) ;
var exp = Expressionable . Create < View_DeviceRoot , MaintenanceRecordHistoryModel > ( ) ;
exp . And ( ( t , x ) = > t . RootName = = "OEM" ) ;
if ( filter ! = null )
{
string MaintenanceType = filter . MaintenanceType . ToString ( ) ;
string Banci = filter . Banci . ToString ( ) ;
exp . AndIF ( filter . EquipmentId ! = 0 , ( t , x ) = > x . EquipmentPrimaryID = = filter . EquipmentId ) ;
exp . AndIF ( filter . StartDate ! = DateTime . MinValue , ( t , x ) = > x . MaintenanceDate > = filter . StartDate ) ;
exp . AndIF ( filter . EndDate ! = DateTime . MaxValue , ( t , x ) = > x . MaintenanceDate < = filter . EndDate ) ;
exp . AndIF ( filter . MaintenanceType ! = EnumMaintenanceType . None , ( t , x ) = > x . MaintenanceType = = MaintenanceType ) ;
exp . AndIF ( filter . Banci ! = Models . Enum . EnumMaintenanceBanciType . None , ( t , x ) = > x . Banci = = Banci ) ;
exp . AndIF ( ! filter . EquipmentName . IsNull ( ) , ( t , x ) = > x . EquipmentName . Contains ( filter . EquipmentName ) | | x . EquipmentID = = filter . EquipmentName ) ;
2024-07-22 07:50:10 +00:00
exp . AndIF ( filter . PlanID ! = 0 , ( t , x ) = > x . PlanID = = filter . PlanID ) ;
2024-07-17 02:32:45 +00:00
}
List < MaintenanceRecordHistoryModel > Datas =
db . Queryable < View_DeviceRoot , MaintenanceRecordHistoryModel > ( ( t , x ) = > new object [ ] {
JoinType . Inner , t . AutoID = = x . EquipmentPrimaryID
} ) . Where ( exp . ToExpression ( ) ) . Select ( ( t , x ) = > x ) . ToList ( ) ;
apiResponseData . Code = 1 ;
apiResponseData . Data = Datas ;
apiResponseData . Message = "" ;
}
catch ( SqlSugarException e )
{
2024-07-22 07:50:10 +00:00
log . Error ( e ) ;
2024-07-17 02:32:45 +00:00
return new APIResponseData { Code = - 1 , Message = e . Message } ;
}
catch ( Exception ex )
{
2024-07-22 07:50:10 +00:00
log . Error ( ex ) ;
2024-07-17 02:32:45 +00:00
throw new Exception ( ex . Message ) ;
}
return apiResponseData ;
}
2024-07-01 16:52:48 +00:00
#endregion
2024-05-28 14:36:38 +00:00
}
}