using ProductionSystem_Log;
using ProductionSystem_Model.DbModel;
using ProductionSystem_Model.DbModel.Para;
using ProductionSystem_Model.DbModel.System;
using ProductionSystem_Model.ViewModel;
using ProductionSystem_Model.ViewModel.Request.Para;
using ProductionSystem_Model.ViewModel.Response.Para;
using ProductionSystem_Model.ViewModel.Response.System;
using SqlSugar;
using System;
using System.Collections.Generic;
namespace ProductionSystem_Service
{
public class ParaColumnConfigService : DbContext
{
///
/// 分页查询参数字段配置项
///
///
///
///
public List GetParaColumnPageList(QueryParaColVM queryParaVM, out int totalCount)
{
totalCount = 0;
try
{
return db.Queryable()
.InnerJoin((a, b) => a.ParaCategory == b.DicCode && b.DicTypeCode == "para_category")
.LeftJoin((a, b, c) => a.TargetPointAddress == c.PointCode && c.IsActive)
.LeftJoin((a, b, c, d) => a.MaxPointAddress == d.PointCode && d.IsActive)
.LeftJoin((a, b, c, d, e) => a.MinPointAddress == e.PointCode && e.IsActive)
.LeftJoin((a, b, c, d, e, f) => a.IsMustPointAddress == f.PointCode && f.IsActive)
.LeftJoin((a, b, c, d, e, f, g) => a.TestResultPointAddress == g.PointCode && g.IsActive)
.LeftJoin((a, b, c, d, e, f, g, h) => a.CollectPointAddress == h.PointCode && h.IsActive)
.WhereIF(queryParaVM != null && !string.IsNullOrWhiteSpace(queryParaVM.ProductType), a => a.ProductType == queryParaVM.ProductType)
.WhereIF(queryParaVM != null && !string.IsNullOrWhiteSpace(queryParaVM.ParaCategory), a => a.ParaCategory == queryParaVM.ParaCategory)
.WhereIF(queryParaVM != null && !string.IsNullOrWhiteSpace(queryParaVM.ParaName), a => a.ParaName.Contains(queryParaVM.ParaName))
.Select((a, b, c, d, e, f, g, h) => new ParaColVM
{
Id = a.Id,
SerialNumber = SqlFunc.RowNumber($"{a.ProductType} asc ,{a.ParaCategory} asc,{a.SortIndex} asc "),
ProductType = a.ProductType,
ParaCategory = a.ParaCategory,
ParaCategoryName = b.DicName,
ParaCode = a.ParaCode,
ParaName = a.ParaName,
TargetPointAddress = c.PointStartAddress,
TargetReadonly = a.TargetReadonly,
StrTargetReadonly = a.TargetReadonly ? "是" : "否",
MaxPointAddress = d.PointStartAddress,
MaxReadonly = a.MaxReadonly,
StrMaxReadonly = a.MaxReadonly ? "是" : "否",
MinPointAddress = e.PointStartAddress,
MinReadonly = a.MinReadonly,
StrMinReadonly = a.MinReadonly ? "是" : "否",
IsMust = a.IsMust,
StrIsMust = a.IsMust ? "是" : "否",
IsMustPointAddress = f.PointStartAddress,
IsShowMain = a.IsShowMain,
StrIsShowMain = a.IsShowMain ? "是" : "否",
IsShowColConfig = a.IsShowColConfig,
StrIsShowColConfig = a.IsShowColConfig ? "是" : "否",
CollectPointAddress = h.PointStartAddress,
TestResultPointAddress = g.PointStartAddress,
SortIndex = a.SortIndex,
IsActive = a.IsActive,
StrIsActive = a.IsActive ? "是" : "否",
Remark = a.Remark
}).OrderBy(a => a.ProductType).OrderBy(a => a.ParaCategory).OrderBy(a => a.SortIndex).ToPageList(queryParaVM.PageIndex, queryParaVM.PageSize, ref totalCount);
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行ParaColumnConfigService下GetParaColumnPageList时异常");
return null;
}
}
///
/// 查询所有参数字段配置项
///
///
public ISugarQueryable QueryParaColumns()
{
try
{
return db.Queryable();
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行ParaColumnConfigService下QueryParaColumns时异常");
return null;
}
}
///
/// 查询有效参数字段配置项
///
///
public ISugarQueryable QueryActiveParaColumns()
{
try
{
return db.Queryable().Where(m => m.IsActive);
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行ParaColumnConfigService下QueryActiveParaColumns时异常");
return null;
}
}
///
/// 根据产品型号查询有效参数字段配置项
///
///
///
public ISugarQueryable QueryActiveParaColumnsByProductType(string productType)
{
try
{
return db.Queryable().Where(m => m.ProductType == productType && m.IsActive).OrderBy(m => m.SortIndex);
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行ParaColumnConfigService下QueryActiveParaColumnsByProductType时异常");
return null;
}
}
///
/// 根据Id查询参数项字段
///
///
///
public T_ParaColumnConfig QueryParaColumnById(string id)
{
try
{
return db.Queryable().First(m => m.Id == id);
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行ParaColumnConfigService下QueryActiveParaColumnById时异常");
return null;
}
}
///
/// 查询参数项字段已存在的最大排序索引
///
///
///
///
public int? QueryParaColumnMaxSortIndex(string productType, string paraCategory)
{
try
{
return db.Queryable()
.Where(m => m.ProductType == productType)
.WhereIF(!string.IsNullOrEmpty(paraCategory), m => m.ParaCategory == paraCategory)
.Select(m => new
{
sortIndex = SqlFunc.AggregateMax(m.SortIndex)
}).First()?.sortIndex;
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行ParaColumnConfigService下QueryParaColumnMaxSortIndex时异常");
return -1;
}
}
///
/// 根据产品型号查询有效参数字段配置项
///
///
///
public List GetActiveParaColumns(string productType)
{
try
{
return db.Queryable().Where(m => m.ProductType == productType && m.IsActive).OrderBy(m => m.SortIndex).ToList();
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行ParaColumnConfigService下GetActiveParaColumns时异常");
return null;
}
}
///
/// 新增参数字段配置项
///
///
///
public (bool isOk, string msg) AddParaColumn(T_ParaColumnConfig t_ParaColumnConfig)
{
try
{
var row = db.Insertable(t_ParaColumnConfig).ExecuteCommand();
return (row > 0, row > 0 ? "操作成功!" : "操作失败!");
}
catch (Exception ex)
{
var errorMsg = $"执行ParaColumnConfigService下AddParaColumn时异常:{ex.Message}";
LogHelper.Error(ex, errorMsg);
return (false, errorMsg);
}
}
///
/// 批量新增参数字段配置项
///
///
///
public (bool isOk, string msg) SaveParaColumn(List t_ParaColumnConfigs)
{
var result = db.UseTran(() =>
{
var rows = db.Insertable(t_ParaColumnConfigs).ExecuteCommand();
if (rows > 0 && rows == t_ParaColumnConfigs.Count)
{
return true;
}
else
{
return false;
}
});
if (!result.Data) //返回值为false
{
return (false, "批量新增参数字段配置项,操作失败!");
}
return (true, "操作成功!");
}
///
/// 更新参数字段配置项
///
///
///
public (bool isOk, string msg) UpdateParaColumn(T_ParaColumnConfig t_ParaColumnConfig)
{
try
{
var row = db.Updateable(t_ParaColumnConfig).ExecuteCommand();
return (row > 0, row > 0 ? "操作成功!" : "操作失败!");
}
catch (Exception ex)
{
var errorMsg = $"执行ParaColumnConfigService下UpdateParaColumn时异常:{ex.Message}";
LogHelper.Error(ex, errorMsg);
return (false, errorMsg);
}
}
///
/// 根据Id删除参数字段配置项
///
///
///
public (bool isOk, string msg) DelParaColumnById(string id)
{
try
{
var row = db.Deleteable().Where(m => m.Id == id).ExecuteCommand();
return (row > 0, row > 0 ? "操作成功!" : "操作失败!");
}
catch (Exception ex)
{
var errorMsg = $"执行ParaColumnConfigService下DelParaColumnById时异常:{ex.Message}";
LogHelper.Error(ex, errorMsg);
return (false, errorMsg);
}
}
///
/// 根据参数项字段Id集合批量更新参数字段配置项状态
///
///
///
///
public (bool isOk, string msg) UpdateParaColumnStatus(List ids, bool? isActive = true)
{
try
{
var row = db.Updateable()
.SetColumns(c => c.IsActive == isActive)
.SetColumns(c => c.UpdatedTime == DateTime.Now)
.SetColumns(c => c.UpdatedBy == CurrentUser.UserName)
.Where(m => ids.Contains(m.Id))
.ExecuteCommand();
return (row > 0, row > 0 ? "操作成功!" : "操作失败!");
}
catch (Exception ex)
{
var errorMsg = $"执行ParaColumnConfigService下UpdateParaColumnStatus时异常:{ex.Message}";
LogHelper.Error(ex, errorMsg);
return (false, errorMsg);
}
}
}
}