123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package service
- import (
- "github.com/gin-gonic/gin"
- "io/ioutil"
- "iot_manager_service/app/file/dao"
- "iot_manager_service/app/file/model"
- userdao "iot_manager_service/app/user/dao"
- "iot_manager_service/util/common"
- "iot_manager_service/util/websocket"
- "net/http"
- "path"
- "strconv"
- "time"
- )
- // 用户管理服务
- var FileService = new(fileService)
- type fileService struct{}
- func (f *fileService) Distribute(encryptedname, savepath string, requserfile *model.ReqUserFile, uid int) *common.Errors {
- file := &dao.File{
- OriginalName: requserfile.OriginalName,
- EncryptedName: encryptedname,
- SavePath: savepath,
- CategoryName: requserfile.CategoryName,
- AuthId: requserfile.AuthId,
- SuffixName: path.Ext(requserfile.OriginalName),
- UploadTime: time.Now(),
- Uploader: uid,
- }
- err := file.Create()
- if err != nil {
- return common.FailResponse("保存文件失败", nil)
- }
- //查询部门下对应的员工
- user := &userdao.User{}
- users, err := user.GetDeptsUsers(requserfile.DeptsId)
- if err != nil {
- return common.FailResponse(err.Error(), nil)
- }
- var userfile []dao.UserFile
- for _, u := range users {
- userfile = append(userfile, dao.UserFile{
- Operator: uid,
- Receiver: u.ID,
- FileID: file.ID,
- EffectiveDate: &requserfile.EffectiveDate,
- OperationStatus: "上传",
- })
- }
- userfiledao := &dao.UserFile{}
- err = userfiledao.BatchUFRecords(userfile)
- if err != nil {
- return common.FailResponse(err.Error(), nil)
- }
- //下发消息
- for _, u := range users {
- websocket.SendMessage(strconv.Itoa(u.ID), "您有新的文件,请注意查收!")
- }
- return common.SuccessResponse("新增记录成功", nil)
- }
- func (f *fileService) GetUserFiles(id string) *common.Errors {
- filedao := &dao.File{}
- uid, _ := strconv.Atoi(id)
- files, err := filedao.GetUserFiles(uid)
- if err != nil {
- return common.FailResponse(err.Error(), nil)
- }
- return common.SuccessResponse("查询成功", gin.H{"files": files})
- }
- func (f *fileService) GetFile(id string, c *gin.Context) {
- filedao := &dao.File{}
- file, err := filedao.GetFile(id)
- if err != nil {
- panic(err.Error())
- }
- encryptedData, err := ioutil.ReadFile(file.SavePath)
- if err != nil {
- panic(err.Error())
- }
- c.Header("Content-Disposition", "attachment;filename="+file.EncryptedName)
- c.Header("Content-Type", "application/octet-stream")
- c.Data(http.StatusOK, "application/octet-stream", encryptedData)
- }
- // 转发
- func (f *fileService) ForwardingFile(uid, fileid int, users []int) *common.Errors {
- var userfile []dao.UserFile
- var nilTime *time.Time
- for _, u := range users {
- userfile = append(userfile, dao.UserFile{
- Operator: uid,
- Receiver: u,
- FileID: fileid,
- EffectiveDate: nilTime,
- OperationStatus: "转发",
- })
- }
- userfiledao := &dao.UserFile{}
- err := userfiledao.BatchUFRecords(userfile)
- if err != nil {
- return common.FailResponse(err.Error(), nil)
- }
- return common.SuccessResponse("新增记录成功", nil)
- }
|