123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package service
- import (
- "app.yhyue.com/moapp/jy_docs/rpc/partnerlib/entity"
- "app.yhyue.com/moapp/jy_docs/rpc/partnerlib/model"
- sm "app.yhyue.com/moapp/jy_docs/services/model"
- "app.yhyue.com/moapp/jy_docs/services/partner"
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/date"
- "crypto/md5"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "html"
- "strings"
- "time"
- )
- func InsertDocinInfos(b []byte) (err error, lastId int64, expectTotal, actualTotal int) {
- var docinInfos model.DocinInfoRes
- //str := string(b)
- //fmt.Println(str, "--------------")
- err = json.Unmarshal(b, &docinInfos)
- if err == nil {
- if expectTotal = len(docinInfos.Data); expectTotal > 0 {
- //剑鱼doc 一份 || 豆丁备份表一份
- var (
- docs []sm.Docin
- docsStatistics []sm.DocStatistics
- docsES []map[string]interface{}
- )
- for _, v := range docinInfos.Data {
- var (
- productName = html.UnescapeString(v.ProductName)
- desc = html.UnescapeString(v.Desc)
- id = fmt.Sprintf("%s-%d", entity.PartnerName, v.ProductId)
- md5Id = GetMD5(id, productName, v.Desc, v.FilePostfix) //判断数据是否已存在 md5
- price = int(v.Price * entity.Multiple) //价格转换
- docTags, docClass = partner.SwitchDocClass(v.PcatName, v.CatName, 1) //标签和分类
- fileType = GetDocFileType(v.FilePostfix)
- uploadDate = date.NowFormat(date.Date_Full_Layout)
- )
- if partner.CheckDocs(id, md5Id) {
- continue
- }
- if v.CreatedDate != "" {
- uploadDate = fmt.Sprintf("%s 00:00:00", strings.Split(v.CreatedDate, " ")[0])
- }
- lastId = v.ProductId
- //tidb 分类
- docsStatistics = append(docsStatistics, sm.DocStatistics{
- AppId: entity.AppId,
- DocId: id,
- UpdateDate: time.Now(),
- DownTimes: v.DownloadCount,
- ViewTimes: v.VisitCount,
- })
- //tidb 文档
- docs = append(docs, sm.Docin{
- Id: id,
- UserId: entity.PartnerName,
- AppId: entity.AppId,
- DocName: productName,
- DocFileType: fileType,
- DocFileSuffix: v.FilePostfix,
- DocFileSize: int(v.FileSize),
- DocPageSize: v.PageCount,
- DocTags: strings.Join(docTags, ","),
- DocClass: docClass,
- UploadDate: uploadDate,
- IsDelete: 0,
- OssDocId: "",
- Md5: md5Id, //
- OssPdfId: "",
- OssTxtId: "",
- Price: price,
- DownOrUp: 0,
- DocSummary: desc,
- PreviewImgId: common.InterfaceToStr(v.ProductId),
- EncryptionLevel: 0,
- Source: 2, //豆丁
- ProductType: v.Ifcharge + entity.Charge, //Ifcharge:是否付费,0:免费,1:收费;ProductType:商品类型:默认:0:全部;1:会员免费;2:精品(付费)
- UpdateDate: date.NowFormat(date.Date_Full_Layout),
- })
- //elastic
- docsES = append(docsES, map[string]interface{}{
- "_id": id,
- "docClass": docClass,
- "docFileSize": int(v.FileSize),
- "docFileType": fileType,
- "docName": productName,
- "docPageSize": v.PageCount,
- "docSummary": desc,
- "docTags": strings.Join(docTags, ","),
- "downTimes": v.DownloadCount,
- "previewImgId": common.InterfaceToStr(v.ProductId),
- "price": price,
- "uploadDate": uploadDate,
- "viewTimes": v.VisitCount,
- "source": 2, //豆丁
- "productType": v.Ifcharge + entity.Charge,
- })
- }
- if len(docs) > 0 {
- actualTotal = len(docs)
- //doc 文库文档数据信息
- partner.DocsInsert(docs, entity.InBatchesCount)
- //docsStatistics 文库文档浏览量及下载量信息
- partner.DocsStatistics(docsStatistics, entity.InBatchesCount)
- //es sync data
- partner.SyncDocsToES(docsES)
- }
- } else {
- msg := entity.Res[docinInfos.Msg]
- if msg == "" {
- msg = "同步豆丁数据异常"
- }
- err = fmt.Errorf(msg)
- }
- }
- return
- }
- func GetMD5(id, productName, desc, FilePostfix string) string {
- hash := md5.Sum([]byte(fmt.Sprintf("%s#%s#%#s#%s", id, productName, desc, FilePostfix)))
- return hex.EncodeToString(hash[:])
- }
- func GetDocFileType(fileSuffix string) (i int) {
- //1 doc 2 pdf 3 xls 4 ppt 5 txt 6 其他
- switch strings.ToLower(fileSuffix) {
- case "doc", "docx":
- i = 1
- case "pdf":
- i = 2
- case "xls", "xlsx":
- i = 3
- case "ppt", "pptx":
- i = 4
- case "txt":
- i = 5
- default:
- i = 6
- }
- return
- }
|