shgx_tz_mom/ZR.Service/mes/pro/ProWorkorderServiceV2.cs
2024-01-20 14:37:32 +08:00

200 lines
7.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
using ZR.Model.MES.pro.DTO;
using ZR.Model.MES.qu;
using ZR.Model.MES.pro;
using Model.DBModel;
using Infrastructure;
using Microsoft.AspNetCore.Hosting;
using MiniExcelLibs;
using System.IO;
using SqlSugar.Extensions;
namespace ZR.Service.mes.pro
{
[AppService(ServiceType = typeof(IProWorkorderServiceV2), ServiceLifetime = LifeTime.Transient)]
public class ProWorkorderServiceV2 : BaseService<ProWorkorder_v2>, IProWorkorderServiceV2
{
public (List<ProWorkorder_v2>, int) GetWorkorderList(int pageNum, int pageSize, int year, int week, int date, int isSchedule)
{
var predicate = Expressionable.Create<ProWorkorder_v2>()
.AndIF(year > 0, it => it.Year == year)
.AndIF(week > 0, it => it.Week == week)
.AndIF(date > 0, it => it.Date == date)
.ToExpression();
int totalCount = 0;
List<ProWorkorder_v2> proWorkorderList = Context.Queryable<ProWorkorder_v2>().Where(predicate).OrderBy(it => it.Id).ToPageList(pageNum, pageSize, ref totalCount);
return (proWorkorderList, totalCount);
}
/// <summary>
/// 获取生产计划id
/// </summary>
/// <returns></returns>
private int Getworkplanid_max()
{
ProWorkorder_v2 max_workoder = Context.Queryable<ProWorkorder_v2>().OrderBy(it => it.Id, OrderByType.Desc).First();
if (max_workoder != null && !string.IsNullOrEmpty(max_workoder.Id) && max_workoder.Id.Substring(2, 8) == DateTime.Now.ToString("yyyyMMdd"))
{
int num = Convert.ToInt32(max_workoder.Id.Substring(10)) + 1;
return num;
}
else
{
return 0;
}
}
public string ImportExceldata(List<ProWorkorder_v2> workorderList)
{
int max_id = Getworkplanid_max();
// 更新生产计划
if (workorderList != null && workorderList.Count > 0)
{
workorderList.ForEach(it =>
{
it.Id = "WO" + DateTime.Now.ToString("yyyyMMdd") + max_id.ToString("000");
it.Remark3 = "Excel导入";
max_id++;
});
foreach (ProWorkorder_v2 item in workorderList)
{
ProWorklplan_v2 planItem = Context.Queryable<ProWorklplan_v2>().Where(it => it.Id == item.FinishedPartNumber).First();
if (planItem != null)
{
Context.Updateable<ProWorklplan_v2>().Where(it => it.Id == planItem.Id)
.SetColumns(it =>
it.NoSchedule == planItem.RequireNum - item.hangNumber
).ExecuteCommandAsync();
}
}
UseTran(() =>
{
// 删除之前的工单
Context.Deleteable<ProWorkorder_v2>().Where(it => it.Year == workorderList[0].Year)
.Where(it => it.Week == workorderList[0].Week)
.Where(it => it.Date == workorderList[0].Date).ExecuteCommand();
//插入
Context.Insertable(workorderList).ExecuteCommand();
});
return "success";
}
//var x = Context.Storageable(workorderList)
// .SplitUpdate(it => it.Any())//存在更新
// .SplitInsert(it => true)//否则插入(更新优先级大于插入)
// .WhereColumns(it => new { it.Year, it.Week, it.Date, it.FinishedPartNumber })//如果不是主键可以这样实现多字段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 ""; //插入可插入部分
}
/// <summary>
/// 下载
/// </summary>
/// <param name="year"></param>
/// <param name="week"></param>
/// <param name="date"></param>
/// <returns></returns>
public (string, string) ExportExceldata(int year, int week, int date)
{
//1.0 读取表数据
var list = Queryable().Where(it => it.Year == year && it.Week == week && it.Date == date).ToList();
//2.0 保存为excel
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
string sFileName = $"{year}年{week}周{date}日计划-{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,
date=date,
title = $"{year}年车镜实业涂装事业{week}周{date}生产滚动表",
workorder = list
};
string templatePath = Path.Combine(webHostEnvironment.WebRootPath, "ImportTemplate", "日生产计划模板1.xlsx");
MiniExcel.SaveAsByTemplate(fullPath, templatePath, Sheet1);
// MiniExcel.SaveAs(fullPath, list);
//3.0 返回路径和文件名
return (sFileName, fullPath);
}
/// <summary>
/// 删除周计划全部
/// </summary>
/// <param name="week"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public int DeleteAllWorkorder(int year, int week, int date)
{
return Context.Deleteable<ProWorkorder_v2>().Where(it => it.Year == year && it.Week == week && it.Date == date).ExecuteCommand();
}
}
}