userDocService.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. package userlib
  2. import (
  3. "app.yhyue.com/moapp/jy_docs/rpc/userlib/userlib"
  4. "app.yhyue.com/moapp/jy_docs/services/model"
  5. docRpcUtil "app.yhyue.com/moapp/jy_docs/services/util"
  6. "errors"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "log"
  10. "time"
  11. )
  12. //文档收藏
  13. func UserDocCollect(userDoc *model.UserDoc) bool {
  14. log.Println("UserDocCollect exec ......")
  15. orm := docRpcUtil.GetJyDocsDB()
  16. docData := model.UserDocData{}
  17. err := orm.Transaction(func(tx *gorm.DB) error {
  18. err0 := orm.Select("id,userId,docId,isCollection").
  19. Where("userId = ? AND docId = ? AND appId = ?", userDoc.UserId, userDoc.DocId, userDoc.AppId).
  20. Find(&docData).Error
  21. if err0 != nil {
  22. log.Println("查询已存在收藏记录失败")
  23. return errors.New("查询已存在收藏记录失败")
  24. }
  25. //已收藏数据处理
  26. if docData.IsCollection == 1 {
  27. log.Println("此文档,该用户已收藏,不可重复收藏")
  28. return errors.New("此文档,该用户已收藏,不可重复收藏")
  29. }
  30. //查询文档基础信息
  31. doc := model.Doc{}
  32. err0 = orm.Where("id = ?", userDoc.DocId).
  33. Find(&doc).Error
  34. if err0 != nil || doc.Id == "" {
  35. log.Println("文档不存在")
  36. return errors.New("文档不存在")
  37. }
  38. userDoc.DocSourceUserId = doc.UserId
  39. userDoc.DocCategory = 2
  40. userDoc.IsDelete = int(userlib.UserDocStatus_Normal)
  41. userDoc.DocName = doc.DocName
  42. userDoc.DocFileType = doc.DocFileType
  43. userDoc.DocFileSuffix = doc.DocFileSuffix
  44. userDoc.DocFileSize = doc.DocFileSize
  45. userDoc.DocPageSize = doc.DocPageSize
  46. userDoc.DocSummary = doc.DocSummary
  47. userDoc.IsCollection = 1
  48. userDoc.Cost = fmt.Sprint(doc.Price)
  49. //已取消收藏,再次进行收藏
  50. if docData.IsCollection == 0 && docData.UserId != "" {
  51. err := orm.Exec("UPDATE user_doc SET isCollection = 1 WHERE id = ?", docData.Id).Error
  52. if err != nil {
  53. log.Println("文档再次收藏失败")
  54. tx.Rollback()
  55. return err
  56. }
  57. //无收藏记录,新增收藏
  58. } else {
  59. //用户文库表添加记录(需要检查是否重复)
  60. timeData := time.Now()
  61. userDoc.CreateAt = timeData
  62. userDoc.UpdateAt = timeData
  63. userDoc.DeletedAt = timeData
  64. err := orm.Create(userDoc).Error
  65. if err != nil {
  66. log.Println("userDocCollect error:", err)
  67. tx.Rollback()
  68. return err
  69. }
  70. }
  71. //用户收藏、兑换记录表添加记录
  72. 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
  73. if err != nil {
  74. log.Println("userDocCollect record insert error:", err)
  75. tx.Rollback()
  76. return err
  77. }
  78. return err
  79. })
  80. if err != nil {
  81. return false
  82. }
  83. return true
  84. }
  85. //文档取消收藏
  86. func UserDocCancelCollect(docId, userId, appId string) bool {
  87. orm := docRpcUtil.GetJyDocsDB()
  88. err := orm.Transaction(func(tx *gorm.DB) error {
  89. //收藏记录详情
  90. userDoc := model.UserDoc{}
  91. err := orm.Where("docId = ? AND userId = ? AND appId = ? AND isCollection = 1", docId, userId, appId).
  92. Find(&userDoc).Error
  93. if err != nil || userDoc.ID == 0 {
  94. log.Println("无此收藏记录,取消收藏失败", err)
  95. return errors.New("无此收藏记录,取消收藏失败")
  96. }
  97. //文档取消收藏状态修改
  98. err = orm.Exec("UPDATE user_doc SET isCollection = 0 WHERE id = ? AND isCollection = 1", userDoc.ID).Error
  99. if err != nil {
  100. log.Println("文档取消收藏失败")
  101. tx.Rollback()
  102. return err
  103. }
  104. //记录文档取消收藏添加记录
  105. 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
  106. if err != nil {
  107. log.Println("userDocCollect record insert error:", err)
  108. tx.Rollback()
  109. return err
  110. }
  111. return nil
  112. })
  113. if err != nil {
  114. return false
  115. }
  116. return true
  117. }
  118. //兑换操作
  119. func UserDocDownload(userDoc *model.UserDoc, cost int) (bool, string) {
  120. log.Println("UserDocCollect exec ......")
  121. msg := "兑换成功"
  122. err := docRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
  123. //用户文库表添加记录
  124. //获取文档有关信息
  125. //查询之前有无兑换记录 0和1只要有一条不能兑换,如果有未删除可以兑换
  126. userDocDownloadList := []model.UserDoc{}
  127. 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)
  128. if err.Error != nil {
  129. log.Println("查询兑换记录失败:", err)
  130. msg = "查询兑换记录失败"
  131. return err.Error
  132. }
  133. if len(userDocDownloadList) > 0 {
  134. msg = "之前已经兑换不能再次兑换"
  135. return errors.New("之前已经兑换不能再次兑换")
  136. }
  137. //先扣除积分
  138. //rpc
  139. /*client := zrpc.MustNewClient(zrpc.RpcClientConf{
  140. Etcd: discov.EtcdConf{
  141. Hosts: hosts,
  142. Key: key,
  143. },
  144. })
  145. integralLib := integralclient.NewIntegral(client)
  146. req := &integral.Req{UserId: userDoc.UserId,
  147. PointType: 2003,
  148. BusinessTypeId: 1,
  149. BusinessType: "1",
  150. Point: int64(cost),
  151. AppId: userDoc.AppId}
  152. res, pointsErr := integralLib.IntegralConsume(context.Background(), req)
  153. log.Println("err ", pointsErr)
  154. log.Println("req ", res)
  155. if (pointsErr != nil) {
  156. log.Println("扣除积分失败:", pointsErr)
  157. msg = "扣除积分失败"
  158. return pointsErr
  159. }
  160. if (res.Code == 0) {
  161. log.Println("扣除积分失败:", pointsErr)
  162. msg = res.Message
  163. return errors.New(res.Message)
  164. }*/
  165. //查询之前有无记录 有记录类型更新兑换,状态改为未删除、没有记录插入一条新纪录
  166. userDocCollectionList := []model.UserDoc{}
  167. err = docRpcUtil.GetJyDocsDB().Table("user_doc").Where(" userId=? and docId=? and appId=? ", userDoc.UserId, userDoc.DocId, userDoc.AppId).Find(&userDocCollectionList)
  168. if err.Error != nil {
  169. log.Println("收藏记录查询:", err)
  170. msg = "收藏记录查询"
  171. return err.Error
  172. }
  173. if len(userDocCollectionList) > 0 {
  174. //兑换记录修改
  175. 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 {
  176. log.Println("兑换记录修改失败:", err)
  177. msg = "兑换记录修改失败"
  178. tx.Rollback()
  179. return err
  180. }
  181. if err.Error != nil {
  182. log.Println("兑换操作流水添加失败:", err)
  183. msg = "收藏记录更改为兑换记录失败"
  184. tx.Rollback()
  185. return err.Error
  186. }
  187. //用户收藏、兑换记录表添加记录
  188. 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)
  189. if err.Error != nil {
  190. log.Println("兑换操作流水添加失败:", err)
  191. msg = "兑换操作流水添加失败"
  192. tx.Rollback()
  193. return err.Error
  194. }
  195. } else {
  196. //用户文库表添加记录
  197. userDoc.CreateAt = time.Now()
  198. userDoc.UpdateAt = time.Now()
  199. userDoc.DeletedAt = time.Now()
  200. userDoc.IsCollection = 0
  201. userDoc.IsDownload = 1
  202. //获取文件基本信息
  203. //查询文档基础信息
  204. doc := model.Doc{}
  205. err0 := docRpcUtil.GetJyDocsDB().
  206. Where("id = ?", userDoc.DocId).
  207. Find(&doc)
  208. if err0.Error != nil {
  209. log.Println("文档不存在")
  210. msg = "文档不存在"
  211. return errors.New("文档不存在")
  212. }
  213. userDoc.DocSourceUserId = doc.UserId
  214. userDoc.IsDelete = int(userlib.UserDocStatus_Normal)
  215. userDoc.DocName = doc.DocName
  216. userDoc.DocFileType = doc.DocFileType
  217. userDoc.DocFileSuffix = doc.DocFileSuffix
  218. userDoc.DocFileSize = doc.DocFileSize
  219. userDoc.DocPageSize = doc.DocPageSize
  220. userDoc.DocSummary = doc.DocSummary
  221. userDoc.Cost = fmt.Sprint(doc.Price)
  222. err = docRpcUtil.GetJyDocsDB().Create(userDoc)
  223. if err.Error != nil {
  224. log.Println("兑换操作添加失败:", err)
  225. msg = "兑换操作添加失败"
  226. tx.Rollback()
  227. return err.Error
  228. }
  229. //用户收藏、兑换记录表添加记录
  230. 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)
  231. if err.Error != nil {
  232. log.Println("兑换操作流水添加失败:", err)
  233. msg = "兑换操作流水添加失败"
  234. tx.Rollback()
  235. return err.Error
  236. }
  237. }
  238. return nil
  239. })
  240. if err != nil {
  241. return false, msg
  242. }
  243. return true, msg
  244. }
  245. //文档删除
  246. func UserDocDelete(userDocId int32, appId, userId string) (bool, string) {
  247. msg := "文档删除成功"
  248. err := docRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
  249. //逻辑删除
  250. err := docRpcUtil.GetJyDocsDB().Exec("UPDATE user_doc SET isDelete = ? WHERE docId = ? and userId=? and appId=?", model.UserDocStatus_LogicDelete, userDocId, userId, appId).Error
  251. if err != nil {
  252. msg = "文档删除失败"
  253. log.Println("文档删除失败:", err)
  254. tx.Rollback()
  255. return err
  256. }
  257. //删除记录新增
  258. err = docRpcUtil.GetJyDocsDB().Exec("insert into del_record (docId, userId, date, operate, appId) values (?,?,?,?,?)", userDocId, userId, time.Now(), 1, appId).Error
  259. if err != nil {
  260. tx.Rollback()
  261. msg = "删除记录新增失败"
  262. log.Println("删除记录新增失败:", err)
  263. return err
  264. }
  265. return nil
  266. })
  267. if err != nil {
  268. return false, msg
  269. }
  270. return true, msg
  271. }
  272. //文档回收
  273. func UserDocRestore(userDocId int32, appId, userId string) (bool, string) {
  274. msg := "文档找回成功"
  275. err := docRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
  276. //逻辑删除
  277. err := docRpcUtil.GetJyDocsDB().Exec("UPDATE user_doc SET isDelete = ? WHERE docId = ? and userId=? and appId=?", model.UserDocStatus_Normal, userDocId, userId, appId).Error
  278. if err != nil {
  279. msg = "文档找回失败"
  280. log.Println("文档找回失败:", err)
  281. tx.Rollback()
  282. return err
  283. }
  284. //删除记录新增
  285. err = docRpcUtil.GetJyDocsDB().Exec("insert into del_record (docId, userId, date, operate, appId) values (?,?,?,?,?)", userDocId, userId, time.Now(), 2, appId).Error
  286. if err != nil {
  287. tx.Rollback()
  288. msg = "删除记录新增失败"
  289. log.Println("删除记录新增失败:", err)
  290. return err
  291. }
  292. return nil
  293. })
  294. if err != nil {
  295. return false, msg
  296. }
  297. return true, msg
  298. } //永久删除
  299. func UserDocPermanentDelete(userDocId int32, appId, userId string) (bool, string) {
  300. msg := "永久删除成功"
  301. err := docRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
  302. //逻辑删除
  303. err := docRpcUtil.GetJyDocsDB().Exec("UPDATE user_doc SET isDelete = ? ,isDownload=0 WHERE docId = ? and userId=? and appId=?", model.UserDocStatus_PermanentlyDelete, userDocId, userId, appId).Error
  304. if err != nil {
  305. msg = "永久删除失败"
  306. log.Println("永久删除失败:", err)
  307. tx.Rollback()
  308. return err
  309. }
  310. //删除记录新增
  311. err = docRpcUtil.GetJyDocsDB().Exec("insert into del_record (docId, userId, date, operate, appId) values (?,?,?,?,?)", userDocId, userId, time.Now(), 4, appId).Error
  312. if err != nil {
  313. tx.Rollback()
  314. msg = "删除记录新增失败"
  315. log.Println("删除记录新增失败:", err)
  316. return err
  317. }
  318. return nil
  319. })
  320. if err != nil {
  321. return false, msg
  322. }
  323. return true, msg
  324. }
  325. //我的文库列表(包括回收站列表)0兑换的 1收藏的 2回收站的
  326. func UserDocsList(in *userlib.UserDocsRequest) ([]*model.UserDoc, int64, bool, string) {
  327. msg := "查询成功"
  328. data := []*model.UserDoc{}
  329. count := int64(0)
  330. startIndex := (in.Page - 1) * in.PageSize
  331. err := docRpcUtil.GetJyDocsDB().Transaction(func(tx *gorm.DB) error {
  332. switch in.UserDocCategory {
  333. case int64(0):
  334. //兑换的
  335. err := docRpcUtil.GetJyDocsDB().Table("user_doc").Where(" userId=? and isDownload=1 and isDelete=? and appId=? ", in.UserId, model.UserDocStatus_Normal, in.AppId).Find(&data)
  336. count = int64(len(data))
  337. 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)
  338. if err.Error != nil {
  339. log.Println("查询兑换记录失败:", err)
  340. msg = "查询兑换记录失败"
  341. return err.Error
  342. }
  343. case int64(1):
  344. //收藏的
  345. err := docRpcUtil.GetJyDocsDB().Table("user_doc").Where(" userId=? and isCollection=1 and appId=? ", in.UserId, in.AppId).Find(&data)
  346. count = int64(len(data))
  347. 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)
  348. if err.Error != nil {
  349. log.Println("查询收藏记录失败:", err)
  350. msg = "查询收藏记录失败"
  351. return err.Error
  352. }
  353. case int64(2):
  354. //回收站
  355. err := docRpcUtil.GetJyDocsDB().Table("user_doc").Where(" userId=? and isDelete=? and appId=? ", in.UserId, model.UserDocStatus_LogicDelete, in.AppId).Find(&data)
  356. count = int64(len(data))
  357. 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)
  358. if err.Error != nil {
  359. log.Println("查询回收站记录失败:", err)
  360. msg = "查询回收站记录失败"
  361. return err.Error
  362. }
  363. }
  364. return nil
  365. })
  366. if err != nil {
  367. return data, count, false, msg
  368. }
  369. return data, count, true, msg
  370. }
  371. /*接口日志添加*/
  372. func InterfaceLog(in *model.InterfaceLog) bool {
  373. orm := docRpcUtil.GetJyDocsDB()
  374. err := orm.Transaction(func(tx *gorm.DB) error {
  375. err := orm.Create(in)
  376. if err.Error != nil {
  377. log.Println("接口日志添加失败:", err)
  378. tx.Rollback()
  379. return err.Error
  380. }
  381. return nil
  382. })
  383. if err != nil {
  384. return false
  385. }
  386. return true
  387. }
  388. //账号合并
  389. func UserMerge(mergeUser, mergedUser, appId string) (bool, string) {
  390. log.Println("UserMerge exec ......")
  391. orm := docRpcUtil.GetJyDocsDB()
  392. docData := []model.UserDoc{}
  393. msg := ""
  394. //先查询被合并账号所有信息
  395. err := orm.Transaction(func(tx *gorm.DB) error {
  396. err0 := orm.Select("*").
  397. Where("userId = ? AND appId = ?", mergedUser, appId).Find(&docData).Error
  398. if err0 != nil {
  399. log.Println("查询被合并账号失败")
  400. return errors.New("查询被合并账号失败")
  401. }
  402. //处理每一条数据
  403. for _, value := range docData {
  404. //1先判断有没有老数据是否需要新增
  405. mergeUserDocData := model.UserDocData{}
  406. err := orm.Table("user_doc").Where(" userId=? and docId=? and appId=? ", mergeUser, value.DocId, appId).Find(&mergeUserDocData)
  407. if err.Error != nil {
  408. log.Printf("查询,userId:[%s],docId:[%s] 失败", mergeUser, value.DocId)
  409. msg = "查询用户信息失败"
  410. return err.Error
  411. }
  412. if mergeUserDocData.UserId == "" {
  413. userDoc := model.UserDoc{}
  414. userDoc.DocSourceUserId = value.DocSourceUserId
  415. userDoc.DocCategory = 2
  416. userDoc.UserId = mergeUser
  417. userDoc.DocId = value.DocId
  418. userDoc.AppId = value.AppId
  419. userDoc.PreviewImgId = value.PreviewImgId
  420. userDoc.PreviewImgUrl = value.PreviewImgUrl
  421. userDoc.IsDelete = int(userlib.UserDocStatus_Normal)
  422. userDoc.DocName = value.DocName
  423. userDoc.DocFileType = value.DocFileType
  424. userDoc.DocFileSuffix = value.DocFileSuffix
  425. userDoc.DocFileSize = value.DocFileSize
  426. userDoc.DocPageSize = value.DocPageSize
  427. userDoc.DocSummary = value.DocSummary
  428. userDoc.IsCollection = value.IsCollection
  429. userDoc.IsDownload = value.IsDownload
  430. userDoc.Cost = value.Cost
  431. timeData := time.Now()
  432. userDoc.CreateAt = timeData
  433. userDoc.UpdateAt = timeData
  434. userDoc.DeletedAt = timeData
  435. err := orm.Create(&userDoc).Error
  436. if err != nil {
  437. log.Println("userDocCollect error:", err)
  438. tx.Rollback()
  439. return err
  440. }
  441. }
  442. if (mergeUserDocData.IsDownload==1){
  443. value.IsDownload=mergeUserDocData.IsDownload
  444. value.IsDelete=mergeUserDocData.IsDelete
  445. }
  446. if (mergeUserDocData.IsCollection==1){
  447. value.IsCollection=mergeUserDocData.IsCollection
  448. }
  449. 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)
  450. if err.Error != nil {
  451. log.Println("文档合并失败")
  452. tx.Rollback()
  453. return err.Error
  454. }
  455. }
  456. return nil
  457. })
  458. if err != nil {
  459. return false, "合并失败"
  460. }
  461. return true, "合并成功"
  462. }