history.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "github.com/gin-gonic/gin"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. )
  11. var HistoryProject = []string{
  12. "_id",
  13. "path",
  14. "collection",
  15. "type",
  16. "userInfo",
  17. "createTime",
  18. }
  19. func BillHistoryList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  20. // ?query={"targetId":"","collection":""}
  21. page, size, query := UtilQueryPageSize(c)
  22. if _, ok := query["targetId"]; !ok {
  23. return errors.New("目标id不能为空"), nil
  24. }
  25. if _, ok := query["collection"]; !ok {
  26. return errors.New("目标类型不能为空"), nil
  27. }
  28. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  29. CollectName: repo.CollectionBillHistory,
  30. Query: query,
  31. Page: page,
  32. Size: size,
  33. Project: HistoryProject,
  34. Sort: bson.M{"createTime": -1},
  35. })
  36. }
  37. func GetBillHistory(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  38. hid := c.Param("id")
  39. id, err := primitive.ObjectIDFromHex(hid)
  40. if err != nil {
  41. return nil, errors.New("非法id")
  42. }
  43. var history model.History
  44. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  45. CollectName: repo.CollectionBillHistory,
  46. Query: repo.Map{"_id": id},
  47. }, &history)
  48. if !found || err != nil {
  49. log.Info(err)
  50. return nil, errors.New("数据未找到")
  51. }
  52. return history, nil
  53. }
  54. func PlanHistoryList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  55. // ?query={"targetId":"","collection":""}
  56. page, size, query := UtilQueryPageSize(c)
  57. if _, ok := query["targetId"]; !ok {
  58. return errors.New("目标id不能为空"), nil
  59. }
  60. if _, ok := query["collection"]; !ok {
  61. return errors.New("目标类型不能为空"), nil
  62. }
  63. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  64. CollectName: repo.CollectionPlanHistory,
  65. Query: query,
  66. Page: page,
  67. Size: size,
  68. Project: HistoryProject,
  69. Sort: bson.M{"createTime": -1},
  70. })
  71. }
  72. func GetPlanHistory(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  73. hid := c.Param("id")
  74. id, err := primitive.ObjectIDFromHex(hid)
  75. if err != nil {
  76. return nil, errors.New("非法id")
  77. }
  78. var history model.History
  79. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  80. CollectName: repo.CollectionPlanHistory,
  81. Query: repo.Map{"_id": id},
  82. }, &history)
  83. if !found || err != nil {
  84. log.Info(err)
  85. return nil, errors.New("数据未找到")
  86. }
  87. return history, nil
  88. }