account_repo.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package repositories
  2. import (
  3. "fmt"
  4. "time"
  5. "xorm.io/xorm"
  6. "xps/datamodels"
  7. "xps/viewmodels"
  8. )
  9. type AccountRepo struct {
  10. engine *xorm.Engine
  11. }
  12. func NewAccountRepo(engine *xorm.Engine) *AccountRepo {
  13. return &AccountRepo{
  14. engine: engine,
  15. }
  16. }
  17. func (d *AccountRepo) GetAccountByName(accountName string)(*viewmodels.AccountInfo, error) {
  18. sqlSelect := ` SELECT A.oc_id,
  19. B.oc_type_id,
  20. B.oc_name,
  21. A.account_id,
  22. A.account_name,
  23. A.account_real_name,
  24. A.password,
  25. A.account_type,
  26. A.account_staff_no,
  27. A.account_phone,
  28. A.account_last_ip,
  29. A.account_loc,
  30. A.status,
  31. A.account_avatar,
  32. A.account_photo,
  33. A.account_real_name,
  34. A.account_intro,
  35. A.wx_id,
  36. A.is_fixed `
  37. sqlFrom := ` FROM account AS A
  38. LEFT JOIN oc AS B ON (A.oc_id = B.oc_id ) `
  39. sqlWhere := ` WHERE A.deleted_flag = 0 `
  40. if accountName != "" {
  41. sqlWhere += ` AND A.account_name like ` + "'%" + accountName + "%'"
  42. }
  43. data := &viewmodels.AccountInfo{}
  44. ok, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Get(data)
  45. if !ok && err != nil {
  46. return nil, err
  47. } else {
  48. return data, nil
  49. }
  50. }
  51. func (d *AccountRepo) GetAccountByCode(code string) (*viewmodels.AccountInfo, error) {
  52. sqlSelect := ` SELECT A.oc_id,
  53. B.oc_type_id,
  54. B.oc_name,
  55. A.account_id,
  56. A.account_name,
  57. A.account_real_name,
  58. A.password,
  59. A.account_type,
  60. A.account_staff_no,
  61. A.account_phone,
  62. A.account_last_ip,
  63. A.account_loc,
  64. A.status,
  65. A.account_avatar,
  66. A.account_photo,
  67. A.account_real_name,
  68. A.account_intro,
  69. A.wx_id,
  70. A.is_fixed `
  71. sqlFrom := ` FROM account AS A
  72. LEFT JOIN oc AS B ON (A.oc_id = B.oc_id ) `
  73. sqlWhere := ` WHERE A.deleted_flag = 0 `
  74. sqlWhere += ` AND (A.account_name = ` + "'" + code + "'"
  75. sqlWhere += ` OR A.account_phone = ` + "'" + code + "'"
  76. sqlWhere += ` OR A.account_mail = ` + "'" + code + "'"
  77. sqlWhere += ` OR A.account_staff_no = ` + "'" + code + "')"
  78. data := &viewmodels.AccountInfo{}
  79. ok, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Get(data)
  80. if !ok && err != nil {
  81. return nil, err
  82. }
  83. return data, nil
  84. }
  85. func (d *AccountRepo) GetById(ocId int64, accountId int64)(*viewmodels.AccountInfo, error){
  86. sqlSelect := ` SELECT A.oc_id,
  87. B.oc_type_id,
  88. B.oc_name,
  89. A.account_id,
  90. A.account_name,
  91. A.account_real_name,
  92. A.password,
  93. A.account_type,
  94. A.account_staff_no,
  95. A.account_phone,
  96. A.account_last_ip,
  97. A.account_loc,
  98. A.status,
  99. A.account_avatar,
  100. A.account_photo,
  101. A.account_real_name,
  102. A.account_intro,
  103. A.wx_id,
  104. A.is_fixed `
  105. sqlFrom := ` FROM account AS A
  106. LEFT JOIN oc AS B ON (A.oc_id = B.oc_id ) `
  107. sqlWhere := ` WHERE A.deleted_flag = 0 AND A.oc_id = ` + fmt.Sprintf("%d", ocId)
  108. sqlWhere += ` AND A.account_id = ` + fmt.Sprintf("%d", accountId)
  109. user := &viewmodels.AccountInfo{}
  110. has, err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Get(user)
  111. if !has || err != nil {
  112. return nil, err
  113. } else {
  114. return user, nil
  115. }
  116. }
  117. func (d *AccountRepo) GetList(m map[string]interface{}) ([]viewmodels.AccountInfo, error) {
  118. sqlSelect := ` SELECT A.oc_id,
  119. B.oc_type_id,
  120. B.oc_name,
  121. A.account_id,
  122. A.account_name,
  123. A.account_real_name,
  124. A.password,
  125. A.account_type,
  126. A.account_staff_no,
  127. A.account_phone,
  128. A.account_last_ip,
  129. A.account_loc,
  130. A.status,
  131. A.account_avatar,
  132. A.account_photo,
  133. A.account_real_name,
  134. A.account_intro,
  135. A.wx_id,
  136. A.is_fixed `
  137. sqlFrom := ` FROM account AS A
  138. LEFT JOIN oc AS B ON (A.oc_id = B.oc_id ) `
  139. sqlWhere := ` WHERE A.deleted_flag = 0 `
  140. if ocId, ok := m["ocId"]; ok {
  141. sqlWhere += " AND A.oc_id = " + fmt.Sprintf("%d", ocId)
  142. }
  143. datalist := make([]viewmodels.AccountInfo, 0)
  144. err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Asc("account_id").Find(&datalist)
  145. if err != nil {
  146. return nil, err
  147. } else {
  148. return datalist, nil
  149. }
  150. }
  151. func (d *AccountRepo) Create(data *datamodels.Account) error {
  152. _, err := d.engine.Insert(data)
  153. return err
  154. }
  155. func (d *AccountRepo) Update(ocId, accountId int64, data *datamodels.Account) error {
  156. _, err := d.engine.
  157. Where("oc_id = ?", ocId).
  158. Where("account_id = ?", accountId).
  159. Update(data)
  160. return err
  161. }
  162. func (d *AccountRepo) Delete(m map[string]interface{}) error {
  163. ocId := m["ocId"].(int64)
  164. accountId := m["accountId"].(int64)
  165. deletedBy := m["deletedBy"].(int64)
  166. data := &datamodels.Account{
  167. OcId: ocId,
  168. AccountId: accountId,
  169. DeletedFlag: 1,
  170. DeletedBy: deletedBy,
  171. DeletedAt: time.Now(),
  172. }
  173. _, err := d.engine.
  174. Where("oc_id = ?", ocId).
  175. Where("account_id = ?", accountId).
  176. Update(data)
  177. return err
  178. }
  179. func (d *AccountRepo) IsUserNameExist(ocId int64, accountName string) (bool, error) {
  180. count, err := d.engine.
  181. Where("oc_id = ?", ocId).
  182. Where("deleted_flag = 0").
  183. Where("account_name = ?", accountName).
  184. Count(new(datamodels.Account))
  185. if err != nil {
  186. return false, err
  187. }
  188. return count > 0, nil
  189. }
  190. func (d *AccountRepo) IsUserPhoneExist(ocId int64, accountPhone string)(bool, error){
  191. count, err := d.engine.
  192. Where("oc_id = ?", ocId).
  193. Where("deleted_flag = 0").
  194. Where("account_phone = ?", accountPhone).
  195. Count(new(datamodels.Account))
  196. if err != nil {
  197. return true, err
  198. }
  199. return count > 0, nil
  200. }
  201. func (d *AccountRepo) IsStaffNoExist(ocId int64, staffNo string)(bool, error){
  202. count, err := d.engine.
  203. Where("oc_id = ?", ocId).
  204. Where("deleted_flag = 0").
  205. Where("account_staff_no = ?", staffNo).
  206. Count(new(datamodels.Account))
  207. if err != nil {
  208. return true, err
  209. }
  210. return count > 0, nil
  211. }
  212. func (d *AccountRepo) IsEmailExist(ocId int64, email string)(bool, error) {
  213. count, err := d.engine.
  214. Where("oc_id = ?", ocId).
  215. Where("deleted_flag = 0").
  216. Where("account_email = ?", email).
  217. Count(new(datamodels.Account))
  218. if err != nil {
  219. return true, err
  220. }
  221. return count > 0, nil
  222. }