docinInfo.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jy_docs/rpc/partnerlib/entity"
  4. "app.yhyue.com/moapp/jy_docs/rpc/partnerlib/model"
  5. sm "app.yhyue.com/moapp/jy_docs/services/model"
  6. "app.yhyue.com/moapp/jy_docs/services/partner"
  7. "crypto/md5"
  8. "encoding/hex"
  9. "encoding/json"
  10. "fmt"
  11. "strings"
  12. "time"
  13. )
  14. func InsertDocinInfos(b []byte) (err error, lastId int64, expectTotal, actualTotal int) {
  15. var docinInfos model.DocinInfoRes
  16. err = json.Unmarshal(b, &docinInfos)
  17. if err == nil {
  18. if expectTotal = len(docinInfos.Data); expectTotal > 0 {
  19. //剑鱼doc 一份 || 豆丁备份表一份
  20. var (
  21. docs []sm.Doc
  22. docsStatistics []sm.DocStatistics
  23. )
  24. for _, v := range docinInfos.Data {
  25. var (
  26. id = fmt.Sprintf("docin-%d", v.ProductId)
  27. md5Id = GetMD5(id, v.ProductName, v.Desc, v.FilePostfix) //判断数据是否已存在 md5
  28. price = v.Price //价格转换
  29. docTags, docClass = partner.SwitchDocClass(v.PcatName, v.CatName, 1) //标签和分类
  30. )
  31. if partner.CheckDocs(id, md5Id) {
  32. continue
  33. }
  34. lastId = v.ProductId
  35. docsStatistics = append(docsStatistics, sm.DocStatistics{
  36. AppId: entity.AppId,
  37. DocId: id,
  38. UpdateDate: time.Now(),
  39. DownTimes: v.DownloadCount,
  40. ViewTimes: v.VisitCount,
  41. })
  42. docs = append(docs, sm.Doc{
  43. Id: id,
  44. UserId: "docin",
  45. AppId: entity.AppId,
  46. DocName: v.ProductName,
  47. DocFileType: GetDocFileType(v.FilePostfix),
  48. DocFileSuffix: v.FilePostfix,
  49. DocFileSize: int(v.FileSize),
  50. DocPageSize: v.PageCount,
  51. DocTags: strings.Join(docTags, ","),
  52. DocClass: docClass,
  53. UploadDate: time.Now(),
  54. IsDelete: 0,
  55. OssDocId: "",
  56. OssDocUrl: "",
  57. Md5: md5Id, //
  58. OssPdfId: "",
  59. OssPdfUrl: "",
  60. OssTxtId: "",
  61. OssTxtUrl: "",
  62. Price: price,
  63. DownOrUp: 0,
  64. DocSummary: v.Desc,
  65. PreviewImgId: "",
  66. PreviewImgUrl: "",
  67. EncryptionLevel: 0,
  68. Source: 1,
  69. ProductType: v.Ifcharge,
  70. UpdateDate: time.Now(),
  71. })
  72. }
  73. if len(docs) > 0 {
  74. actualTotal = len(docs)
  75. //doc 文库文档数据信息
  76. partner.DocsInsert(docs, entity.InBatchesCount)
  77. //docsStatistics 文库文档浏览量及下载量信息
  78. partner.DocsStatistics(docsStatistics, entity.InBatchesCount)
  79. }
  80. }
  81. }
  82. return
  83. }
  84. func GetMD5(id, productName, desc, FilePostfix string) string {
  85. hash := md5.Sum([]byte(fmt.Sprintf("%s#%s#%#s#%s", id, productName, desc, FilePostfix)))
  86. return hex.EncodeToString(hash[:])
  87. }
  88. func GetDocFileType(fileSuffix string) (i int) {
  89. //1 doc 2 pdf 3 xls 4 ppt 5 txt 6 其他
  90. switch strings.ToLower(fileSuffix) {
  91. case "doc":
  92. i = 1
  93. case "pdf":
  94. i = 2
  95. case "xls", "xlsx":
  96. i = 3
  97. case "ppt":
  98. i = 4
  99. case "txt":
  100. i = 5
  101. default:
  102. i = 6
  103. }
  104. return
  105. }