123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- 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, RpcConnect *grpc.ClientConn) (int32, string, []viewmodels.Safe)
- Post(bidsectionId int, code string, position string, createTime string, inspection string, RpcConnect *grpc.ClientConn) (int32, string)
- ValidRule(ctx iris.Context) (viewmodels.Safe, error)
- }
- // //返回service操作类
- type safeService struct {
- valid string
- validCreate string
- }
- //创建项目用户service
- func NewSafeService() SafeService {
- return &safeService{
- valid: "/api/safe",
- validCreate: "/api/safe/create",
- }
- }
- func (s *safeService) Get(id int, RpcConnect *grpc.ClientConn) (int32, string, []viewmodels.Safe) {
- // 1.结束后关闭
- // defer s.rpcClient.Close()
- // 使用连接,创建HelloService实例
- safeCmList := make([]models.CmSafe, 0)
- safeList := make([]viewmodels.Safe, 0)
- rpcClient := rpc.NewSafeClient(RpcConnect)
- 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.Format("2006-01-02 15:04:05")
- 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(bidsectionId int, code string, position string, createTime string, inspection string, RpcConnect *grpc.ClientConn) (int32, string) {
- rpcClient := rpc.NewSafeClient(RpcConnect)
- ctx, cancel := context.WithTimeout(context.Background(), time.Second)
- defer cancel()
- res, err := rpcClient.PostSafe(ctx, &rpc.SafePostRequest{BidsectionId: int32(bidsectionId), 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{}
- if ctx.Path() == s.valid {
- err := ctx.ReadForm(&safeVaild)
- if err != nil {
- log.Println("safe-ValidRule-ReadJson转换异常, error=", err)
- return safeVaild, err
- }
- err = safeVaild.ValidateList()
- }
- if ctx.Path() == s.validCreate {
- err := ctx.ReadJSON(&safeVaild)
- if err != nil {
- log.Println("safe-ValidRule-ReadForm转换异常, error=", err)
- return safeVaild, err
- }
- err = safeVaild.ValidateCreate()
- }
- return safeVaild, nil
- }
|