166 lines
6.2 KiB
C#
166 lines
6.2 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Globalization;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Infrastructure.Attribute;
|
|||
|
|
using JinianNet.JNTemplate;
|
|||
|
|
using Model.DBModel;
|
|||
|
|
using Quartz;
|
|||
|
|
using SqlSugar;
|
|||
|
|
using SqlSugar.IOC;
|
|||
|
|
using ZR.Model.MES.pro;
|
|||
|
|
using ZR.Model.MES.pro.DTO;
|
|||
|
|
using ZR.Model.MES.wms;
|
|||
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|||
|
|
|
|||
|
|
namespace ZR.Tasks.TaskScheduler
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 毛坯库存调试定时扣除
|
|||
|
|
///
|
|||
|
|
/// </summary>
|
|||
|
|
[AppService(ServiceType = typeof(Job_Blank), ServiceLifetime = LifeTime.Scoped)]
|
|||
|
|
public class Job_Blank : JobBase, IJob
|
|||
|
|
{
|
|||
|
|
//private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|||
|
|
|
|||
|
|
public async Task Execute(IJobExecutionContext context)
|
|||
|
|
{
|
|||
|
|
await ExecuteJob(context, async () => await Run());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task Run()
|
|||
|
|
{
|
|||
|
|
await Task.Delay(1);
|
|||
|
|
//TODO 业务逻辑
|
|||
|
|
DateTime nowTime = DateTime.Now;
|
|||
|
|
List<ProWorkorder_v2> proWorkorders = GetDodayWorkOrder();
|
|||
|
|
if (proWorkorders.Count > 0)
|
|||
|
|
{
|
|||
|
|
int num = 0;
|
|||
|
|
foreach (var item in proWorkorders)
|
|||
|
|
{
|
|||
|
|
using (var _db = DbScoped.SugarScope)
|
|||
|
|
{
|
|||
|
|
_db.Ado.BeginTran();
|
|||
|
|
var predicate = Expressionable
|
|||
|
|
.Create<WmBlankInventory>()
|
|||
|
|
.And(it => it.BlankNum == item.BlankNumber)
|
|||
|
|
.And(it => it.Type == (item.Remark1.Contains("返工件") ? 2 : 1))
|
|||
|
|
.ToExpression();
|
|||
|
|
WmBlankInventory wmBlankInventory = _db.Queryable<WmBlankInventory>()
|
|||
|
|
.Where(predicate)
|
|||
|
|
.First();
|
|||
|
|
if (wmBlankInventory != null)
|
|||
|
|
{
|
|||
|
|
int newQuantity = wmBlankInventory.Quantity ?? 0 - item.PreviousNumber;
|
|||
|
|
_db.Updateable(wmBlankInventory).SetColumns(it => it.Quantity == newQuantity);
|
|||
|
|
WmBlankRecord res =
|
|||
|
|
new()
|
|||
|
|
{
|
|||
|
|
Id = SnowFlakeSingle.Instance.NextId().ToString(),
|
|||
|
|
FkBlankInventoryId = wmBlankInventory.Id,
|
|||
|
|
BlankNum = wmBlankInventory.BlankNum,
|
|||
|
|
ChangeQuantity = item.PreviousNumber,
|
|||
|
|
Type = 2,
|
|||
|
|
Status = 1,
|
|||
|
|
ActionTime = nowTime,
|
|||
|
|
Remark =
|
|||
|
|
"自动添加备注:工单主键"
|
|||
|
|
+ item.Id
|
|||
|
|
+ ",上件数"
|
|||
|
|
+ item.PreviousNumber
|
|||
|
|
+ "每日调试自动毛坯出库。",
|
|||
|
|
CreatedBy = "系统",
|
|||
|
|
CreatedTime = nowTime
|
|||
|
|
};
|
|||
|
|
//添加库存记录
|
|||
|
|
_db.Insertable(res).ExecuteCommand();
|
|||
|
|
_db.Ado.CommitTran();
|
|||
|
|
num++;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("调试工单毛坯号异常");
|
|||
|
|
_db.Ado.RollbackTran();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
Console.WriteLine("毛坯调试自动出库数:" + num);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("无调试工单");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取今日带调试的工单列表
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
private List<ProWorkorder_v2> GetDodayWorkOrder()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var today = DateTime.Now;
|
|||
|
|
int year = today.Year;
|
|||
|
|
int week = GetWeekOfYear(today);
|
|||
|
|
int date = ConvertDayOfWeekToCustomFormat(today.DayOfWeek);
|
|||
|
|
using (var _db = DbScoped.SugarScope)
|
|||
|
|
{
|
|||
|
|
var predicate = Expressionable
|
|||
|
|
.Create<ProWorkorder_v2>()
|
|||
|
|
.And(it => it.Year == year)
|
|||
|
|
.And(it => it.Week == week)
|
|||
|
|
.And(it => it.Date == date)
|
|||
|
|
.And(it => it.Remark2.Contains("调试"))
|
|||
|
|
.And(it => it.Remark3 == "是")
|
|||
|
|
.ToExpression();
|
|||
|
|
return _db.Queryable<ProWorkorder_v2>()
|
|||
|
|
.Where(predicate)
|
|||
|
|
.OrderBy(it => it.Sort)
|
|||
|
|
.ToList();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取今天是本年的第几周
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="date"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
static int GetWeekOfYear(DateTime date)
|
|||
|
|
{
|
|||
|
|
// 使用ISO 8601标准计算一年中的第几周
|
|||
|
|
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(
|
|||
|
|
date,
|
|||
|
|
CalendarWeekRule.FirstFourDayWeek, // 每年的第一周必须至少有4天属于该年
|
|||
|
|
DayOfWeek.Monday
|
|||
|
|
); // 设置一周的第一天为星期一
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取星期几的数字(7为周日)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="dayOfWeek"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
static int ConvertDayOfWeekToCustomFormat(DayOfWeek dayOfWeek)
|
|||
|
|
{
|
|||
|
|
// 将 DayOfWeek 枚举值转换为自定义格式:周一为1,周日为7
|
|||
|
|
if (dayOfWeek == DayOfWeek.Sunday)
|
|||
|
|
{
|
|||
|
|
return 7;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return (int)dayOfWeek + 1;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|