fileService.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package service
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "io/ioutil"
  5. "iot_manager_service/app/file/dao"
  6. "iot_manager_service/app/file/model"
  7. userdao "iot_manager_service/app/user/dao"
  8. "iot_manager_service/util/common"
  9. "iot_manager_service/util/websocket"
  10. "net/http"
  11. "path"
  12. "strconv"
  13. "time"
  14. )
  15. // 用户管理服务
  16. var FileService = new(fileService)
  17. type fileService struct{}
  18. func (f *fileService) Distribute(encryptedname, savepath string, requserfile *model.ReqUserFile, uid int) *common.Errors {
  19. file := &dao.File{
  20. OriginalName: requserfile.OriginalName,
  21. EncryptedName: encryptedname,
  22. SavePath: savepath,
  23. CategoryName: requserfile.CategoryName,
  24. AuthId: requserfile.AuthId,
  25. SuffixName: path.Ext(requserfile.OriginalName),
  26. UploadTime: time.Now(),
  27. Uploader: uid,
  28. }
  29. err := file.Create()
  30. if err != nil {
  31. return common.FailResponse("保存文件失败", nil)
  32. }
  33. //查询部门下对应的员工
  34. user := &userdao.User{}
  35. users, err := user.GetDeptsUsers(requserfile.DeptsId)
  36. if err != nil {
  37. return common.FailResponse(err.Error(), nil)
  38. }
  39. var userfile []dao.UserFile
  40. for _, u := range users {
  41. userfile = append(userfile, dao.UserFile{
  42. Operator: uid,
  43. Receiver: u.ID,
  44. FileID: file.ID,
  45. EffectiveDate: &requserfile.EffectiveDate,
  46. OperationStatus: "上传",
  47. })
  48. }
  49. userfiledao := &dao.UserFile{}
  50. err = userfiledao.BatchUFRecords(userfile)
  51. if err != nil {
  52. return common.FailResponse(err.Error(), nil)
  53. }
  54. //下发消息
  55. for _, u := range users {
  56. websocket.SendMessage(strconv.Itoa(u.ID), "您有新的文件,请注意查收!")
  57. }
  58. return common.SuccessResponse("新增记录成功", nil)
  59. }
  60. func (f *fileService) GetUserFiles(id string) *common.Errors {
  61. filedao := &dao.File{}
  62. uid, _ := strconv.Atoi(id)
  63. files, err := filedao.GetUserFiles(uid)
  64. if err != nil {
  65. return common.FailResponse(err.Error(), nil)
  66. }
  67. return common.SuccessResponse("查询成功", gin.H{"files": files})
  68. }
  69. func (f *fileService) GetFile(id string, c *gin.Context) {
  70. filedao := &dao.File{}
  71. file, err := filedao.GetFile(id)
  72. if err != nil {
  73. panic(err.Error())
  74. }
  75. encryptedData, err := ioutil.ReadFile(file.SavePath)
  76. if err != nil {
  77. panic(err.Error())
  78. }
  79. c.Header("Content-Disposition", "attachment;filename="+file.EncryptedName)
  80. c.Header("Content-Type", "application/octet-stream")
  81. c.Data(http.StatusOK, "application/octet-stream", encryptedData)
  82. }
  83. // 转发
  84. func (f *fileService) ForwardingFile(uid, fileid int, users []int) *common.Errors {
  85. var userfile []dao.UserFile
  86. var nilTime *time.Time
  87. for _, u := range users {
  88. userfile = append(userfile, dao.UserFile{
  89. Operator: uid,
  90. Receiver: u,
  91. FileID: fileid,
  92. EffectiveDate: nilTime,
  93. OperationStatus: "转发",
  94. })
  95. }
  96. userfiledao := &dao.UserFile{}
  97. err := userfiledao.BatchUFRecords(userfile)
  98. if err != nil {
  99. return common.FailResponse(err.Error(), nil)
  100. }
  101. return common.SuccessResponse("新增记录成功", nil)
  102. }