UserSignature.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package Account
  2. import (
  3. "ColdVerify_server/lib"
  4. "ColdVerify_server/logs"
  5. "log"
  6. "time"
  7. "github.com/beego/beego/v2/adapter/orm"
  8. orm2 "github.com/beego/beego/v2/client/orm"
  9. _ "github.com/go-sql-driver/mysql"
  10. )
  11. type UserSignature struct {
  12. Id int `orm:"column(ID);size(11);auto;pk"`
  13. T_Distributor_id string `orm:"size(256);null"` // 分销商id
  14. T_uuid string `orm:"size(256);null"` // 公司uuid
  15. T_name string `orm:"size(256);null"` // 姓名
  16. T_type int `orm:"size(256);default(1)"` // 1-签名 2-公章
  17. T_signature string `orm:"size(256);null"` // 签名
  18. T_State int `orm:"size(200);default(1)"` // 0删除 1正常
  19. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  20. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  21. }
  22. type UserSignature_R struct {
  23. Id int
  24. T_Distributor_id string
  25. T_Distributor_name string
  26. T_uuid string
  27. T_name string
  28. T_signature string
  29. T_type int
  30. }
  31. func (t *UserSignature) TableName() string {
  32. return "user_signature" // 数据库名称 // ************** 替换 FormulaList **************
  33. }
  34. func init() {
  35. //注册模型
  36. orm.RegisterModel(new(UserSignature))
  37. }
  38. // -------------------------------------------------------------
  39. func UserSignatureToUserSignature_R(T UserSignature, distributorMap map[string]string) (T_r UserSignature_R) {
  40. T_r.Id = T.Id
  41. T_r.T_uuid = T.T_uuid
  42. T_r.T_Distributor_id = T.T_Distributor_id
  43. T_Distributor_name, ok := distributorMap[T.T_Distributor_id]
  44. if !ok {
  45. T_Distributor_name = ""
  46. }
  47. T_r.T_Distributor_name = T_Distributor_name
  48. T_r.T_name = T.T_name
  49. T_r.T_signature = T.T_signature
  50. T_r.T_type = T.T_type
  51. return T_r
  52. }
  53. // 获取 ById
  54. func Read_UserSignature_ById(id int) (r UserSignature, is bool) {
  55. o := orm.NewOrm()
  56. r = UserSignature{Id: id, T_State: 1}
  57. err := o.Read(&r, "T_State", "Id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  58. if err != nil {
  59. logs.Error(lib.FuncName(), err)
  60. return r, false
  61. }
  62. return r, true
  63. }
  64. // 获取 By T_name
  65. func Read_UserSignatureByT_name(T_uuid, T_name string) (r UserSignature, is bool) {
  66. o := orm.NewOrm()
  67. qs := o.QueryTable(new(UserSignature))
  68. err := qs.Filter("T_uuid", T_uuid).Filter("T_name", T_name).Filter("T_State", 1).One(&r)
  69. if err != nil {
  70. return r, false
  71. }
  72. return r, true
  73. }
  74. // 添加
  75. func Add_UserSignature(r UserSignature) (id int64, is bool) {
  76. o := orm.NewOrm()
  77. id, err := o.Insert(&r)
  78. if err != nil {
  79. logs.Error(lib.FuncName(), err)
  80. return 0, false
  81. }
  82. return id, true
  83. }
  84. // 删除
  85. func Delete_UserSignature(v UserSignature) bool {
  86. o := orm.NewOrm()
  87. v.T_State = 0
  88. if num, err := o.Update(&v, "T_State"); err == nil {
  89. log.Println("Number of records updated in database:", num)
  90. } else {
  91. return false
  92. }
  93. return true
  94. }
  95. // 修改
  96. func Update_UserSignature(m UserSignature, cols ...string) bool {
  97. o := orm.NewOrm()
  98. if num, err := o.Update(&m, cols...); err == nil {
  99. log.Println("Number of records updated in database:", num)
  100. return true
  101. }
  102. return false
  103. }
  104. // 获取列表
  105. func Read_UserSignature_List(T_uuid string, T_name string, T_type, page int, page_z int, distributorMap map[string]string) ([]UserSignature_R, int64) {
  106. o := orm.NewOrm()
  107. // 也可以直接使用 Model 结构体作为表名
  108. var r []UserSignature
  109. qs := o.QueryTable(new(UserSignature))
  110. var offset int64
  111. if page <= 1 {
  112. offset = 0
  113. } else {
  114. offset = int64((page - 1) * page_z)
  115. }
  116. cond := orm.NewCondition()
  117. cond1 := cond.And("T_State", 1).And("T_uuid", T_uuid).AndCond(cond.Or("T_name__icontains", T_name))
  118. if T_type > 0 {
  119. cond1 = cond1.And("T_type", T_type)
  120. }
  121. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  122. cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
  123. // 转换
  124. var UserSignature_r []UserSignature_R
  125. for _, v := range r {
  126. UserSignature_r = append(UserSignature_r, UserSignatureToUserSignature_R(v, distributorMap))
  127. }
  128. return UserSignature_r, cnt
  129. }