123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- package userlib
- import (
- "app.yhyue.com/moapp/jy_docs/rpc/userlib/userlib"
- "app.yhyue.com/moapp/jy_docs/services/model"
- docRpcUtil "app.yhyue.com/moapp/jy_docs/services/util"
- "errors"
- "fmt"
- "gorm.io/gorm"
- "log"
- "time"
- )
- //文档收藏
- func UserDocCollect(userDoc *model.UserDoc) bool {
- log.Println("UserDocCollect exec ......")
- orm := docRpcUtil.GetJyDocsDB()
- docData := model.UserDocData{}
- err := orm.Transaction(func(tx *gorm.DB) error {
- err0 := orm.Select("id,userId,docId,isCollection").
- Where("userId = ? AND docId = ? AND appId = ?", userDoc.UserId, userDoc.DocId, userDoc.AppId).
- Find(&docData).Error
- if err0 != nil {
- log.Println("查询已存在收藏记录失败")
- return errors.New("查询已存在收藏记录失败")
- }
- //已收藏数据处理
- if docData.IsCollection == 1 {
- log.Println("此文档,该用户已收藏,不可重复收藏")
- return errors.New("此文档,该用户已收藏,不可重复收藏")
- }
- //查询文档基础信息
- doc := model.Doc{}
- err0 = orm.Where("id = ?", userDoc.DocId).
- Find(&doc).Error
- if err0 != nil || doc.Id == "" {
- log.Println("文档不存在")
- return errors.New("文档不存在")
- }
- userDoc.DocSourceUserId = doc.UserId
- userDoc.DocCategory = 2
- userDoc.IsDelete = int(userlib.UserDocStatus_Normal)
- userDoc.DocName = doc.DocName
- userDoc.DocFileType = doc.DocFileType
- userDoc.DocFileSuffix = doc.DocFileSuffix
- userDoc.DocFileSize = doc.DocFileSize
- userDoc.DocPageSize = doc.DocPageSize
- userDoc.DocSummary = doc.DocSummary
- userDoc.IsCollection = 1
- userDoc.Cost = fmt.Sprint(doc.Price)
- //已取消收藏,再次进行收藏
- if docData.IsCollection == 0 && docData.UserId != "" {
- err := orm.Exec("UPDATE user_doc SET isCollection = 1 WHERE id = ?", docData.Id).Error
- if err != nil {
- log.Println("文档再次收藏失败")
- tx.Rollback()
- return err
- }
- //无收藏记录,新增收藏
- } else {
- //用户文库表添加记录(需要检查是否重复)
- timeData := time.Now()
- userDoc.CreateAt = timeData
- userDoc.UpdateAt = timeData
- userDoc.DeletedAt = timeData
- err := orm.Create(userDoc).Error
- if err != nil {
- log.Println("userDocCollect error:", err)
- tx.Rollback()
- return err
- }
- }
- //用户收藏、兑换记录表添加记录
- err := orm.Exec("insert into download_collection_record (docId,userId,appId,sourceUserId,category,cost) values (?,?,?,?,?,?)", userDoc.DocId, userDoc.UserId, userDoc.AppId, userDoc.DocSourceUserId, 2, doc.Price).Error
- if err != nil {
- log.Println("userDocCollect record insert error:", err)
- tx.Rollback()
- return err
- }
- return err
- })
- if err != nil {
- return false
- }
- return true
- }
- //文档取消收藏
- func UserDocCancelCollect(docId, userId, appId string) bool {
- orm := docRpcUtil.GetJyDocsDB()
- err := orm.Transaction(func(tx *gorm.DB) error {
- //收藏记录详情
- userDoc := model.UserDoc{}
- err := orm.Where("docId = ? AND userId = ? AND appId = ? AND isCollection = 1", docId, userId, appId).
- Find(&userDoc).Error
- if err != nil || userDoc.ID == 0 {
- log.Println("无此收藏记录,取消收藏失败", err)
- return errors.New("无此收藏记录,取消收藏失败")
- }
- //文档取消收藏状态修改
- err = orm.Exec("UPDATE user_doc SET isCollection = 0 WHERE id = ? AND isCollection = 1", userDoc.ID).Error
- if err != nil {
- log.Println("文档取消收藏失败")
- tx.Rollback()
- return err
- }
- //记录文档取消收藏添加记录
- err = orm.Exec("insert into download_collection_record (docId,userId,appId,sourceUserId,category,cost) values (?,?,?,?,?,?)", userDoc.DocId, userDoc.UserId, userDoc.AppId, userDoc.DocSourceUserId, 3, userDoc.Cost).Error
- if err != nil {
- log.Println("userDocCollect record insert error:", err)
- tx.Rollback()
- return err
- }
- return nil
- })
- if err != nil {
- return false
- }
- return true
- }
- //兑换操作
- func UserDocDownload(userDoc *model.UserDoc, cost int) (bool, string) {
- log.Println("UserDocCollect exec ......")
- msg := "兑换成功"
- err := docRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
- //用户文库表添加记录
- //获取文档有关信息
- //查询之前有无兑换记录 0和1只要有一条不能兑换,如果有未删除可以兑换
- userDocDownloadList := []model.UserDoc{}
- err := docRpcUtil.GetJyDocsDB().Table("user_doc").Where(" userId=? and docId=? and isDownload=1 and (isDelete=? or isDelete=?) and appId=?", userDoc.UserId, userDoc.DocId, model.UserDocStatus_Normal, model.UserDocStatus_LogicDelete, userDoc.AppId).Find(&userDocDownloadList)
- if err.Error != nil {
- log.Println("查询兑换记录失败:", err)
- msg = "查询兑换记录失败"
- return err.Error
- }
- if len(userDocDownloadList) > 0 {
- msg = "之前已经兑换不能再次兑换"
- return errors.New("之前已经兑换不能再次兑换")
- }
- //先扣除积分
- //rpc
- /*client := zrpc.MustNewClient(zrpc.RpcClientConf{
- Etcd: discov.EtcdConf{
- Hosts: hosts,
- Key: key,
- },
- })
- integralLib := integralclient.NewIntegral(client)
- req := &integral.Req{UserId: userDoc.UserId,
- PointType: 2003,
- BusinessTypeId: 1,
- BusinessType: "1",
- Point: int64(cost),
- AppId: userDoc.AppId}
- res, pointsErr := integralLib.IntegralConsume(context.Background(), req)
- log.Println("err ", pointsErr)
- log.Println("req ", res)
- if (pointsErr != nil) {
- log.Println("扣除积分失败:", pointsErr)
- msg = "扣除积分失败"
- return pointsErr
- }
- if (res.Code == 0) {
- log.Println("扣除积分失败:", pointsErr)
- msg = res.Message
- return errors.New(res.Message)
- }*/
- //查询之前有无记录 有记录类型更新兑换,状态改为未删除、没有记录插入一条新纪录
- userDocCollectionList := []model.UserDoc{}
- err = docRpcUtil.GetJyDocsDB().Table("user_doc").Where(" userId=? and docId=? and appId=? ", userDoc.UserId, userDoc.DocId, userDoc.AppId).Find(&userDocCollectionList)
- if err.Error != nil {
- log.Println("收藏记录查询:", err)
- msg = "收藏记录查询"
- return err.Error
- }
- if len(userDocCollectionList) > 0 {
- //兑换记录修改
- if err := tx.Exec("update user_doc set isDownload=? , isDelete=? ,update_at=? where id =?", 1, model.UserDocStatus_Normal, time.Now(), userDocCollectionList[0].ID).Error; err != nil {
- log.Println("兑换记录修改失败:", err)
- msg = "兑换记录修改失败"
- tx.Rollback()
- return err
- }
- if err.Error != nil {
- log.Println("兑换操作流水添加失败:", err)
- msg = "收藏记录更改为兑换记录失败"
- tx.Rollback()
- return err.Error
- }
- //用户收藏、兑换记录表添加记录
- err := docRpcUtil.GetJyDocsDB().Exec("insert into download_collection_record (docId,userId,sourceUserId,category,cost,date,appId) values (?,?,?,?,?,?,?)", userDoc.DocId, userDoc.UserId, userDoc.DocSourceUserId, model.UserDocCategory_Download, cost, time.Now(), userDoc.AppId)
- if err.Error != nil {
- log.Println("兑换操作流水添加失败:", err)
- msg = "兑换操作流水添加失败"
- tx.Rollback()
- return err.Error
- }
- } else {
- //用户文库表添加记录
- userDoc.CreateAt = time.Now()
- userDoc.UpdateAt = time.Now()
- userDoc.DeletedAt = time.Now()
- userDoc.IsCollection = 0
- userDoc.IsDownload = 1
- //获取文件基本信息
- //查询文档基础信息
- doc := model.Doc{}
- err0 := docRpcUtil.GetJyDocsDB().
- Where("id = ?", userDoc.DocId).
- Find(&doc)
- if err0.Error != nil {
- log.Println("文档不存在")
- msg = "文档不存在"
- return errors.New("文档不存在")
- }
- userDoc.DocSourceUserId = doc.UserId
- userDoc.IsDelete = int(userlib.UserDocStatus_Normal)
- userDoc.DocName = doc.DocName
- userDoc.DocFileType = doc.DocFileType
- userDoc.DocFileSuffix = doc.DocFileSuffix
- userDoc.DocFileSize = doc.DocFileSize
- userDoc.DocPageSize = doc.DocPageSize
- userDoc.DocSummary = doc.DocSummary
- userDoc.Cost = fmt.Sprint(doc.Price)
- err = docRpcUtil.GetJyDocsDB().Create(userDoc)
- if err.Error != nil {
- log.Println("兑换操作添加失败:", err)
- msg = "兑换操作添加失败"
- tx.Rollback()
- return err.Error
- }
- //用户收藏、兑换记录表添加记录
- err := docRpcUtil.GetJyDocsDB().Exec("insert into download_collection_record (docId,userId,sourceUserId,category,cost,date,appId) values (?,?,?,?,?,?,?)", userDoc.DocId, userDoc.UserId, userDoc.DocSourceUserId, model.UserDocCategory_Download, doc.Price, time.Now(), userDoc.AppId)
- if err.Error != nil {
- log.Println("兑换操作流水添加失败:", err)
- msg = "兑换操作流水添加失败"
- tx.Rollback()
- return err.Error
- }
- }
- return nil
- })
- if err != nil {
- return false, msg
- }
- return true, msg
- }
- //文档删除
- func UserDocDelete(userDocId int32, appId, userId string) (bool, string) {
- msg := "文档删除成功"
- err := docRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
- //逻辑删除
- err := docRpcUtil.GetJyDocsDB().Exec("UPDATE user_doc SET isDelete = ? WHERE docId = ? and userId=? and appId=?", model.UserDocStatus_LogicDelete, userDocId, userId, appId).Error
- if err != nil {
- msg = "文档删除失败"
- log.Println("文档删除失败:", err)
- tx.Rollback()
- return err
- }
- //删除记录新增
- err = docRpcUtil.GetJyDocsDB().Exec("insert into del_record (docId, userId, date, operate, appId) values (?,?,?,?,?)", userDocId, userId, time.Now(), 1, appId).Error
- if err != nil {
- tx.Rollback()
- msg = "删除记录新增失败"
- log.Println("删除记录新增失败:", err)
- return err
- }
- return nil
- })
- if err != nil {
- return false, msg
- }
- return true, msg
- }
- //文档回收
- func UserDocRestore(userDocId int32, appId, userId string) (bool, string) {
- msg := "文档找回成功"
- err := docRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
- //逻辑删除
- err := docRpcUtil.GetJyDocsDB().Exec("UPDATE user_doc SET isDelete = ? WHERE docId = ? and userId=? and appId=?", model.UserDocStatus_Normal, userDocId, userId, appId).Error
- if err != nil {
- msg = "文档找回失败"
- log.Println("文档找回失败:", err)
- tx.Rollback()
- return err
- }
- //删除记录新增
- err = docRpcUtil.GetJyDocsDB().Exec("insert into del_record (docId, userId, date, operate, appId) values (?,?,?,?,?)", userDocId, userId, time.Now(), 2, appId).Error
- if err != nil {
- tx.Rollback()
- msg = "删除记录新增失败"
- log.Println("删除记录新增失败:", err)
- return err
- }
- return nil
- })
- if err != nil {
- return false, msg
- }
- return true, msg
- } //永久删除
- func UserDocPermanentDelete(userDocId int32, appId, userId string) (bool, string) {
- msg := "永久删除成功"
- err := docRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
- //逻辑删除
- err := docRpcUtil.GetJyDocsDB().Exec("UPDATE user_doc SET isDelete = ? ,isDownload=0 WHERE docId = ? and userId=? and appId=?", model.UserDocStatus_PermanentlyDelete, userDocId, userId, appId).Error
- if err != nil {
- msg = "永久删除失败"
- log.Println("永久删除失败:", err)
- tx.Rollback()
- return err
- }
- //删除记录新增
- err = docRpcUtil.GetJyDocsDB().Exec("insert into del_record (docId, userId, date, operate, appId) values (?,?,?,?,?)", userDocId, userId, time.Now(), 4, appId).Error
- if err != nil {
- tx.Rollback()
- msg = "删除记录新增失败"
- log.Println("删除记录新增失败:", err)
- return err
- }
- return nil
- })
- if err != nil {
- return false, msg
- }
- return true, msg
- }
- //我的文库列表(包括回收站列表)0兑换的 1收藏的 2回收站的
- func UserDocsList(in *userlib.UserDocsRequest) ([]*model.UserDoc, int64, bool, string) {
- msg := "查询成功"
- data := []*model.UserDoc{}
- count := int64(0)
- startIndex := (in.Page - 1) * in.PageSize
- err := docRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
- switch in.UserDocCategory {
- case int64(0):
- //兑换的
- err := docRpcUtil.GetJyDocsDB().Table("user_doc").Where(" userId=? and isDownload=1 and isDelete=? and appId=? ", in.UserId, model.UserDocStatus_Normal, in.AppId).Find(&data)
- count = int64(len(data))
- err = docRpcUtil.GetJyDocsDB().Table("user_doc").Where(" userId=? and isDownload=1 and isDelete=? and appId=? order by create_at desc limit ?,?", in.UserId, model.UserDocStatus_Normal, in.AppId, startIndex, in.PageSize).Find(&data)
- if err.Error != nil {
- log.Println("查询兑换记录失败:", err)
- msg = "查询兑换记录失败"
- return err.Error
- }
- case int64(1):
- //收藏的
- err := docRpcUtil.GetJyDocsDB().Table("user_doc").Where(" userId=? and isCollection=1 and appId=? ", in.UserId, in.AppId).Find(&data)
- count = int64(len(data))
- err = docRpcUtil.GetJyDocsDB().Table("user_doc").Where("userId=? and isCollection=1 and appId=? order by create_at desc limit ?,?", in.UserId, in.AppId, startIndex, in.PageSize).Find(&data)
- if err.Error != nil {
- log.Println("查询收藏记录失败:", err)
- msg = "查询收藏记录失败"
- return err.Error
- }
- case int64(2):
- //回收站
- err := docRpcUtil.GetJyDocsDB().Table("user_doc").Where(" userId=? and isDelete=? and appId=? ", in.UserId, model.UserDocStatus_LogicDelete, in.AppId).Find(&data)
- count = int64(len(data))
- err = docRpcUtil.GetJyDocsDB().Table("user_doc").Where(" userId=? and isDelete=? and appId=? order by create_at desc limit ?,?", in.UserId, model.UserDocStatus_LogicDelete, in.AppId, startIndex, in.PageSize).Find(&data)
- if err.Error != nil {
- log.Println("查询回收站记录失败:", err)
- msg = "查询回收站记录失败"
- return err.Error
- }
- }
- return nil
- })
- if err != nil {
- return data, count, false, msg
- }
- return data, count, true, msg
- }
- /*接口日志添加*/
- func InterfaceLog(in *model.InterfaceLog) bool {
- orm := docRpcUtil.GetJyDocsDB()
- err := orm.Transaction(func(tx *gorm.DB) error {
- err := orm.Create(in)
- if err.Error != nil {
- log.Println("接口日志添加失败:", err)
- tx.Rollback()
- return err.Error
- }
- return nil
- })
- if err != nil {
- return false
- }
- return true
- }
- //账号合并
- func UserMerge(mergeUser, mergedUser, appId string) (bool, string) {
- log.Println("UserMerge exec ......")
- orm := docRpcUtil.GetJyDocsDB()
- docData := []model.UserDoc{}
- msg := ""
- //先查询被合并账号所有信息
- err := orm.Transaction(func(tx *gorm.DB) error {
- err0 := orm.Select("*").
- Where("userId = ? AND appId = ?", mergedUser, appId).Find(&docData).Error
- if err0 != nil {
- log.Println("查询被合并账号失败")
- return errors.New("查询被合并账号失败")
- }
- //处理每一条数据
- for _, value := range docData {
- //1先判断有没有老数据是否需要新增
- mergeUserDocData := model.UserDocData{}
- err := orm.Table("user_doc").Where(" userId=? and docId=? and appId=? ", mergeUser, value.DocId, appId).Find(&mergeUserDocData)
- if err.Error != nil {
- log.Printf("查询,userId:[%s],docId:[%s] 失败", mergeUser, value.DocId)
- msg = "查询用户信息失败"
- return err.Error
- }
- if mergeUserDocData.UserId == "" {
- userDoc := model.UserDoc{}
- userDoc.DocSourceUserId = value.DocSourceUserId
- userDoc.DocCategory = 2
- userDoc.UserId = mergeUser
- userDoc.DocId = value.DocId
- userDoc.AppId = value.AppId
- userDoc.PreviewImgId = value.PreviewImgId
- userDoc.PreviewImgUrl = value.PreviewImgUrl
- userDoc.IsDelete = int(userlib.UserDocStatus_Normal)
- userDoc.DocName = value.DocName
- userDoc.DocFileType = value.DocFileType
- userDoc.DocFileSuffix = value.DocFileSuffix
- userDoc.DocFileSize = value.DocFileSize
- userDoc.DocPageSize = value.DocPageSize
- userDoc.DocSummary = value.DocSummary
- userDoc.IsCollection = value.IsCollection
- userDoc.IsDownload = value.IsDownload
- userDoc.Cost = value.Cost
- timeData := time.Now()
- userDoc.CreateAt = timeData
- userDoc.UpdateAt = timeData
- userDoc.DeletedAt = timeData
- err := orm.Create(&userDoc).Error
- if err != nil {
- log.Println("userDocCollect error:", err)
- tx.Rollback()
- return err
- }
- }
- if (mergeUserDocData.IsDownload==1){
- value.IsDownload=mergeUserDocData.IsDownload
- value.IsDelete=mergeUserDocData.IsDelete
- }
- if (mergeUserDocData.IsCollection==1){
- value.IsCollection=mergeUserDocData.IsCollection
- }
- err = orm.Exec("UPDATE user_doc SET isCollection = ?,isDelete=? , isDownload=? ,update_at=?,cost=? WHERE id = ?", value.IsCollection, model.UserDocCategory_SelfUpload, value.IsDownload,time.Now().Local(), value.Cost,mergeUserDocData.Id)
- if err.Error != nil {
- log.Println("文档合并失败")
- tx.Rollback()
- return err.Error
- }
- }
- return nil
- })
- if err != nil {
- return false, "合并失败"
- }
- return true, "合并成功"
- }
|