面包店
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

299 lignes
8.0 KiB

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