using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using CK.SCP.Models; using CK.SCP.Models.ScpEntity; using System.Data.Entity.Migrations; using CK.SCP.Utils; using CK.SCP.Models.Enums; using System.Data.Entity.Core; namespace CK.SCP.Controller { public class SCP_TB_VENDER_CONTROLLER { public static List GetlistByName(TA_VENDER model) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { return db.TA_VENDER.Where(p => p.VendName == model.VendName).ToList(); } } public static List Getlist() { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { return db.TA_VENDER.ToList(); } } public static TB_FACTORY GetFactory(string site) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { return db.TB_FACTORY.Where(p=>p.ErpSite== site).FirstOrDefault(); } } public static List Getlist1(string site) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { return db.TA_VENDER.Where(p=>p.Site== site).ToList(); } } public static List Getlist(List lsVenderID ) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { return db.TA_VENDER.Where(p=> lsVenderID.Contains(p.VendId)).ToList(); } } public static List Getlist(string p_site) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { return db.TA_VENDER.Where(p => p.Site==p_site ).ToList(); } } public static TA_VENDER GetVender(string p_VenderID,string p_Site) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { return db.TA_VENDER.Where(p => p.VendId==p_VenderID && p.Site==p_Site).FirstOrDefault(); } } public static List GetVendNameList(List p_list, string p_site) { List _ls = new List(); using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { IQueryable query = db.TA_VENDER; if (p_list.Count > 0) { query = query.Where(p => p_list.Contains(p.VendId)); } if (!string.IsNullOrEmpty(p_site)) { query = query.Where(p => p.Site == p_site); } _ls=query.ToList(); } return _ls; } public static ResultObject Save_TA_VENDER(List p_entitys) { ResultObject _ret = new ResultObject(); try { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { foreach (var itm in p_entitys) { var _entity = db.TA_VENDER.Where(p => p.UID == itm.UID).FirstOrDefault(); _entity.Quota = itm.Quota; db.TA_VENDER.AddOrUpdate(p => p.UID, _entity); } if (_ret.MessageList.Count > 0) { _ret.State = ReturnStatus.Failed; } else { if (db.SaveChanges() != -1) { _ret.State = ReturnStatus.Succeed; _ret.Result = true; } 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_TB_VENDER_CONTROLLER), "Save_TA_VENDER", 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_TB_VENDER_CONTROLLER), "Save_TA_VENDER", 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_TB_VENDER_CONTROLLER), "Save_TA_VENDER", 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_TB_VENDER_CONTROLLER), "Save_TA_VENDER", e.Message); _ret.Result = false; _ret.ErrorList.Add(e); throw e; } return _ret; } } }