userDocService.go 14 KB

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