VerifyTemplateMapData.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package InfoCollection
  2. import (
  3. "ColdVerify_server/lib"
  4. "ColdVerify_server/logs"
  5. "fmt"
  6. "github.com/beego/beego/v2/adapter/orm"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  8. _ "github.com/go-sql-driver/mysql"
  9. "time"
  10. )
  11. // 模板
  12. type InfoTemplateMapData struct {
  13. Id int `orm:"column(ID);size(11);auto;pk"`
  14. T_InfoCollection_id string `orm:"index,size(256);null"` // 信息采集id
  15. T_InfoTemplate_id string `orm:"index,size(256);null"` // 模板id
  16. T_InfoTemplateMap_id string `orm:"index,size(256);null"` // 标签id
  17. T_value string `orm:"size(1024);null"` // 内容
  18. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  19. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  20. }
  21. type InfoTemplateMapData_R struct {
  22. T_id int
  23. T_InfoTemplateMap_id string // 标签id
  24. T_label int //
  25. T_name string // 标题
  26. T_text string // 标题
  27. T_value string //
  28. }
  29. func (t *InfoTemplateMapData) TableName() string {
  30. return "info_template_map_data" // 数据库名称 // ************** 替换 FormulaList **************
  31. }
  32. func init() {
  33. //注册模型
  34. orm.RegisterModel(new(InfoTemplateMapData))
  35. }
  36. // -------------------------------------------------------------
  37. func InfoTemplateMapToInfoTemplateMapData_R(T InfoTemplateMap) (T_r InfoTemplateMapData_R) {
  38. T_r.T_InfoTemplateMap_id = T.T_id
  39. T_r.T_label = T.T_label
  40. T_r.T_name = T.T_name
  41. T_r.T_text = T.T_text
  42. T_r.T_value = ""
  43. return T_r
  44. }
  45. func InfoTemplateMapDataToInfoTemplateMapData_R(T InfoTemplateMap, InfoTemplateMapData map[string]InfoTemplateMapData) (T_r InfoTemplateMapData_R) {
  46. T_r.T_InfoTemplateMap_id = T.T_id
  47. T_r.T_label = T.T_label
  48. T_r.T_name = T.T_name
  49. T_r.T_text = T.T_text
  50. T_r.T_value = ""
  51. if v, ok := InfoTemplateMapData[T.T_id]; ok {
  52. T_r.T_value = v.T_value
  53. }
  54. return T_r
  55. }
  56. // ---------------- 特殊方法 -------------------
  57. // 添加
  58. func AddOrUpdate_InfoTemplateMapData(List []InfoTemplateMapData) (ids []int64, is bool) {
  59. o := orm.NewOrm()
  60. qs := o.QueryTable(new(InfoTemplateMapData))
  61. o.Begin()
  62. for _, v := range List {
  63. var r InfoTemplateMapData
  64. // 创建验证模版数据,有则更新,没有则创建
  65. if v.T_InfoTemplateMap_id == "gVqW" {
  66. fmt.Println(v.T_value)
  67. }
  68. err := qs.Filter("T_InfoCollection_id", v.T_InfoCollection_id).Filter("T_InfoTemplate_id", v.T_InfoTemplate_id).Filter("T_InfoTemplateMap_id", v.T_InfoTemplateMap_id).One(&r)
  69. if err != nil {
  70. if err.Error() == orm.ErrNoRows.Error() {
  71. id, err := o.Insert(&v)
  72. if err != nil {
  73. logs.Error(lib.FuncName(), err)
  74. o.Rollback()
  75. return ids, false
  76. }
  77. ids = append(ids, id)
  78. } else {
  79. logs.Error(lib.FuncName(), err)
  80. o.Rollback()
  81. return ids, false
  82. }
  83. }
  84. // 更新验证模版数据
  85. if r.Id > 0 && len(v.T_value) > 0 {
  86. v.Id = r.Id
  87. //_, err = o.Update(&v, "T_flow_sort", "T_max_time", "T_min_time", "T_value")
  88. _, err = o.QueryTable(new(InfoTemplateMapData)).Filter("T_InfoCollection_id", v.T_InfoCollection_id).Filter("T_InfoTemplate_id", v.T_InfoTemplate_id).
  89. Filter("T_InfoTemplateMap_id", v.T_InfoTemplateMap_id).Update(orm2.Params{"T_value": v.T_value})
  90. if err != nil {
  91. logs.Error(lib.FuncName(), err)
  92. o.Rollback()
  93. return ids, false
  94. }
  95. ids = append(ids, int64(r.Id))
  96. }
  97. }
  98. o.Commit()
  99. return ids, true
  100. }
  101. // 获取列表
  102. func Read_InfoTemplateMapData_List(T_InfoCollection_id, T_InfoTemplate_id string, Map_List []InfoTemplateMap) []InfoTemplateMapData_R {
  103. o := orm.NewOrm()
  104. // 也可以直接使用 Model 结构体作为表名
  105. var r []InfoTemplateMapData
  106. qs := o.QueryTable(new(InfoTemplateMapData))
  107. cond := orm.NewCondition()
  108. cond1 := cond.And("T_InfoCollection_id", T_InfoCollection_id).And("T_InfoTemplate_id", T_InfoTemplate_id)
  109. qs.SetCond((*orm2.Condition)(cond1)).All(&r)
  110. if len(r) == 0 {
  111. // 转换
  112. var InfoTemplateMapData_r []InfoTemplateMapData_R
  113. for k, v := range Map_List {
  114. Data_r := InfoTemplateMapToInfoTemplateMapData_R(v)
  115. Data_r.T_id = k
  116. InfoTemplateMapData_r = append(InfoTemplateMapData_r, Data_r)
  117. }
  118. return InfoTemplateMapData_r
  119. }
  120. // 转换
  121. var InfoTemplateMapData_r []InfoTemplateMapData_R
  122. for k, v := range Map_List {
  123. Data_r := InfoTemplateMapDataToInfoTemplateMapData_R(v, InfoTemplateMapDataToMap(r))
  124. Data_r.T_id = k
  125. InfoTemplateMapData_r = append(InfoTemplateMapData_r, Data_r)
  126. }
  127. return InfoTemplateMapData_r
  128. }
  129. // 获取列表
  130. func Read_MapData_List(T_InfoCollection_id, T_InfoTemplate_id string) []InfoTemplateMapData {
  131. o := orm.NewOrm()
  132. // 也可以直接使用 Model 结构体作为表名
  133. var r []InfoTemplateMapData
  134. qs := o.QueryTable(new(InfoTemplateMapData))
  135. cond := orm.NewCondition()
  136. cond1 := cond.And("T_InfoCollection_id", T_InfoCollection_id).And("T_InfoTemplate_id", T_InfoTemplate_id)
  137. qs.SetCond((*orm2.Condition)(cond1)).All(&r)
  138. return r
  139. }
  140. func InfoTemplateMapDataToMap(T []InfoTemplateMapData) map[string]InfoTemplateMapData {
  141. maps := make(map[string]InfoTemplateMapData, len(T))
  142. for _, v := range T {
  143. if _, ok := maps[v.T_InfoTemplateMap_id]; ok {
  144. continue
  145. }
  146. maps[v.T_InfoTemplateMap_id] = v
  147. }
  148. return maps
  149. }
  150. // 清除T_value数据
  151. func Clear_InfoTemplateMapData_T_value(T_InfoCollection_id, T_InfoTemplate_id, T_InfoTemplateMap_id string) (err error) {
  152. o := orm.NewOrm()
  153. qs := o.QueryTable(new(InfoTemplateMapData))
  154. var r InfoTemplateMapData
  155. // 创建验证模版数据,有则更新,没有则创建
  156. err = qs.Filter("T_InfoCollection_id", T_InfoCollection_id).Filter("T_InfoTemplate_id", T_InfoTemplate_id).Filter("T_InfoTemplateMap_id", T_InfoTemplateMap_id).One(&r)
  157. if err != nil {
  158. logs.Error(lib.FuncName(), err)
  159. return err
  160. }
  161. r.T_value = ""
  162. _, err = o.Update(&r, "T_value")
  163. if err != nil {
  164. logs.Error(lib.FuncName(), err)
  165. return err
  166. }
  167. return nil
  168. }