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 apiParams) : base(apiParams) { } public DataSet GetDatas() { DataSet dsDatas = new DataSet("Datas"); try { DateTime sDate = DateTime.Today.AddMonths(-1); if (!ApiParameters.ContainsKey("sDate")) { if (DateTime.TryParse(ApiParameters["sDate"], out 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")) { if (DateTime.TryParse(ApiParameters["eDate"], out eDate)) { if (eDate.Date > DateTime.Today) { sDate = DateTime.Today.AddDays(1).AddMilliseconds(-1); } } } string EquipmentDisPlayID = string.Empty; if (!ApiParameters.ContainsKey("EquipmentDisPlayID")) { EquipmentDisPlayID = ApiParameters["EquipmentDisPlayID"]?.Trim(); } string TagFormStatus = string.Empty; if (!ApiParameters.ContainsKey("TagFormStatus")) { TagFormStatus = ApiParameters["TagFormStatus"]?.Trim(); } var exp = Expressionable.Create() .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().Where(exp).ToList(); dsDatas.Tables.Add(Datas.ToDataTable()); return dsDatas; } catch (SqlException sqlEx) { throw sqlEx; } catch (Exception ex) { log.Error(ex); throw ex; } } /// /// 数据新增 /// /// /// /// public APIResponseData Creat(DataTable Data, out DataTable dtData) { dtData = null; try { if (Data == null || Data.Rows.Count == 0) throw new ArgumentException($"待插入的数据库对象不能为空"); if (!ApiParameters.ContainsKey("OPERATORAUTOID")) throw new ArgumentException("传入的操作员对象参数不正确,操作员不能为空!"); int Operation = 0; if (!int.TryParse(ApiParameters["OPERATORAUTOID"], out Operation)) throw new ArgumentException("传入的操作员对象参数不正确!"); devMain.BeginTran(); TagRecordInfo Model = DTOHelper.DataTableToList(Data)?.FirstOrDefault(); if (Model.AutoID != 0) { // tag 表单修改 TagRecordInfo tag = devMain.Queryable().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().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; } } /// /// 接受/维修 /// /// /// /// 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.DataTableToList(Data)?.FirstOrDefault(); TagRecordInfo tagRecordInfo = devMain.Queryable().First(x => x.AutoID == Model.AutoID); if (tagRecordInfo == null) throw new Exception($"找不到Tag编号为【{Model.AutoID}】的Tag表单!"); if (!ApiParameters.ContainsKey("TagOperation")) throw new ArgumentException("传入的电子签操作员对象参数不正确,操作员不能为空!"); int Operation = 0; if (!int.TryParse(ApiParameters["TagOperation"], out Operation)) throw new ArgumentException("传入的电子签操作员对象参数不正确!"); if (!ApiParameters.ContainsKey("TagOperationDate")) throw new ArgumentException("传入的电子签操作时间不能为空!"); if (!DateTime.TryParse(ApiParameters["TagOperationDate"], out CurrentTime)) throw new ArgumentException("传入的电子签操作时间参数不正确!"); string TagOperationName = string.Empty; if (!ApiParameters.ContainsKey("TagOperationName")) throw new ArgumentException("传入的电子签操作人名称不能为空!"); 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; } } } }