using System; using System.Collections; using System.Collections.Generic; using System.Data.Entity.Migrations; using System.Linq; using System.Text; using System.Text.RegularExpressions; 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; namespace CK.SCP.Controller { public class SCP_INFO_CONTROLLER { public static void Get_V_INFO_List(V_INFO p_entity,Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_INFO; if (p_entity.ID != 0) { q = q.Where(p => p.ID == p_entity.ID); } if (!string.IsNullOrEmpty(p_entity.FactoryCode)) { q = q.Where(p => p.FactoryCode.Contains(p_entity.FactoryCode)); } if (!string.IsNullOrEmpty(p_entity.InfoType)) { q = q.Where(p => p.InfoType.Contains(p_entity.InfoType)); } if (!string.IsNullOrEmpty(p_entity.FactoryName)) { q = q.Where(p => p.FactoryName.Contains(p_entity.FactoryName)); } if (!string.IsNullOrEmpty(p_entity.SupplierCode)) { if (!string.IsNullOrEmpty(p_entity.UserName)) { q = q.Where(p => p.SupplierCode.Contains(p_entity.SupplierCode) || p.UserName.Contains(p_entity.UserName)); } else { q = q.Where(p => p.SupplierCode.Contains(p_entity.SupplierCode)); } } else { if (!string.IsNullOrEmpty(p_entity.UserName)) { q = q.Where(p => p.UserName.Contains(p_entity.UserName)); } } //var _time = DateTime.Now.AddMonths(-1); //q = q.Where(p => p.AddTime > _time); if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (!string.IsNullOrEmpty(p_entity.Title)) { q = q.Where(p => p.Title.Contains(p_entity.Title)); } if (!string.IsNullOrEmpty(p_entity.Content)) { q = q.Where(p => p.Content.Contains(p_entity.Content)); } if (!string.IsNullOrEmpty(p_entity.Enable)) { q = q.Where(p => p.Enable.Contains(p_entity.Enable)); } //if (p_entity.UserInAddress != null && p_entity.UserInAddress.Count > 0) //{ // q = q.Where(p => p_entity.UserInAddress.Contains(p.FactoryCode)); //} //if (p_entity.UserInVendIds!= null && p_entity.UserInVendIds.Count > 0) //{ // q = q.Where(p => p_entity.UserInVendIds.Contains(p.SupplierCode)); //} _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_INFO_CONTROLLER), "Get_V_INFO_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_INFO_CONTROLLER), "Get_V_INFO_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_INFO_CONTROLLER), "Get_V_INFO_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_INFO_CONTROLLER), "Get_V_INFO_List", e.Message); throw e; } } public static void Get_V_INFO_New(V_INFO p_entity, Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable q = db.V_INFO; if (p_entity.ID != 0) { q = q.Where(p => p.ID == p_entity.ID); } if (!string.IsNullOrEmpty(p_entity.FactoryCode)) { q = q.Where(p => p.FactoryCode.Contains(p_entity.FactoryCode)); } if (!string.IsNullOrEmpty(p_entity.InfoType)) { q = q.Where(p => p.InfoType.Contains(p_entity.InfoType)); } if (!string.IsNullOrEmpty(p_entity.FactoryName)) { q = q.Where(p => p.FactoryName.Contains(p_entity.FactoryName)); } if (!string.IsNullOrEmpty(p_entity.SupplierCode)) { q = q.Where(p => p.SupplierCode.Contains(p_entity.SupplierCode)); } if (!string.IsNullOrEmpty(p_entity.VendName)) { q = q.Where(p => p.VendName.Contains(p_entity.VendName)); } if (!string.IsNullOrEmpty(p_entity.Title)) { q = q.Where(p => p.Title.Contains(p_entity.Title)); } if (!string.IsNullOrEmpty(p_entity.Content)) { q = q.Where(p => p.Content.Contains(p_entity.Content)); } if (!string.IsNullOrEmpty(p_entity.UserName)) { q = q.Where(p => p.UserName.Contains(p_entity.UserName)); } if (!string.IsNullOrEmpty(p_entity.Enable)) { q = q.Where(p => p.Enable.Contains(p_entity.Enable)); } //if (p_entity.UserInAddress != null && p_entity.UserInAddress.Count > 0) //{ // q = q.Where(p => p_entity.UserInAddress.Contains(p.Address)); //} _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_INFO_CONTROLLER), "Get_V_INFO_New", 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_INFO_CONTROLLER), "Get_V_INFO_New", 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_INFO_CONTROLLER), "Get_V_INFO_New", 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_INFO_CONTROLLER), "Get_V_INFO_New", e.Message); throw e; } } public static ResultObject SaveInfo(TA_INFO model) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { db.TA_Info.AddOrUpdate(model); if (db.SaveChanges() != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } else { _ret.State = ReturnStatus.Failed; _ret.Result = false; _ret.Message = "数据更新失败!"; } } } 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_INFO_CONTROLLER), "SaveInfo", 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_INFO_CONTROLLER), "SaveInfo", 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_INFO_CONTROLLER), "SaveInfo", 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_INFO_CONTROLLER), "SaveInfo", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } public static bool UpdateInfo(TA_INFO model) { try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { db.TA_Info.AddOrUpdate(p => p.ID, model); db.SaveChanges(); } } 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); }); } LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_INFO_CONTROLLER), "UpdateInfo", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_INFO_CONTROLLER), "UpdateInfo", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_INFO_CONTROLLER), "UpdateInfo", 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) { LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_INFO_CONTROLLER), "UpdateInfo", e.Message); throw e; } return true; } public static TA_INFO GetlistByBillNo(int id) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { return db.TA_Info.SingleOrDefault(p => p.ID == id); } } public static V_INFO GetInfoByBillNo(int id) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { return db.V_INFO.SingleOrDefault(p => p.ID == id); } } public static void DeleteById(int id) { try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var info = db.TA_Info.SingleOrDefault(p => p.ID == id); if (info != null) db.TA_Info.Remove(info); db.SaveChanges(); } } 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); }); } LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_INFO_CONTROLLER), "DeleteById", sb.ToString()); throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString()); } catch (OptimisticConcurrencyException ex)//并发冲突异常 { LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_INFO_CONTROLLER), "DeleteById", ex.ToString()); throw new ScpException(ResultCode.Exception, "9999", ex.ToString()); } catch (ScpException ex) { LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_INFO_CONTROLLER), "DeleteById", 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) { LogHelper.Writlog(LogHelper.LogType.Error, typeof(SCP_INFO_CONTROLLER), "DeleteById", e.Message); throw e; } } } }