VerifyTemplateMapData.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package InfoCollection
  2. import (
  3. "ColdVerify_server/lib"
  4. "ColdVerify_server/logs"
  5. "github.com/beego/beego/v2/adapter/orm"
  6. orm2 "github.com/beego/beego/v2/client/orm"
  7. _ "github.com/go-sql-driver/mysql"
  8. "time"
  9. )
  10. // 模板
  11. type InfoTemplateMapData struct {
  12. Id int `orm:"column(ID);size(11);auto;pk"`
  13. //T_source int `orm:"size(2);"` // 来源 0-所有 1-方案 2-报告 3-表单
  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. 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)
  66. if err != nil {
  67. if err.Error() == orm.ErrNoRows.Error() {
  68. id, err := o.Insert(&v)
  69. if err != nil {
  70. logs.Error(lib.FuncName(), err)
  71. o.Rollback()
  72. return ids, false
  73. }
  74. ids = append(ids, id)
  75. } else {
  76. logs.Error(lib.FuncName(), err)
  77. o.Rollback()
  78. return ids, false
  79. }
  80. }
  81. // 更新验证模版数据
  82. if r.Id > 0 && len(v.T_value) > 0 {
  83. v.Id = r.Id
  84. //_, err = o.Update(&v, "T_flow_sort", "T_max_time", "T_min_time", "T_value")
  85. _, err = o.QueryTable(new(InfoTemplateMapData)).Filter("T_InfoCollection_id", v.T_InfoCollection_id).Filter("T_InfoTemplate_id", v.T_InfoTemplate_id).
  86. Filter("T_InfoTemplateMap_id", v.T_InfoTemplateMap_id).Update(orm2.Params{"T_value": v.T_value})
  87. if err != nil {
  88. logs.Error(lib.FuncName(), err)
  89. o.Rollback()
  90. return ids, false
  91. }
  92. ids = append(ids, int64(r.Id))
  93. }
  94. }
  95. o.Commit()
  96. return ids, true
  97. }
  98. // 获取列表
  99. func Read_InfoTemplateMapData_List(T_InfoCollection_id, T_InfoTemplate_id string, Map_List []InfoTemplateMap) []InfoTemplateMapData_R {
  100. o := orm.NewOrm()
  101. // 也可以直接使用 Model 结构体作为表名
  102. var r []InfoTemplateMapData
  103. qs := o.QueryTable(new(InfoTemplateMapData))
  104. cond := orm.NewCondition()
  105. cond1 := cond.And("T_InfoCollection_id", T_InfoCollection_id).And("T_InfoTemplate_id", T_InfoTemplate_id)
  106. qs.SetCond((*orm2.Condition)(cond1)).All(&r)
  107. if len(r) == 0 {
  108. // 转换
  109. var InfoTemplateMapData_r []InfoTemplateMapData_R
  110. for k, v := range Map_List {
  111. Data_r := InfoTemplateMapToInfoTemplateMapData_R(v)
  112. Data_r.T_id = k
  113. InfoTemplateMapData_r = append(InfoTemplateMapData_r, Data_r)
  114. }
  115. return InfoTemplateMapData_r
  116. }
  117. // 转换
  118. var InfoTemplateMapData_r []InfoTemplateMapData_R
  119. for k, v := range Map_List {
  120. Data_r := InfoTemplateMapDataToInfoTemplateMapData_R(v, InfoTemplateMapDataToMap(r))
  121. Data_r.T_id = k
  122. InfoTemplateMapData_r = append(InfoTemplateMapData_r, Data_r)
  123. }
  124. return InfoTemplateMapData_r
  125. }
  126. // 获取列表
  127. func Read_MapData_List(T_source int, T_InfoCollection_id, T_InfoTemplate_id string) []InfoTemplateMapData {
  128. o := orm.NewOrm()
  129. // 也可以直接使用 Model 结构体作为表名
  130. var r []InfoTemplateMapData
  131. qs := o.QueryTable(new(InfoTemplateMapData))
  132. cond := orm.NewCondition()
  133. source := []int{T_source}
  134. if T_source == 2 {
  135. source = append(source, 3)
  136. }
  137. if T_source != 3 {
  138. source = append(source, 0)
  139. }
  140. cond1 := cond.And("T_source__in", source).And("T_InfoCollection_id", T_InfoCollection_id).And("T_InfoTemplate_id", T_InfoTemplate_id)
  141. qs.SetCond((*orm2.Condition)(cond1)).All(&r)
  142. return r
  143. }
  144. func InfoTemplateMapDataToMap(T []InfoTemplateMapData) map[string]InfoTemplateMapData {
  145. maps := make(map[string]InfoTemplateMapData, len(T))
  146. for _, v := range T {
  147. if _, ok := maps[v.T_InfoTemplateMap_id]; ok {
  148. continue
  149. }
  150. maps[v.T_InfoTemplateMap_id] = v
  151. }
  152. return maps
  153. }
  154. // 清除T_value数据
  155. func Clear_InfoTemplateMapData_T_value(T_InfoCollection_id, T_InfoTemplate_id, T_InfoTemplateMap_id string) (err error) {
  156. o := orm.NewOrm()
  157. qs := o.QueryTable(new(InfoTemplateMapData))
  158. var r InfoTemplateMapData
  159. // 创建验证模版数据,有则更新,没有则创建
  160. 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)
  161. if err != nil {
  162. logs.Error(lib.FuncName(), err)
  163. return err
  164. }
  165. r.T_value = ""
  166. _, err = o.Update(&r, "T_value")
  167. if err != nil {
  168. logs.Error(lib.FuncName(), err)
  169. return err
  170. }
  171. return nil
  172. }