progress_add.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package project
  4. import (
  5. "context"
  6. "github.com/jaryhe/gopkgs/database"
  7. "github.com/jaryhe/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "google.golang.org/grpc/status"
  10. "smart-site-management/errors"
  11. "smart-site-management/model"
  12. "smart-site-management/pb/v1"
  13. "time"
  14. )
  15. // 添加工程进度
  16. func ProjectProgressAdd(ctx context.Context, req *v1.ProjectProgressAddRequest) (reply *v1.ProjectProgressAddReply, err error) {
  17. reply = &v1.ProjectProgressAddReply{}
  18. if req.ProjectId == 0 || req.Name == "" || req.ExpectStartTime == 0 || req.ExpectEndTime == 0 || req.ExpectEndTime < req.ExpectStartTime{
  19. return nil, errors.ParamsError
  20. }
  21. project := &model.TProject{}
  22. where := map[string]interface{}{
  23. "id":req.ProjectId,
  24. }
  25. err = project.Find(database.DB(), where)
  26. if err != nil {
  27. logger.Error("ProjectProgressAdd",
  28. zap.String("err", err.Error()))
  29. return nil, errors.DataBaseError
  30. }
  31. if project.Id == 0 {
  32. return nil, errors.NoRecordError
  33. }
  34. if req.ExpectStartTime < project.StartDay.Unix() {
  35. return nil, status.Error(10003, "进度开始时间不得小于工程开工时间")
  36. }
  37. if req.ExpectEndTime > project.EndDay.Unix() {
  38. return nil, status.Error(10003, "进度结束时间不得大于工程竣工时间")
  39. }
  40. p := &model.TProjectSchedule{}
  41. where = map[string]interface{}{
  42. "project_id":req.ProjectId,
  43. "name":req.Name,
  44. }
  45. err = p.Find(database.DB(), where, nil)
  46. if err != nil {
  47. logger.Error("ProjectProgressAdd",
  48. zap.String("err", err.Error()))
  49. return nil, errors.DataBaseError
  50. }
  51. if p.ID > 0 {
  52. return nil, errors.ProjectProgressExistError
  53. }
  54. now := time.Now()
  55. p.Name = req.Name
  56. p.ProjectId = req.ProjectId
  57. p.Status = 0
  58. p.ExpectStartTime = time.Unix(req.ExpectStartTime, 0)
  59. p.ExpectEndTime = time.Unix(req.ExpectEndTime, 0)
  60. p.UpdatedAt = now
  61. p.CreatedAt = now
  62. err = p.Insert(database.DB())
  63. if err != nil {
  64. logger.Error("ProjectProgressAdd",
  65. zap.String("err", err.Error()))
  66. return nil, errors.DataBaseError
  67. }
  68. reply.Id = p.ID
  69. return reply, nil
  70. }