308 lines
11 KiB
C#
308 lines
11 KiB
C#
using Infrastructure;
|
||
using Infrastructure.Attribute;
|
||
using Microsoft.AspNetCore.Hosting;
|
||
using MiniExcelLibs;
|
||
using Model.DBModel;
|
||
using SqlSugar;
|
||
using System;
|
||
using System.IO;
|
||
using ZR.Model.mes.pro;
|
||
using ZR.Model.MES.pro.DTO;
|
||
using ZR.Model.MES.wms;
|
||
using ZR.Service.mes.pro.IService;
|
||
|
||
namespace ZR.Service.mes.pro
|
||
{
|
||
|
||
[AppService(ServiceType = typeof(IProWorkplanServiceV2), ServiceLifetime = LifeTime.Transient)]
|
||
public class ProWorkplanServiceV2 : BaseService<ProWorklplan_v2>, IProWorkplanServiceV2
|
||
{
|
||
|
||
public (List<ProWorklplanDto>, int) GetAllData(int pageNum, int pageSize, int year, int week, string partNumber, string color)
|
||
{
|
||
var predicate = Expressionable.Create<ProWorklplan_v2>()
|
||
.AndIF(year > 0, it => it.Year == year)
|
||
.AndIF(week > 0, it => it.Week == week)
|
||
.AndIF(!string.IsNullOrEmpty(partNumber), it => it.Partnumber.Contains(partNumber))
|
||
.AndIF(!string.IsNullOrEmpty(color), it => it.ColorCode.Contains(color))
|
||
.ToExpression();
|
||
int totalCount = 0;
|
||
List<ProWorklplan_v2> proWorkplanList = Context.Queryable<ProWorklplan_v2>()
|
||
.Where(predicate)
|
||
.OrderBy(it => it.Id)
|
||
.ToPageList(pageNum, pageSize, ref totalCount);
|
||
// 计划正确性检查
|
||
List<ProWorklplanDto> proWorkplanDtoList = new List<ProWorklplanDto>();
|
||
foreach (ProWorklplan_v2 item in proWorkplanList)
|
||
{
|
||
ProWorklplanDto plan = new()
|
||
{
|
||
Id = item.Id,
|
||
Week = item.Week,
|
||
Year = item.Year,
|
||
BlankNum = item.BlankNum,
|
||
Partnumber = item.Partnumber,
|
||
ProductName = item.ProductName,
|
||
Specification = item.Specification,
|
||
ColorCode = item.ColorCode,
|
||
ProductionBeat = item.ProductionBeat,
|
||
ProductTime = item.ProductTime,
|
||
NoSchedule = item.NoSchedule,
|
||
QualificationRate = item.QualificationRate,
|
||
RequireHanger = item.RequireHanger,
|
||
AllHangerNum = item.AllHangerNum,
|
||
EveryHangerNum = item.EveryHangerNum,
|
||
RequireNum = item.RequireNum,
|
||
TurnNumber = item.TurnNumber,
|
||
Remark = item.Remark,
|
||
CreatedBy = item.CreatedBy,
|
||
CreatedTime = item.CreatedTime,
|
||
UpdatedBy = item.UpdatedBy,
|
||
UpdatedTime = item.UpdatedTime,
|
||
};
|
||
plan.State = CheckWorkPlan(item);
|
||
proWorkplanDtoList.Add(plan);
|
||
}
|
||
|
||
return (proWorkplanDtoList, totalCount);
|
||
}
|
||
|
||
|
||
public List<ProWorklplan_v2> GetProWorkplanById(string id)
|
||
{
|
||
return Context.Queryable<ProWorklplan_v2>().Where(it => it.Id == id).ToList();
|
||
}
|
||
|
||
|
||
|
||
public int AddWorkPlan(ProWorklplan_v2 proWorkplan)
|
||
{
|
||
proWorkplan.Id = "MP" + DateTime.Now.ToString("yyyyMMdd") + Getworkplanid_max().ToString("000");
|
||
proWorkplan.Remark = "手动插入";
|
||
return Context.Insertable(proWorkplan).ExecuteCommand();
|
||
}
|
||
/// <summary>
|
||
/// 获取生产计划id
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
private int Getworkplanid_max()
|
||
{
|
||
|
||
ProWorklplan_v2 max_workplan = Context.Queryable<ProWorklplan_v2>().OrderBy(it => it.Id, OrderByType.Desc).First();
|
||
if (max_workplan != null && !string.IsNullOrEmpty(max_workplan.Id) && max_workplan.Id.Substring(2, 8) == DateTime.Now.ToString("yyyyMMdd"))
|
||
{
|
||
int num = Convert.ToInt32(max_workplan.Id.Substring(10)) + 1;
|
||
return num;
|
||
}
|
||
else
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
|
||
}
|
||
|
||
// 修改
|
||
public int UpdateWorkPlan(ProWorklplan_v2 proWorkplan)
|
||
{
|
||
|
||
|
||
|
||
return Context.Updateable<ProWorklplan_v2>()
|
||
.SetColumns(it => new ProWorklplan_v2()
|
||
{
|
||
BlankNum = proWorkplan.BlankNum,
|
||
Partnumber = proWorkplan.Partnumber,
|
||
ProductName = proWorkplan.ProductName,
|
||
Specification = proWorkplan.Specification,
|
||
ColorCode = proWorkplan.ColorCode,
|
||
RequireNum = proWorkplan.RequireNum,
|
||
EveryHangerNum = proWorkplan.EveryHangerNum,
|
||
ProductionBeat = proWorkplan.ProductionBeat,
|
||
AllHangerNum = proWorkplan.AllHangerNum,
|
||
TurnNumber = proWorkplan.TurnNumber,
|
||
ProductTime = proWorkplan.ProductTime,
|
||
RequireHanger = proWorkplan.RequireHanger,
|
||
})
|
||
.Where(it => it.Id == proWorkplan.Id)
|
||
.ExecuteCommand();
|
||
}
|
||
|
||
public int DeleteWorkPlan(string id)
|
||
{
|
||
return Context.Deleteable<ProWorklplan_v2>().In(id).ExecuteCommand();
|
||
}
|
||
|
||
|
||
public string ImportExceldata(List<ProWorklplan_v2> worklplanList)
|
||
{
|
||
int max_id = Getworkplanid_max();
|
||
worklplanList.ForEach(it =>
|
||
{
|
||
it.Id = "MP" + DateTime.Now.ToString("yyyyMMdd") + max_id.ToString("000");
|
||
it.Remark = "Excel导入";
|
||
max_id++;
|
||
});
|
||
|
||
var x = Context.Storageable(worklplanList)
|
||
.SplitUpdate(it => it.Any())//存在更新
|
||
.SplitInsert(it => true)//否则插入(更新优先级大于插入)
|
||
.WhereColumns(it => new { it.Year, it.Week, it.Partnumber })//如果不是主键可以这样实现(多字段it=>new{it.x1,it.x2})
|
||
.ToStorage();
|
||
|
||
x.AsInsertable.ExecuteCommand();//插入可插入部分;
|
||
x.AsUpdateable.IgnoreColumns(it => it.Id).ExecuteCommand();//存在更新
|
||
|
||
string msg = string.Format(" 插入{0} 更新{1} 错误数据{2} 不计算数据{3} 删除数据{4} 总共{5}",
|
||
x.InsertList.Count,
|
||
x.UpdateList.Count,
|
||
x.ErrorList.Count,
|
||
x.IgnoreList.Count,
|
||
x.DeleteList.Count,
|
||
x.TotalList.Count);
|
||
|
||
|
||
return msg; //插入可插入部分
|
||
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 下载
|
||
/// </summary>
|
||
/// <param name="year"></param>
|
||
/// <param name="week"></param>
|
||
/// <returns></returns>
|
||
public (string, string) ExportExceldata(int year, int week)
|
||
{
|
||
//1.0 读取表数据
|
||
var list = Queryable().Where(it => it.Year == year && it.Week == week).ToList();
|
||
|
||
//2.0 保存为excel
|
||
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
|
||
string sFileName = $"{year}年{week}周计划-{DateTime.Now:MM-dd-HHmmss}.xlsx";
|
||
|
||
|
||
string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "export", sFileName);
|
||
|
||
|
||
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
|
||
|
||
|
||
|
||
var Sheet1 = new
|
||
{
|
||
year = year,
|
||
week = week,
|
||
title = $"{year}年车镜实业涂装事业{week}周生产滚动表",
|
||
|
||
workplan = list
|
||
};
|
||
string templatePath = Path.Combine(webHostEnvironment.WebRootPath, "ImportTemplate", "周计划标准模板1.xlsx");
|
||
MiniExcel.SaveAsByTemplate(fullPath, templatePath, Sheet1);
|
||
// MiniExcel.SaveAs(fullPath, list);
|
||
|
||
|
||
//3.0 返回路径和文件名
|
||
return (sFileName, fullPath);
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
public List<ProWorkorder> GetWorkorderListByPlanId(string id)
|
||
{
|
||
return Context.Queryable<ProWorkorder>().Where(it => it.FkProPlanId == id).OrderBy("priority desc ").ToList();
|
||
}
|
||
|
||
public List<ProWorkorder> GetWorkorderListById(string id)
|
||
{
|
||
return Context.Queryable<ProWorkorder>().Where(it => it.Id == id).ToList();
|
||
}
|
||
|
||
public int AddWorkorder(ProWorkorder proWorkorder)
|
||
{
|
||
|
||
return Context.Insertable(proWorkorder).ExecuteCommand();
|
||
}
|
||
|
||
public int UpdateWorkorder(ProWorkorder proWorkorder)
|
||
{
|
||
return Context.Updateable(proWorkorder).ExecuteCommand();
|
||
}
|
||
|
||
public int DeleteWorkorder(string id)
|
||
{
|
||
return Context.Deleteable<ProWorkorder>().In(id).ExecuteCommand();
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 周计划汇总
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
public WorkplanSummaryDto GetWeekSummary(int year, int week)
|
||
{
|
||
return Context.Queryable<ProWorklplan_v2>().Where(it => it.Year == year && it.Week == week)
|
||
.GroupBy(it => new { it.Week })
|
||
.Select(it => new WorkplanSummaryDto
|
||
{
|
||
requireNum = SqlFunc.AggregateSum(it.RequireNum),
|
||
requireHanger = SqlFunc.AggregateSum(it.RequireHanger),
|
||
turnnum = SqlFunc.AggregateSum(it.TurnNumber),
|
||
productiontime = SqlFunc.AggregateSum(it.ProductTime),
|
||
|
||
})
|
||
.First();
|
||
|
||
|
||
|
||
}
|
||
/// <summary>
|
||
/// 删除周计划全部
|
||
/// </summary>
|
||
/// <param name="week"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
public int DeleteAllWorkPlan(int year, int week)
|
||
{
|
||
return Context.Deleteable<ProWorklplan_v2>().Where(it => it.Year == year && it.Week == week).ExecuteCommand();
|
||
}
|
||
|
||
public int CheckWorkPlan(ProWorklplan_v2 proWorkplan)
|
||
{
|
||
try
|
||
{
|
||
if (string.IsNullOrEmpty(proWorkplan.Partnumber))
|
||
{
|
||
return 0;
|
||
}
|
||
WmMaterial material = Context.Queryable<WmMaterial>()
|
||
.Where(it => it.Partnumber == proWorkplan.Partnumber)
|
||
.Where(it => it.Status == 1)
|
||
.First();
|
||
// 物料号不存在
|
||
if (material == null)
|
||
{
|
||
return 1;
|
||
}
|
||
// 规格与颜色异常
|
||
if (material.Specification != proWorkplan.Specification || material.Color != proWorkplan.ColorCode)
|
||
{
|
||
return 2;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new Exception(ex.Message);
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|