userDocService.go 13 KB

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