safe_service.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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) (int32, string, []viewmodels.Safe)
  19. Post(id int, code string, position string, createTime string, inspection string) (int32, string)
  20. ValidRule(ctx iris.Context) (viewmodels.Safe, error)
  21. }
  22. // //返回service操作类
  23. type safeService struct {
  24. rpcClient *grpc.ClientConn
  25. valid string
  26. }
  27. //创建项目用户service
  28. func NewSafeService() SafeService {
  29. return &safeService{
  30. rpcClient: newGrpcClient(conf.NodeRpcHost),
  31. valid: "/api/safe",
  32. }
  33. }
  34. func newGrpcClient(address string) *grpc.ClientConn {
  35. // 启动grpc客户端,连接grpc服务端
  36. conn, err := grpc.Dial(address, grpc.WithInsecure())
  37. if err != nil {
  38. log.Fatalf("did not connect: %v", err)
  39. }
  40. // defer conn.Close()
  41. return conn
  42. }
  43. func (s *safeService) Get(id int) (int32, string, []viewmodels.Safe) {
  44. // 1.结束后关闭
  45. // defer s.rpcClient.Close()
  46. // 使用连接,创建HelloService实例
  47. safeCmList := make([]models.CmSafe, 0)
  48. safeList := make([]viewmodels.Safe, 0)
  49. rpcClient := rpc.NewSafeClient(s.rpcClient)
  50. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  51. defer cancel()
  52. res, err := rpcClient.GetSafeList(ctx, &rpc.SafeGetRequest{Id: int32(id)})
  53. if err != nil {
  54. log.Fatalf("could not greet: %v", err)
  55. return -1, "", safeList
  56. }
  57. err = json.Unmarshal([]byte(res.GetData()), &safeCmList)
  58. if err != nil {
  59. msg := fmt.Sprintf("Json转换出错:err=", err)
  60. log.Println("Json转换出错:err=", err)
  61. return -1, msg, safeList
  62. }
  63. for _, item := range safeCmList {
  64. safeVM := viewmodels.Safe{}
  65. safeVM.Code = item.Code
  66. bidId, _ := comm.AesEncrypt(strconv.Itoa(item.BidsectionId), conf.SignSecret)
  67. safeVM.BidsectionId = bidId
  68. safeVM.CreateTime = item.CreateTime
  69. safeVM.Demand = item.Demand
  70. safeVM.EndTime = item.EndTime
  71. id, _ := comm.AesEncrypt(strconv.Itoa(item.Id), conf.SignSecret)
  72. safeVM.Id = id
  73. safeVM.Inspection = item.Inspection
  74. safeVM.InspectionDetail = item.InspectionDetail
  75. safeVM.Position = item.Position
  76. safeVM.Status = item.Status
  77. safeList = append(safeList, safeVM)
  78. }
  79. return res.GetCode(), res.GetMsg(), safeList
  80. }
  81. func (s *safeService) Post(id int, code string, position string, createTime string, inspection string) (int32, string) {
  82. rpcClient := rpc.NewSafeClient(s.rpcClient)
  83. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  84. defer cancel()
  85. res, err := rpcClient.PostSafe(ctx, &rpc.SafePostRequest{Id: int32(id), Code: code, Position: position, CreateTime: createTime, Inspection: inspection})
  86. if err != nil {
  87. log.Fatalf("could not greet: %v", err)
  88. return -1, ""
  89. }
  90. return res.GetCode(), res.GetMsg()
  91. }
  92. func (s *safeService) ValidRule(ctx iris.Context) (viewmodels.Safe, error) {
  93. safeVaild := viewmodels.Safe{}
  94. err := ctx.ReadForm(&safeVaild)
  95. //ReadJSON
  96. if err != nil {
  97. log.Println("account-ValidRule-ReadForm转换异常, error=", err)
  98. return safeVaild, err
  99. }
  100. if ctx.Path() == s.valid {
  101. err = safeVaild.ValidateList()
  102. }
  103. if err != nil {
  104. log.Println("获取安全巡检列表, error=", err)
  105. return safeVaild, err
  106. }
  107. return safeVaild, nil
  108. }