InfoCollection.go 11 KB

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