using System; using System.Collections.Generic; using System.Data.Entity.Migrations; using System.Linq; using System.Text; using System.Threading.Tasks; using CK.SCP.Models; using CK.SCP.Models.Enums; using CK.SCP.Models.ScpEntity; using CK.SCP.Models.ScpEntity.ExcelImportEntity; using CK.SCP.Utils; using System.Data.Entity.Core; using CK.SCP.Models.ScpEntity.ExcelExportEnttity; using System.Text.RegularExpressions; using System.Threading; using System.Data; using Newtonsoft.Json; using CK.SCP.Models.UniApiEntity; using CK.SCP.Models.AppBoxEntity; namespace CK.SCP.Controller { public class SCP_MPO_CONTROLLER { /// /// 获取一般材料订单 /// /// /// public static void Get_V_TB_MPO_List(V_TB_MPO p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_MPO; if (p_entity.DocDateBegin != DateTime.MinValue) { q = q.Where(p => p.DocDate >= p_entity.DocDateBegin); } if (p_entity.DocDateEnd != DateTime.MinValue) { q = q.Where(p => p.DocDate <= p_entity.DocDateEnd); } if (!string.IsNullOrEmpty(p_entity.PurdocNO)) { q = q.Where(p => p.PurdocNO == p_entity.PurdocNO.Trim()); } if (p_entity.UserInVendIds != null && p_entity.UserInVendIds.Count > 0) { q = q.Where(p => p_entity.UserInVendIds.Contains(p.VendorNO)); } _ret.State = ReturnStatus.Succeed; _ret.Result = q; p_action(_ret); } } 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); }); } _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_List", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_List", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_List", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(e); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_List", e.Message); throw e; } } /// /// 获取一般材料订单明细 /// /// /// public static void Get_V_TB_MPO_DETAIL_List(V_TB_MPO_DETAIL p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_TB_MPO_DETAIL; if (p_entity.purdocnolist != null && p_entity.purdocnolist.Count > 0) { q = q.Where(p => p_entity.purdocnolist.Contains(p.PurdocNO)); } _ret.State = ReturnStatus.Succeed; _ret.Result = q; p_action(_ret); } } 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); }); } _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_DETAIL_List", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_DETAIL_List", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_DETAIL_List", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; _ret.ErrorList.Add(e); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Get_V_TB_MPO_DETAIL_List", e.Message); throw e; } } /// /// 一般材料订单明细列表 /// /// PurdocNOlist /// public static List GetMPODetailList(List p_lst) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { return db.V_TB_MPO_DETAIL.Where(p => p_lst.Contains(p.PurdocNO) && p.Ctype != "D").ToList(); } } /// /// 作废传接口 /// /// /// public static ResultObject Save_ts_uni_api(List ls) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { { List lineError = new List(); List ErrorList = new List(); foreach (var po in ls) { var po1 = db.TB_MATERIALORDERS.FirstOrDefault(t => t.PurdocNO == po && t.Ctype != "D"); po1.AcceptStatus = (int)GeneralMaterialOrderState.Finish; db.TB_MATERIALORDERS.AddOrUpdate(po1); var po2 = db.TB_MATERIALORDERS_DETAIL.Where(t => t.PurdocNO == po && t.Ctype != "D").ToList(); foreach (var po3 in po2) { po3.AcceptStatus = (int)GeneralMaterialOrderState.Finish; db.TB_MATERIALORDERS_DETAIL.AddOrUpdate(po3); } var lx = db.TS_UNI_API.Where(t => t.BillNum == po && t.Receiver == "I").ToList(); foreach (var po4 in lx) { po4.State = 1; po4.Receiver = "D"; db.TS_UNI_API.AddOrUpdate(po4); } } } if (_ret.MessageList.Count == 0) { int state = db.SaveChanges(); if (state != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; } } } 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); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Save_PO_TO_ASK", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Save_PO_TO_ASK", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Save_PO_TO_ASK", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_PO_CONTROLLER), "Save_PO_TO_ASK", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } /// /// 保存状态 /// /// /// /// public static ResultObject Save_MPO_STATE(List p_list, GeneralMaterialOrderState p_state) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { switch (p_state) { case GeneralMaterialOrderState.Finish: var orblist = db.TB_MATERIALORDERS.Where(q => p_list.Contains(q.PurdocNO)).ToList(); var ctylist = orblist.Select(q => q.Ctype).ToList(); if (ctylist.Contains("D")) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "存在已经作废的数据,请重新选择!"; _ret.MessageList.Add(_ret.Message); return _ret; } var acclist = orblist.Select(q => q.AcceptStatus).ToList(); if (acclist.Contains((int)GeneralMaterialOrderState.Finish)) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "存在已经确认的数据,请重新选择!"; _ret.MessageList.Add(_ret.Message); return _ret; } foreach (var item in p_list) { var tbMater = db.TB_MATERIALORDERS.FirstOrDefault(q => q.PurdocNO == item); tbMater.AcceptStatus = (int)GeneralMaterialOrderState.Finish; var tbMaterList = db.TB_MATERIALORDERS_DETAIL.Where(q => q.PurdocNO == item).ToList(); tbMaterList.ForEach(q => { q.AcceptStatus = (int)GeneralMaterialOrderState.Finish; }); db.TB_MATERIALORDERS.AddOrUpdate(itm1 => itm1.UID, tbMater); db.TB_MATERIALORDERS_DETAIL.AddOrUpdate(itm => itm.UID, tbMaterList.ToArray()); } break; case GeneralMaterialOrderState.New: var MAblist = db.TB_MATERIALORDERS.Where(q => p_list.Contains(q.PurdocNO)).ToList(); var ctypelist = MAblist.Select(q => q.Ctype).ToList(); if (ctypelist.Contains("D")) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "存在已经作废的数据,请重新选择!"; _ret.MessageList.Add(_ret.Message); return _ret; } var acceplist = MAblist.Select(q => q.AcceptStatus).ToList(); if (acceplist.Contains((int)GeneralMaterialOrderState.New)) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "存在已经待确认的数据,请重新选择!"; _ret.MessageList.Add(_ret.Message); return _ret; } foreach (var item in p_list) { var tbMater = db.TB_MATERIALORDERS.FirstOrDefault(q => q.PurdocNO == item); tbMater.AcceptStatus = (int)GeneralMaterialOrderState.New; var tbMaterList = db.TB_MATERIALORDERS_DETAIL.Where(q => q.PurdocNO == item).ToList(); tbMaterList.ForEach(q => { q.AcceptStatus = (int)GeneralMaterialOrderState.New; q.UpdateTime = DateTime.Now.ToString(); }); db.TB_MATERIALORDERS.AddOrUpdate(itm1 => itm1.UID, tbMater); db.TB_MATERIALORDERS_DETAIL.AddOrUpdate(itm => itm.UID, tbMaterList.ToArray()); } break; } if (db.SaveChanges() != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; return _ret; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "数据更新失败!"; _ret.MessageList.Add(_ret.Message); return _ret; } } } 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); }); } _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(dbEx); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MPO_CONTROLLER), "Save_MPO_STATE", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MPO_CONTROLLER), "Save_MPO_STATE", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.ErrorList.Add(ex); LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MPO_CONTROLLER), "Save_MPO_STATE", ex.ToString()); if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException)) { var inner = (UpdateException)ex.InnerException; throw new ScpException(ResultCode.Exception, "0000", ex.ToString()); } else { if (ex.InnerException != null) throw ex.InnerException; } } catch (Exception e) { _ret.State = ReturnStatus.Failed; LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_MPO_CONTROLLER), "Save_MPO_STATE", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); } return _ret; } /// /// 获取材料订单主表数据 /// /// /// public static TB_MATERIALORDERS GetTbMaterialorders(string PurdocNO) { TB_MATERIALORDERS MATERIALORDERS = new TB_MATERIALORDERS(); using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { MATERIALORDERS = db.TB_MATERIALORDERS.FirstOrDefault(q => q.PurdocNO == PurdocNO); } return MATERIALORDERS; } /// /// 获取材料订单明细表数据 /// /// /// public static List GetTbMaterialordersDetailList(string PurdocNO) { List MATERIALORDERS = new List(); using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { MATERIALORDERS = db.TB_MATERIALORDERS_DETAIL.Where(q => q.PurdocNO == PurdocNO).ToList(); } return MATERIALORDERS; } /// /// 打印数据 /// /// /// public static DataSet MPO_REPORT(string PurdocNO) { DataSet ds = new DataSet(); TB_MATERIALORDERS order = GetTbMaterialorders(PurdocNO); V_MPO_PRINT print = new V_MPO_PRINT(); print.PURDOCNO = order.PurdocNO; print.DOCDATE = order.DocDate.ToString("yyyy-MM-dd"); print.PHONE = order.PHONE; print.FAXNUM = order.FAXNUM; print.PURGRPDESC = order.PurgrpDesc; print.VendorNO = order.VendorNO; print.FIRSTTELNO = order.FIRSTTELNO; print.STAGEDESC = order.STAGEDESC; TA_VENDER vender = GET_TA_VENDER(order.VendorNO); if (vender != null) { print.CAAIFax = vender.VendName; print.CurrentUserAdress = vender.Address; print.RESPONSIBLESALESPERSON = vender.Contacter; print.SupplierFax = vender.Fax; } List detailList = GetTbMaterialordersDetailList(PurdocNO); List lsDetail = new List(); decimal SUM = 0.00M; detailList.ForEach(p => { V_MPO_PRINT_DETAIL _detail = new V_MPO_PRINT_DETAIL(); _detail.PURCHASEREQNO = p.PurchaseReqNO; _detail.PROPOSERDESC = p.ProposerDesc; _detail.MTLNO = p.MtlNO; _detail.SHORTTEXT = p.Shorttext; _detail.SIZECOL = p.SizeCol; _detail.UNITDES = p.Unitdes; _detail.QUANTITY = p.Quantity.ToString("#.####"); _detail.PRICE = p.PRICE.ToString("#.######"); _detail.NETVALUE = p.NetValue.ToString("#.######"); _detail.PURINFORECORD = p.PurinfoReCord; _detail.ITEMDELIVERYDATE = p.ItemDeliveryDate.ToString("yyyy-MM-dd"); _detail.GLACCTNODES = p.Glaccnum; _detail.COSTCENTERDES = p.CostCenterDes; lsDetail.Add(_detail); SUM += p.NetValue; }); print.Total = SUM.ToString("F6"); var dt = ConvertHelper.ToDataTable(new List { print }); ds.Tables.Add(dt); var dt1 = ConvertHelper.ToDataTable(lsDetail); ds.Tables.Add(dt1); return ds; } /// /// 获取打印相关的供应商信息 /// /// /// public static TA_VENDER GET_TA_VENDER (string VENDID) { TA_VENDER _VENDER = new TA_VENDER(); using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { _VENDER = db.TA_VENDER.FirstOrDefault(q => q.VendId == VENDID); } return _VENDER; } } }