InfoCollection.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package InfoCollection
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/logs"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "github.com/astaxie/beego/cache"
  10. "github.com/beego/beego/v2/adapter/orm"
  11. orm2 "github.com/beego/beego/v2/client/orm"
  12. _ "github.com/go-sql-driver/mysql"
  13. "time"
  14. )
  15. // 模板
  16. type InfoCollection struct {
  17. Id int `orm:"column(ID);size(11);auto;pk"`
  18. T_InfoCollection_id string `orm:"size(256);null"` // 信息采集ID
  19. T_uuid string `orm:"size(256);null"` // 用户 UUID
  20. T_name string `orm:"size(256);null"` // 标题
  21. T_InfoTemplate_class string `orm:"size(256);null"` // 模板id
  22. T_InfoTemplate_id string `orm:"size(256);null"` // 模板id
  23. T_status int `orm:"size(2);default(0)"` // 状态 1 待提交
  24. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  25. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  26. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  27. }
  28. type InfoCollection_R struct {
  29. Id int
  30. T_InfoCollection_id string // 信息采集ID
  31. T_uuid string // 用户 UUID
  32. T_uuid_name string // 用户姓名
  33. T_name string // 标题
  34. T_InfoTemplate_class string // 模板id
  35. T_InfoTemplate_id string // 模板id
  36. T_status int // 状态
  37. T_State int // 0 删除 1 正常
  38. CreateTime string
  39. UpdateTime string
  40. }
  41. func (t *InfoCollection) TableName() string {
  42. return "info_collection" // 数据库名称 // ************** 替换 FormulaList **************
  43. }
  44. var redisCache_InfoCollection cache.Cache
  45. func init() {
  46. //注册模型
  47. orm.RegisterModel(new(InfoCollection))
  48. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  49. "redis_"+"InfoCollection", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  50. logs.Println(config)
  51. var err error
  52. redisCache_InfoCollection, err = cache.NewCache("redis", config)
  53. if err != nil || redisCache_InfoCollection == nil {
  54. errMsg := "failed to init redis"
  55. logs.Println(errMsg, err)
  56. }
  57. }
  58. // -------------------------------------------------------------
  59. func InfoCollectionToInfoCollection_R(T InfoCollection, adminMap map[string]string) (T_r InfoCollection_R) {
  60. T_r.T_InfoCollection_id = T.T_InfoCollection_id
  61. T_r.T_uuid = T.T_uuid
  62. T_r.T_name = T.T_name
  63. T_r.T_InfoTemplate_class = T.T_InfoTemplate_class
  64. T_r.T_InfoTemplate_id = T.T_InfoTemplate_id
  65. T_r.T_status = T.T_status
  66. T_r.T_State = T.T_State
  67. T_r.T_uuid_name = adminMap[T.T_uuid]
  68. T_r.UpdateTime = T.UpdateTime.Format("2006-01-02 15:04:05")
  69. T_r.CreateTime = T.CreateTime.Format("2006-01-02 15:04:05")
  70. //......
  71. return T_r
  72. }
  73. // ---------------- Redis -------------------
  74. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  75. func Redis_InfoCollection_Set(key string, r InfoCollection) (err error) {
  76. //json序列化
  77. str, err := json.Marshal(r)
  78. if err != nil {
  79. logs.Error(lib.FuncName(), err)
  80. return
  81. }
  82. err = redisCache_InfoCollection.Put(key, str, 24*time.Hour)
  83. if err != nil {
  84. logs.Println("set key:", key, ",value:", str, err)
  85. }
  86. return
  87. }
  88. // if r,is :=Redis_Get(T_sn);is{
  89. // return r,nil
  90. // }
  91. func Redis_InfoCollection_Get(key string) (r InfoCollection, is bool) {
  92. if redisCache_InfoCollection.IsExist(key) {
  93. logs.Println("找到key:", key)
  94. v := redisCache_InfoCollection.Get(key)
  95. json.Unmarshal(v.([]byte), &r)
  96. return r, true
  97. }
  98. logs.Println("没有 找到key:", key)
  99. return InfoCollection{}, false
  100. }
  101. func Redis_InfoCollection_DelK(key string) (err error) {
  102. err = redisCache_InfoCollection.Delete(key)
  103. return
  104. }
  105. // ---------------- 特殊方法 -------------------
  106. // 获取 ById
  107. func Read_InfoCollection_ById(id int) (r InfoCollection, is bool) {
  108. o := orm.NewOrm()
  109. r = InfoCollection{Id: id}
  110. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  111. if err != nil {
  112. logs.Error(lib.FuncName(), err)
  113. return r, false
  114. }
  115. return r, true
  116. }
  117. // 获取 By
  118. func Read_InfoCollection(T_InfoCollection_id string) (r InfoCollection, is bool) {
  119. if r, is = Redis_InfoCollection_Get(T_InfoCollection_id); is == true {
  120. return r, true
  121. }
  122. o := orm.NewOrm()
  123. qs := o.QueryTable(new(InfoCollection))
  124. //err := qs.Filter("T_InfoCollection_id", T_InfoCollection_id).Filter("T_State", 1).One(&r)
  125. err := qs.Filter("T_InfoCollection_id", T_InfoCollection_id).Filter("T_State", 1).One(&r)
  126. if err != nil {
  127. return r, false
  128. }
  129. Redis_InfoCollection_Set(T_InfoCollection_id, r)
  130. return r, true
  131. }
  132. // 添加
  133. func Add_InfoCollection(r InfoCollection) (string, error) {
  134. o := orm.NewOrm()
  135. // 查询标题是否重复
  136. err := o.Read(&r, "T_name")
  137. if err == nil {
  138. return "", errors.New("信息采集标题重复")
  139. }
  140. // 生成编号
  141. rand_x := 0
  142. for true {
  143. r.T_InfoCollection_id = lib.GetRandstring(12, "abcdefghijklmnopqrstuvwxyz0123456789", int64(rand_x)) // 1,336,336
  144. err = o.Read(&r, "T_InfoCollection_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  145. if err != nil {
  146. break
  147. }
  148. rand_x += 1
  149. }
  150. _, err = o.Insert(&r)
  151. if err != nil {
  152. logs.Error(lib.FuncName(), err)
  153. return "", errors.New("添加失败")
  154. }
  155. Redis_InfoCollection_Set(r.T_InfoCollection_id, r)
  156. return r.T_InfoCollection_id, nil
  157. }
  158. // 删除
  159. func Delete_InfoCollection(v InfoCollection) bool {
  160. o := orm.NewOrm()
  161. v.T_State = 0
  162. if num, err := o.Update(&v, "T_State"); err == nil {
  163. logs.Println("Number of records updated in database:", num)
  164. } else {
  165. logs.Error(lib.FuncName(), err)
  166. return false
  167. }
  168. Redis_InfoCollection_DelK(v.T_InfoCollection_id)
  169. return true
  170. }
  171. // 修改
  172. func Update_InfoCollection(m InfoCollection, cols ...string) bool {
  173. o := orm.NewOrm()
  174. if num, err := o.Update(&m, cols...); err == nil {
  175. logs.Println("Number of records updated in database:", num)
  176. Redis_InfoCollection_Set(m.T_InfoCollection_id, m)
  177. return true
  178. } else {
  179. logs.Error(lib.FuncName(), err)
  180. }
  181. return false
  182. }
  183. // 获取用户信息采集列表
  184. func Read_UserInfoCollection_List(T_uuid string, T_name string, adminMap map[string]string, page int, page_z int) ([]InfoCollection_R, int) {
  185. o := orm.NewOrm()
  186. qs := o.QueryTable(new(InfoCollection))
  187. var r []InfoCollection
  188. var offset int64
  189. if page <= 1 {
  190. offset = 0
  191. } else {
  192. offset = int64((page - 1) * page_z)
  193. }
  194. cond := orm.NewCondition()
  195. cond1 := cond.And("T_name__icontains", T_name).And("T_State", 1)
  196. if len(T_uuid) > 0 {
  197. cond1 = cond1.And("T_uuid", T_uuid)
  198. }
  199. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  200. cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
  201. // 转换
  202. var InfoCollectionList []InfoCollection_R
  203. for _, v := range r {
  204. InfoCollectionList = append(InfoCollectionList, InfoCollectionToInfoCollection_R(v, adminMap))
  205. }
  206. return InfoCollectionList, int(cnt)
  207. }
  208. // 获取信息采集列表
  209. func Read_InfoCollection_List(T_uuid, T_admin string, T_name string, adminMap map[string]string, page int, page_z int) ([]InfoCollection_R, int) {
  210. o := orm.NewOrm()
  211. qs := o.QueryTable(new(InfoCollection))
  212. var r []InfoCollection
  213. var offset int64
  214. if page <= 1 {
  215. offset = 0
  216. } else {
  217. offset = int64((page - 1) * page_z)
  218. }
  219. cond := orm.NewCondition()
  220. cond1 := cond.AndCond(cond.Or("T_name__icontains", T_name).Or("T_InfoCollection_id", T_name)).And("T_State", 1)
  221. if len(T_uuid) > 0 {
  222. cond1 = cond1.And("T_uuid", T_uuid)
  223. }
  224. if len(T_admin) > 0 {
  225. cond1 = cond1.AndCond(cond.Or("T_scheme", T_admin).Or("T_collection", T_admin).
  226. Or("T_reporting", T_admin).Or("T_delivery", T_admin))
  227. }
  228. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  229. cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
  230. // 转换
  231. var InfoCollectionList []InfoCollection_R
  232. for _, v := range r {
  233. InfoCollectionList = append(InfoCollectionList, InfoCollectionToInfoCollection_R(v, adminMap))
  234. }
  235. return InfoCollectionList, int(cnt)
  236. }