PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/yoyo/CardInChinaNew/CardInChina.Data.Entity/Interface/CustomerService.cs

#
C# | 142 lines | 90 code | 25 blank | 27 comment | 17 complexity | e818d09de14ea3f82d10025dada6bcc1 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity.Validation;
  4. using System.Linq;
  5. using System.Text;
  6. using CardInChina.Data.Entity.Entity;
  7. namespace CardInChina.Data.Entity.Interface
  8. {
  9. public class CustomerService
  10. {
  11. private CICEntities _db = new CICEntities();
  12. private SysVerifyService svs = new SysVerifyService();
  13. public Customer Login(string account, string password)
  14. {
  15. if (string.IsNullOrEmpty(account))
  16. throw new ArgumentNullException("account");
  17. if (string.IsNullOrEmpty(password))
  18. throw new ArgumentNullException("password");
  19. // always convert alias or email to lower case
  20. account = account.ToLower();
  21. using (CICEntities db = new CICEntities())
  22. {
  23. var sql = db.Customers.Where(c => (c.Account == account || c.Email == account)
  24. && c.Password == MySqlFunctions.OldPassword(password));
  25. return sql.FirstOrDefault();
  26. // TODO: 登陆的IP和常用IP的国家不同
  27. }
  28. }
  29. public Customer GetCustomerByCustomerID(int customerID)
  30. {
  31. //using (CICEntities db = new CICEntities())
  32. //{
  33. var customer = _db.Customers.FirstOrDefault(c => c.CustomerID == customerID);
  34. return customer;
  35. //}
  36. }
  37. public Customer Register(string email, string alias, string password, string referer, int? recommended, string ipAddress)
  38. {
  39. if (string.IsNullOrEmpty(email))
  40. throw new ArgumentNullException("email");
  41. if (string.IsNullOrEmpty(alias))
  42. throw new ArgumentNullException("alias");
  43. if (string.IsNullOrEmpty(password))
  44. throw new ArgumentNullException("password");
  45. using (CICEntities db = new CICEntities())
  46. {
  47. if (db.Customers.FirstOrDefault(p => p.Account == alias || p.Email == email) != null)
  48. throw new ApplicationException("帐号或邮箱已经存在,注册失败");
  49. var customer = new Customer()
  50. {
  51. Account = alias,
  52. Email = email,
  53. Password = MySqlFunctions.OldPassword(password),
  54. MacID = "00-00-00-00-00-00",
  55. RegisterDate = DateTime.UtcNow,
  56. LastLogonDate = DateTime.UtcNow,
  57. Recommended = recommended,
  58. Referer = referer,
  59. IP = ipAddress,
  60. //IPCountry = "",
  61. //VerifyCode = "",
  62. //VerifyMethod = "",
  63. };
  64. db.Customers.Add(customer);
  65. try
  66. {
  67. db.SaveChanges();
  68. }
  69. catch (DbEntityValidationException e)
  70. {
  71. // TODO:
  72. throw;
  73. }
  74. // VerifyCode旨在验证Balance(余额)是否被篡改
  75. customer.Balance = 0;
  76. customer.VerifyCode = customer.GenerateVerifyCode();
  77. //TODO: 黑名单鉴别
  78. //sql_black="select * from blacklist where blacklist_value='"&ip&"' or blacklist_value='"&email&"' or blacklist_value='"&phone&"' or blacklist_value='"&customer_name&"' or blacklist_value='"&mac_id&"' "
  79. //Set rs_black=conn.execute(sql_black)
  80. //If Not rs_black.eof Then
  81. // sql=sql&",cust_memo='与黑名单中记录 有关联!blacklist_id="&rs_black("blacklist_id")&"'"
  82. //End If
  83. //sql=sql&" where cust_id="&cust_id
  84. //conn.execute sql
  85. //TODO: 发送认证邮件
  86. //var v = new SysVerify
  87. //{
  88. // Code = Guid.NewGuid().ToString(),
  89. // ExpireAfter = 7 * 24 * 3600, // 7天
  90. // ForId = customer.CustomerID,
  91. //};
  92. //db.SysVerifies.Add(v);
  93. svs.CreateOne(customer.CustomerID, 7 * 24 * 3600); // 7天
  94. try
  95. {
  96. db.SaveChanges();
  97. }
  98. catch (DbEntityValidationException e)
  99. {
  100. // TODO:
  101. throw;
  102. }
  103. return customer;
  104. }
  105. }
  106. public Customer SetEmailVerified(int cid)
  107. {
  108. using (CICEntities db = new CICEntities())
  109. {
  110. var cc = db.Customers.Single(p => p.CustomerID == cid);
  111. cc.EmailConfirmed = true;
  112. db.SaveChanges();
  113. return cc;
  114. }
  115. }
  116. }
  117. }