蛋蛋星球RabbitMq消费项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

303 lines
8.1 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "fmt"
  6. "xorm.io/xorm"
  7. )
  8. // UserisExistByUsernameAndPassword is usernameAndPassword exist
  9. func UserisExistByUsernameAndPassword(Db *xorm.Engine, username, password, zone string) (bool, error) {
  10. sess := Db.Where("(username = ? or phone=?) AND password = ?", username, username, password)
  11. if zone != "" && zone != "86" {
  12. sess = sess.And("zone=?", zone)
  13. }
  14. has, err := sess.Exist(&model.User{})
  15. if err != nil {
  16. return false, err
  17. }
  18. return has, nil
  19. }
  20. // UserisExistByMobile is exist
  21. func UserisExistByMobile(Db *xorm.Engine, n string) (bool, error) {
  22. has, err := Db.Where("phone = ? and phone<>''", n).Exist(&model.User{})
  23. if err != nil {
  24. return false, err
  25. }
  26. return has, nil
  27. }
  28. // UserInByUIDByLevel is In查询 以及是否是有效用户
  29. func UserInByUIDByLevel(Db *xorm.Engine, ids []int, levelID interface{}) (*[]model.User, error) {
  30. var m []model.User
  31. if err := Db.In("uid", ids).Where("level = ?", levelID).
  32. Find(&m); err != nil {
  33. return nil, logx.Warn(err)
  34. }
  35. return &m, nil
  36. }
  37. // UserFindByMobile search user by mobile
  38. func UserFindByMobile(Db *xorm.Engine, mobile string) (*model.User, error) {
  39. var m model.User
  40. if has, err := Db.Where("phone LIKE ? AND delete_at = 0", "%"+mobile).
  41. Get(&m); err != nil || has == false {
  42. return nil, logx.Warn(err)
  43. }
  44. return &m, nil
  45. }
  46. // UserFindExistByMobile search user by mobile
  47. func UserFindExistByMobile(Db *xorm.Engine, mobile string) (*model.User, bool, error) {
  48. var m model.User
  49. has, err := Db.Where("(phone = ? OR uid = ?) AND delete_at = 0", mobile, mobile).Get(&m)
  50. if err != nil {
  51. logx.Infof("UserFindExistByMobile err")
  52. return nil, false, logx.Warn(err)
  53. }
  54. return &m, has, nil
  55. }
  56. // UserFindByMobile search user by mobile
  57. func UserFindByMobileAll(Db *xorm.Engine, mobile string) (*model.User, error) {
  58. var m model.User
  59. if has, err := Db.Where("(phone = ? OR uid = ?)", mobile, mobile).
  60. Get(&m); err != nil || has == false {
  61. return nil, logx.Warn(err)
  62. }
  63. return &m, nil
  64. }
  65. // UserGetByMobileIgnoreDelete search user by mobile ignore delete
  66. func UserGetByMobileIgnoreDelete(Db *xorm.Engine, mobile, zone string) (*model.User, bool, error) {
  67. m := new(model.User)
  68. sess := Db.Where("phone = ?", mobile)
  69. if zone != "" && zone != "86" {
  70. sess = sess.And("zone=?", zone)
  71. }
  72. has, err := sess.Get(m)
  73. if err != nil {
  74. return nil, false, logx.Warn(err)
  75. }
  76. return m, has, nil
  77. }
  78. // UsersFindByMobileLike search users by mobile
  79. func UsersFindByMobileLike(Db *xorm.Engine, mobile string) (*[]model.User, error) {
  80. var m []model.User
  81. if err := Db.Where("phone like ?", "%"+mobile+"%").
  82. Find(&m); err != nil {
  83. return nil, logx.Warn(err)
  84. }
  85. return &m, nil
  86. }
  87. // UsersFindByNickNameLike search users by nickname
  88. func UsersFindByNickNameLike(Db *xorm.Engine, nickname string) (*[]model.User, error) {
  89. var m []model.User
  90. if err := Db.Where("nickname like ?", "%"+nickname+"%").
  91. Find(&m); err != nil {
  92. return nil, logx.Warn(err)
  93. }
  94. return &m, nil
  95. }
  96. // UsersInByIds is 根据ids 查找users
  97. func UsersInByIds(Db *xorm.Engine, ids []int, limit, start int) (*[]model.User, error) {
  98. var m []model.User
  99. if limit == 0 && start == 0 {
  100. if err := Db.In("uid", ids).
  101. Find(&m); err != nil {
  102. return nil, logx.Warn(err)
  103. }
  104. return &m, nil
  105. }
  106. if err := Db.In("uid", ids).Limit(limit, start).
  107. Find(&m); err != nil {
  108. return nil, logx.Warn(err)
  109. }
  110. return &m, nil
  111. }
  112. // UsersInByIdsWhereLv is 根据ids和 lv会员等级 查找users
  113. func UsersInByIdsWhereLv(Db *xorm.Engine, ids []int, lv interface{}, limit, start int) (*[]model.User, error) {
  114. var m []model.User
  115. if limit == 0 && start == 0 {
  116. if err := Db.Where("level = ?", lv).In("uid", ids).
  117. Find(&m); err != nil {
  118. return nil, logx.Warn(err)
  119. }
  120. return &m, nil
  121. }
  122. if err := Db.Where("level = ?", lv).In("uid", ids).Limit(limit, start).
  123. Find(&m); err != nil {
  124. return nil, logx.Warn(err)
  125. }
  126. return &m, nil
  127. }
  128. // UsersInByIdsByAscWhereLv is 根据ids和 lv会员等级 查找users 升排序
  129. func UsersInByIdsByAscWhereLv(Db *xorm.Engine, ids []int, lv interface{}, limit, start int, c string) (*[]model.User, error) {
  130. var m []model.User
  131. if limit == 0 && start == 0 {
  132. if err := Db.Where("level = ?", lv).In("uid", ids).Asc(c).
  133. Find(&m); err != nil {
  134. return nil, logx.Warn(err)
  135. }
  136. return &m, nil
  137. }
  138. if err := Db.Where("level = ?", lv).In("uid", ids).Asc(c).Limit(limit, start).
  139. Find(&m); err != nil {
  140. return nil, logx.Warn(err)
  141. }
  142. return &m, nil
  143. }
  144. // UsersInByIdsByDescWhereLv is 根据ids和 lv会员等级 查找users 降排序
  145. func UsersInByIdsByDescWhereLv(Db *xorm.Engine, ids []int, lv interface{}, limit, start int, c string) (*[]model.User, error) {
  146. var m []model.User
  147. if limit == 0 && start == 0 {
  148. if err := Db.Where("level = ?", lv).In("uid", ids).Desc(c).
  149. Find(&m); err != nil {
  150. return nil, logx.Warn(err)
  151. }
  152. return &m, nil
  153. }
  154. if err := Db.Where("level = ?", lv).In("uid", ids).Desc(c).Limit(limit, start).
  155. Find(&m); err != nil {
  156. return nil, logx.Warn(err)
  157. }
  158. return &m, nil
  159. }
  160. // UserFindByArkidUserName search user by mobile
  161. func UserFindByArkidUserName(Db *xorm.Engine, name string) (*model.User, error) {
  162. var m model.User
  163. if has, err := Db.Where("username = ?", name).
  164. Get(&m); err != nil || has == false {
  165. return nil, logx.Warn(err)
  166. }
  167. return &m, nil
  168. }
  169. // UserFindByID is find user byid
  170. func UserFindByID(Db *xorm.Engine, id interface{}) (*model.User, error) {
  171. var m model.User
  172. if _, err := Db.Where("uid = ?", id).
  173. Get(&m); err != nil {
  174. return nil, logx.Warn(err)
  175. }
  176. return &m, nil
  177. }
  178. func UserFindByIDs(Db *xorm.Engine, uids []int) (*[]model.User, error) {
  179. var m []model.User
  180. if err := Db.In("uid", uids).Find(&m); err != nil {
  181. return nil, logx.Warn(err)
  182. }
  183. return &m, nil
  184. }
  185. // UsersInByIdsByDesc is 根据某列 降序
  186. func UsersInByIdsByDesc(Db *xorm.Engine, ids []int, limit, start int, c string) (*[]model.User, error) {
  187. var m []model.User
  188. if limit == 0 && start == 0 {
  189. if err := Db.In("uid", ids).Desc(c).
  190. Find(&m); err != nil {
  191. return nil, logx.Warn(err)
  192. }
  193. return &m, nil
  194. }
  195. if err := Db.In("uid", ids).Desc(c).Limit(limit, start).
  196. Find(&m); err != nil {
  197. return nil, logx.Warn(err)
  198. }
  199. return &m, nil
  200. }
  201. // UsersInByIdsByAsc is 根据某列 升序
  202. func UsersInByIdsByAsc(Db *xorm.Engine, ids []int, limit, start int, c string) (*[]model.User, error) {
  203. var m []model.User
  204. if limit == 0 && start == 0 {
  205. if err := Db.In("uid", ids).Asc(c).
  206. Find(&m); err != nil {
  207. return nil, logx.Warn(err)
  208. }
  209. return &m, nil
  210. }
  211. if err := Db.In("uid", ids).Asc(c).Limit(limit, start).
  212. Find(&m); err != nil {
  213. return nil, logx.Warn(err)
  214. }
  215. return &m, nil
  216. }
  217. // UserInsert is insert user
  218. func UserInsert(Db *xorm.Engine, user *model.User) (int64, error) {
  219. affected, err := Db.Insert(user)
  220. if err != nil {
  221. return 0, err
  222. }
  223. return affected, nil
  224. }
  225. // UserIsExistByMobile is mobile exist
  226. func UserIsExistByMobile(Db *xorm.Engine, mobile string) (bool, error) {
  227. //fmt.Println(mobile)
  228. has, err := Db.Where("phone = ? OR uid = ?", mobile, mobile).Exist(&model.User{})
  229. fmt.Println(has, mobile)
  230. if err != nil {
  231. return false, err
  232. }
  233. return has, nil
  234. }
  235. // UserIsExistByID is mobile exist by id
  236. func UserIsExistByID(Db *xorm.Engine, id string) (bool, error) {
  237. has, err := Db.Where("uid = ?", id).Exist(&model.User{})
  238. if err != nil {
  239. return false, err
  240. }
  241. return has, nil
  242. }
  243. // UserUpdate is update user
  244. func UserUpdate(Db *xorm.Engine, uid interface{}, user *model.User, forceColums ...string) (int64, error) {
  245. var (
  246. affected int64
  247. err error
  248. )
  249. if forceColums != nil {
  250. affected, err = Db.Where("uid=?", uid).Cols(forceColums...).Update(user)
  251. } else {
  252. affected, err = Db.Where("uid=?", uid).Update(user)
  253. }
  254. if err != nil {
  255. return 0, err
  256. }
  257. return affected, nil
  258. }
  259. func UpdateUserFinValid() {
  260. }
  261. // UserDelete is delete user
  262. func UserDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
  263. return Db.Where("uid = ?", uid).Delete(model.User{})
  264. }
  265. func UserDeleteWithSess(sess *xorm.Session, uid interface{}) (int64, error) {
  266. return sess.Where("uid = ?", uid).Delete(model.User{})
  267. }
  268. func UserFindByLevel(eg *xorm.Engine, level int) []model.User {
  269. var data []model.User
  270. eg.Where("level=?", level).Find(&data)
  271. return data
  272. }