using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IStation.DAL
{
///
/// RepairRequestForm
///
public partial class RepairRequestForm : CorpTraceDAL
{
///
///
///
public override ConnectionConfig ConnectionConfig
{
get
{
return ConfigHelper.DefaultConnectionConfig;
}
}
///
/// 通过 BelongType 和BelongID 获取
///
public List GetByBelongTypeAndBelongID(long CorpID, string BelongType, long BelongID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.BelongType == BelongType && x.BelongID == BelongID).ToList();
}
}
///
/// 通过 CorpID 获取最后几条数据
///
public List GetLastByCorpID(long CorpID,int Number)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID)
.OrderBy(x=>x.CreateTime,OrderByType.Desc)
.Take(Number).ToList();
}
}
///
/// 通过 ProductID 获取
///
public List GetByProductID(long CorpID, long ProductID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.ProductID == ProductID).ToList();
}
}
///
/// 通过 CorpID 获取所有待受理的数据
///
public List GetPendingByCorpID(long CorpID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.FormStatus ==(int)Entity.Repair.eRequestStatus.Pending).ToList();
}
}
///
/// 通过 CorpID 获取待受理的数量
///
public int GetPendingCountByCorpID(long CorpID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.FormStatus == (int)Entity.Repair.eRequestStatus.Pending).Count();
}
}
///
/// 获取某日的数量
///
public int GetCountOfDay(long CorpID, DateTime Day)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.CreateTime >= Day.Date && x.CreateTime < Day.Date.AddDays(1)).Count();
}
}
///
/// 更新 FormStatus
///
public bool UpdateFormStatus(long CorpID, long ID, int FormStatus, long UpdateUserID, DateTime UpdateTime)
{
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
{
return db.Updateable()
.SetColumns(x => x.FormStatus == FormStatus)
.SetColumns(x => x.UpdateUserID == UpdateUserID)
.SetColumns(x => x.UpdateTime == UpdateTime)
.Where(x => x.CorpID == CorpID && x.ID == ID)
.ExecuteCommand() > 0;
}
}
///
/// 插入一条
///
public override long Insert(Entity.RepairRequestForm entity)
{
if (string.IsNullOrEmpty(entity.FormNo))
{
var count = GetCountOfDay(entity.CorpID, entity.CreateTime) + 1;
entity.FormNo = $"R{entity.CreateTime.ToString("yyyyMMdd")}{count.ToString("000000")}";
}
var entity4Log = new Entity.RepairRequestLog();
entity4Log.CorpID = entity.CorpID;
return base.Insert(entity);
}
///
/// 插入
///
public long Insert(Entity.RepairRequestForm entity, Entity.RepairRequestLog entity4Log, List entity4FileList)
{
if (string.IsNullOrEmpty(entity.FormNo))
{
var count = GetCountOfDay(entity.CorpID, entity.CreateTime) + 1;
entity.FormNo = $"R{entity.CreateTime.ToString("yyyyMMdd")}{count.ToString("000000")}";
}
using (var db = new SqlSugarClient(ConnectionConfig))
{
try
{
db.BeginTran();
var formId = db.Insertable(entity).ExecuteReturnSnowflakeId();
if (formId < 1)
{
db.RollbackTran();
return default;
}
entity4Log.FormID = formId;
var logId = db.Insertable(entity4Log).ExecuteReturnSnowflakeId();
if (logId < 1)
{
db.RollbackTran();
return default;
}
if (entity4FileList != null && entity4FileList.Count > 0)
{
entity4FileList.ForEach(x => x.FormID = formId);
var fileIds = db.Insertable(entity4FileList).ExecuteReturnSnowflakeIdList();
if (fileIds == null || fileIds.Count != entity4FileList.Count)
{
db.RollbackTran();
return default;
}
}
db.CommitTran();
return formId;
}
catch
{
db.RollbackTran();
throw;
}
}
}
///
/// 驳回
///
public bool Reject(long CorpID, long ID, string Reason, long UpdateUserID, DateTime UpdateTime)
{
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
{
try
{
db.BeginTran();
var bol= db.Updateable()
.SetColumns(x => x.FormStatus == (int)Entity.Repair.eRequestStatus.Rejected)
.SetColumns(x => x.UpdateUserID == UpdateUserID)
.SetColumns(x => x.UpdateTime == UpdateTime)
.Where(x => x.CorpID == CorpID && x.ID == ID)
.ExecuteCommand() > 0;
if (!bol)
{
db.RollbackTran();
return false;
}
var entity4Log = new Entity.RepairRequestLog();
entity4Log.CorpID = CorpID;
entity4Log.FormID = ID;
entity4Log.OperateType = (int)Entity.Repair.eRequestOperateType.Reject;
entity4Log.OperateContent = "驳回";
entity4Log.OperateTime = UpdateTime;
entity4Log.OperateUserID = UpdateUserID;
entity4Log.OperateNote = Reason;
var logId = db.Insertable(entity4Log).ExecuteReturnSnowflakeId();
if (logId < 1)
{
db.RollbackTran();
return false;
}
db.CommitTran();
return true;
}
catch
{
db.RollbackTran();
throw;
}
}
}
///
/// 受理
///
public bool Accept(long CorpID, long ID,string Note, long UpdateUserID, DateTime UpdateTime)
{
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
{
try
{
db.BeginTran();
var entity = db.Queryable().Where(x => x.CorpID == CorpID && x.ID == ID).First();
if (entity == null)
{
db.RollbackTran();
return false;
}
if (entity.FormStatus != (int)Entity.Repair.eRequestStatus.Pending)
{
db.RollbackTran();
return false;
}
#region 更新报修单
var bol = db.Updateable()
.SetColumns(x => x.FormStatus == (int)Entity.Repair.eRequestStatus.Accepted)
.SetColumns(x => x.UpdateUserID == UpdateUserID)
.SetColumns(x => x.UpdateTime == UpdateTime)
.Where(x => x.CorpID == CorpID && x.ID == ID)
.ExecuteCommand() > 0;
if (!bol)
{
db.RollbackTran();
return false;
}
#endregion
#region 生成报修日志
var entity4Log = new Entity.RepairRequestLog();
entity4Log.CorpID = CorpID;
entity4Log.FormID = ID;
entity4Log.OperateType = (int)Entity.Repair.eRequestOperateType.Accept;
entity4Log.OperateContent = "受理";
entity4Log.OperateTime = UpdateTime;
entity4Log.OperateUserID = UpdateUserID;
entity4Log.OperateNote = Note;
var logId = db.Insertable(entity4Log).ExecuteReturnSnowflakeId();
if (logId < 1)
{
db.RollbackTran();
return false;
}
#endregion
#region 生成维修单
var taskCount = db.Queryable()
.Where(x => x.CorpID == CorpID && x.CreateTime >= UpdateTime.Date && x.CreateTime < UpdateTime.Date.AddDays(1)).Count();
var entity4Task = new Entity.RepairTaskForm();
entity4Task.CorpID = entity.CorpID;
entity4Task.BelongType = entity.BelongType;
entity4Task.BelongID = entity.BelongID;
entity4Task.ProductID= entity.ProductID;
entity4Task.RequestID = entity.ID;
entity4Task.FormNo = $"T{UpdateTime.ToString("yyyyMMdd")}{taskCount.ToString("000000")}";
entity4Task.FormName = entity.FormName;
entity4Task.FormStatus = (int)Entity.Repair.eTaskStatus.Accepted;
entity4Task.Faq=entity.Faq;
entity4Task.Question=entity.Question;
entity4Task.Urgency = entity.Urgency;
entity4Task.Address = entity.Address;
entity4Task.Location = entity.Location;
entity4Task.CreateUserID = UpdateUserID;
entity4Task.CreateTime = UpdateTime;
var taskId = db.Insertable(entity4Task).ExecuteReturnSnowflakeId();
if (taskId < 1)
{
db.RollbackTran();
return false;
}
#endregion
#region 生成维修单日志
var entity4TaskLog = new Entity.RepairTaskLog();
entity4TaskLog.CorpID = entity.CorpID;
entity4TaskLog.FormID = taskId;
entity4TaskLog.OperateType = (int)Entity.Repair.eTaskOperateType.Accept;
entity4TaskLog.OperateContent = "受理";
entity4TaskLog.OperateTime = UpdateTime;
entity4TaskLog.OperateUserID = UpdateUserID;
entity4TaskLog.OperateNote = Note;
var taskLogId = db.Insertable(entity4TaskLog).ExecuteReturnSnowflakeId();
if (taskLogId < 1)
{
db.RollbackTran();
return false;
}
#endregion
#region 生成维修文件
var requestFileList = db.Queryable().Where(x => x.CorpID == CorpID && x.FormID == ID).ToList();
if (requestFileList != null && requestFileList.Count > 0)
{
var entity4LogFileList = new List();
foreach (var requestFile in requestFileList)
{
var entity4LogFile = new Entity.RepairTaskLogFile();
entity4LogFile.CorpID = requestFile.CorpID;
entity4LogFile.FormID = taskId;
entity4LogFile.LogID = taskLogId;
entity4LogFile.FileFormat = requestFile.FileFormat;
entity4LogFile.FileName = requestFile.FileName;
entity4LogFile.FileSuffix = requestFile.FileSuffix;
entity4LogFile.StorageHouse = requestFile.StorageHouse;
entity4LogFile.StorageCode = requestFile.StorageCode;
entity4LogFile.SortCode = requestFile.SortCode;
entity4LogFile.Description = requestFile.Description;
entity4LogFileList.Add(entity4LogFile);
}
var logFileIdList = db.Insertable(entity4LogFileList).ExecuteReturnSnowflakeIdList();
if (logFileIdList == null || logFileIdList.Count != entity4LogFileList.Count)
{
db.RollbackTran();
return false;
}
}
#endregion
db.CommitTran();
return true;
}
catch
{
db.RollbackTran();
throw;
}
}
}
///
/// 获取我的模糊分页列表
///
public List GetMyFluzzyPageList
(
long CorpID,
long CreateUserID,
int? FormStatus,
int? Urgency,
DateTime? StartTime,
DateTime? EndTime,
int PageIndex,
int PageSize,
ref int Total
)
{
if (PageIndex < 1)
PageIndex = 1;
if (PageSize < 1)
PageSize = 1;
var exp = Expressionable.Create();
exp.And(x => x.CorpID == CorpID);
exp.And(x => x.CreateUserID == CreateUserID);
exp.AndIF(FormStatus.HasValue, x => x.FormStatus == FormStatus.Value);
exp.AndIF(Urgency.HasValue, x => x.Urgency == Urgency.Value);
exp.AndIF(StartTime.HasValue, x => x.CreateTime >= StartTime.Value);
exp.AndIF(EndTime.HasValue, x => x.CreateTime <= EndTime.Value);
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(exp.ToExpression())
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.ToPageList(PageIndex, PageSize, ref Total); ;
}
}
///
/// 获取模糊分页列表
///
public List GetFluzzyPageList
(
long CorpID,
string BelongType,
long? BelongID,
long? CreateUserID,
int? FormStatus,
int? Urgency,
string FormNo,
DateTime? StartTime,
DateTime? EndTime,
int PageIndex,
int PageSize,
ref int Total
)
{
if (PageIndex < 1)
PageIndex = 1;
if (PageSize < 1)
PageSize = 1;
var exp = Expressionable.Create();
exp.And(x => x.CorpID == CorpID);
exp.AndIF(!string.IsNullOrEmpty(BelongType),x=>x.BelongType==BelongType);
exp.AndIF(BelongID.HasValue,x=>x.BelongID==BelongID);
exp.AndIF(CreateUserID.HasValue,x=>x.CreateUserID==CreateUserID);
exp.AndIF(FormStatus.HasValue, x => x.FormStatus == FormStatus.Value);
exp.AndIF(Urgency.HasValue, x => x.Urgency == Urgency.Value);
exp.AndIF(!string.IsNullOrEmpty(FormNo),x=>x.FormNo.Contains(FormNo));
exp.AndIF(StartTime.HasValue, x => x.CreateTime >= StartTime.Value);
exp.AndIF(EndTime.HasValue, x => x.CreateTime <= EndTime.Value);
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(exp.ToExpression())
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.ToPageList(PageIndex, PageSize, ref Total); ;
}
}
///
/// 获取待派单分页列表
///
public List> GetJustAccepedPageList
(
long CorpID,
string BelongType,
long? BelongID,
long? CreateUserID,
int? Urgency,
string FormNo,
DateTime? StartTime,
DateTime? EndTime,
int PageIndex,
int PageSize,
ref int Total
)
{
if (PageIndex < 1)
PageIndex = 1;
if (PageSize < 1)
PageSize = 1;
var exp = Expressionable.Create();
exp.And((x, y) => x.CorpID == CorpID);
exp.AndIF(!string.IsNullOrEmpty(BelongType), (x, y) => x.BelongType == BelongType);
exp.AndIF(BelongID.HasValue, (x, y) => x.BelongID == BelongID);
exp.AndIF(CreateUserID.HasValue, (x, y) => x.CreateUserID == CreateUserID);
exp.And((x, y) => x.FormStatus == (int)Entity.Repair.eRequestStatus.Accepted);
exp.AndIF(Urgency.HasValue, (x, y) => x.Urgency == Urgency.Value);
exp.AndIF(!string.IsNullOrEmpty(FormNo), (x, y) => x.FormNo.Contains(FormNo));
exp.AndIF(StartTime.HasValue, (x, y) => x.CreateTime >= StartTime.Value);
exp.AndIF(EndTime.HasValue, (x, y) => x.CreateTime <= EndTime.Value);
exp.And((x, y) => y.FormStatus == (int)Entity.Repair.eTaskStatus.Accepted);
using (var db = new SqlSugarClient(ConnectionConfig))
{
var list= db.Queryable()
.InnerJoin((x, y) =>y.RequestID!=null&& x.ID == y.RequestID.Value)
.Where(exp.ToExpression())
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.Select((x, y) => new { x,y})
.ToPageList(PageIndex, PageSize, ref Total);
return list?.Select(x => new Tuple(x.x, x.y)).ToList();
}
}
}
}