2024-05-28 14:36:38 +00:00
using DeviceRepair.Models ;
2024-06-05 17:09:59 +00:00
using DeviceRepair.Models.DeviceRepair.ExportView ;
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.Linq ;
namespace DeviceRepair.DataAccess
{
/// <summary>
/// 设备维修
/// </summary>
public class MaintenanceAccess : DbContext < DeviceWarrantyRequestForm >
{
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 MaintenanceAccess manager ;
public static MaintenanceAccess Instance
{
get
{
if ( manager = = null )
manager = new MaintenanceAccess ( ) ;
return manager ;
}
}
/// <summary>
/// 查询数据
/// </summary>
/// <returns></returns>
2024-07-22 07:50:10 +00:00
public APIResponseData GetDatas ( DeviceWarrantyRequestFormFilter FilterInfo , string LoginCode )
2024-05-28 14:36:38 +00:00
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = $"获取数据失败!" } ;
try
{
2024-07-22 07:50:10 +00:00
List < DeviceInformationInfo > Devs = DeviceAccess . Instance . GetDevsByLoginAuths ( LoginCode ) ;
if ( Devs . Count = = 0 )
return apiResponseData ;
int [ ] DevIds = Devs . Select ( x = > x . AutoID ) . ToArray ( ) ;
2024-05-28 14:36:38 +00:00
db . ChangeDatabase ( "main" ) ;
var exp = Expressionable . Create < DeviceWarrantyRequestForm > ( ) ;
if ( FilterInfo ! = null )
{
exp . AndIF ( ! string . IsNullOrWhiteSpace ( FilterInfo . EquipmentID ) , x = > x . EquipmentID . Contains ( FilterInfo . EquipmentID ) ) ;
exp . AndIF ( FilterInfo . StartTime . HasValue , x = > x . CreatOn > = FilterInfo . StartTime . Value ) ;
exp . AndIF ( FilterInfo . EndTime . HasValue , x = > x . CreatOn < = FilterInfo . EndTime . Value ) ;
exp . AndIF ( FilterInfo . Status = = DeviceWarrantyRequestFormStatus . AwaitingRepair ,
x = > SqlFunc . Subqueryable < DeviceWarrantyRequestMaintaionInfo > ( ) . Where ( s = > s . FormID = = x . AutoID ) . NotAny ( ) ) ;
exp . AndIF ( FilterInfo . Status = = DeviceWarrantyRequestFormStatus . AwaitingApproval ,
2024-06-05 17:09:59 +00:00
x = > SqlFunc . Subqueryable < DeviceWarrantyRequestMaintaionInfo > ( ) . Where ( s = > s . FormID = = x . AutoID & & s . SubmitBy > 0 ) . Any ( ) & & ! SqlFunc . HasNumber ( x . RestorationConfirmationBy ) ) ;
2024-05-28 14:36:38 +00:00
exp . AndIF ( FilterInfo . Status = = DeviceWarrantyRequestFormStatus . BeComplate ,
2024-06-05 17:09:59 +00:00
x = > SqlFunc . Subqueryable < DeviceWarrantyRequestMaintaionInfo > ( ) . Where ( s = > s . FormID = = x . AutoID & & s . SubmitBy > 0 & & s . ValidateBy > 0 ) . Any ( ) & & SqlFunc . HasNumber ( x . RestorationConfirmationBy ) ) ;
2024-05-28 14:36:38 +00:00
bool isDown = FilterInfo . DownStatus = = DeviceRunningStatus . Stop ;
exp . AndIF ( FilterInfo . DownStatus ! = DeviceRunningStatus . All ,
x = > x . IsDown = = isDown ) ;
}
2024-07-22 07:50:10 +00:00
List < DeviceWarrantyRequestForm > Datas = CurrentDb . AsQueryable ( ) . With ( SqlWith . NoLock )
. Where ( x = > SqlFunc . ContainsArray ( DevIds , x . EquipmentPK ) )
. Where ( exp . ToExpression ( ) ) . ToList ( ) ;
2024-05-28 14:36:38 +00:00
//List<DeviceWarrantyRequestForm> Datas = CurrentDb.AsQueryable().Where(exp.ToExpression()).ToList();
//int[] pks = Datas.Select(x => x.AutoID).ToArray();
//Dictionary<int, DeviceWarrantyRequestMaintaionInfo> Maintaions = db.Queryable<DeviceWarrantyRequestMaintaionInfo>()
// .With(SqlWith.NoLock)
// .Where(x => SqlFunc.ContainsArray(pks, x.FormID)).ToList().ToDictionary(x => x.FormID, x => x);
//int[] mpks = Maintaions.Select(x => x.Value.AutoID).ToArray();
//List<DeviceWarrantyRequestAccessoriesInfo> Accessories = db.Queryable<DeviceWarrantyRequestAccessoriesInfo>()
// .With(SqlWith.NoLock)
// .Where(x => SqlFunc.ContainsArray(mpks, x.MaintaionID)).ToList();
//foreach (DeviceWarrantyRequestForm item in Datas)
//{
// if (Maintaions.ContainsKey(item.AutoID))
// {
// item.MaintaionInfo = Maintaions[item.AutoID];
// if (Accessories.Any(x => x.MaintaionID == item.MaintaionInfo.AutoID))
// {
// item.MaintaionInfo.Accessories = Accessories.Where(x => x.MaintaionID == item.MaintaionInfo.AutoID)?.ToList();
// }
// }
//}
apiResponseData . Code = 1 ;
apiResponseData . Data = Datas ;
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="Entity"></param>
/// <returns></returns>
public APIResponseData DeviceMaintion ( DeviceWarrantyRequestMaintaionInfo Entity , List < DeviceWarrantyRequestAccessoriesInfo > AccessoriesItems , HeaderModel Operation )
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = $"数据插入失败!" } ;
try
{
if ( Entity = = null )
{
throw new Exception ( "待插入的数据库对象不能为空!" ) ;
}
DateTime CurrentTime = DateTime . Now ;
if ( Entity . AutoID > 0 )
{
/// 修改
Entity . ModifyBy = Operation . Operator ;
Entity . ModifyOn = CurrentTime ;
}
else
{
/// 新增
Entity . CreatOn = CurrentTime ;
Entity . CreateBy = Operation . Operator ;
Entity . Guid = Guid . NewGuid ( ) ;
}
if ( Entity . IsSubmit )
{
Entity . SubmitBy = Operation . Operator ;
Entity . SubmitOn = CurrentTime ;
}
2024-05-30 02:11:52 +00:00
try
2024-05-28 14:36:38 +00:00
{
2024-05-30 02:11:52 +00:00
db . ChangeDatabase ( "main" ) ;
db . BeginTran ( ) ;
if ( db . Queryable < DeviceWarrantyRequestMaintaionInfo > ( ) . Any ( x = > x . FormID = = Entity . AutoID & & x . SubmitBy > 0 ) )
throw new Exception ( $"当前数据已被处理,请刷新后再试!" ) ;
DeviceWarrantyRequestMaintaionInfo MaintaionInfo = db . Saveable ( Entity ) . ExecuteReturnEntity ( ) ;
if ( MaintaionInfo ! = null )
2024-05-28 14:36:38 +00:00
{
2024-05-30 02:11:52 +00:00
bool IsSuccess = true ;
if ( AccessoriesItems ! = null )
2024-05-28 14:36:38 +00:00
{
2024-05-30 02:11:52 +00:00
foreach ( var item in AccessoriesItems )
{
item . Guid = Guid . NewGuid ( ) ;
item . MaintaionID = MaintaionInfo . AutoID ;
item . CreatBy = Operation . Operator ;
item . CreatOn = CurrentTime ;
}
IsSuccess = db . Saveable ( AccessoriesItems ) . ExecuteCommand ( ) = = AccessoriesItems . Count ;
2024-05-28 14:36:38 +00:00
}
2024-05-30 02:11:52 +00:00
if ( IsSuccess )
{
db . CommitTran ( ) ;
apiResponseData . Code = 1 ;
apiResponseData . Message = string . Empty ;
}
2024-05-28 14:36:38 +00:00
}
2024-05-30 02:11:52 +00:00
else
2024-05-28 14:36:38 +00:00
{
2024-05-30 02:11:52 +00:00
db . RollbackTran ( ) ;
2024-05-28 14:36:38 +00:00
}
}
2024-05-30 02:11:52 +00:00
catch ( Exception )
2024-05-28 14:36:38 +00:00
{
2024-05-30 02:11:52 +00:00
2024-05-28 14:36:38 +00:00
db . RollbackTran ( ) ;
2024-05-30 02:11:52 +00:00
throw ;
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 ;
if ( ( ( System . Data . SqlClient . SqlException ) ex ) . Number = = 2627 )
{
apiResponseData . Message = $"当前数据已被处理,请刷新后再试!" ;
}
else
{
apiResponseData . Message = ex . Message ;
}
}
return apiResponseData ;
}
/// <summary>
/// 停机单新增
/// </summary>
/// <param name="Form"></param>
/// <returns></returns>
public APIResponseData DeviceDownFormAdd ( DeviceWarrantyRequestForm Form , HeaderModel Operation )
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = $"数据插入失败!" } ;
try
{
if ( Form = = null )
{
throw new Exception ( "待插入的数据库对象不能为空!" ) ;
}
DateTime CurrentTime = DateTime . Now ;
db . ChangeDatabase ( "main" ) ;
db . BeginTran ( ) ;
#region 2024 年 5 月 21 日 会 议 , 确 定 一 个 设 备 , 同 时 能 存 在 多 个 维 修 单
//// 当前设备存在未完成的设备停机单
//if (db.Queryable<DeviceWarrantyRequestForm>().Any(x =>
// x.EquipmentID == Form.EquipmentID &&
// (
// !SqlFunc.Subqueryable<DeviceWarrantyRequestMaintaionInfo>().Where(s => s.FormID == x.AutoID).Any() ||
// SqlFunc.Subqueryable<DeviceWarrantyRequestMaintaionInfo>().Where(s => s.FormID == x.AutoID && (s.SubmitBy == 0 || s.ValidateBy == 0)).Any())
//))
//{
// throw new Exception("当前设备存在未完成的设备停机单!");
//}
#endregion
Form . GUID = Guid . NewGuid ( ) ;
Form . CreatBy = Operation . Operator ;
Form . CreatorName = Operation . OperatorName ;
Form . CreatOn = CurrentTime ;
2024-05-30 02:11:52 +00:00
try
2024-05-28 14:36:38 +00:00
{
2024-05-30 02:11:52 +00:00
int AutoID = db . Saveable ( Form ) . ExecuteReturnEntity ( ) ? . AutoID ? ? 0 ;
if ( AutoID > 0 )
{
db . CommitTran ( ) ;
apiResponseData . Code = 1 ;
apiResponseData . Message = string . Empty ;
apiResponseData . Data = AutoID ;
}
else
{
db . RollbackTran ( ) ;
}
2024-05-28 14:36:38 +00:00
}
2024-05-30 02:11:52 +00:00
catch ( Exception )
2024-05-28 14:36:38 +00:00
{
db . RollbackTran ( ) ;
2024-05-30 02:11:52 +00:00
throw ;
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="AutoID"></param>
/// <param name="Operation"></param>
/// <returns></returns>
public APIResponseData DownFormAssessment ( DeviceWarrantyEvaluatorInfo Evaluator , HeaderModel Operation )
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = $"数据处理失败!" } ;
try
{
if ( Evaluator . FormID < = 0 )
{
throw new Exception ( "待处理的停机单主键编号无效!" ) ;
}
if ( ! Operation . OperatorGUID . HasValue | | Operation . OperatorGUID = = Guid . Empty )
{
throw new Exception ( "传入的操作人参数无效!" ) ;
}
DateTime CurrentTime = DateTime . Now ;
db . ChangeDatabase ( "main" ) ;
DeviceWarrantyRequestForm Entity = db . Queryable < DeviceWarrantyRequestForm > ( ) . First ( x = > x . AutoID = = Evaluator . FormID ) ;
if ( Entity = = null )
{
throw new Exception ( "待处理的停机单主键编号无效!" ) ;
}
if ( Evaluator . EvaluatorCode . ToUpper ( ) ! = "QE" & & Evaluator . EvaluatorCode . ToUpper ( ) ! = "PE" )
{
throw new Exception ( $"程序出错,未知的评估编码:{Evaluator.EvaluatorCode.ToUpper()}! " ) ;
}
Evaluator . CreatorName = Operation . OperatorName ;
Evaluator . CreateBy = Operation . Operator ;
Evaluator . CreatOn = CurrentTime ;
Evaluator . Guid = Guid . NewGuid ( ) ;
//switch (AssessmentCode.ToUpper())
//{
// case "QA":
// Entity.EvaluatornameQa = Operation.OperatorName;
// Entity.EvaluatoronQa = CurrentTime;
// Entity.EvaluatorcodeQa = Operation.OperatorGUID;
// break;
// case "EA":
// Entity.EvaluatornameEa = Operation.OperatorName;
// Entity.EvaluatoronEa = CurrentTime;
// Entity.EvaluatorcodeEa = Operation.OperatorGUID;
// break;
// case "MT":
// Entity.EvaluatornameMt = Operation.OperatorName;
// Entity.EvaluatoronMt = CurrentTime;
// Entity.EvaluatorcodeMt = Operation.OperatorGUID;
// break;
// case "TL":
// Entity.EvaluatornameTl = Operation.OperatorName;
// Entity.EvaluatoronTl = CurrentTime;
// Entity.EvaluatorcodeTl = Operation.OperatorGUID;
// break;
// default:
// break;
//}
if ( db . Insertable ( Evaluator ) . ExecuteCommand ( ) > 0 )
{
apiResponseData . Code = 1 ;
apiResponseData . Message = string . Empty ;
}
}
2024-06-02 16:38:52 +00:00
catch ( System . Data . SqlClient . SqlException ex )
{
2024-07-22 07:50:10 +00:00
log . Error ( ex ) ;
2024-06-02 16:38:52 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = ex . Number = = 2627 ? "当前数据已被处理,请刷新后在试!" : ex . Message ;
}
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="AutoID"></param>
/// <param name="Status"></param>
/// <returns></returns>
public APIResponseData ChangeDownStatus ( int AutoID , bool Status , HeaderModel Operation )
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = $"数据处理失败!" } ;
2024-06-10 17:33:11 +00:00
db . ChangeDatabase ( "main" ) ;
db . BeginTran ( ) ;
2024-05-28 14:36:38 +00:00
try
{
if ( AutoID < = 0 )
{
throw new Exception ( "待处理的停机单主键编号无效!" ) ;
}
DateTime CurrentTime = DateTime . Now ;
2024-06-10 17:33:11 +00:00
if ( db . Queryable < DeviceWarrantyRequestMaintaionInfo > ( ) . Any ( x = > x . FormID = = AutoID & & x . SubmitBy > 0 ) )
{
throw new Exception ( "数据已被修改!" ) ;
}
2024-05-28 14:36:38 +00:00
DeviceWarrantyRequestForm Entity = db . Queryable < DeviceWarrantyRequestForm > ( ) . First ( x = > x . AutoID = = AutoID ) ;
if ( Entity = = null )
{
throw new Exception ( "待处理的停机单主键编号无效!" ) ;
}
Entity . IsDown = Status ;
if ( db . Updateable ( Entity ) . UpdateColumns ( it = > new { it . IsDown } ) . ExecuteCommand ( ) > 0 )
{
2024-06-10 17:33:11 +00:00
db . CommitTran ( ) ;
2024-05-28 14:36:38 +00:00
apiResponseData . Code = 1 ;
apiResponseData . Message = string . Empty ;
2024-07-22 07:50:10 +00:00
return apiResponseData ;
2024-05-28 14:36:38 +00:00
}
}
catch ( SqlSugarException e )
{
2024-06-10 17:33:11 +00:00
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 )
{
2024-06-10 17:33:11 +00:00
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 ;
}
2024-06-10 17:33:11 +00:00
db . RollbackTran ( ) ;
2024-05-28 14:36:38 +00:00
return apiResponseData ;
}
/// <summary>
/// 双重验证
/// </summary>
/// <param name="t">1: 技术人员确认 2: 工程师或维修主管确认</param>
/// <param name="Operation"></param>
/// <returns></returns>
public APIResponseData ValidateMaintenance ( int AutoID , int t , HeaderModel Operation )
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = $"数据处理失败!" } ;
try
{
if ( AutoID < = 0 )
{
throw new Exception ( "待处理的停机单主键编号无效!" ) ;
}
DateTime CurrentTime = DateTime . Now ;
db . ChangeDatabase ( "main" ) ;
DeviceWarrantyRequestMaintaionInfo Entity = db . Queryable < DeviceWarrantyRequestMaintaionInfo > ( ) . First ( x = > x . FormID = = AutoID ) ;
if ( Entity = = null )
{
throw new Exception ( "待处理的停机单主键编号无效!" ) ;
}
if ( t = = 1 )
{
2024-06-02 16:38:52 +00:00
if ( Entity . ValidateBy > 0 )
throw new Exception ( "当前维修单数据已被处理,请刷新后再试!" ) ;
2024-05-28 14:36:38 +00:00
Entity . ValidateBy = Operation . Operator ;
Entity . ValidateOn = CurrentTime ;
}
else if ( t = = 2 )
{
2024-06-02 16:38:52 +00:00
if ( Entity . Validate2By > 0 )
throw new Exception ( "当前维修单数据已被处理,请刷新后再试!" ) ;
2024-05-28 14:36:38 +00:00
Entity . Validate2By = Operation . Operator ;
Entity . Validate2On = CurrentTime ;
}
else
{
throw new Exception ( "传入的确认人岗位错误!" ) ;
}
if ( db . Updateable ( Entity ) . ExecuteCommand ( ) > 0 )
{
apiResponseData . Code = 1 ;
apiResponseData . Message = string . Empty ;
}
}
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="AutoID"></param>
/// <param name="Operation"></param>
/// <returns></returns>
public APIResponseData DeviceResumptionComfirm ( int AutoID , HeaderModel Operation )
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = $"数据处理失败!" } ;
try
{
if ( AutoID < = 0 )
{
throw new Exception ( "待处理的停机单主键编号无效!" ) ;
}
DateTime CurrentTime = DateTime . Now ;
db . ChangeDatabase ( "main" ) ;
DeviceWarrantyRequestForm Entity = db . Queryable < DeviceWarrantyRequestForm > ( ) . First ( x = > x . AutoID = = AutoID ) ;
if ( Entity = = null )
{
throw new Exception ( "待处理的停机单主键编号无效!" ) ;
}
if ( Entity . MaintaionItems ! = null & & Entity . MaintaionItems . SubmitBy > 0 & &
( ( Entity . InProduction & & Entity . EvaluatorItems ? . Count = = 2 ) | | ! Entity . InProduction ) & &
( ( Entity . MaintaionItems . BeValidate & & Entity . MaintaionItems . Validate2By > 0 & & Entity . MaintaionItems . ValidateBy > 0 ) | | ! Entity . MaintaionItems . BeValidate )
& & Entity . RestorationConfirmationBy = = 0
)
{
Entity . RestorationConfirmationOn = CurrentTime ;
Entity . RestorationConfirmationBy = Operation . Operator ;
Entity . RestorationConfirmationOnName = Operation . OperatorName ;
if ( db . Updateable ( Entity ) . ExecuteCommand ( ) > 0 )
{
apiResponseData . Code = 1 ;
apiResponseData . Message = string . Empty ;
}
}
else
{
2024-06-10 17:33:11 +00:00
throw new Exception ( "待处理的停机单状态不正确,或已被其他用户处理,请重试!" ) ;
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 ;
}
2024-05-30 02:11:52 +00:00
/// <summary>
/// 判断维修单是否提交
/// </summary>
/// <param name="FormAutoId"></param>
/// <returns></returns>
public APIResponseData MaintionDataIsSubmit ( int FormAutoId )
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = $"数据插入失败!" } ;
try
{
if ( FormAutoId = = 0 )
throw new Exception ( "待查询的维修单编号不能为空!" ) ;
db . ChangeDatabase ( "main" ) ;
int Count = db . Queryable < DeviceWarrantyRequestMaintaionInfo > ( ) . With ( SqlWith . NoLock ) . Count ( x = > x . FormID = = FormAutoId & & x . SubmitBy > 0 ) ;
apiResponseData . Code = 1 ;
apiResponseData . Data = Count ;
}
catch ( SqlSugarException e )
{
2024-07-22 07:50:10 +00:00
log . Error ( e ) ;
2024-05-30 02:11:52 +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-30 02:11:52 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = ex . Message ;
}
return apiResponseData ;
}
2024-06-05 17:09:59 +00:00
2024-07-22 07:50:10 +00:00
public APIResponseData GetXlsxData ( DeviceWarrantyRequestFormFilter FilterInfo , string LoginCode )
2024-06-05 17:09:59 +00:00
{
APIResponseData apiResponseData = new APIResponseData { Code = - 1 , Message = $"获取数据失败!" } ;
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 = $"获取到的数据为空!" } ; ;
}
2024-06-10 17:33:11 +00:00
2024-07-22 07:50:10 +00:00
db . ChangeDatabase ( "main" ) ;
Dictionary < int , string > RootIds = db . Ado . SqlQuery < DeviceRouteInfo > ( @"select AutoID,RootName as Name from dbo.View_DeviceRoot" ) . ToDictionary ( x = > x . AutoID , x = > x . Name ) ;
2024-06-10 17:33:11 +00:00
2024-06-05 17:09:59 +00:00
var exp = Expressionable . Create < DeviceWarrantyRequestForm > ( ) ;
if ( FilterInfo ! = null )
{
exp . AndIF ( ! string . IsNullOrWhiteSpace ( FilterInfo . EquipmentID ) , x = > x . EquipmentID . Contains ( FilterInfo . EquipmentID ) ) ;
exp . AndIF ( FilterInfo . StartTime . HasValue , x = > x . CreatOn > = FilterInfo . StartTime . Value ) ;
exp . AndIF ( FilterInfo . EndTime . HasValue , x = > x . CreatOn < = FilterInfo . EndTime . Value ) ;
exp . AndIF ( FilterInfo . Status = = DeviceWarrantyRequestFormStatus . AwaitingRepair ,
x = > SqlFunc . Subqueryable < DeviceWarrantyRequestMaintaionInfo > ( ) . Where ( s = > s . FormID = = x . AutoID ) . NotAny ( ) ) ;
exp . AndIF ( FilterInfo . Status = = DeviceWarrantyRequestFormStatus . AwaitingApproval ,
x = > SqlFunc . Subqueryable < DeviceWarrantyRequestMaintaionInfo > ( ) . Where ( s = > s . FormID = = x . AutoID & & s . SubmitBy > 0 ) . Any ( ) & & ! SqlFunc . HasNumber ( x . RestorationConfirmationBy ) ) ;
exp . AndIF ( FilterInfo . Status = = DeviceWarrantyRequestFormStatus . BeComplate ,
x = > SqlFunc . Subqueryable < DeviceWarrantyRequestMaintaionInfo > ( ) . Where ( s = > s . FormID = = x . AutoID & & s . SubmitBy > 0 & & s . ValidateBy > 0 ) . Any ( ) & & SqlFunc . HasNumber ( x . RestorationConfirmationBy ) ) ;
bool isDown = FilterInfo . DownStatus = = DeviceRunningStatus . Stop ;
exp . AndIF ( FilterInfo . DownStatus ! = DeviceRunningStatus . All ,
x = > x . IsDown = = isDown ) ;
}
List < DeviceWarrantyRequestForm > Datas = CurrentDb . AsQueryable ( ) . With ( SqlWith . NoLock ) . Where ( exp . ToExpression ( ) ) . ToList ( ) ;
Dictionary < int , UserInfoModel > Users = db . Queryable < UserInfoModel > ( ) . ToList ( ) . ToDictionary ( x = > x . AutoID , x = > x ) ;
Dictionary < int , FieldsInfo > Fields = db . Queryable < FieldsInfo > ( ) . ToList ( ) . ToDictionary ( x = > x . AutoID , x = > x ) ;
2024-07-22 07:50:10 +00:00
Dictionary < int , DeviceInformationInfo > devs = Devs . ToDictionary ( x = > x . AutoID , x = > x ) ;
2024-06-05 17:09:59 +00:00
int i = 1 ;
List < MaintainOrderView > views = new List < MaintainOrderView > ( ) ;
foreach ( DeviceWarrantyRequestForm item in Datas )
{
2024-07-22 07:50:10 +00:00
if ( ! devs . ContainsKey ( item . EquipmentPK ) )
continue ;
int Plant = RootIds [ item . EquipmentPK ] = = "KH" ? 1303 : 9997 ;
2024-06-05 17:09:59 +00:00
MaintainOrderView view = new MaintainOrderView
{
AutoNumber = i ,
2024-06-10 17:33:11 +00:00
Plant = Plant ,
2024-06-05 17:09:59 +00:00
AutoID = item . AutoID ,
EquipmentID = item . EquipmentID ,
EquipmentName = item . EquipmentName ,
FormCreatOnDate = item . CreatOn . Value . ToString ( "yyyy-M-dd" ) ,
FormCreatOnTime = item . CreatOn . Value . ToString ( "HH:mm:ss" ) ,
ReferenceOnDate = "" ,
ReferenceOnTime = "" ,
2024-07-01 16:52:48 +00:00
FaultSymptoms = item . FaultSymptoms ,
2024-06-05 17:09:59 +00:00
} ;
2024-07-01 16:52:48 +00:00
if ( item . RestorationConfirmationBy > 0 )
{
view . ComplateDate = item . RestorationConfirmationOn . Value . ToString ( "yyyy-M-dd" ) ;
view . ComplateTime = item . RestorationConfirmationOn . Value . ToString ( "HH:mm:ss" ) ;
2024-07-22 07:50:10 +00:00
view . ComplateBy = item . RestorationConfirmationOnName ;
2024-07-01 16:52:48 +00:00
}
2024-06-05 17:09:59 +00:00
if ( item . MaintaionItems ! = null & & item . MaintaionItems . SubmitBy > 0 )
{
DeviceWarrantyRequestMaintaionInfo repair = item . MaintaionItems ;
view . RepairPersonnel = Users [ repair . SubmitBy ] . RealName ;
view . RepairStartDate = repair . MaintainStartTime . ToString ( "yyyy-M-dd" ) ;
2024-06-10 17:33:11 +00:00
view . RepairFinishDate = repair . MaintainEndTime . ToString ( "yyyy-M-dd" ) ;
2024-06-05 17:09:59 +00:00
view . MaintainCause = repair . MaintainCause ;
view . MaintainContent = repair . MaintainContent ;
if ( repair . AccessoriesItems = = null | | repair . AccessoriesItems . Count = = 0 )
{
view . Accessories = "N/A" ;
}
else
{
view . Accessories = string . Join ( "," , repair . AccessoriesItems . Select ( x = > x . FieldName ) . ToArray ( ) ) ;
}
TimeSpan Downtime = repair . MaintainEndTime - item . CreatOn . Value ;
2024-06-10 17:33:11 +00:00
view . Downtime = Convert . ToDecimal ( Math . Round ( Downtime . TotalHours , 2 ) ) ;
2024-06-05 17:09:59 +00:00
TimeSpan RepairDuration = repair . MaintainEndTime - repair . MaintainStartTime ;
2024-06-10 17:33:11 +00:00
view . RepairDuration = Convert . ToDecimal ( Math . Round ( RepairDuration . TotalHours ) ) ;
2024-06-05 17:09:59 +00:00
view . Maintenance = Fields [ repair . Maintenance ] . FieldText ;
view . SymptomlDistinction = Fields [ repair . SymptomlDistinction ] . FieldText ;
view . ValidateNo = repair . BeValidate ? repair . ValidateNo : "N/A" ;
}
views . Add ( view ) ;
i + + ;
}
apiResponseData . Code = 1 ;
apiResponseData . Data = views ;
apiResponseData . Message = "" ;
}
catch ( SqlSugarException e )
{
2024-07-22 07:50:10 +00:00
log . Error ( e ) ;
2024-06-05 17:09:59 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = e . Message ;
}
catch ( Exception ex )
{
2024-07-22 07:50:10 +00:00
log . Error ( ex ) ;
2024-06-05 17:09:59 +00:00
apiResponseData . Code = - 1 ;
apiResponseData . Message = ex . Message ;
}
return apiResponseData ;
}
2024-07-17 02:32:45 +00:00
2024-05-28 14:36:38 +00:00
}
}