user_model.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // ==========================================================================
  2. // This is auto-generated by gf cli tool. You may not really want to edit it.
  3. // ==========================================================================
  4. package user
  5. import (
  6. "database/sql"
  7. "github.com/gogf/gf/database/gdb"
  8. "github.com/gogf/gf/frame/g"
  9. "github.com/gogf/gf/frame/gmvc"
  10. "time"
  11. )
  12. // arModel is a active record design model for table user operations.
  13. type arModel struct {
  14. gmvc.M
  15. }
  16. var (
  17. // Table is the table name of user.
  18. Table = "user"
  19. // Model is the model object of user.
  20. Model = &arModel{g.DB("default").Table(Table).Safe()}
  21. // Columns defines and stores column names for table user.
  22. Columns = struct {
  23. Id string //
  24. UserName string // 用户名
  25. Mobile string // 中国手机不带国家代码,国际手机号格式为:国家代码-手机号
  26. UserNickname string // 用户昵称
  27. Birthday string // 生日
  28. CreateTime string // 注册时间
  29. UserPassword string // 登录密码;cmf_password加密
  30. UserStatus string // 用户状态;0:禁用,1:正常,2:未验证
  31. UserEmail string // 用户登录邮箱
  32. Sex string // 性别;0:保密,1:男,2:女
  33. Avatar string // 用户头像
  34. LastLoginTime string // 最后登录时间
  35. LastLoginIp string // 最后登录ip
  36. DeptId string // 部门id
  37. Remark string // 备注
  38. IsAdmin string // 是否后台管理员 1 是 0 否
  39. }{
  40. Id: "id",
  41. UserName: "user_name",
  42. Mobile: "mobile",
  43. UserNickname: "user_nickname",
  44. Birthday: "birthday",
  45. CreateTime: "create_time",
  46. UserPassword: "user_password",
  47. UserStatus: "user_status",
  48. UserEmail: "user_email",
  49. Sex: "sex",
  50. Avatar: "avatar",
  51. LastLoginTime: "last_login_time",
  52. LastLoginIp: "last_login_ip",
  53. DeptId: "dept_id",
  54. Remark: "remark",
  55. IsAdmin: "is_admin",
  56. }
  57. )
  58. // FindOne is a convenience method for Model.FindOne.
  59. // See Model.FindOne.
  60. func FindOne(where ...interface{}) (*Entity, error) {
  61. return Model.FindOne(where...)
  62. }
  63. // FindAll is a convenience method for Model.FindAll.
  64. // See Model.FindAll.
  65. func FindAll(where ...interface{}) ([]*Entity, error) {
  66. return Model.FindAll(where...)
  67. }
  68. // FindValue is a convenience method for Model.FindValue.
  69. // See Model.FindValue.
  70. func FindValue(fieldsAndWhere ...interface{}) (gdb.Value, error) {
  71. return Model.FindValue(fieldsAndWhere...)
  72. }
  73. // FindArray is a convenience method for Model.FindArray.
  74. // See Model.FindArray.
  75. func FindArray(fieldsAndWhere ...interface{}) ([]gdb.Value, error) {
  76. return Model.FindArray(fieldsAndWhere...)
  77. }
  78. // FindCount is a convenience method for Model.FindCount.
  79. // See Model.FindCount.
  80. func FindCount(where ...interface{}) (int, error) {
  81. return Model.FindCount(where...)
  82. }
  83. // Insert is a convenience method for Model.Insert.
  84. func Insert(data ...interface{}) (result sql.Result, err error) {
  85. return Model.Insert(data...)
  86. }
  87. // InsertIgnore is a convenience method for Model.InsertIgnore.
  88. func InsertIgnore(data ...interface{}) (result sql.Result, err error) {
  89. return Model.InsertIgnore(data...)
  90. }
  91. // Replace is a convenience method for Model.Replace.
  92. func Replace(data ...interface{}) (result sql.Result, err error) {
  93. return Model.Replace(data...)
  94. }
  95. // Save is a convenience method for Model.Save.
  96. func Save(data ...interface{}) (result sql.Result, err error) {
  97. return Model.Save(data...)
  98. }
  99. // Update is a convenience method for Model.Update.
  100. func Update(dataAndWhere ...interface{}) (result sql.Result, err error) {
  101. return Model.Update(dataAndWhere...)
  102. }
  103. // Delete is a convenience method for Model.Delete.
  104. func Delete(where ...interface{}) (result sql.Result, err error) {
  105. return Model.Delete(where...)
  106. }
  107. // As sets an alias name for current table.
  108. func (m *arModel) As(as string) *arModel {
  109. return &arModel{m.M.As(as)}
  110. }
  111. // TX sets the transaction for current operation.
  112. func (m *arModel) TX(tx *gdb.TX) *arModel {
  113. return &arModel{m.M.TX(tx)}
  114. }
  115. // Master marks the following operation on master node.
  116. func (m *arModel) Master() *arModel {
  117. return &arModel{m.M.Master()}
  118. }
  119. // Slave marks the following operation on slave node.
  120. // Note that it makes sense only if there's any slave node configured.
  121. func (m *arModel) Slave() *arModel {
  122. return &arModel{m.M.Slave()}
  123. }
  124. // LeftJoin does "LEFT JOIN ... ON ..." statement on the model.
  125. // The parameter <table> can be joined table and its joined condition,
  126. // and also with its alias name, like:
  127. // Table("user").LeftJoin("user_detail", "user_detail.uid=user.uid")
  128. // Table("user", "u").LeftJoin("user_detail", "ud", "ud.uid=u.uid")
  129. func (m *arModel) LeftJoin(table ...string) *arModel {
  130. return &arModel{m.M.LeftJoin(table...)}
  131. }
  132. // RightJoin does "RIGHT JOIN ... ON ..." statement on the model.
  133. // The parameter <table> can be joined table and its joined condition,
  134. // and also with its alias name, like:
  135. // Table("user").RightJoin("user_detail", "user_detail.uid=user.uid")
  136. // Table("user", "u").RightJoin("user_detail", "ud", "ud.uid=u.uid")
  137. func (m *arModel) RightJoin(table ...string) *arModel {
  138. return &arModel{m.M.RightJoin(table...)}
  139. }
  140. // InnerJoin does "INNER JOIN ... ON ..." statement on the model.
  141. // The parameter <table> can be joined table and its joined condition,
  142. // and also with its alias name, like:
  143. // Table("user").InnerJoin("user_detail", "user_detail.uid=user.uid")
  144. // Table("user", "u").InnerJoin("user_detail", "ud", "ud.uid=u.uid")
  145. func (m *arModel) InnerJoin(table ...string) *arModel {
  146. return &arModel{m.M.InnerJoin(table...)}
  147. }
  148. // Fields sets the operation fields of the model, multiple fields joined using char ','.
  149. func (m *arModel) Fields(fields string) *arModel {
  150. return &arModel{m.M.Fields(fields)}
  151. }
  152. // FieldsEx sets the excluded operation fields of the model, multiple fields joined using char ','.
  153. func (m *arModel) FieldsEx(fields string) *arModel {
  154. return &arModel{m.M.FieldsEx(fields)}
  155. }
  156. // Option sets the extra operation option for the model.
  157. func (m *arModel) Option(option int) *arModel {
  158. return &arModel{m.M.Option(option)}
  159. }
  160. // OmitEmpty sets OPTION_OMITEMPTY option for the model, which automatically filers
  161. // the data and where attributes for empty values.
  162. func (m *arModel) OmitEmpty() *arModel {
  163. return &arModel{m.M.OmitEmpty()}
  164. }
  165. // Filter marks filtering the fields which does not exist in the fields of the operated table.
  166. func (m *arModel) Filter() *arModel {
  167. return &arModel{m.M.Filter()}
  168. }
  169. // Where sets the condition statement for the model. The parameter <where> can be type of
  170. // string/map/gmap/slice/struct/*struct, etc. Note that, if it's called more than one times,
  171. // multiple conditions will be joined into where statement using "AND".
  172. // Eg:
  173. // Where("uid=10000")
  174. // Where("uid", 10000)
  175. // Where("money>? AND name like ?", 99999, "vip_%")
  176. // Where("uid", 1).Where("name", "john")
  177. // Where("status IN (?)", g.Slice{1,2,3})
  178. // Where("age IN(?,?)", 18, 50)
  179. // Where(User{ Id : 1, UserName : "john"})
  180. func (m *arModel) Where(where interface{}, args ...interface{}) *arModel {
  181. return &arModel{m.M.Where(where, args...)}
  182. }
  183. // And adds "AND" condition to the where statement.
  184. func (m *arModel) And(where interface{}, args ...interface{}) *arModel {
  185. return &arModel{m.M.And(where, args...)}
  186. }
  187. // Or adds "OR" condition to the where statement.
  188. func (m *arModel) Or(where interface{}, args ...interface{}) *arModel {
  189. return &arModel{m.M.Or(where, args...)}
  190. }
  191. // Group sets the "GROUP BY" statement for the model.
  192. func (m *arModel) Group(groupBy string) *arModel {
  193. return &arModel{m.M.Group(groupBy)}
  194. }
  195. // Order sets the "ORDER BY" statement for the model.
  196. func (m *arModel) Order(orderBy ...string) *arModel {
  197. return &arModel{m.M.Order(orderBy...)}
  198. }
  199. // Limit sets the "LIMIT" statement for the model.
  200. // The parameter <limit> can be either one or two number, if passed two number is passed,
  201. // it then sets "LIMIT limit[0],limit[1]" statement for the model, or else it sets "LIMIT limit[0]"
  202. // statement.
  203. func (m *arModel) Limit(limit ...int) *arModel {
  204. return &arModel{m.M.Limit(limit...)}
  205. }
  206. // Offset sets the "OFFSET" statement for the model.
  207. // It only makes sense for some databases like SQLServer, PostgreSQL, etc.
  208. func (m *arModel) Offset(offset int) *arModel {
  209. return &arModel{m.M.Offset(offset)}
  210. }
  211. // Page sets the paging number for the model.
  212. // The parameter <page> is started from 1 for paging.
  213. // Note that, it differs that the Limit function start from 0 for "LIMIT" statement.
  214. func (m *arModel) Page(page, limit int) *arModel {
  215. return &arModel{m.M.Page(page, limit)}
  216. }
  217. // Batch sets the batch operation number for the model.
  218. func (m *arModel) Batch(batch int) *arModel {
  219. return &arModel{m.M.Batch(batch)}
  220. }
  221. // Cache sets the cache feature for the model. It caches the result of the sql, which means
  222. // if there's another same sql request, it just reads and returns the result from cache, it
  223. // but not committed and executed into the database.
  224. //
  225. // If the parameter <duration> < 0, which means it clear the cache with given <name>.
  226. // If the parameter <duration> = 0, which means it never expires.
  227. // If the parameter <duration> > 0, which means it expires after <duration>.
  228. //
  229. // The optional parameter <name> is used to bind a name to the cache, which means you can later
  230. // control the cache like changing the <duration> or clearing the cache with specified <name>.
  231. //
  232. // Note that, the cache feature is disabled if the model is operating on a transaction.
  233. func (m *arModel) Cache(duration time.Duration, name ...string) *arModel {
  234. return &arModel{m.M.Cache(duration, name...)}
  235. }
  236. // Data sets the operation data for the model.
  237. // The parameter <data> can be type of string/map/gmap/slice/struct/*struct, etc.
  238. // Eg:
  239. // Data("uid=10000")
  240. // Data("uid", 10000)
  241. // Data(g.Map{"uid": 10000, "name":"john"})
  242. // Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"})
  243. func (m *arModel) Data(data ...interface{}) *arModel {
  244. return &arModel{m.M.Data(data...)}
  245. }
  246. // All does "SELECT FROM ..." statement for the model.
  247. // It retrieves the records from table and returns the result as []*Entity.
  248. // It returns nil if there's no record retrieved with the given conditions from table.
  249. //
  250. // The optional parameter <where> is the same as the parameter of Model.Where function,
  251. // see Model.Where.
  252. func (m *arModel) All(where ...interface{}) ([]*Entity, error) {
  253. all, err := m.M.All(where...)
  254. if err != nil {
  255. return nil, err
  256. }
  257. var entities []*Entity
  258. if err = all.Structs(&entities); err != nil && err != sql.ErrNoRows {
  259. return nil, err
  260. }
  261. return entities, nil
  262. }
  263. // One retrieves one record from table and returns the result as *Entity.
  264. // It returns nil if there's no record retrieved with the given conditions from table.
  265. //
  266. // The optional parameter <where> is the same as the parameter of Model.Where function,
  267. // see Model.Where.
  268. func (m *arModel) One(where ...interface{}) (*Entity, error) {
  269. one, err := m.M.One(where...)
  270. if err != nil {
  271. return nil, err
  272. }
  273. var entity *Entity
  274. if err = one.Struct(&entity); err != nil && err != sql.ErrNoRows {
  275. return nil, err
  276. }
  277. return entity, nil
  278. }
  279. // FindOne retrieves and returns a single Record by Model.WherePri and Model.One.
  280. // Also see Model.WherePri and Model.One.
  281. func (m *arModel) FindOne(where ...interface{}) (*Entity, error) {
  282. one, err := m.M.FindOne(where...)
  283. if err != nil {
  284. return nil, err
  285. }
  286. var entity *Entity
  287. if err = one.Struct(&entity); err != nil && err != sql.ErrNoRows {
  288. return nil, err
  289. }
  290. return entity, nil
  291. }
  292. // FindAll retrieves and returns Result by by Model.WherePri and Model.All.
  293. // Also see Model.WherePri and Model.All.
  294. func (m *arModel) FindAll(where ...interface{}) ([]*Entity, error) {
  295. all, err := m.M.FindAll(where...)
  296. if err != nil {
  297. return nil, err
  298. }
  299. var entities []*Entity
  300. if err = all.Structs(&entities); err != nil && err != sql.ErrNoRows {
  301. return nil, err
  302. }
  303. return entities, nil
  304. }
  305. // Chunk iterates the table with given size and callback function.
  306. func (m *arModel) Chunk(limit int, callback func(entities []*Entity, err error) bool) {
  307. m.M.Chunk(limit, func(result gdb.Result, err error) bool {
  308. var entities []*Entity
  309. err = result.Structs(&entities)
  310. if err == sql.ErrNoRows {
  311. return false
  312. }
  313. return callback(entities, err)
  314. })
  315. }
  316. // LockUpdate sets the lock for update for current operation.
  317. func (m *arModel) LockUpdate() *arModel {
  318. return &arModel{m.M.LockUpdate()}
  319. }
  320. // LockShared sets the lock in share mode for current operation.
  321. func (m *arModel) LockShared() *arModel {
  322. return &arModel{m.M.LockShared()}
  323. }
  324. // Unscoped enables/disables the soft deleting feature.
  325. func (m *arModel) Unscoped() *arModel {
  326. return &arModel{m.M.Unscoped()}
  327. }