safe_service.go 3.6 KB

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