diff --git a/ICSSoft.FromERP/App.config b/ICSSoft.FromERP/App.config index 58b428b..eeb2f00 100644 --- a/ICSSoft.FromERP/App.config +++ b/ICSSoft.FromERP/App.config @@ -1,4 +1,4 @@ - + @@ -12,4 +12,4 @@ - + diff --git a/ICSSoft.FromERP/HttpHelper.cs b/ICSSoft.FromERP/HttpHelper.cs new file mode 100644 index 0000000..be0ad83 --- /dev/null +++ b/ICSSoft.FromERP/HttpHelper.cs @@ -0,0 +1,120 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net.Http; +using System.Net; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace ICS.Common.Helpers +{ + public class HttpHelper + { + #region Get请求 + ///// + ///// 发送http Get请求 + ///// + ///// + ///// + //public static string GetRequest(string url) + //{ + // HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; + // request.Method = "GET"; + // request.ContentType = "application/x-www-form-urlencoded";//链接类型 + // string result = GetResponseString(request.GetResponse() as HttpWebResponse); + // return result; + //} + /// + /// 从HttpWebResponse对象中提取响应的数据转换为字符串 + /// + /// + /// + public static string GetResponseString(HttpWebResponse webresponse) + { + using (Stream s = webresponse.GetResponseStream()) + { + StreamReader reader = new StreamReader(s, Encoding.UTF8); + return reader.ReadToEnd(); + } + } + #endregion + + + public static bool PingTest(string ip) + { + System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping(); + + System.Net.NetworkInformation.PingReply pingStatus = + ping.Send(IPAddress.Parse(ip), 1000); + + if (pingStatus.Status == System.Net.NetworkInformation.IPStatus.Success) + { + return true; + } + else + { + return false; + } + } + + public static bool WebRequestTest(string url) + { + try + { + System.Net.WebRequest myRequest = System.Net.WebRequest.Create(url); + System.Net.WebResponse myResponse = myRequest.GetResponse(); + } + catch (System.Net.WebException) + { + return false; + } + return true; + } + + /// + /// POST请求 + /// + /// 地址 + /// 请求json + /// token + /// + public static async Task HttpClientPost(string url, string requestJson, string contentType = "application/json", string token = "") where T : new() + { + + string result = string.Empty; + Uri postUrl = new Uri(url); + using (HttpContent httpContent = new StringContent(requestJson, System.Text.Encoding.UTF8, contentType)) + { + //使用注入的httpclientfactory获取client + using (var httpClient = new HttpClient()) + { + //设置请求头 + //设置超时时间 + if (!string.IsNullOrEmpty(token)) + httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); + httpClient.Timeout = new TimeSpan(0, 0, 60); + var response = httpClient.PostAsync(url, httpContent).Result; + + result = response.Content.ReadAsStringAsync().Result; + return JsonConvert.DeserializeObject(result); + } + } + + } + + public static async Task HttpClientGet(string url) where T : new() + { + using (HttpClient httpClient = new HttpClient()) + { + HttpResponseMessage res = httpClient.GetAsync(url).Result; + var t = res.Content.ReadAsStringAsync().Result; + return JsonConvert.DeserializeObject(t); + + }; + + } + } +} diff --git a/ICSSoft.FromERP/ICSAddStdWorkHourFromMES.cs b/ICSSoft.FromERP/ICSAddStdWorkHourFromMES.cs new file mode 100644 index 0000000..75a5c94 --- /dev/null +++ b/ICSSoft.FromERP/ICSAddStdWorkHourFromMES.cs @@ -0,0 +1,324 @@ +using ICS.Common.Helpers; +using Newtonsoft.Json; +using Quartz; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Security.Policy; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ICSSoft.FromERP +{ + public class ICSAddStdWorkHourFromMES : IJob + { + private static object key = new object(); + private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + public void Execute(IJobExecutionContext context) + { + try + { + lock (key) + { + log.Info("开始……………………………………………………………………"); + Execute(); + log.Info("结束……………………………………………………………………"); + } + } + catch (Exception ex) + { + log.Error(ex.ToString()); + } + } + + public async void Execute() + { + try + { + Configuration config = GetConfig(); + string url = config.ConnectionStrings.ConnectionStrings["APIAddStdWorkHour"].ConnectionString.ToString(); + log.Info("获取创建定额工时接口 " + url); + if (string.IsNullOrEmpty(url)) + { + return; + } + string conStr = ICSHelper.GetConnectString(); + string Namespace = this.GetType().Namespace; + List ErrorCode = new List(); + + // 业务逻辑 从u9那边料品关联工时表 找到没有工时数据的料品 再向mes这边根据料号找到数据 去匹配条件 计算出工时 给u9 + // 二 分别抓取整机、本体部、驱动部未统计的料号 本体部、驱动部逆向去找型号 + // 准备请求接口的数据集合 + var AddList = new List(); + #region 未统计的整机料号 + string sqls = @"select a.code,mf.Id as MFId,mfvb.ValueModel,mfvb.NominalDiameter,ISNULL(SpecialRequirement_ValveBodySpec,'') as SpecialRequirement from ERP.U9DB.dbo.CBO_ItemMaster a + join (select ROW_NUMBER() over(partition by ManufacturingSerial order by Id desc) as MFIdFlag,Id,ItemModel,ManufacturingSerial,TenantId from IcsManufacturingHead ) mf + on mf.MFIdFlag = 1 and a.code = mf.ManufacturingSerial --and mf.TenantId = '' + join IcsMfValveBodySpec mfvb on mf.Id = mfvb.MFId + left join ERP.U9DB.dbo.CA_StdWorkingHours b + on a.Id = b.ItemMaster + LEFT OUTER JOIN ERP.U9DB.dbo.Base_Organization AS oo + ON oo.ID = a.Org + where oo.Code = '01' and b.id is null and a.code like 'KA%' and mfvb.ValueModel is not null"; + DataTable dt = ICSHelper.ExecuteTable(conStr, sqls); + + if (dt != null) + { + log.Info("查询到整机待统计数量:" + dt.Rows.Count + "。"); + //准备整机条件 + string argsqls = @"select ItemModel,Type,NominalDiameter,Coefficient,ManHour,AdditiveManHour,ISNULL(SpecialRequirement,'') as SpecialRequirement + from IcsProductionDurationStatistics + where Type = 0 --and TenantId = ''"; + DataTable argdt = ICSHelper.ExecuteTable(conStr, argsqls); + if (argdt != null) + { + foreach (DataRow dr in dt.Rows) + { + // 整机条件过滤 阀门型号、口径、特殊要求 + DataRow[] infoArgRow = argdt?.Select("ItemModel='" + dr["ValueModel"].ToString() + + "' and SpecialRequirement = '" + dr["SpecialRequirement"].ToString() //特殊条件都为空 或者有特殊条件 + + "' and NominalDiameter = '" + dr["NominalDiameter"].ToString() + + "'"); + if (infoArgRow.Length > 0) + { + var Manhour = infoArgRow[0]["ManHour"].ToDecimal(); //本体基础工时 + var AdditiveManHour = infoArgRow[0]["AdditiveManHour"].ToDecimal();//追加工时 + var Coefficient = infoArgRow[0]["Coefficient"].ToDecimal();//系数 + AddList.Add(new AddStdWorkHourItem() + { + ORGCode = "01",//u9 Base_Organization表 的 code 字段 组织编码 + WorkHour = (Manhour + AdditiveManHour) * Coefficient, // 整机计算逻辑:(本体基础工时型号 + 追加工时) * 系数 + CreatedByCode = "", + DepartmentCode = "",//部门 + ItemCode = dr["code"].ToString() + }); + } + else + { + //记录未匹配到条件 统计失败的料号 + ErrorCode.Add(dr["code"].ToString()); + } + } + } + else + { + log.Info("整机待统计数量:" + dt.Rows.Count + "。但未查询到整机条件配置,已跳过生成。"); + } + } + else + { + log.Info("查询到整机待统计数量:0。"); + } + #endregion + + #region 未统计的本体部 + string vbssqls = @"select b.MFId,a.code,mfvb.ValueModel,mfvb.NominalDiameter,ISNULL(SpecialRequirement_ValveBodySpec,'') as SpecialRequirement from ERP.U9DB.dbo.CBO_ItemMaster a + join (select ROW_NUMBER() over(partition by PartItemCode order by MFId desc) as Flag,PartItemCode,MFId from ICSPartItem where PartItemName = '阀本体部') b -- and TenantId = '' + on a.code = b.PartItemCode and b.Flag = 1 + join IcsMfValveBodySpec mfvb on b.MFId = mfvb.MFId + left join ERP.U9DB.dbo.CA_StdWorkingHours c on a.Id = c.ItemMaster + LEFT OUTER JOIN ERP.U9DB.dbo.Base_Organization AS oo ON oo.ID = a.Org + where oo.Code = '01' and c.id is null and a.code like 'A2001%'"; + DataTable vbsdt = ICSHelper.ExecuteTable(conStr, vbssqls); + if (vbsdt != null) + { + log.Info("查询到本体部待统计数量:" + vbsdt.Rows.Count + "。"); + //准备本体部条件 ItemModel型号 驱动部类型时此字段为驱动型号 + string vbsargsqls = @"select ItemModel,Type,NominalDiameter,Coefficient,ManHour,AdditiveManHour,ISNULL(SpecialRequirement,'') as SpecialRequirement + from IcsProductionDurationStatistics + where Type = 1 --and TenantId = ''"; + DataTable vbsargdt = ICSHelper.ExecuteTable(conStr, vbsargsqls); + if (vbsargdt != null) + { + foreach (DataRow dr in vbsdt.Rows) + { + // 本体部条件过滤 阀门型号、口径、特殊条件 + DataRow[] infoVbsRow = vbsargdt?.Select("ItemModel='" + dr["ValueModel"].ToString() + + "' and SpecialRequirement = '" + dr["SpecialRequirement"].ToString() //特殊条件都为空 或者有特殊条件 + + "' and NominalDiameter = '" + dr["NominalDiameter"].ToString() + + "'"); + if (infoVbsRow.Length > 0) + { + var Manhour = infoVbsRow[0]["ManHour"].ToDecimal(); //本体基础工时 + var AdditiveManHour = infoVbsRow[0]["AdditiveManHour"].ToDecimal();//追加工时 + var Coefficient = infoVbsRow[0]["Coefficient"].ToDecimal();//系数 + AddList.Add(new AddStdWorkHourItem() + { + ORGCode = "01",//u9 Base_Organization表 的 code 字段 组织编码 + WorkHour = (Manhour + AdditiveManHour) * Coefficient, // 本体部计算逻辑:(本体基础工时型号 + 追加工时) * 系数 + CreatedByCode = "", + DepartmentCode = "",//部门 + ItemCode = dr["code"].ToString() + }); + } + else + { + //记录未匹配到条件 统计失败的料号 + ErrorCode.Add(dr["code"].ToString()); + } + } + } + else + { + log.Info("本体部待统计数量:" + vbsdt.Rows.Count + "。但未查询到本体部条件配置,已跳过生成。"); + } + } + else + { + log.Info("查询到本体部待统计数量:0。"); + } + #endregion + #region 未统计的驱动部 + string dssqls = @"select d.DriveModel,case when ISNULL(d.HandwheelMechanism,'NON') = 'NON' then 1 else 0 end as HandwheelMechanism,b.MFId,a.code + from ERP.U9DB.dbo.CBO_ItemMaster a + join (select ROW_NUMBER() over(partition by PartItemCode order by MFId desc) as Flag,PartItemCode,MFId from ICSPartItem where PartItemName = '驱动部') b + on a.code = b.PartItemCode + join IcsMfDriverSpec d on b.MFId = d.MFId + left join ERP.U9DB.dbo.CA_StdWorkingHours c on a.Id = c.ItemMaster + LEFT OUTER JOIN ERP.U9DB.dbo.Base_Organization AS oo ON oo.ID = a.Org + where oo.Code = '01' and c.id is null and a.code like 'A2002%''"; + DataTable dsdt = ICSHelper.ExecuteTable(conStr, dssqls); + if (dsdt != null) + { + log.Info("查询到驱动部待统计数量:" + dsdt.Rows.Count + "。"); + //准备驱动部条件 ItemModel型号 驱动部类型时此字段为驱动型号 + string dsArgsqls = @"select ItemModel,Type,Coefficient,ManHour,AdditiveManHour,HandwheelMechanism + from IcsProductionDurationStatistics + where Type = 2 --and TenantId = ''"; + DataTable dsArgdt = ICSHelper.ExecuteTable(conStr, dsArgsqls); + if (dsArgdt != null) + { + foreach (DataRow dr in vbsdt.Rows) + { + // 驱动部条件过滤 驱动型号、是否有手轮机构 + DataRow[] infoDsArgRow = dsArgdt?.Select("ItemModel='" + dr["DriveModel"].ToString() + + "' and HandwheelMechanism = '" + dr["HandwheelMechanism"].ToString() + + "'"); + if (infoDsArgRow.Length > 0) + { + var Manhour = infoDsArgRow[0]["ManHour"].ToDecimal(); //本体基础工时 + var AdditiveManHour = infoDsArgRow[0]["AdditiveManHour"].ToDecimal();//追加工时 + var Coefficient = infoDsArgRow[0]["Coefficient"].ToDecimal();//系数 + AddList.Add(new AddStdWorkHourItem() + { + ORGCode = "01",//u9 Base_Organization表 的 code 字段 组织编码 + WorkHour = (Manhour + AdditiveManHour) * Coefficient, // 驱动部计算逻辑:(本体基础工时型号 + 追加工时) * 系数 + CreatedByCode = "", + DepartmentCode = "",//部门 + ItemCode = dr["code"].ToString() + }); + } + else + { + //记录未匹配到条件 统计失败的料号 + ErrorCode.Add(dr["code"].ToString()); + } + } + } + else + { + log.Info("驱动部待统计数量:" + dsdt.Rows.Count + "。但未查询到驱动部条件配置,已跳过生成。"); + } + } + else + { + log.Info("查询到驱动部待统计数量:0。"); + } + #endregion + + if (ErrorCode.Any()) + { + log.Info("未匹配到统计条件的料号:" + string.Join(",", ErrorCode) + "。"); + } + log.Info("创建定额工时Req:" + JsonConvert.SerializeObject(AddList)); + var response = await HttpHelper.HttpClientPost(url, JsonConvert.SerializeObject(AddList)); + //返回成功后 用流水号填写 表头产品名称、产品编码、u9料号 + if (response != null) + { + if (response.IsOK) + { + log.Info("请求成功。" + response.Message); + } + else + { + log.Info("请求失败。" + response.Message); + } + } + else + { + log.Info("未拿到响应,请求失败。"); + } + + } + catch (Exception ex) + { + log.Error(ex.ToString()); + } + } + public static Configuration GetConfig() + { + Assembly assembly = Assembly.GetCallingAssembly(); + string path = string.Format("{0}.config", assembly.Location); + if (!File.Exists(path)) + { + throw new FileNotFoundException(path + "路径下的文件未找到!"); + } + try + { + ExeConfigurationFileMap configFile = new ExeConfigurationFileMap(); + configFile.ExeConfigFilename = path; + Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None); + return config; + } + catch (Exception) + { + throw; + } + } + } + /// + /// 创建定额工时接口入参 + /// + public class AddStdWorkHourItem + { + /// + /// 组织编码 + /// + public string ORGCode { get; set; } + /// + /// 料品编码 + /// + public string ItemCode { get; set; } + /// + /// 创建人编码 + /// + public string CreatedByCode { get; set; } + /// + /// 工时(单位:小时) + /// + public decimal WorkHour { get; set; } + /// + /// 部门编码 + /// + public string DepartmentCode { get; set; } + } + /// + /// 响应 + /// + public class AddStdWorkHourReq + { + /// + /// true:成功;false:失败 + /// + public bool IsOK { get; set; } + /// + /// 消息 + /// + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/ICSSoft.FromERP/ICSSoft.FromERP.csproj b/ICSSoft.FromERP/ICSSoft.FromERP.csproj index b775878..24123d7 100644 --- a/ICSSoft.FromERP/ICSSoft.FromERP.csproj +++ b/ICSSoft.FromERP/ICSSoft.FromERP.csproj @@ -1,5 +1,5 @@  - + Debug AnyCPU @@ -10,7 +10,7 @@ Properties ICSSoft.FromERP ICSSoft.FromERP - v4.0 + v4.8 512 @@ -20,6 +20,7 @@ + true @@ -29,6 +30,7 @@ DEBUG;TRACE prompt 4 + false pdbonly @@ -37,13 +39,14 @@ TRACE prompt 4 + false ..\..\..\智绿SRM\ICSSoft.SendMail.dll - - ..\..\..\50-job\Scheduler_4.0\Scheduler\JOB\log4net.dll + + ..\packages\log4net.2.0.17\lib\net45\log4net.dll ..\..\..\50-job\Scheduler_4.0\Scheduler\JOB\Newtonsoft.Json.DLL @@ -53,9 +56,45 @@ bin\Debug\Quartz.dll + + + ..\packages\System.IO.4.3.0\lib\net462\System.IO.dll + True + True + + + ..\packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll + True + True + + + ..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll + True + True + + + ..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net463\System.Security.Cryptography.Algorithms.dll + True + True + + + ..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll + True + True + + + ..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll + True + True + + + ..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll + True + True + @@ -66,6 +105,8 @@ + + @@ -115,6 +156,7 @@ Designer +