123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package InfoCollection
- import (
- "ColdVerify_server/lib"
- "ColdVerify_server/logs"
- "fmt"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- _ "github.com/go-sql-driver/mysql"
- "time"
- )
- // 模板
- type InfoTemplateMapData struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_InfoCollection_id string `orm:"index,size(256);null"` // 信息采集id
- T_InfoTemplate_id string `orm:"index,size(256);null"` // 模板id
- T_InfoTemplateMap_id string `orm:"index,size(256);null"` // 标签id
- T_value string `orm:"size(1024);null"` // 内容
- CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
- UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
- }
- type InfoTemplateMapData_R struct {
- T_id int
- T_InfoTemplateMap_id string // 标签id
- T_label int //
- T_name string // 标题
- T_text string // 标题
- T_value string //
- }
- func (t *InfoTemplateMapData) TableName() string {
- return "info_template_map_data" // 数据库名称 // ************** 替换 FormulaList **************
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(InfoTemplateMapData))
- }
- // -------------------------------------------------------------
- func InfoTemplateMapToInfoTemplateMapData_R(T InfoTemplateMap) (T_r InfoTemplateMapData_R) {
- T_r.T_InfoTemplateMap_id = T.T_id
- T_r.T_label = T.T_label
- T_r.T_name = T.T_name
- T_r.T_text = T.T_text
- T_r.T_value = ""
- return T_r
- }
- func InfoTemplateMapDataToInfoTemplateMapData_R(T InfoTemplateMap, InfoTemplateMapData map[string]InfoTemplateMapData) (T_r InfoTemplateMapData_R) {
- T_r.T_InfoTemplateMap_id = T.T_id
- T_r.T_label = T.T_label
- T_r.T_name = T.T_name
- T_r.T_text = T.T_text
- T_r.T_value = ""
- if v, ok := InfoTemplateMapData[T.T_id]; ok {
- T_r.T_value = v.T_value
- }
- return T_r
- }
- // ---------------- 特殊方法 -------------------
- // 添加
- func AddOrUpdate_InfoTemplateMapData(List []InfoTemplateMapData) (ids []int64, is bool) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(InfoTemplateMapData))
- o.Begin()
- for _, v := range List {
- var r InfoTemplateMapData
- // 创建验证模版数据,有则更新,没有则创建
- if v.T_InfoTemplateMap_id == "gVqW" {
- fmt.Println(v.T_value)
- }
- 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)
- if err != nil {
- if err.Error() == orm.ErrNoRows.Error() {
- id, err := o.Insert(&v)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- o.Rollback()
- return ids, false
- }
- ids = append(ids, id)
- } else {
- logs.Error(lib.FuncName(), err)
- o.Rollback()
- return ids, false
- }
- }
- // 更新验证模版数据
- if r.Id > 0 && len(v.T_value) > 0 {
- v.Id = r.Id
- //_, err = o.Update(&v, "T_flow_sort", "T_max_time", "T_min_time", "T_value")
- _, err = o.QueryTable(new(InfoTemplateMapData)).Filter("T_InfoCollection_id", v.T_InfoCollection_id).Filter("T_InfoTemplate_id", v.T_InfoTemplate_id).
- Filter("T_InfoTemplateMap_id", v.T_InfoTemplateMap_id).Update(orm2.Params{"T_value": v.T_value})
- if err != nil {
- logs.Error(lib.FuncName(), err)
- o.Rollback()
- return ids, false
- }
- ids = append(ids, int64(r.Id))
- }
- }
- o.Commit()
- return ids, true
- }
- // 获取列表
- func Read_InfoTemplateMapData_List(T_InfoCollection_id, T_InfoTemplate_id string, Map_List []InfoTemplateMap) []InfoTemplateMapData_R {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- var r []InfoTemplateMapData
- qs := o.QueryTable(new(InfoTemplateMapData))
- cond := orm.NewCondition()
- cond1 := cond.And("T_InfoCollection_id", T_InfoCollection_id).And("T_InfoTemplate_id", T_InfoTemplate_id)
- qs.SetCond((*orm2.Condition)(cond1)).All(&r)
- if len(r) == 0 {
- // 转换
- var InfoTemplateMapData_r []InfoTemplateMapData_R
- for k, v := range Map_List {
- Data_r := InfoTemplateMapToInfoTemplateMapData_R(v)
- Data_r.T_id = k
- InfoTemplateMapData_r = append(InfoTemplateMapData_r, Data_r)
- }
- return InfoTemplateMapData_r
- }
- // 转换
- var InfoTemplateMapData_r []InfoTemplateMapData_R
- for k, v := range Map_List {
- Data_r := InfoTemplateMapDataToInfoTemplateMapData_R(v, InfoTemplateMapDataToMap(r))
- Data_r.T_id = k
- InfoTemplateMapData_r = append(InfoTemplateMapData_r, Data_r)
- }
- return InfoTemplateMapData_r
- }
- // 获取列表
- func Read_MapData_List(T_InfoCollection_id, T_InfoTemplate_id string) []InfoTemplateMapData {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- var r []InfoTemplateMapData
- qs := o.QueryTable(new(InfoTemplateMapData))
- cond := orm.NewCondition()
- cond1 := cond.And("T_InfoCollection_id", T_InfoCollection_id).And("T_InfoTemplate_id", T_InfoTemplate_id)
- qs.SetCond((*orm2.Condition)(cond1)).All(&r)
- return r
- }
- func InfoTemplateMapDataToMap(T []InfoTemplateMapData) map[string]InfoTemplateMapData {
- maps := make(map[string]InfoTemplateMapData, len(T))
- for _, v := range T {
- if _, ok := maps[v.T_InfoTemplateMap_id]; ok {
- continue
- }
- maps[v.T_InfoTemplateMap_id] = v
- }
- return maps
- }
- // 清除T_value数据
- func Clear_InfoTemplateMapData_T_value(T_InfoCollection_id, T_InfoTemplate_id, T_InfoTemplateMap_id string) (err error) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(InfoTemplateMapData))
- var r InfoTemplateMapData
- // 创建验证模版数据,有则更新,没有则创建
- 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)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- r.T_value = ""
- _, err = o.Update(&r, "T_value")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- return nil
- }
|