account_repo.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package system
  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(ocId int64) ([]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. datalist := make([]viewmodels.AccountInfo, 0)
  141. err := d.engine.SQL(sqlSelect + sqlFrom + sqlWhere).Asc("account_id").Find(&datalist)
  142. if err != nil {
  143. return nil, err
  144. } else {
  145. return datalist, nil
  146. }
  147. }
  148. func (d *AccountRepo) Create(data *datamodels.Account) error {
  149. _, err := d.engine.Insert(data)
  150. return err
  151. }
  152. func (d *AccountRepo) Update(ocId, accountId int64, data *datamodels.Account) error {
  153. _, err := d.engine.
  154. Where("oc_id = ?", ocId).
  155. Where("account_id = ?", accountId).
  156. 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.
  171. Where("oc_id = ?", ocId).
  172. Where("account_id = ?", accountId).
  173. Update(data)
  174. return err
  175. }
  176. func (d *AccountRepo) IsUserNameExist(ocId int64, accountName string) (bool, error) {
  177. count, err := d.engine.
  178. Where("deleted_flag = 0").
  179. Where("account_name = ?", accountName).
  180. Count(new(datamodels.Account))
  181. if err != nil {
  182. return false, err
  183. }
  184. return count > 0, nil
  185. }
  186. func (d *AccountRepo) IsUserPhoneExist(ocId int64, accountPhone string)(bool, error){
  187. count, err := d.engine.
  188. Where("deleted_flag = 0").
  189. Where("account_phone = ?", accountPhone).
  190. Count(new(datamodels.Account))
  191. if err != nil {
  192. return true, err
  193. }
  194. return count > 0, nil
  195. }
  196. func (d *AccountRepo) IsStaffNoExist(ocId int64, staffNo string)(bool, error){
  197. count, err := d.engine.
  198. Where("deleted_flag = 0").
  199. Where("account_staff_no = ?", staffNo).
  200. Count(new(datamodels.Account))
  201. if err != nil {
  202. return true, err
  203. }
  204. return count > 0, nil
  205. }
  206. func (d *AccountRepo) IsEmailExist(ocId int64, email string)(bool, error) {
  207. count, err := d.engine.
  208. Where("deleted_flag = 0").
  209. Where("account_email = ?", email).
  210. Count(new(datamodels.Account))
  211. if err != nil {
  212. return true, err
  213. }
  214. return count > 0, nil
  215. }