123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- package services
- import (
- "context"
- "encoding/json"
- "fmt"
- "log"
- "strconv"
- "time"
- "go.mod/models"
- "github.com/kataras/iris/v12"
- "go.mod/comm"
- "go.mod/conf"
- rpc "go.mod/proto"
- "go.mod/web/viewmodels"
- "google.golang.org/grpc"
- )
- type SafeService interface {
- Get(id int) (int32, string, []viewmodels.Safe)
- Post(id int, code string, position string, createTime string, inspection string) (int32, string)
- ValidRule(ctx iris.Context) (viewmodels.Safe, error)
- }
- // //返回service操作类
- type safeService struct {
- rpcClient *grpc.ClientConn
- valid string
- }
- //创建项目用户service
- func NewSafeService() SafeService {
- return &safeService{
- rpcClient: newGrpcClient(conf.NodeRpcHost),
- valid: "/api/safe",
- }
- }
- func newGrpcClient(address string) *grpc.ClientConn {
- // 启动grpc客户端,连接grpc服务端
- conn, err := grpc.Dial(address, grpc.WithInsecure())
- if err != nil {
- log.Fatalf("did not connect: %v", err)
- }
- // defer conn.Close()
- return conn
- }
- func (s *safeService) Get(id int) (int32, string, []viewmodels.Safe) {
- // 1.结束后关闭
- // defer s.rpcClient.Close()
- // 使用连接,创建HelloService实例
- safeCmList := make([]models.CmSafe, 0)
- safeList := make([]viewmodels.Safe, 0)
- rpcClient := rpc.NewSafeClient(s.rpcClient)
- ctx, cancel := context.WithTimeout(context.Background(), time.Second)
- defer cancel()
- res, err := rpcClient.GetSafeList(ctx, &rpc.SafeGetRequest{Id: int32(id)})
- if err != nil {
- log.Fatalf("could not greet: %v", err)
- return -1, "", safeList
- }
- err = json.Unmarshal([]byte(res.GetData()), &safeCmList)
- if err != nil {
- msg := fmt.Sprintf("Json转换出错:err=", err)
- log.Println("Json转换出错:err=", err)
- return -1, msg, safeList
- }
- for _, item := range safeCmList {
- safeVM := viewmodels.Safe{}
- safeVM.Code = item.Code
- bidId, _ := comm.AesEncrypt(strconv.Itoa(item.BidsectionId), conf.SignSecret)
- safeVM.BidsectionId = bidId
- safeVM.CreateTime = item.CreateTime
- safeVM.Demand = item.Demand
- safeVM.EndTime = item.EndTime
- id, _ := comm.AesEncrypt(strconv.Itoa(item.Id), conf.SignSecret)
- safeVM.Id = id
- safeVM.Inspection = item.Inspection
- safeVM.InspectionDetail = item.InspectionDetail
- safeVM.Position = item.Position
- safeVM.Status = item.Status
- safeList = append(safeList, safeVM)
- }
- return res.GetCode(), res.GetMsg(), safeList
- }
- func (s *safeService) Post(id int, code string, position string, createTime string, inspection string) (int32, string) {
- rpcClient := rpc.NewSafeClient(s.rpcClient)
- ctx, cancel := context.WithTimeout(context.Background(), time.Second)
- defer cancel()
- res, err := rpcClient.PostSafe(ctx, &rpc.SafePostRequest{Id: int32(id), Code: code, Position: position, CreateTime: createTime, Inspection: inspection})
- if err != nil {
- log.Fatalf("could not greet: %v", err)
- return -1, ""
- }
- return res.GetCode(), res.GetMsg()
- }
- func (s *safeService) ValidRule(ctx iris.Context) (viewmodels.Safe, error) {
- safeVaild := viewmodels.Safe{}
- err := ctx.ReadForm(&safeVaild)
- //ReadJSON
- if err != nil {
- log.Println("account-ValidRule-ReadForm转换异常, error=", err)
- return safeVaild, err
- }
- if ctx.Path() == s.valid {
- err = safeVaild.ValidateList()
- }
- if err != nil {
- log.Println("获取安全巡检列表, error=", err)
- return safeVaild, err
- }
- return safeVaild, nil
- }
|