123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package stdlib
- import (
- "app.yhyue.com/moapp/jy_docs/services/model"
- jyDocsRpcUtil "app.yhyue.com/moapp/jy_docs/services/util"
- elastic "app.yhyue.com/moapp/jybase/esv7"
- "errors"
- "gorm.io/gorm"
- "log"
- "time"
- )
- const DocOn = 1
- const DocOff = 0
- // 上架文档
- func ChangeDocumentOn(doc *model.Doc, reason int32) bool {
- // 查文档
- docData := model.Doc{}
- rs := jyDocsRpcUtil.GetJyDocsDB().Table("doc").Where("id=? and isDelete=0 and downOrUp=0 and appId=?", doc.Id, doc.AppId).Find(&docData)
- if rs.Error != nil {
- log.Printf("上架文档 appId:%s,Id:%s, error: %s\n", doc.AppId, doc.Id, rs.Error)
- return false
- } else {
- if docData.Id == "" {
- log.Printf("上架文档 appId:%s,Id:%s, error: %s\n", doc.AppId, doc.Id, "文档不存在或已经被上架")
- return false
- }
- }
- statisticsData := map[string]interface{}{}
- var downTimes int64
- var viewTimes int64
- result := jyDocsRpcUtil.GetJyDocsDB().Table("doc_statistics").Where("docId=? and appId=?", doc.Id, doc.AppId).Find(&statisticsData)
- if result.Error != nil {
- log.Printf("上架文档 查询浏览次数下载量出错 appId:%s,Id:%s, error:%s\n", doc.AppId, doc.Id, result.Error)
- return false
- } else {
- if statisticsData["id"] != nil {
- if statisticsData["downTimes"] != nil {
- downTimes = statisticsData["downTimes"].(int64)
- }
- if statisticsData["viewTimes"] != nil {
- viewTimes = statisticsData["viewTimes"].(int64)
- }
- }
- }
- // 上架
- err := jyDocsRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
- // 更新大库状态为上架 1-上架
- if err := tx.Exec("update doc set downOrUp=? where id=? and appId=?", DocOn, doc.Id, doc.AppId).Error; err != nil {
- return err
- }
- // 存上架记录
- date := time.Now().Local().Format("2006-01-02 15:04:05")
- if err := tx.Exec("insert into doc_change_record (docId,date,downOrUp,reason,appId) values (?,?,?,?,?)", doc.Id, date, DocOn, reason, doc.AppId).Error; err != nil {
- return err
- }
- saveData := map[string]interface{}{
- "_id": docData.Id,
- "docName": docData.DocName,
- "docTags": docData.DocTags,
- "docClass": docData.DocClass,
- "price": docData.Price,
- "downTimes": downTimes,
- "viewTimes": viewTimes,
- "docSummary": docData.DocSummary,
- "uploadDate": docData.UploadDate.Format("2006-01-02 15:04:05"),
- "docFileSize": docData.DocFileSize,
- "docPageSize": docData.DocPageSize,
- "docFileType": docData.DocFileType,
- }
- if rs := elastic.Save(jyDocsRpcUtil.Es_JyDoc, "_doc", saveData); rs != true {
- return errors.New("发布到检索库失败")
- }
- return nil
- })
- if err != nil {
- log.Printf("上架文档 appId:%s,Id:%s, error:%s\n", doc.AppId, doc.Id, err)
- return false
- }
- log.Println("上架文档成功:" + doc.Id)
- return true
- }
- // 下架文档
- func ChangeDocumentOff(doc *model.Doc, reason int32) bool {
- // 查文档
- docData := map[string]interface{}{}
- rs := jyDocsRpcUtil.GetJyDocsDB().Table("doc").Where("id=? and isDelete=0 and downOrUp=1 and appId=?", doc.Id, doc.AppId).Find(&docData)
- if rs.Error != nil {
- log.Printf("下架文档 appId:%s,Id:%s, error:%s\n", doc.AppId, doc.Id, rs.Error)
- return false
- } else {
- if docData["id"] == nil {
- log.Printf("下架文档 appId:%s,Id:%s, error:%s\n", doc.AppId, doc.Id, "文档不存在或已经被下架")
- return false
- }
- }
- // 下架
- err := jyDocsRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
- // 更新大库状态为下架 0-下架
- if err := tx.Exec("update doc set downOrUp=? where id=? and appId=?", DocOff, doc.Id, doc.AppId).Error; err != nil {
- return err
- }
- // 存下架记录
- date := time.Now().Local().Format("2006-01-02 15:04:05")
- if err := tx.Exec("insert into doc_change_record (docId,date,downOrUp,reason,appId) values (?,?,?,?,?)", doc.Id, date, DocOff, reason, doc.AppId).Error; err != nil {
- return err
- }
- if rs := elastic.DelById(jyDocsRpcUtil.Es_JyDoc, "doc", doc.Id); rs != true {
- return errors.New("从检索库删除失败")
- }
- return nil
- })
- if err != nil {
- log.Printf("下架文档 appId:%s,Id:%s, error:%s:\n", doc.AppId, doc.Id, err)
- return false
- }
- log.Println("下架文档成功:" + doc.Id)
- return true
- }
|