userDocService.go 18 KB

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