using Infrastructure.Attribute; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using SqlSugar; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using ZR.Common; using ZR.Model.mes.md; using ZR.Model.mes.pro; using ZR.Model.MES.wm; using ZR.Service.mes.pro.IService; using ZR.Service.MES.md.IService; using static System.Net.WebRequestMethods; using JinianNet.JNTemplate; using static Aliyun.OSS.Model.LiveChannelStat; namespace ZR.Service.mes.pro { [AppService(ServiceType = typeof(IProWorkorderService), ServiceLifetime = LifeTime.Transient)] public class ProWorkorderService : BaseService, IProWorkorderService { public (List, int) GetWorkorderList(int pageNum, int pageSize, int year, int week, int date, int isSchedule) { var predicate = Expressionable.Create() .AndIF(year > 0, it => it.Year == year) .AndIF(week > 0, it => it.Week == week) .AndIF(date > 0, it => it.Date == date) .AndIF(isSchedule == 0, it => it.Wrokerorder_status.Equals(isSchedule)) .AndIF(isSchedule >= 1, it => it.Wrokerorder_status >= 1) .ToExpression(); int totalCount = 0; List proWorkorderList = Context.Queryable().Where(predicate).OrderBy(it => it.Order).ToPageList(pageNum, pageSize, ref totalCount); return (proWorkorderList, totalCount); } public int SetWorkorderSechedule(string id, DateTime arrange_starttime, DateTime arrange_endtime) { //获取排序最大值 if (CacheHelper.GetCache("workorder_id_max") == null) { int? workorder_id_max = Context.Queryable().OrderBy(it => it.Order, OrderByType.Desc).First().Order; //初次 if (workorder_id_max == null || workorder_id_max == 0) { workorder_id_max = 1; } CacheHelper.SetCache("workorder_id_max", workorder_id_max); } CacheHelper.SetCache("workorder_id_max", (int)CacheHelper.GetCache("workorder_id_max") + 1); TimeSpan productionTime = arrange_endtime - arrange_starttime; return Context.Updateable() .SetColumns(it => new ProWorkorder() { ArrangeStarttime = arrange_starttime, ArrangeEndtime = arrange_endtime, Wrokerorder_status = 1, ProductionTime = (decimal)productionTime.TotalMinutes, Order = (int)CacheHelper.GetCache("workorder_id_max") }) .Where(it => it.Id.Equals(id)) .ExecuteCommand(); } public int ResetWorkorderSechedule(string id) { return Context.Updateable() .SetColumns(it => it.Wrokerorder_status == 0) .SetColumns(it => it.Order == 0) .Where(it => it.Id.Equals(id)) .ExecuteCommand(); } public int SortWorkorderSchedule(string id, int order) { return Context.Updateable() .SetColumns(it => it.Order == order) .Where(it => it.Id.Equals(id)) .ExecuteCommand(); } public int ReleaseProduction(string id, HttpContext context) { int result = Context.Updateable() .SetColumns(it => it.Wrokerorder_status == 2) .Where(it => it.Id.Equals(id)) .ExecuteCommand(); ProWorkorder workorder = Context.Queryable().Where(it => it.Id == id).First(); if (result == 1) { // 生成领料单 // 若没有此零件号的领料单,就新增,否则,修改累加 bool isExit = Context.Queryable().Where(it => it.WorkorderId == workorder.Id).Any(); if (!isExit) { // 新增 领料单 WmMaterialrequisition materialrequisition = new WmMaterialrequisition(); materialrequisition.CreatedBy = context.User?.Identity?.Name; materialrequisition.CreatedTime = DateTime.Now; materialrequisition.Year = workorder.Year; materialrequisition.Week = workorder.Week; materialrequisition.Date = workorder.Date; materialrequisition.Id = DateTime.Now.ToString("yyyyMMddHHmmss"); materialrequisition.Requirenum = workorder.Actualnumber; materialrequisition.Workblankpartnumber = workorder.Workblankpartnumber; materialrequisition.Status = "0"; Context.Insertable(materialrequisition).ExecuteCommandAsync(); // 新增工单-领料关联表 WmWorkorderMr mr = new WmWorkorderMr(); mr.Id = DateTime.Now.ToString("yyyyMMddHHmmss"); mr.WorkorderId = workorder.Id; mr.MaterialrequisitionId = materialrequisition.Id; mr.CreatedBy = context.User?.Identity?.Name; mr.CreatedTime = DateTime.Now; Context.Insertable(mr).ExecuteCommandAsync(); } else { UseTran(() => { //查询当前工单下的领料数量 List wm = Context.Queryable() .LeftJoin((m, mr) => m.Id == mr.WorkorderId) .LeftJoin((m, mr, r) => mr.MaterialrequisitionId == r.Id) .Where(m => m.Id == id) .Select((m, mr, r) => r) .ToList(); wm[0].Requirenum += workorder.Actualnumber; wm[0].UpdatedTime = DateTime.Now; wm[0].UpdatedBy = context.User?.Identity?.Name; //更新领料单 Context.Updateable(wm[0]); }); } } return result; } } }