using System; using System.Collections.Generic; using System.Data.Entity.Migrations; using System.Linq; using CK.SCP.Models; using CK.SCP.Models.Enums; using CK.SCP.Models.ScpEntity; using CK.SCP.Models.UniApiEntity; using CK.SCP.Utils; using CK.SCP.Controller; using System.Text; using System.Data.Entity.Core; namespace CK.SCP.GrupUniApi.Controller { public static class OdbcApiQadController { public static void GetNewCtrlList() { UniApiEntities idb = EntitiesFactory.CreateUniApiInstance(); idb.Database.CommandTimeout = 60 * 20; var tableNameList = EnumHelper.EnumToList(); // var qadCtrlList = idb.xxqad_ctrl.Where(p => p.xxqad_domain == ScpCache.Config.QAD域 && p.xxqad_scm == "0").ToList(); var qadCtrlList = idb.xxqad_ctrl.Where(p => p.xxqad_scm == "0" ).ToList(); foreach (var qadCtrl in qadCtrlList) { if (tableNameList.All(p => p.Name != qadCtrl.xxqad_table)) continue; try { ScpEntities wdb = EntitiesFactory.CreateScpInstance(); QadTableName qadTableName; Enum.TryParse(qadCtrl.xxqad_table, false, out qadTableName); switch (qadTableName) { case QadTableName.xxqad_pt_mstr: //物料 if (GlobalVar.ApiConfig.Qad零件) { Console.WriteLine($"发现 QAD 接口数据:{qadCtrl.xxqad_table}:{qadCtrl.xxqad_table_qty}"); UpdateParts(idb, wdb, qadCtrl.xxqad_seq); } else continue; break; case QadTableName.xxqad_vd_mstr: //供应商 if (GlobalVar.ApiConfig.Qad供应商) { Console.WriteLine($"发现 QAD 接口数据:{qadCtrl.xxqad_table}:{qadCtrl.xxqad_table_qty}"); UpdateVender(idb, wdb, qadCtrl.xxqad_seq); } else continue; break; case QadTableName.xxqad_vp_mstr: //供应商物料 if (GlobalVar.ApiConfig.Qad供应商零件) { Console.WriteLine($"发现 QAD 接口数据:{qadCtrl.xxqad_table}:{qadCtrl.xxqad_table_qty}"); UpdateVendParts(idb, wdb, qadCtrl.xxqad_seq); } else continue; break; case QadTableName.xxqad_pod_det: //采购订单 if (GlobalVar.ApiConfig.Qad采购订单) { Console.WriteLine($"发现 QAD 接口数据:{qadCtrl.xxqad_table}:{qadCtrl.xxqad_table_qty}"); UpdatePurchaseOrders(idb, wdb, qadCtrl.xxqad_seq); } else continue; break; case QadTableName.xxqad_pprice_det: //采购价格 Console.WriteLine($"发现 QAD 接口数据:{qadCtrl.xxqad_table}:{qadCtrl.xxqad_table_qty}"); UpdatePrice(idb, wdb, qadCtrl.xxqad_seq); break; } qadCtrl.xxqad_scm = ((int)UniApiState.成功).ToString(); EntitiesFactory.SaveDb(wdb); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常 { var sb = new StringBuilder(); foreach (var error in dbEx.EntityValidationErrors.ToList()) { error.ValidationErrors.ToList().ForEach(i => { sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage); }); } qadCtrl.xxqad_scm = ((int)UniApiState.失败).ToString(); qadCtrl.xxqad_rmks += " SCM:" + sb.ToString(); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { qadCtrl.xxqad_scm = ((int)UniApiState.失败).ToString(); qadCtrl.xxqad_rmks += " SCM:" + ex.Message; } catch (Exception ex) { qadCtrl.xxqad_scm = ((int)UniApiState.失败).ToString(); qadCtrl.xxqad_rmks += " SCM:" + ex.Message; } finally { EntitiesFactory.SaveDb(idb); } } } /// /// 更新采购订单 /// /// /// /// private static void UpdatePurchaseOrders(UniApiEntities idb, ScpEntities wdb, string seq) { // var qadPoDetailList = idb.xxqad_pod_det.Where(p => p.xxqad_pod_seq == seq).ToList(); 原来的 xxqad_pod_modtype 1为大订单 var qadPoDetailList = idb.xxqad_pod_det.Where(p => p.xxqad_pod_seq == seq && p.xxqad_pod_scmread == "0").ToList(); var wmsPoList = new List(); var wmsPoDetailList = new List(); var qadPoNbrList = qadPoDetailList.Select(p => p.xxqad_pod_nbr.ToUpper()).DistinctBy(p => p); foreach (var qadPoNbr in qadPoNbrList) { var qadDataList = qadPoDetailList.Where(p => p.xxqad_pod_nbr == qadPoNbr).ToList(); if (qadDataList.Count == 0) { continue; } var firstqadData = qadDataList[0]; //var wmsPo = wdb.TF_PO.SingleOrDefault(p => p.ErpBillNum == qadPoNbr); var wmsPo = wdb.TB_PO.SingleOrDefault(p => p.ErpBillNum == qadPoNbr); if (wmsPo == null) { int temp; string strPoBillNum = SCP_BILLCODE_CONTROLLER.MakePOCode(); wmsPo = new TB_PO { PoBillNum = qadPoNbr,//订单号 ErpBillNum = qadPoNbr,//ERP订单号 VendId = firstqadData.xxqad_pod_vend,//供应商编号 ModType = Int32.TryParse(firstqadData.xxqad_pod_modtype, out temp) ? temp : (int?)null, //单据类型(1:日程单,2:离散订单) Contacter = firstqadData.xxqad_pod_attn,//联系人 Site = firstqadData.xxqad_pod_domain,//地点 //Buyer = firstqadData.xxqad_pod_buyer,//采购员 //BuyerPhone = firstqadData.xxqad_pod_buyer_phone,//采购员电话 State = (int)FormState.开放,//状态 Remark = firstqadData.xxqad_pod_site,//说明 CreateTime = firstqadData.xxqad_pod_createdt == null ? DateTime.Now : (DateTime)firstqadData.xxqad_pod_createdt,//创建日期 CreateUser = firstqadData.xxqad_pod_createur,//创建用户 //UpdateTime = firstqadData.xxqad_pod_updatedt,//更新日期 //UpdateUser = firstqadData.xxqad_pod_updateur,//更新用户 IsDeleted = false, GUID = System.Guid.NewGuid(), BeginTime = firstqadData.xxqad_pod_createdt,//创建日期 EndTime = firstqadData.xxqad_pod_due_date//收货结束时间 }; wmsPoList.Add(wmsPo); } wmsPo.Contacter = firstqadData.xxqad_pod_attn; wmsPo.Buyer = firstqadData.xxqad_pod_buyer; wmsPo.BuyerPhone = firstqadData.xxqad_pod_buyer_phone; wmsPo.UpdateTime = firstqadData.xxqad_pod_updatedt;//更新日期 wmsPo.UpdateUser = firstqadData.xxqad_pod_updateur;//更新用户 foreach (var qadData in qadDataList) { try { var lineNum = Convert.ToInt32(qadData.xxqad_pod_line); //var wmsPoDetail = // wdb.TF_PO_DETAIL.SingleOrDefault( // p => p.ErpBillNum == qadPoNbr && p.LineNum == lineNum); var wmsPoDetail = wdb.TB_PO_DETAIL.SingleOrDefault( p => p.PoBillNum == qadPoNbr && p.PoLine == lineNum); if (wmsPoDetail == null) { wmsPoDetail = new TB_PO_DETAIL { PoBillNum = qadPoNbr,//订单号 PoLine = lineNum,//订单行 IsDeleted = false, GUID = System.Guid.NewGuid() }; wmsPoDetailList.Add(wmsPoDetail); } wmsPoDetail.PartCode = qadData.xxqad_pod_part.ToUpper();//零件号-物料号 wmsPoDetail.PlanQty = qadData.xxqad_pod_qty_ord;//订货数量-采购量 //wmsPoDetail.ShippedQty = qadData.xxqad_pod_qty_rct;//已发数量-已收货量 //wmsPoDetail.ReceivedQty = qadData.xxqad_pod_qty_rct;//已收数量-已收货量 //wmsPoDetail.RejectQty = qadData.xxqad_pod_qty_rct;//已退数量-已收货量 wmsPoDetail.BeginTime = firstqadData.xxqad_pod_createdt;//收货开始时间 wmsPoDetail.EndTime = qadData.xxqad_pod_due_date;//收货结束时间 wmsPoDetail.PoUnit = qadData.xxqad_pod_um;//采购单位 wmsPoDetail.LocUnit = qadData.xxqad_pod_loc_um.ToUpper();//存储单位 wmsPoDetail.DockCode = qadData.xxqad_pod_loc; wmsPoDetail.Price = ScpCache.Config.项目名称 == ProjectName.重庆北汽模塑.ToString() ? 0 : qadData.xxqad_pod_price; wmsPoDetail.Currency = qadData.xxqad_pod_curr;//币种 wmsPoDetail.PackQty = qadData.xxqad_pod_qty_std;//标包数量 wmsPoDetail.UnConv = qadData.xxqad_pod_um_conv;//转换率 wmsPoDetail.DockCode = qadData.xxqad_pod_loc; //wmsPoDetail.DockCode = ?;// wmsPoDetail.State = Convert.ToInt32(qadData.xxqad_pod_status);//状态 wmsPoDetail.Remark = qadData.xxqad_pod_domain;//备注 wmsPoDetail.CreateTime = firstqadData.xxqad_pod_createdt == null ? DateTime.Now : (DateTime)firstqadData.xxqad_pod_createdt;//创建日期 wmsPoDetail.CreateUser = firstqadData.xxqad_pod_createur;//创建用户 wmsPoDetail.UpdateTime = firstqadData.xxqad_pod_updatedt;//更新日期 wmsPoDetail.UpdateUser = firstqadData.xxqad_pod_updateur;//更新用户 //if (wmsPoDetail.BillQty != 0 && wmsPoDetail.BillQty <= wmsPoDetail.ReceivedQty) // wmsPoDetail.State = (int)FormState.关闭; if (wmsPoDetail.PlanQty != 0 && wmsPoDetail.PlanQty <= wmsPoDetail.ReceivedQty) wmsPoDetail.State = (int)FormState.关闭; qadData.xxqad_pod_scmread = ((int)UniApiState.成功).ToString(); } catch (Exception ex) { qadData.xxqad_pod_scmread = ((int)UniApiState.失败).ToString(); qadData.xxqad_pod_rmks += " SCM:" + ex.Message; } qadData.xxqad_pod_updateur = ScpCache.Config.SCP用户名; qadData.xxqad_pod_updatedt = DateTime.Now; } wmsPo.State = qadDataList.All(p => p.xxqad_pod_status == ((int)FormState.关闭).ToString()) ? (int)FormState.关闭 : (int)FormState.开放; } wdb.TB_PO.AddOrUpdate(wmsPoList.ToArray()); wdb.TB_PO_DETAIL.AddOrUpdate(wmsPoDetailList.ToArray()); Console.WriteLine($"更新 采购订单 数据:{qadPoDetailList.Count}"); } /// /// 更新采购订单 /// /// /// /// private static void UpdatePrice(UniApiEntities idb, ScpEntities scpdb, string seq) { var _list = idb.xxqad_pprice_det.Where(p => p.xxqad_pprice_seq == seq && p.xxqad_pprice_scmread == "0").ToList(); var PriceList = new List(); foreach (var itm in _list) { var _price= new TB_PRICE { VendId = itm.xxqad_pprice_list, Unit = itm.xxqad_pprice_um, PartCode = itm.xxqad_pprice_part, Site = itm.xxqad_pprice_domain, Remarks = itm.xxqad_pprice_rmks, StartTime = itm.xxqad_pprice_start, EndTime = itm.xxqad_pprice_end, Curr = itm.xxqad_pprice_curr }; _price.Amt = itm.xxqad_pprice_amt; PriceList.Add(_price); } scpdb.TB_PRICE.AddOrUpdate(PriceList.ToArray()); Console.WriteLine($"更新采购价格数据:{PriceList.Count}"); } /// /// 更新供应商零件 /// /// /// /// private static void UpdateVendParts(UniApiEntities idb, ScpEntities wdb, string seq) { var qadDataList = idb.xxqad_vp_mstr.Where(p => p.xxqad_vp_seq == seq ).ToList(); var wmsDataList = new List(); foreach (var qadData in qadDataList) { try { if (string.IsNullOrEmpty(qadData.xxqad_vp_vend_part)) qadData.xxqad_vp_vend_part = qadData.xxqad_vp_part; var wmsData = wdb.TA_VEND_PART.SingleOrDefault( p => p.VendId == qadData.xxqad_vp_addr.ToUpper() && p.PartCode == qadData.xxqad_vp_part.ToUpper() && p.VendPartCode == qadData.xxqad_vp_vend_part.ToUpper() && p.Site==qadData.xxqad_vp_domain ) ?? new TA_VEND_PART { VendId = qadData.xxqad_vp_addr.ToUpper(),//供应商编号 PartCode = qadData.xxqad_vp_part.ToUpper(),//对应物料号 VendPartCode = qadData.xxqad_vp_vend_part.ToUpper(),//供应商物料号 VendPackQty = qadData.xxqad_vp_comment,//供应商物料号 //PoUnit = qadData.,//采购单位 State = 1,//状态(0:失效 1:有效) Remark = "", CreateTime = qadData.xxqad_vp_createdt == null ? DateTime.Now : (DateTime)qadData.xxqad_vp_createdt,//创建日期 CreateUser = qadData.xxqad_vp_createur,//创建用户 UpdateTime = qadData.xxqad_vp_updatedt,//更新日期 UpdateUser = qadData.xxqad_vp_updateur,//更新用户 TransportationTime=qadData.xxqad_vp_vend_lead, IsDeleted = false, GUID = new Guid() }; wmsData.Site = string.IsNullOrEmpty(wmsData.Site) ? qadData.xxqad_vp_domain : wmsData.Site; wmsData.VendPartCode = qadData.xxqad_vp_vend_part?.ToUpper() ?? qadData.xxqad_vp_part.ToUpper(); wmsData.VendPackQty = qadData.xxqad_vp_comment; wmsData.TransportationTime = qadData.xxqad_vp_vend_lead; wmsDataList.Add(wmsData); qadData.xxqad_vp_scmread = ((int)UniApiState.成功).ToString(); } catch (Exception ex) { qadData.xxqad_vp_scmread = ((int)UniApiState.失败).ToString(); qadData.xxqad_vp_rmks += " SCM:" + ex.Message; } qadData.xxqad_vp_updateur = ScpCache.Config.SCP用户名; qadData.xxqad_vp_updatedt = DateTime.Now; } wdb.TA_VEND_PART.AddOrUpdate(p=>new { p.PartCode,p.VendId, p.Site }, wmsDataList.ToArray()); Console.WriteLine($"更新 供应商零件 数据:{wmsDataList.Count}"); } /// /// 更新供应商 /// /// /// /// private static void UpdateVender(UniApiEntities idb, ScpEntities wdb, string seq) { var qadDataList = idb.xxqad_vd_mstr.Where(p => p.xxqad_vd_seq == seq ).ToList(); var wmsDataList = new List(); foreach (var qadData in qadDataList) { try { var wmsData = wdb.TA_VENDER.SingleOrDefault(p => p.VendId == qadData.xxqad_vd_addr.ToUpper() && p.Site==qadData.xxqad_vd_domain) ?? new TA_VENDER { VendId = qadData.xxqad_vd_addr.ToUpper(), VendAbbCode = "0", State = 1 }; wmsData.VendName = string.IsNullOrEmpty(qadData.xxqad_vd_name)?qadData.xxqad_vd_addr:qadData.xxqad_vd_name; wmsData.VendType = qadData.xxqad_vd_type.ToUpper().ToString(); wmsData.Country = qadData.xxqad_vd_country; wmsData.City = qadData.xxqad_vd_city; wmsData.Currency = qadData.xxqad_vd_curr.ToUpper(); wmsData.Address = qadData.xxqad_vd_line1 + qadData.xxqad_vd_line2 + qadData.xxqad_vd_line3; wmsData.ZipCode = qadData.xxqad_vd_pst_id; wmsData.Contacter = qadData.xxqad_vd_attn; wmsData.Phone = qadData.xxqad_vd_phone; wmsData.Fax = qadData.xxqad_vd_fax; wmsData.Site = string.IsNullOrEmpty(wmsData.Site)? qadData.xxqad_vd_domain:wmsData.Site; wmsData.Tax = qadData.xxqad_vd_tax;//税率 wmsDataList.Add(wmsData); qadData.xxqad_vd_scmread = ((int)UniApiState.成功).ToString(); } catch (Exception ex) { qadData.xxqad_vd_scmread = ((int)UniApiState.失败).ToString(); qadData.xxqad_vd_rmks += " SCM:" + ex.Message; } qadData.xxqad_vd_updateur = ScpCache.Config.SCP用户名; qadData.xxqad_vd_updatedt = DateTime.Now; } if (wmsDataList.Count == 0) { Console.WriteLine("更新 供应商 数据:"+ seq+"无数据"); return; } wdb.TA_VENDER.AddOrUpdate(p=>new {p.VendId,p.Site } ,wmsDataList.ToArray()); Console.WriteLine($"更新 供应商 数据:{qadDataList.Count}"); } /// /// 更新零件 /// /// /// /// private static void UpdateParts(UniApiEntities idb, ScpEntities wdb, string seq) { var qadDataList = idb.xxqad_pt_mstr.Where(p => p.xxqad_pt_seq == seq ).ToList(); var wmsDataList = new List(); foreach (var qadData in qadDataList) { try { var wmsData = wdb.TA_PART.SingleOrDefault(p => p.ErpPartCode == qadData.xxqad_pt_part.ToUpper() && p.Site==qadData.xxqad_pt_domain) ?? new TA_PART { PartCode = qadData.xxqad_pt_part.ToUpper(), ErpPartCode = qadData.xxqad_pt_part.ToUpper(), }; wmsData.PartDesc1 = qadData.xxqad_pt_desc1; wmsData.PartDesc2 = qadData.xxqad_pt_desc2; wmsData.ProjectId = qadData.xxqad_pt_group.ToUpper(); wmsData.Unit = !string.IsNullOrEmpty(qadData.xxqad_pt_um)? qadData.xxqad_pt_um.ToUpper():"EA"; wmsData.PartGroup = qadData.xxqad_pt_dsgn_grp.ToUpper(); wmsData.State = qadData.xxqad_pt_status.ToUpper(); wmsData.Site = qadData.xxqad_pt_domain; wmsDataList.Add(wmsData); qadData.xxqad_pt_scmread = ((int)UniApiState.成功).ToString(); } catch (Exception ex) { qadData.xxqad_pt_scmread = ((int)UniApiState.失败).ToString(); qadData.xxqad_pt_rmks += " SCM:" + ex.Message; } qadData.xxqad_pt_updateur = ScpCache.Config.SCP用户名; qadData.xxqad_pt_updatedt = DateTime.Now; } wdb.TA_PART.AddOrUpdate(p =>new { p.PartCode, p.Site }, wmsDataList.ToArray()); Console.WriteLine($"更新 零件信息 数据:{qadDataList.Count}"); } } }