123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- package InfoCollection
- import (
- "ColdVerify_server/conf"
- "ColdVerify_server/lib"
- "ColdVerify_server/logs"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/astaxie/beego/cache"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- _ "github.com/go-sql-driver/mysql"
- "time"
- )
- // 模板
- type InfoCollection struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_InfoCollection_id string `orm:"size(256);null"` // 信息采集ID
- T_uuid string `orm:"size(256);null"` // 用户 UUID
- T_name string `orm:"size(256);null"` // 标题
- T_InfoTemplate_class string `orm:"size(256);null"` // 模板id
- T_InfoTemplate_id string `orm:"size(256);null"` // 模板id
- T_status int `orm:"size(2);default(0)"` // 状态 1 待提交
- T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
- 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 InfoCollection_R struct {
- Id int
- T_InfoCollection_id string // 信息采集ID
- T_uuid string // 用户 UUID
- T_uuid_name string // 用户姓名
- T_name string // 标题
- T_InfoTemplate_class string // 模板id
- T_InfoTemplate_id string // 模板id
- T_status int // 状态
- T_State int // 0 删除 1 正常
- CreateTime string
- UpdateTime string
- }
- func (t *InfoCollection) TableName() string {
- return "info_collection" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redisCache_InfoCollection cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(InfoCollection))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_"+"InfoCollection", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- logs.Println(config)
- var err error
- redisCache_InfoCollection, err = cache.NewCache("redis", config)
- if err != nil || redisCache_InfoCollection == nil {
- errMsg := "failed to init redis"
- logs.Println(errMsg, err)
- }
- }
- // -------------------------------------------------------------
- func InfoCollectionToInfoCollection_R(T InfoCollection, adminMap map[string]string) (T_r InfoCollection_R) {
- T_r.T_InfoCollection_id = T.T_InfoCollection_id
- T_r.T_uuid = T.T_uuid
- T_r.T_name = T.T_name
- T_r.T_InfoTemplate_class = T.T_InfoTemplate_class
- T_r.T_InfoTemplate_id = T.T_InfoTemplate_id
- T_r.T_status = T.T_status
- T_r.T_State = T.T_State
- T_r.T_uuid_name = adminMap[T.T_uuid]
- T_r.UpdateTime = T.UpdateTime.Format("2006-01-02 15:04:05")
- T_r.CreateTime = T.CreateTime.Format("2006-01-02 15:04:05")
- //......
- return T_r
- }
- // ---------------- Redis -------------------
- // Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_InfoCollection_Set(key string, r InfoCollection) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- err = redisCache_InfoCollection.Put(key, str, 24*time.Hour)
- if err != nil {
- logs.Println("set key:", key, ",value:", str, err)
- }
- return
- }
- // if r,is :=Redis_Get(T_sn);is{
- // return r,nil
- // }
- func Redis_InfoCollection_Get(key string) (r InfoCollection, is bool) {
- if redisCache_InfoCollection.IsExist(key) {
- logs.Println("找到key:", key)
- v := redisCache_InfoCollection.Get(key)
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- logs.Println("没有 找到key:", key)
- return InfoCollection{}, false
- }
- func Redis_InfoCollection_DelK(key string) (err error) {
- err = redisCache_InfoCollection.Delete(key)
- return
- }
- // ---------------- 特殊方法 -------------------
- // 获取 ById
- func Read_InfoCollection_ById(id int) (r InfoCollection, is bool) {
- o := orm.NewOrm()
- r = InfoCollection{Id: id}
- err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, false
- }
- return r, true
- }
- // 获取 By
- func Read_InfoCollection(T_InfoCollection_id string) (r InfoCollection, is bool) {
- if r, is = Redis_InfoCollection_Get(T_InfoCollection_id); is == true {
- return r, true
- }
- o := orm.NewOrm()
- qs := o.QueryTable(new(InfoCollection))
- //err := qs.Filter("T_InfoCollection_id", T_InfoCollection_id).Filter("T_State", 1).One(&r)
- err := qs.Filter("T_InfoCollection_id", T_InfoCollection_id).Filter("T_State", 1).One(&r)
- if err != nil {
- return r, false
- }
- Redis_InfoCollection_Set(T_InfoCollection_id, r)
- return r, true
- }
- // 添加
- func Add_InfoCollection(r InfoCollection) (string, error) {
- o := orm.NewOrm()
- // 查询标题是否重复
- err := o.Read(&r, "T_name")
- if err == nil {
- return "", errors.New("信息采集标题重复")
- }
- // 生成编号
- rand_x := 0
- for true {
- r.T_InfoCollection_id = lib.GetRandstring(12, "abcdefghijklmnopqrstuvwxyz0123456789", int64(rand_x)) // 1,336,336
- err = o.Read(&r, "T_InfoCollection_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- break
- }
- rand_x += 1
- }
- _, err = o.Insert(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return "", errors.New("添加失败")
- }
- Redis_InfoCollection_Set(r.T_InfoCollection_id, r)
- return r.T_InfoCollection_id, nil
- }
- // 删除
- func Delete_InfoCollection(v InfoCollection) bool {
- o := orm.NewOrm()
- v.T_State = 0
- if num, err := o.Update(&v, "T_State"); err == nil {
- logs.Println("Number of records updated in database:", num)
- } else {
- logs.Error(lib.FuncName(), err)
- return false
- }
- Redis_InfoCollection_DelK(v.T_InfoCollection_id)
- return true
- }
- // 修改
- func Update_InfoCollection(m InfoCollection, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, cols...); err == nil {
- logs.Println("Number of records updated in database:", num)
- Redis_InfoCollection_Set(m.T_InfoCollection_id, m)
- return true
- } else {
- logs.Error(lib.FuncName(), err)
- }
- return false
- }
- // 获取用户信息采集列表
- func Read_UserInfoCollection_List(T_uuid string, T_name string, adminMap map[string]string, page int, page_z int) ([]InfoCollection_R, int) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(InfoCollection))
- var r []InfoCollection
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- cond := orm.NewCondition()
- cond1 := cond.And("T_name__icontains", T_name).And("T_State", 1)
- if len(T_uuid) > 0 {
- cond1 = cond1.And("T_uuid", T_uuid)
- }
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
- cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
- // 转换
- var InfoCollectionList []InfoCollection_R
- for _, v := range r {
- InfoCollectionList = append(InfoCollectionList, InfoCollectionToInfoCollection_R(v, adminMap))
- }
- return InfoCollectionList, int(cnt)
- }
- // 获取信息采集列表
- func Read_InfoCollection_List(T_uuid, T_admin string, T_name string, adminMap map[string]string, page int, page_z int) ([]InfoCollection_R, int) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(InfoCollection))
- var r []InfoCollection
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- cond := orm.NewCondition()
- cond1 := cond.AndCond(cond.Or("T_name__icontains", T_name).Or("T_InfoCollection_id", T_name)).And("T_State", 1)
- if len(T_uuid) > 0 {
- cond1 = cond1.And("T_uuid", T_uuid)
- }
- if len(T_admin) > 0 {
- cond1 = cond1.AndCond(cond.Or("T_scheme", T_admin).Or("T_collection", T_admin).
- Or("T_reporting", T_admin).Or("T_delivery", T_admin))
- }
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
- cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
- // 转换
- var InfoCollectionList []InfoCollection_R
- for _, v := range r {
- InfoCollectionList = append(InfoCollectionList, InfoCollectionToInfoCollection_R(v, adminMap))
- }
- return InfoCollectionList, int(cnt)
- }
|