InfoCollection.go 8.3 KB

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