using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IStation.DAL
{
///
/// RepairTaskForm
///
public partial class RepairTaskForm : CorpTraceDAL
{
///
///
///
public override ConnectionConfig ConnectionConfig
{
get
{
return ConfigHelper.DefaultConnectionConfig;
}
}
///
/// 通过 RequestID 获取
///
public List GetByRequestID(long CorpID, long RequestID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.RequestID!=null && x.RequestID.Value == RequestID)
.OrderBy(x=>x.CreateTime,OrderByType.Desc)
.ToList();
}
}
///
/// 通过 RequestIds 获取
///
public List GetByRequestIds(long CorpID, List RequestIds)
{
if (RequestIds == null || RequestIds.Count < 1)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.RequestID!=null && RequestIds.Contains(x.RequestID.Value))
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.ToList();
}
}
///
/// 通过 CorpID 获取
///
public int GetCountByCorpID(long CorpID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID)
.Count();
}
}
///
/// 通过 CorpID 获取所有未验收的数据
///
public List GetUnCheckedByCorpID(long CorpID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID &&x.FormStatus<(int)Entity.Repair.eTaskStatus.Succeed)
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.ToList();
}
}
///
/// 获取某日的数量
///
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 bool Assign(long CorpID, long ID,long RepairUserID, string Note, long UpdateUserID, DateTime UpdateTime)
{
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
{
try
{
db.BeginTran();
var bol = db.Updateable()
.SetColumns(x => x.FormStatus == (int)Entity.Repair.eTaskStatus.Assigned)
.SetColumns(x=>x.RepairUserID == RepairUserID)
.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.RepairTaskLog();
entity4Log.CorpID = CorpID;
entity4Log.FormID = ID;
entity4Log.OperateType = (int)Entity.Repair.eTaskOperateType.Assign;
entity4Log.OperateContent = "派单";
entity4Log.OperateTime = UpdateTime;
entity4Log.OperateUserID = UpdateUserID;
entity4Log.OperateNote = Note;
var logId = db.Insertable(entity4Log).ExecuteReturnSnowflakeId();
if (logId < 1)
{
db.RollbackTran();
return false;
}
db.CommitTran();
return true;
}
catch
{
db.RollbackTran();
throw;
}
}
}
///
/// 接单
///
public bool Receive(long CorpID, long ID, string Note, long UpdateUserID, DateTime UpdateTime)
{
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
{
try
{
db.BeginTran();
var bol = db.Updateable()
.SetColumns(x => x.FormStatus == (int)Entity.Repair.eTaskStatus.Received)
.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.RepairTaskLog();
entity4Log.CorpID = CorpID;
entity4Log.FormID = ID;
entity4Log.OperateType = (int)Entity.Repair.eTaskOperateType.Receive;
entity4Log.OperateContent = "接单";
entity4Log.OperateTime = UpdateTime;
entity4Log.OperateUserID = UpdateUserID;
entity4Log.OperateNote = Note;
var logId = db.Insertable(entity4Log).ExecuteReturnSnowflakeId();
if (logId < 1)
{
db.RollbackTran();
return false;
}
db.CommitTran();
return true;
}
catch
{
db.RollbackTran();
throw;
}
}
}
///
/// 开工
///
public bool Start(long CorpID,long ID,string Note, List entity4FileList,long UpdateUserID,DateTime UpdateTime)
{
using (var 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.eTaskStatus.Received)
{
db.RollbackTran();
return false;
}
var bol = db.Updateable()
.SetColumns(x => x.FormStatus == (int)Entity.Repair.eTaskStatus.Started)
.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.RepairTaskLog();
entity4Log.CorpID = CorpID;
entity4Log.FormID = ID;
entity4Log.OperateType = (int)Entity.Repair.eTaskOperateType.Start;
entity4Log.OperateContent = "开工";
entity4Log.OperateTime = UpdateTime;
entity4Log.OperateUserID = UpdateUserID;
entity4Log.OperateNote = Note;
var logId = db.Insertable(entity4Log).ExecuteReturnSnowflakeId();
if (logId < 1)
{
db.RollbackTran();
return false;
}
if (entity4FileList != null && entity4FileList.Count > 0)
{
entity4FileList.ForEach(x => {
x.FormID = ID;
x.LogID = logId;
});
var fileIds = db.Insertable(entity4FileList).ExecuteReturnSnowflakeIdList();
if (fileIds == null || fileIds.Count != entity4FileList.Count)
{
db.RollbackTran();
return false;
}
}
db.CommitTran();
return true;
}
catch
{
db.RollbackTran();
throw;
}
}
}
///
/// 暂停
///
public bool Pause(long CorpID,long ID,string Note,List entity4FileList, long UpdateUserID, DateTime UpdateTime)
{
using (var 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.eTaskStatus.Paused)
{
db.RollbackTran();
return false;
}
if (entity.FormStatus < (int)Entity.Repair.eTaskStatus.Started)
{
db.RollbackTran();
return false;
}
if (entity.FormStatus >= (int)Entity.Repair.eTaskStatus.Finished)
{
db.RollbackTran();
return false;
}
var bol = db.Updateable()
.SetColumns(x => x.FormStatus == (int)Entity.Repair.eTaskStatus.Paused)
.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.RepairTaskLog();
entity4Log.CorpID = CorpID;
entity4Log.FormID = ID;
entity4Log.OperateType = (int)Entity.Repair.eTaskOperateType.Pause;
entity4Log.OperateContent = "暂停";
entity4Log.OperateTime = UpdateTime;
entity4Log.OperateUserID = UpdateUserID;
entity4Log.OperateNote = Note;
var logId = db.Insertable(entity4Log).ExecuteReturnSnowflakeId();
if (logId < 1)
{
db.RollbackTran();
return false;
}
if (entity4FileList != null && entity4FileList.Count > 0)
{
entity4FileList.ForEach(x => {
x.FormID = ID;
x.LogID = logId;
});
var fileIds = db.Insertable(entity4FileList).ExecuteReturnSnowflakeIdList();
if (fileIds == null || fileIds.Count != entity4FileList.Count)
{
db.RollbackTran();
return false;
}
}
db.CommitTran();
return true;
}
catch
{
db.RollbackTran();
throw;
}
}
}
///
/// 重新开工
///
public bool Restart(long CorpID,long ID,string Note,List entity4FileList, long UpdateUserID, DateTime UpdateTime)
{
using (var 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.eTaskStatus.Paused)
{
db.RollbackTran();
return false;
}
var bol = db.Updateable()
.SetColumns(x => x.FormStatus == (int)Entity.Repair.eTaskStatus.Restarted)
.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.RepairTaskLog();
entity4Log.CorpID = CorpID;
entity4Log.FormID = ID;
entity4Log.OperateType = (int)Entity.Repair.eTaskOperateType.Restart;
entity4Log.OperateContent = "重新开工";
entity4Log.OperateTime = UpdateTime;
entity4Log.OperateUserID = UpdateUserID;
entity4Log.OperateNote = Note;
var logId = db.Insertable(entity4Log).ExecuteReturnSnowflakeId();
if (logId < 1)
{
db.RollbackTran();
return false;
}
if (entity4FileList != null && entity4FileList.Count > 0)
{
entity4FileList.ForEach(x => {
x.FormID = ID;
x.LogID = logId;
});
var fileIds = db.Insertable(entity4FileList).ExecuteReturnSnowflakeIdList();
if (fileIds == null || fileIds.Count != entity4FileList.Count)
{
db.RollbackTran();
return false;
}
}
db.CommitTran();
return true;
}
catch
{
db.RollbackTran();
throw;
}
}
}
///
/// 完工
///
public bool Finish(long CorpID,long ID, string Note, List entity4FileList, long UpdateUserID, DateTime UpdateTime)
{
using (var 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.eTaskStatus.Started&&entity.FormStatus!=(int)Entity.Repair.eTaskStatus.Restarted)
{
db.RollbackTran();
return false;
}
var bol = db.Updateable()
.SetColumns(x => x.FormStatus == (int)Entity.Repair.eTaskStatus.Finished)
.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.RepairTaskLog();
entity4Log.CorpID = CorpID;
entity4Log.FormID = ID;
entity4Log.OperateType = (int)Entity.Repair.eTaskOperateType.Finish;
entity4Log.OperateContent = "完工";
entity4Log.OperateTime = UpdateTime;
entity4Log.OperateUserID = UpdateUserID;
entity4Log.OperateNote = Note;
var logId = db.Insertable(entity4Log).ExecuteReturnSnowflakeId();
if (logId < 1)
{
db.RollbackTran();
return false;
}
if (entity4FileList != null && entity4FileList.Count > 0)
{
entity4FileList.ForEach(x => {
x.FormID = ID;
x.LogID = logId;
});
var fileIds = db.Insertable(entity4FileList).ExecuteReturnSnowflakeIdList();
if (fileIds == null || fileIds.Count != entity4FileList.Count)
{
db.RollbackTran();
return false;
}
}
db.CommitTran();
return true;
}
catch
{
db.RollbackTran();
throw;
}
}
}
///
/// 验收通过
///
public bool Success(long CorpID, long ID, string Note, long UpdateUserID, DateTime UpdateTime)
{
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
{
try
{
db.BeginTran();
var bol = db.Updateable()
.SetColumns(x => x.FormStatus == (int)Entity.Repair.eTaskStatus.Succeed)
.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.RepairTaskLog();
entity4Log.CorpID = CorpID;
entity4Log.FormID = ID;
entity4Log.OperateType = (int)Entity.Repair.eTaskOperateType.Success;
entity4Log.OperateContent = "验收通过";
entity4Log.OperateTime = UpdateTime;
entity4Log.OperateUserID = UpdateUserID;
entity4Log.OperateNote = Note;
var logId = db.Insertable(entity4Log).ExecuteReturnSnowflakeId();
if (logId < 1)
{
db.RollbackTran();
return false;
}
db.CommitTran();
return true;
}
catch
{
db.RollbackTran();
throw;
}
}
}
///
/// 获取模糊列表
///
public List> GetFluzzyList
(
long CorpID,
string BelongType,
long? BelongID,
long? ProductID,
long? RepairUserID,
int? FormStatus,
int? Urgency,
string FormNo,
DateTime? StartTime,
DateTime? EndTime
)
{
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(ProductID.HasValue, (x, y) => x.ProductID == ProductID);
exp.AndIF(RepairUserID.HasValue, (x, y) => x.RepairUserID.Value == RepairUserID.Value);
exp.AndIF(FormStatus.HasValue, (x, y) => x.FormStatus == FormStatus.Value);
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);
using (var db = new SqlSugarClient(ConnectionConfig))
{
var list = db.Queryable()
.LeftJoin((x, y) => x.RequestID == y.ID)
.Where(exp.ToExpression())
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.Select((x, y) => new { x, y })
.ToList();
return list?.Select(x => new Tuple(x.x, x.y)).ToList();
}
}
///
/// 获取分页列表
///
public List> GetPageList
(
long CorpID,
string BelongType,
long? BelongID,
long? ProductID,
long? RepairUserID,
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, 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(ProductID.HasValue, (x, y) => x.ProductID == ProductID);
exp.AndIF(RepairUserID.HasValue, (x, y) => x.RepairUserID.Value == RepairUserID.Value);
exp.AndIF(FormStatus.HasValue, (x, y) => x.FormStatus == FormStatus.Value);
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);
using (var db = new SqlSugarClient(ConnectionConfig))
{
var list= db.Queryable()
.LeftJoin((x, y) => x.RequestID == y.ID)
.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();
}
}
///
/// 获取进行中的分页列表
///
public List> GetProgressPageList
(
long CorpID,
string BelongType,
long? BelongID,
long? ProductID,
long? RepairUserID,
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(ProductID.HasValue, (x, y) => x.ProductID == ProductID);
exp.AndIF(RepairUserID.HasValue, (x, y) => x.RepairUserID.Value == RepairUserID.Value);
exp.And((x,y)=>x.FormStatus>=(int)Entity.Repair.eTaskStatus.Received&&x.FormStatus<(int)Entity.Repair.eTaskStatus.Finished);
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);
using (var db = new SqlSugarClient(ConnectionConfig))
{
var list = db.Queryable()
.LeftJoin((x, y) => x.RequestID == y.ID)
.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();
}
}
///
/// 获取已完成的分页列表
///
public List> GetHasFinishedPageList
(
long CorpID,
string BelongType,
long? BelongID,
long? ProductID,
long? RepairUserID,
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(ProductID.HasValue, (x, y) => x.ProductID == ProductID);
exp.AndIF(RepairUserID.HasValue, (x, y) => x.RepairUserID.Value == RepairUserID.Value);
exp.And((x, y) => x.FormStatus >= (int)Entity.Repair.eTaskStatus.Finished );
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);
using (var db = new SqlSugarClient(ConnectionConfig))
{
var list = db.Queryable()
.LeftJoin((x, y) => x.RequestID == y.ID)
.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();
}
}
}
}