2024-07-27 01:44:19 +00:00
|
|
|
|
using DeviceRepair.DataAccess.Data;
|
|
|
|
|
using DeviceRepair.Models;
|
2024-11-09 04:25:57 +00:00
|
|
|
|
using DeviceRepair.Models.OperationHistory.Field;
|
2024-07-27 01:44:19 +00:00
|
|
|
|
using DeviceRepair.Utils;
|
|
|
|
|
using NLog;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.SqlClient;
|
2024-08-02 02:52:45 +00:00
|
|
|
|
using System.Linq;
|
2024-07-27 01:44:19 +00:00
|
|
|
|
|
|
|
|
|
namespace DeviceRepair.DataAccess.SysCommon
|
|
|
|
|
{
|
|
|
|
|
public class CustomFieldDa : BaseDa
|
|
|
|
|
{
|
|
|
|
|
private static readonly Logger log = LogManager.GetCurrentClassLogger();
|
2024-11-09 04:25:57 +00:00
|
|
|
|
|
2024-07-27 01:44:19 +00:00
|
|
|
|
public CustomFieldDa(IDictionary<string, string> apiParams) : base(apiParams)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-02 02:52:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据字段编码获取数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2024-07-27 01:44:19 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-08-02 02:52:45 +00:00
|
|
|
|
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);
|
2024-07-27 01:44:19 +00:00
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-02 02:52:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 修改状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public APIResponseData Change_Field_Status()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
DateTime CurrentDate = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
if (!ApiParameters.ContainsKey("OPERATORAUTOID"))
|
|
|
|
|
throw new ArgumentException("传入的操作员对象参数不正确,操作员不能为空!");
|
2024-07-27 01:44:19 +00:00
|
|
|
|
|
2024-08-02 02:52:45 +00:00
|
|
|
|
int Operation = 0;
|
|
|
|
|
if (!int.TryParse(ApiParameters["OPERATORAUTOID"], out Operation))
|
|
|
|
|
throw new ArgumentException("传入的操作员对象参数不正确!");
|
2024-07-27 01:44:19 +00:00
|
|
|
|
|
2024-08-02 02:52:45 +00:00
|
|
|
|
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();
|
2024-11-09 04:25:57 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 操作日志
|
|
|
|
|
OperationHistory(new FieldOpsHistory
|
|
|
|
|
{
|
|
|
|
|
GUID = Guid.NewGuid(),
|
|
|
|
|
FieldAutoID = data.AutoID,
|
|
|
|
|
FieldHisGuid = data.GUID,
|
|
|
|
|
Note = Description,
|
|
|
|
|
}, Status ? EnumOperationType.UnLock : EnumOperationType.Lock, "FieldLog", CurrentDate);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-02 02:52:45 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2024-11-09 16:05:40 +00:00
|
|
|
|
EnumOperationType OpsType = EnumOperationType.Add;
|
2024-08-02 02:52:45 +00:00
|
|
|
|
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();
|
|
|
|
|
|
2024-11-09 04:25:57 +00:00
|
|
|
|
FieldDataHistory his = null;
|
|
|
|
|
|
2024-08-02 02:52:45 +00:00
|
|
|
|
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}】的数据,无法重复添加!");
|
|
|
|
|
|
2024-11-09 16:05:40 +00:00
|
|
|
|
Item.AutoID = devMain.Insertable(Item).ExecuteReturnIdentity();
|
|
|
|
|
if (Item.AutoID == 0)
|
2024-08-02 02:52:45 +00:00
|
|
|
|
throw new Exception($"执行自定义字段新增失败!");
|
2024-11-09 04:25:57 +00:00
|
|
|
|
|
|
|
|
|
devMain.CommitTran();
|
2024-08-02 02:52:45 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-11-09 16:05:40 +00:00
|
|
|
|
OpsType = EnumOperationType.Change;
|
2024-08-02 02:52:45 +00:00
|
|
|
|
/* 修改 */
|
|
|
|
|
var old = devMain.Queryable<FieldsInfo>().Single(x => x.AutoID == Item.AutoID);
|
|
|
|
|
if (old == null)
|
|
|
|
|
throw new Exception($"传入的修改对象出错,主键编号为【{Item.AutoID}】的数据不存在!");
|
|
|
|
|
|
2024-11-09 16:05:40 +00:00
|
|
|
|
if (old.FieldText == Item.FieldText && old.FieldType == Item.FieldType && old.FieldValue == Item.FieldValue)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("当前数据无更改!");
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-09 04:25:57 +00:00
|
|
|
|
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() })
|
2024-08-02 02:52:45 +00:00
|
|
|
|
.Where(x => x.AutoID == Item.AutoID)
|
|
|
|
|
.ExecuteCommand() != 1)
|
|
|
|
|
throw new Exception($"执行自定义字段修改失败!");
|
2024-11-09 04:25:57 +00:00
|
|
|
|
|
|
|
|
|
devMain.CommitTran();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (his != null)
|
|
|
|
|
{
|
|
|
|
|
devLog.Insertable(his).ExecuteCommand();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 操作日志
|
|
|
|
|
OperationHistory(new FieldOpsHistory
|
|
|
|
|
{
|
|
|
|
|
GUID = Guid.NewGuid(),
|
|
|
|
|
FieldAutoID = Item.AutoID,
|
|
|
|
|
FieldHisGuid = Item.GUID,
|
2024-11-09 16:05:40 +00:00
|
|
|
|
}, OpsType, "FieldLog", CurrentDate);
|
2024-11-09 04:25:57 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
|
2024-08-02 02:52:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new APIResponseData { Code = 1 };
|
|
|
|
|
}
|
|
|
|
|
catch (SqlException sqlEx)
|
|
|
|
|
{
|
|
|
|
|
devMain.RollbackTran();
|
|
|
|
|
throw sqlEx;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
devMain.RollbackTran();
|
|
|
|
|
log.Error(ex);
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-27 01:44:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|