safe_service.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "strconv"
  8. "time"
  9. "go.mod/models"
  10. "github.com/kataras/iris/v12"
  11. "go.mod/comm"
  12. "go.mod/conf"
  13. rpc "go.mod/proto"
  14. "go.mod/web/viewmodels"
  15. "google.golang.org/grpc"
  16. )
  17. type SafeService interface {
  18. Get(id int, RpcConnect *grpc.ClientConn) (int32, string, []viewmodels.Safe)
  19. Post(bidsectionId int, code string, position string, createTime string, inspection string, RpcConnect *grpc.ClientConn) (int32, string)
  20. ValidRule(ctx iris.Context) (viewmodels.Safe, error)
  21. }
  22. // //返回service操作类
  23. type safeService struct {
  24. valid string
  25. validCreate string
  26. }
  27. //创建项目用户service
  28. func NewSafeService() SafeService {
  29. return &safeService{
  30. valid: "/api/safe",
  31. validCreate: "/api/safe/create",
  32. }
  33. }
  34. func (s *safeService) Get(id int, RpcConnect *grpc.ClientConn) (int32, string, []viewmodels.Safe) {
  35. // 1.结束后关闭
  36. // defer s.rpcClient.Close()
  37. // 使用连接,创建HelloService实例
  38. safeCmList := make([]models.CmSafe, 0)
  39. safeList := make([]viewmodels.Safe, 0)
  40. rpcClient := rpc.NewSafeClient(RpcConnect)
  41. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  42. defer cancel()
  43. res, err := rpcClient.GetSafeList(ctx, &rpc.SafeGetRequest{Id: int32(id)})
  44. if err != nil {
  45. log.Fatalf("could not greet: %v", err)
  46. return -1, "", safeList
  47. }
  48. err = json.Unmarshal([]byte(res.GetData()), &safeCmList)
  49. if err != nil {
  50. msg := fmt.Sprintf("Json转换出错:err=", err)
  51. log.Println("Json转换出错:err=", err)
  52. return -1, msg, safeList
  53. }
  54. for _, item := range safeCmList {
  55. safeVM := viewmodels.Safe{}
  56. safeVM.Code = item.Code
  57. bidId, _ := comm.AesEncrypt(strconv.Itoa(item.BidsectionId), conf.SignSecret)
  58. safeVM.BidsectionId = bidId
  59. // safeVM.CreateTime = item.CreateTime
  60. safeVM.Demand = item.Demand
  61. safeVM.EndTime = item.EndTime.Format("2006-01-02 15:04:05")
  62. id, _ := comm.AesEncrypt(strconv.Itoa(item.Id), conf.SignSecret)
  63. safeVM.Id = id
  64. safeVM.Inspection = item.Inspection
  65. safeVM.InspectionDetail = item.InspectionDetail
  66. safeVM.Position = item.Position
  67. safeVM.Status = item.Status
  68. safeList = append(safeList, safeVM)
  69. }
  70. return res.GetCode(), res.GetMsg(), safeList
  71. }
  72. func (s *safeService) Post(bidsectionId int, code string, position string, createTime string, inspection string, RpcConnect *grpc.ClientConn) (int32, string) {
  73. rpcClient := rpc.NewSafeClient(RpcConnect)
  74. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  75. defer cancel()
  76. res, err := rpcClient.PostSafe(ctx, &rpc.SafePostRequest{BidsectionId: int32(bidsectionId), Code: code, Position: position, CreateTime: createTime, Inspection: inspection})
  77. if err != nil {
  78. log.Fatalf("could not greet: %v", err)
  79. return -1, ""
  80. }
  81. return res.GetCode(), res.GetMsg()
  82. }
  83. func (s *safeService) ValidRule(ctx iris.Context) (viewmodels.Safe, error) {
  84. safeVaild := viewmodels.Safe{}
  85. if ctx.Path() == s.valid {
  86. err := ctx.ReadForm(&safeVaild)
  87. if err != nil {
  88. log.Println("safe-ValidRule-ReadJson转换异常, error=", err)
  89. return safeVaild, err
  90. }
  91. err = safeVaild.ValidateList()
  92. }
  93. if ctx.Path() == s.validCreate {
  94. err := ctx.ReadJSON(&safeVaild)
  95. if err != nil {
  96. log.Println("safe-ValidRule-ReadForm转换异常, error=", err)
  97. return safeVaild, err
  98. }
  99. err = safeVaild.ValidateCreate()
  100. }
  101. return safeVaild, nil
  102. }