zhuangpei-mesbackend/DOAN.Service/MES/product/ProWorkorderService.cs
qianhao.xu 4a15a0babb 调整
2024-07-17 09:05:53 +08:00

225 lines
7.5 KiB
C#

using System;
using SqlSugar;
using Infrastructure.Attribute;
using DOAN.Model;
using DOAN.Model.MES.product;
using DOAN.Model.MES.product.Dto;
using DOAN.Repository;
using DOAN.Service.MES.product.IService;
using System.Linq;
using SqlSugar.Extensions;
using MimeKit.Tnef;
namespace DOAN.Service.MES.product
{
/// <summary>
/// 生产工单Service业务层处理
/// </summary>
[AppService(ServiceType = typeof(IProWorkorderService), ServiceLifetime = LifeTime.Transient)]
public class ProWorkorderService : BaseService<ProWorkorder>, IProWorkorderService
{
/// <summary>
/// 查询生产工单列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<ProWorkorderDto> GetList(ProWorkorderQueryDto parm)
{
if (parm.WorkorderDate != null && parm.WorkorderDate.Length > 0)
{
parm.WorkorderDate[0] = parm.WorkorderDate[0].Date;
parm.WorkorderDate[1] = parm.WorkorderDate[1].Date;
}
var predicate = Expressionable.Create<ProWorkorder>()
.AndIF(!string.IsNullOrEmpty(parm.ProductionName), it => it.ProductionName.Contains(parm.ProductionName))
.AndIF(!string.IsNullOrEmpty(parm.ProductionCode), it => it.ProductionCode.Contains(parm.ProductionCode))
.AndIF(!string.IsNullOrEmpty(parm.CustomCode), it => it.CustomCode.Contains(parm.CustomCode))
.AndIF(parm.WorkorderDate != null && parm.WorkorderDate[0] > DateTime.MinValue, it => it.WorkorderDate >= parm.WorkorderDate[0])
.AndIF(parm.WorkorderDate != null && parm.WorkorderDate[1] > DateTime.MinValue, it => it.WorkorderDate <= parm.WorkorderDate[1])
.AndIF(parm.Year > 0, it => it.Year == parm.Year)
.AndIF(parm.Week > 0, it => it.Week == parm.Week)
.AndIF(parm.Date > 0, it => it.Date == parm.Date)
;
var response = Queryable()
.Where(predicate.ToExpression())
.ToPage<ProWorkorder, ProWorkorderDto>(parm);
return response;
}
/// <summary>
/// 获取详情
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public ProWorkorder GetInfo(string Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
return response;
}
/// <summary>
/// 添加生产工单
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public ProWorkorder AddProWorkorder(ProWorkorder model)
{
model.Id = SnowFlakeSingle.Instance.NextId().ToString();
return Context.Insertable(model).ExecuteReturnEntity();
}
/// <summary>
/// 修改生产工单
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int UpdateProWorkorder(ProWorkorder model)
{
var response = Update(w => w.Id == model.Id, it => new ProWorkorder()
{
ProductionName = model.ProductionName,
ProductionCode = model.ProductionCode,
CustomCode = model.CustomCode,
DeliveryNum = model.DeliveryNum,
Unit = model.Unit,
PackageNum = model.PackageNum,
Sort = model.Sort,
WorkorderDate = model.WorkorderDate,
Year = model.Year,
Week = model.Week,
Date = model.Date,
Priority = model.Priority,
Status = model.Status,
Remark = model.Remark,
CreatedBy = model.CreatedBy,
CreatedTime = model.CreatedTime,
UpdatedBy = model.UpdatedBy,
UpdatedTime = model.UpdatedTime,
});
return response;
}
/// <summary>
/// 生成工单号
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public int Generate_workorder(ProWorkorderQueryDto2 parm)
{
DateTime update_time = parm.WorkorderDate.Date;
List<ProWorkorder> proWorkorderList = Context.Queryable<ProWorkorder>().Where(it => it.WorkorderDate == update_time)
.Where(it => it.Status == 1)
.OrderBy(it => it.Sort).ToList();
if (proWorkorderList != null && proWorkorderList.Count() > 0)
{
string baseSort = update_time.ToString("yyyyMMdd");
int index = 1;
foreach (ProWorkorder item in proWorkorderList)
{
item.Workorder = baseSort + index.ToString("000");
item.Sort = index*10;
index++;
}
}
return Context.Updateable(proWorkorderList).ExecuteCommand();
}
/// <summary>
/// 插入工单
/// </summary>
/// <param name="proWorkorder"></param>
/// <param name="next_id"></param>
/// <returns></returns>
public int Insert_workOrder(ProWorkorder proWorkorder, string next_id)
{
int result = 0;
proWorkorder.Id = SnowFlakeSingle.Instance.NextId().ToString();
proWorkorder.WorkorderDate = proWorkorder.WorkorderDate.Value.Date;
UseTran2(() =>
{
int sortNum = Context.Queryable<ProWorkorder>().Where(it => it.Id == next_id).Select(it => it.Sort.Value).First();
Context.Updateable<ProWorkorder>()
.Where(it => it.WorkorderDate == proWorkorder.WorkorderDate)
.Where(it => it.Sort >= sortNum)
.SetColumns(it => new ProWorkorder() { Sort = it.Sort + 1 })
.ExecuteCommand();
proWorkorder.Sort = sortNum;
Context.Insertable(proWorkorder).ExecuteCommand();
});
Generate_workorder(new ProWorkorderQueryDto2()
{
WorkorderDate = proWorkorder.WorkorderDate.Value
});
return result;
}
/// <summary>
/// 移动工单移动工单
/// </summary>
/// <param name="id"></param>
/// <param name="type"></param>
/// <returns></returns>
public int MoveWorkorder(string id, int type)
{
int result = 0;
ProWorkorder toMove = Context.Queryable<ProWorkorder>().Where(it => it.Id == id).First();
var pervious = Context.Queryable<ProWorkorder>()
.Where(it => it.WorkorderDate == toMove.WorkorderDate);
//上移动
if (type == 1)
{
pervious = pervious.Where(it => it.Sort <= toMove.Sort).OrderByDescending(it => it.Sort);
}
//下移
else if (type == 2)
{
pervious = pervious.Where(it => it.Sort >= toMove.Sort).OrderBy(it => it.Sort);
}
ProWorkorder exchange = pervious.Skip(1).Take(1).First();
if (exchange != null)
{
int temp = toMove.Sort.Value;
toMove.Sort = exchange.Sort;
exchange.Sort = temp;
result += Context.Updateable(toMove).ExecuteCommand();
result += Context.Updateable(exchange).ExecuteCommand();
}
return result;
}
}
}