fileService.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package service
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/file/dao"
  5. "iot_manager_service/app/file/model"
  6. userdao "iot_manager_service/app/user/dao"
  7. "iot_manager_service/util/common"
  8. "path"
  9. "strconv"
  10. "time"
  11. )
  12. // 用户管理服务
  13. var FileService = new(fileService)
  14. type fileService struct{}
  15. func (f *fileService) Distribute(encryptedname, savepath string, requserfile *model.ReqUserFile, uid int) *common.Errors {
  16. file := &dao.File{
  17. OriginalName: requserfile.OriginalName,
  18. EncryptedName: encryptedname,
  19. SavePath: savepath,
  20. CategoryName: requserfile.CategoryName,
  21. AuthId: requserfile.AuthId,
  22. SuffixName: path.Ext(requserfile.OriginalName),
  23. UploadTime: time.Now(),
  24. Uploader: uid,
  25. }
  26. err := file.Create()
  27. if err != nil {
  28. return common.FailResponse("保存文件失败", nil)
  29. }
  30. //查询部门下对应的员工
  31. user := &userdao.User{}
  32. users, err := user.GetDeptsUsers(requserfile.DeptsId)
  33. if err != nil {
  34. return common.FailResponse(err.Error(), nil)
  35. }
  36. var userfile []dao.UserFile
  37. for _, u := range users {
  38. userfile = append(userfile, dao.UserFile{
  39. Operator: uid,
  40. Receiver: u.ID,
  41. FileID: file.ID,
  42. EffectiveDate: requserfile.EffectiveDate,
  43. OperationStatus: "上传",
  44. })
  45. }
  46. userfiledao := &dao.UserFile{}
  47. err = userfiledao.BatchUFRecords(userfile)
  48. if err != nil {
  49. return common.FailResponse(err.Error(), nil)
  50. }
  51. return common.SuccessResponse("新增记录成功", nil)
  52. }
  53. func (f *fileService) GetUserFiles(id string) *common.Errors {
  54. filedao := &dao.File{}
  55. uid, _ := strconv.Atoi(id)
  56. files, err := filedao.GetUserFiles(uid)
  57. if err != nil {
  58. return common.FailResponse(err.Error(), nil)
  59. }
  60. return common.SuccessResponse("查询成功", gin.H{"files": files})
  61. }