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; using System.IO; using System.Security.Cryptography; namespace CK.SCP.Controller { public class SecurityUrl { public static string key = "22010220102212"; public SecurityUrl() { // // TODO: 在此处添加构造函数逻辑 // } /// /// 加密 /// /// /// 必须是8位的字符串 /// public static string Encode(string str, string key) { DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8)); provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8)); byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(str); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write); stream2.Write(bytes, 0, bytes.Length); stream2.FlushFinalBlock(); StringBuilder builder = new StringBuilder(); foreach (byte num in stream.ToArray()) { builder.AppendFormat("{0:X2}", num); } stream.Close(); return builder.ToString(); } /// /// Des 解密 GB2312 /// /// Desc string /// Key ,必须为8位 /// public static string Decode(string str, string key) { DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8)); provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8)); byte[] buffer = new byte[str.Length / 2]; for (int i = 0; i < (str.Length / 2); i++) { int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10); buffer[i] = (byte)num2; } MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write); stream2.Write(buffer, 0, buffer.Length); stream2.FlushFinalBlock(); stream.Close(); return Encoding.GetEncoding("UTF-8").GetString(stream.ToArray()); } } public class PublicDataController { public static string GetUserlistSql(string p_roleName, string p_Domain, string p_username) { StringBuilder _builder = new StringBuilder(); _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_Domain); if (!string.IsNullOrEmpty(p_username)) { _builder.AppendFormat(" AND u.Name like '%{0}%'" , p_username); } return _builder.ToString(); } public static void Get_UserList(string p_username,string p_roleName, string p_Domain,Action>> p_action) { ResultObject> _ret = new ResultObject>(); try { using (AppBoxContext db = EntitiesFactory.CreateAppBoxInstance()) { string sql = GetUserlistSql(p_roleName, p_Domain, p_username); 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(); } } } }