InfoCollection.go 11 KB

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