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.ScpEntity; using CK.SCP.Utils; using CK.SCP.Models.AppBoxEntity; using CK.SCP.Models.Enums; using System.Data.Entity.Core; namespace CK.SCP.Controller { public class PublicDataController { public static string GetUserlistSql(string p_roName,string p_name,string p_roleName,string p_site) { StringBuilder _builder = new StringBuilder(); //if (p_name == "供应商") //{ // _builder.Append("select u.*from Users u "); // _builder.Append("Inner join RoleUsers ru on u.ID = ru.UserID "); // _builder.Append("Inner join Roles r on ru.roleid = r.ID "); // _builder.Append("Inner join FactoryUsers fu on u.ID = fu.UserID "); // _builder.Append("inner join TA_FACTORY f on fu.FACTORY_ID = f.ID "); // _builder.AppendFormat("where r.id = '{0}' AND F.FactoryId='{1}'", p_roleName, p_site); //} //else //{ _builder.Append("select u.*from Users u "); _builder.Append("Inner join RoleUsers ru on u.ID = ru.UserID "); _builder.Append("Inner join Roles r on ru.roleid = r.ID "); _builder.Append("Inner join FactoryUsers fu on u.ID = fu.UserID "); _builder.Append("inner join TA_FACTORY f on fu.FACTORY_ID = f.ID "); _builder.AppendFormat("where r.id = '{0}' AND F.ErpSite='{1}'", p_roleName, p_site); //} if (!string.IsNullOrEmpty(p_roName)) { _builder.AppendFormat(" AND u.Name like '%{0}%'", p_roName); } return _builder.ToString(); } public static void Get_UserList(string p_username,string p_name,string p_roleName,string p_Site,Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (AppBoxContext db = EntitiesFactory.CreateAppBoxInstance()) { string sql = GetUserlistSql(p_username,p_name, p_roleName,p_Site); IQueryable q = db.Database.SqlQuery(sql).AsQueryable(); _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(PublicDataController), "Get_UserList", 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(PublicDataController), "Get_UserList", 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(PublicDataController), "Get_UserList", 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(PublicDataController), "Get_UserList", e.Message); throw e; } } public static TB_PublicData GetlistByBillNo(int id) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { return db.TB_PublicData.SingleOrDefault(p => p.ID == id); } } public static bool SaveInfo(TB_PublicData model) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { db.TB_PublicData.AddOrUpdate(model); db.SaveChanges(); } return true; } public static bool UpdateInfo(TB_PublicData model) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { db.TB_PublicData.AddOrUpdate(p => p.ID, model); db.SaveChanges(); } return true; } public static void DeleteById(int id) { using (ScpEntities db = EntitiesFactory.CreateScpInstance()) { var info = db.TB_PublicData.SingleOrDefault(p => p.ID == id); if (info != null) db.TB_PublicData.Remove(info); db.SaveChanges(); } } } }