account_repo.go 6.4 KB

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