dws24.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "time"
  6. "gorm.io/gorm"
  7. )
  8. // 学位
  9. type Dws24 struct {
  10. ID int64 `gorm:"primary_key" json:"-"`
  11. DegreeId string `json:"degree_id"`
  12. Name string `json:"name"`
  13. BirthDate string `json:"birth_date"`
  14. Gender string `json:"gender"`
  15. Major string `json:"major"`
  16. DegreeName string `json:"degree_name"`
  17. AwardName string `json:"award_name"`
  18. AwardDate string `json:"award_date"`
  19. CreatedAt int64 `json:"-"`
  20. UpdatedAt int64 `json:"-"`
  21. }
  22. func (Dws24) TableName() string {
  23. return "t_adm_dws24"
  24. }
  25. // Insert 插入一条记录
  26. func (p *Dws24) Insert(db *gorm.DB) error {
  27. timeNow := time.Now().Unix()
  28. p.CreatedAt = timeNow
  29. p.UpdatedAt = timeNow
  30. err := db.Create(p).Error
  31. return err
  32. }
  33. func (p *Dws24) InsertByMap(db *gorm.DB, data map[string]interface{}) error {
  34. data["updated_at"] = time.Now().Unix()
  35. err := db.Model(p).Create(data).Error
  36. return err
  37. }
  38. func (p *Dws24) Delete(db *gorm.DB, filter map[string]interface{}) error {
  39. err := db.Where(filter).Delete(p).Error
  40. return err
  41. }
  42. func (p *Dws24) Save(db *gorm.DB) error {
  43. timeNow := time.Now().Unix()
  44. p.UpdatedAt = timeNow
  45. //db.Model().Update()
  46. err := db.Save(p).Error
  47. return err
  48. }
  49. func (p *Dws24) Update(db *gorm.DB) error {
  50. timeNow := time.Now().Unix()
  51. p.CreatedAt = 0
  52. p.UpdatedAt = timeNow
  53. err := db.Model(p).Updates(p).Error
  54. return err
  55. }
  56. func (p *Dws24) UpdateByMap(db *gorm.DB, data map[string]interface{}) error {
  57. data["updated_at"] = time.Now().Unix()
  58. err := db.Model(p).Updates(data).Error
  59. return err
  60. }
  61. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  62. func (p *Dws24) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  63. /*if filed == nil {
  64. return errors.ParamsError
  65. }*/
  66. timeNow := time.Now().Unix()
  67. filed["updated_at"] = timeNow
  68. err := db.Model(p).Updates(filed).Error
  69. return err
  70. }
  71. func (p *Dws24) Query(db *gorm.DB, filter map[string]interface{}) error {
  72. err := db.Where(filter).Find(p).Error
  73. if p.ID == 0 {
  74. return gorm.ErrRecordNotFound
  75. }
  76. return err
  77. }
  78. func (p *Dws24) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  79. err := db.Where(filter).Find(out).Error
  80. return err
  81. }