DeviceManager/DeviceRepair.DataAccess/Tag/TagDa.cs
2024-08-05 17:21:06 +08:00

229 lines
8.7 KiB
C#

using DeviceRepair.DataAccess.Data;
using DeviceRepair.Models;
using DeviceRepair.Models.Tag;
using DeviceRepair.Utils;
using NLog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace DeviceRepair.DataAccess.Tag
{
public class TagDa : BaseDa
{
private static readonly Logger log = LogManager.GetCurrentClassLogger();
public TagDa(IDictionary<string, string> apiParams) : base(apiParams)
{
}
public DataSet GetDatas()
{
DataSet dsDatas = new DataSet("Datas");
try
{
DateTime sDate = DateTime.Today.AddMonths(-1);
if (!ApiParameters.ContainsKey("sDate"))
{
sDate = base.GetParamDateTime("sDate", "开始时间");
if (sDate < new DateTime(1900, 01, 01))
{
sDate = new DateTime(1900, 01, 01);
}
}
DateTime eDate = DateTime.Today.AddDays(1).AddMilliseconds(-1);
if (!ApiParameters.ContainsKey("eDate"))
{
eDate = base.GetParamDateTime("eDate", "结束时间");
if (eDate.Date > DateTime.Today)
{
sDate = DateTime.Today.AddDays(1).AddMilliseconds(-1);
}
}
string EquipmentDisPlayID = string.Empty;
if (!ApiParameters.ContainsKey("EquipmentDisPlayID"))
{
EquipmentDisPlayID = base.GetParamString("EquipmentDisPlayID", "设备编号");
}
string TagFormStatus = string.Empty;
if (!ApiParameters.ContainsKey("TagFormStatus"))
{
TagFormStatus = base.GetParamString("TagFormStatus", "表单状态");
}
var exp = Expressionable.Create<TagRecordInfo>()
.And(t1 => t1.CreatOn >= sDate)
.And(t1 => t1.CreatOn <= eDate)
.AndIF(!string.IsNullOrWhiteSpace(EquipmentDisPlayID), t1 => t1.EquipmentDisPlayID == EquipmentDisPlayID)
.AndIF(!string.IsNullOrWhiteSpace(TagFormStatus), t1 => t1.TagFormStatus == TagFormStatus).ToExpression();//拼接表达式
var Datas = devMain.Queryable<TagRecordInfo>().Where(exp).ToList();
dsDatas.Tables.Add(Datas.ToDataTable());
return dsDatas;
}
catch (SqlException sqlEx)
{
throw sqlEx;
}
catch (Exception ex)
{
log.Error(ex);
throw ex;
}
}
/// <summary>
/// 数据新增
/// </summary>
/// <param name="Data"></param>
/// <param name="dtData"></param>
/// <returns></returns>
public APIResponseData Creat(DataTable Data, out DataTable dtData)
{
dtData = null;
try
{
if (Data == null || Data.Rows.Count == 0)
throw new ArgumentException($"待插入的数据库对象不能为空");
int Operation = base.GetParamInt("OPERATORAUTOID", "操作员");
devMain.BeginTran();
TagRecordInfo Model = DTOHelper<TagRecordInfo>.DataTableToList(Data)?.FirstOrDefault();
if (Model.AutoID != 0)
{
// tag 表单修改
TagRecordInfo tag = devMain.Queryable<TagRecordInfo>().First(x => x.AutoID == Model.AutoID);
if (tag == null)
throw new Exception($"TagNo.{Model.AutoID}的数据不存在,修改失败!");
if (tag.TagFormStatus == "C")
throw new Exception($"TagNo.{Model.AutoID}的数据已被修改!");
tag.BrokenDamaged = Model.BrokenDamaged;
tag.Access = Model.Access;
tag.Clean = Model.Clean;
tag.Inspect = Model.Inspect;
tag.SourceOfContamination = Model.SourceOfContamination;
tag.NotInUse = Model.NotInUse;
tag.NotNeeded = Model.NotNeeded;
tag.UnsafeConditions = Model.UnsafeConditions;
tag.MissingComponents = Model.MissingComponents;
tag.DetailsOfIssue = Model.DetailsOfIssue;
tag.ModifyBy = Operation;
tag.ModifyName = ApiParameters["OPERATORNAME"];
tag.ModifyOn = DateTime.Now;
if (devMain.Updateable(tag).ExecuteCommand() > 0)
{
devMain.CommitTran();
dtData = tag.toDataTable();
return new APIResponseData { Code = 1 };
}
}
DeviceInformationInfo dev = devMain.Queryable<DeviceInformationInfo>().First(x => x.EquipmentID == Model.EquipmentDisPlayID && x.EquipmentStatus == 1);
if (dev == null)
throw new Exception("传入的设备信息出错或设备未启用!");
Model.EquipmentDisPlayID = dev.EquipmentID;
Model.EquipmentName = dev.EquipmentName;
Model.EquipmentID = dev.AutoID;
Model.CreateBy = Operation;
Model.CreatOn = DateTime.Now;
Model.CreatorName = ApiParameters["OPERATORNAME"];
Model.TagFormStatus = "N";
TagRecordInfo Rtn = devMain.Insertable(Model).ExecuteReturnEntity();
if (Rtn == null)
{
throw new Exception("数据库写入出错!");
}
devMain.CommitTran();
dtData = Model.toDataTable();
return new APIResponseData { Code = 1 };
}
catch (SqlException sqlEx)
{
devMain.RollbackTran();
throw sqlEx;
}
catch (Exception ex)
{
devMain.RollbackTran();
log.Error(ex);
throw ex;
}
}
/// <summary>
/// 接受/维修
/// </summary>
/// <param name="Data"></param>
/// <param name="dtData"></param>
/// <returns></returns>
public APIResponseData Receipt(DataTable Data, out DataTable dtData)
{
try
{
dtData = null;
DateTime CurrentTime = DateTime.Now;
if (Data == null || Data.Rows.Count == 0)
throw new ArgumentException($"待插入的数据库对象不能为空");
TagRecordInfo Model = DTOHelper<TagRecordInfo>.DataTableToList(Data)?.FirstOrDefault();
TagRecordInfo tagRecordInfo = devMain.Queryable<TagRecordInfo>().First(x => x.AutoID == Model.AutoID);
if (tagRecordInfo == null)
throw new Exception($"找不到Tag编号为【{Model.AutoID}】的Tag表单!");
int Operation = base.GetParamInt("TagOperation", "电子签操作员");
CurrentTime = base.GetParamDateTime("TagOperationDate", "电子签操作时间");
string TagOperationName = base.GetParamString("TagOperationName", "电子签操作人名称");
TagOperationName = ApiParameters["TagOperationName"];
tagRecordInfo.ReceiptBy = Operation;
tagRecordInfo.ReceiptName = TagOperationName;
tagRecordInfo.ReceiptOn = CurrentTime;
tagRecordInfo.ReceiptDetailsOfIssue = Model.ReceiptDetailsOfIssue;
tagRecordInfo.TagFormStatus = "C";
devMain.BeginTran();
int Rtn = devMain.Updateable(tagRecordInfo).ExecuteCommand();
if (Rtn > 0)
{
devMain.CommitTran();
dtData = tagRecordInfo.toDataTable();
return new APIResponseData { Code = 1 };
}
else
{
devMain.RollbackTran();
return new APIResponseData { Code = -1, Message = "维修数据失败!" };
}
}
catch (SqlException sqlEx)
{
devMain.RollbackTran();
throw sqlEx;
}
catch (Exception ex)
{
devMain.RollbackTran();
log.Error(ex);
throw ex;
}
}
}
}