DeviceManager/DeviceRepair.DataAccess/SysCommon/CustomFieldDa.cs
2024-11-10 00:05:40 +08:00

302 lines
12 KiB
C#

using DeviceRepair.DataAccess.Data;
using DeviceRepair.Models;
using DeviceRepair.Models.OperationHistory.Field;
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.SysCommon
{
public class CustomFieldDa : BaseDa
{
private static readonly Logger log = LogManager.GetCurrentClassLogger();
public CustomFieldDa(IDictionary<string, string> apiParams) : base(apiParams)
{
}
/// <summary>
/// 根据字段编码获取数据
/// </summary>
/// <returns></returns>
public DataSet GetDatas()
{
DataSet dsDatas = new DataSet("Datas");
string[] FieldCodes = new string[] { };
try
{
if (ApiParameters.ContainsKey("FieldCode"))
{
FieldCodes = ApiParameters["FieldCode"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
var exp = Expressionable.Create<FieldsInfo>()
.AndIF(FieldCodes.Length > 0, x => SqlFunc.ContainsArray(FieldCodes, x.FieldCode)).ToExpression();//拼接表达式
List<FieldsInfo> Datas = devMain.Queryable<FieldsInfo>().Where(exp).ToList();
DataTable table = Datas.ToDataTable();
dsDatas.Tables.Add(table);
return dsDatas;
}
catch (SqlException sqlEx)
{
throw sqlEx;
}
catch (Exception ex)
{
log.Error(ex);
throw ex;
}
}
/// <summary>
/// 检验数据是否存在
/// </summary>
/// <param name="BeExists"></param>
/// <returns></returns>
public APIResponseData Get_Field_Exists(out bool BeExists)
{
try
{
BeExists = true;
if (!ApiParameters.ContainsKey("Code"))
throw new ArgumentException("传入的字段名称不能为空!");
if (!ApiParameters.ContainsKey("Text"))
throw new ArgumentException("传入的字段值不能为空!");
string Code = ApiParameters["Code"]?.Trim() ?? "";
string Text = ApiParameters["Text"]?.Trim() ?? "";
if (Code.IsNull() || Text.IsNull())
throw new ArgumentException("传入的字段名称或字段值不能为空!");
BeExists = devMain.Queryable<FieldsInfo>().Any(x => x.FieldCode.Equals(Code, StringComparison.CurrentCultureIgnoreCase) && x.FieldText.Equals(Text, StringComparison.CurrentCultureIgnoreCase));
return new APIResponseData { Code = 1 };
}
catch (SqlException sqlEx)
{
devMain.RollbackTran();
throw sqlEx;
}
catch (Exception ex)
{
devMain.RollbackTran();
log.Error(ex);
throw ex;
}
}
/// <summary>
/// 修改状态
/// </summary>
/// <returns></returns>
public APIResponseData Change_Field_Status()
{
try
{
DateTime CurrentDate = DateTime.Now;
if (!ApiParameters.ContainsKey("OPERATORAUTOID"))
throw new ArgumentException("传入的操作员对象参数不正确,操作员不能为空!");
int Operation = 0;
if (!int.TryParse(ApiParameters["OPERATORAUTOID"], out Operation))
throw new ArgumentException("传入的操作员对象参数不正确!");
if (!ApiParameters.ContainsKey("AutoID"))
throw new ArgumentException("传入的主键编号参数不能为空!");
if (!ApiParameters.ContainsKey("Description"))
throw new ArgumentException("传入的字段说明参数不能为空!");
if (!ApiParameters.ContainsKey("Status"))
throw new ArgumentException("传入的状态字段不能为空!");
int AutoId = 0;
if (!int.TryParse(ApiParameters["AutoID"], out AutoId))
throw new ArgumentException("传入的主键编号参数不正确!");
bool Status = false;
if (!bool.TryParse(ApiParameters["Status"], out Status))
throw new ArgumentException("传入的字段状态参数不正确!");
string Description = ApiParameters["Description"] ?? "";
devMain.BeginTran();
FieldsInfo data = devMain.Queryable<FieldsInfo>().Single(x => x.AutoID == AutoId);
if (data == null)
throw new Exception($"{(Status ? "" : "")}出错,主键编号为【{AutoId}】的数据不存在!");
int Count = devMain.Updateable<FieldsInfo>().UpdateColumns(x => new { x.Status, x.Description, x.ModifyBy, x.ModifyOn })
.SetColumns(x => new FieldsInfo() { Status = Status, Description = Description, ModifyBy = Operation, ModifyOn = CurrentDate })
.Where(x => x.AutoID == AutoId)
.ExecuteCommand();
bool isSuccess = Count > 0;
if (!isSuccess)
throw new Exception("修改字段状态失败!");
devMain.CommitTran();
try
{
// 操作日志
OperationHistory(new FieldOpsHistory
{
GUID = Guid.NewGuid(),
FieldAutoID = data.AutoID,
FieldHisGuid = data.GUID,
Note = Description,
}, Status ? EnumOperationType.UnLock : EnumOperationType.Lock, "FieldLog", CurrentDate);
}
catch
{
}
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>
/// <returns></returns>
public APIResponseData InsertOrEdit_Field_Data(DataTable Data)
{
try
{
EnumOperationType OpsType = EnumOperationType.Add;
DateTime CurrentDate = DateTime.Now;
if (!ApiParameters.ContainsKey("OPERATORAUTOID"))
throw new ArgumentException("传入的操作员对象参数不正确,操作员不能为空!");
int Operation = 0;
if (!int.TryParse(ApiParameters["OPERATORAUTOID"], out Operation))
throw new ArgumentException("传入的操作员对象参数不正确!");
devMain.BeginTran();
/* 自定义字段对象 */
FieldsInfo Item = DTOHelper<FieldsInfo>.DataTableToList(Data)?.FirstOrDefault();
FieldDataHistory his = null;
if (Item.AutoID == 0)
{
/* 新增 */
Item.GUID = Guid.NewGuid();
Item.CreatBy = Operation;
Item.CreatOn = CurrentDate;
Item.Status = true;
if (devMain.Queryable<FieldsInfo>().Any(x => x.FieldCode.Equals(Item.FieldCode, StringComparison.CurrentCultureIgnoreCase)
&& x.FieldText.Equals(Item.FieldText, StringComparison.CurrentCultureIgnoreCase)))
throw new Exception($"已存在名称为【{Item.FieldText}】的数据,无法重复添加!");
Item.AutoID = devMain.Insertable(Item).ExecuteReturnIdentity();
if (Item.AutoID == 0)
throw new Exception($"执行自定义字段新增失败!");
devMain.CommitTran();
}
else
{
OpsType = EnumOperationType.Change;
/* 修改 */
var old = devMain.Queryable<FieldsInfo>().Single(x => x.AutoID == Item.AutoID);
if (old == null)
throw new Exception($"传入的修改对象出错,主键编号为【{Item.AutoID}】的数据不存在!");
if (old.FieldText == Item.FieldText && old.FieldType == Item.FieldType && old.FieldValue == Item.FieldValue)
{
throw new Exception("当前数据无更改!");
}
his = new FieldDataHistory
{
FieldID = old.AutoID,
CreatBy = old.CreatBy,
CreatOn = old.CreatOn,
Description = old.Description,
FieldCode = old.FieldCode,
FieldText = old.FieldText,
FieldType = old.FieldType,
FieldValue = old.FieldValue,
GUID = old.GUID,
ModifyBy = old.ModifyBy,
ModifyOn = old.ModifyOn,
OperationDate = CurrentDate,
Operator = Operation,
OperationIP = base.OperationInfo.IPAddress,
OperationComputer = base.OperationInfo.ComputerName,
OperateMAC = base.OperationInfo.Mac,
Status = old.Status
};
if (devMain.Updateable<FieldsInfo>().UpdateColumns(x => new { x.FieldText, x.FieldValue, x.FieldType, x.ModifyBy, x.ModifyOn, x.GUID })
.SetColumns(x => new FieldsInfo() { FieldText = Item.FieldText, FieldValue = Item.FieldValue, FieldType = Item.FieldType, ModifyBy = Operation, ModifyOn = CurrentDate, GUID = Guid.NewGuid() })
.Where(x => x.AutoID == Item.AutoID)
.ExecuteCommand() != 1)
throw new Exception($"执行自定义字段修改失败!");
devMain.CommitTran();
}
try
{
if (his != null)
{
devLog.Insertable(his).ExecuteCommand();
}
// 操作日志
OperationHistory(new FieldOpsHistory
{
GUID = Guid.NewGuid(),
FieldAutoID = Item.AutoID,
FieldHisGuid = Item.GUID,
}, OpsType, "FieldLog", CurrentDate);
}
catch
{
}
return new APIResponseData { Code = 1 };
}
catch (SqlException sqlEx)
{
devMain.RollbackTran();
throw sqlEx;
}
catch (Exception ex)
{
devMain.RollbackTran();
log.Error(ex);
throw ex;
}
}
}
}