package controller import ( v1 "demo/api/v1" "demo/internal/app" "demo/internal/model" "encoding/json" "fmt" "github.com/gogf/gf/v2/net/ghttp" "github.com/pkg/errors" "github.com/siddontang/go/log" "time" ) // GetReceiver // @Summary 接收器管理查看 // @Description add by json account // @Tags 接收器管理 // @Accept json // @Produce json // @Param index path int true "int " // @Param pagesize path int true "int " // @Param code path string true "string 接收器编码" // @Param status path int true "string 状态" // @Success 200 {object} v1.GetReceiverRes // @Router /receiver/list [get] func GetReceiver(r *ghttp.Request) { var req *v1.GetReceiverReq appG := app.Ghttp{C: r} err := appG.C.Parse(&req) if err != nil { err = errors.Wrap(err, "GetNeckRingListing-error") log.Error(err) appG.Response(app.StatusInternalServerError, app.ERROR, err) return } receiverList, count, err := srv.GetReceiverList(req.Index, req.PageSize, req.Code, req.Status) if err != nil { return } resp := v1.GetReceiverRes{} dataList := make([]*v1.Receiver, 0) for _, item := range receiverList { var data v1.Receiver data.ReceiverId = item.Receiver.Id data.Code = item.IMEI data.Pastureid = item.Receiver.Pastureid data.BarId = item.Receiver.BarId data.BarName = item.BarName data.SimId = item.Receiver.Id data.Status = item.Receiver.Status data.Location = item.BarName if !item.UpdateDate.IsZero() { data.UpdateDate = item.Receiver.UpdateDate.Format("2006-01-02 15:04:05") } data.Card = item.Sim.Card data.PastureName = item.PastureName data.PastureCode = item.PastureCode data.Id = item.Receiver.Id if item.Image != "" { data.Image = fmt.Sprintf("image/%s", item.Image) } style := new(v1.Style) coordinatesmap := make(map[string]string) if item.Coordinates != "" { err := json.Unmarshal([]byte(item.Coordinates), &coordinatesmap) if err == nil { if _, ok := coordinatesmap["left"]; ok { if coordinatesmap["left"] != "" { style.Left = coordinatesmap["left"] } } if _, ok := coordinatesmap["top"]; ok { if coordinatesmap["top"] != "" { style.Top = coordinatesmap["top"] } } } } data.Style = style dataList = append(dataList, &data) } resp.List = dataList resp.Total = count appG.Response(app.StatusOK, app.SUCCESS, resp) } //AddReceiver //@Summary 添加接收器 //@Description add by json account //@Tags 接收器管理 //@Accept json //@Produce json //@Param account body v1.AddReceiverReq true "Add account" //@Router /receiver/add [post] func AddReceiver(r *ghttp.Request) { var req *v1.AddReceiverReq appG := app.Ghttp{C: r} err := appG.C.Parse(&req) if err != nil { err = errors.Wrap(err, "AddReceiver-error") log.Error(err) appG.Response(app.StatusInternalServerError, app.ERROR, err) return } exist, err := srv.CheckReceiverCode(0, req.Code) if err != nil { err = errors.Wrap(err, "AddReceiver-error") log.Error(err) appG.Response(app.StatusInternalServerError, app.ERROR, err) return } if exist { appG.Response(app.StatusInternalServerError, app.ERROR, "IMEI卡号已经存在!!!") return } err = srv.AddReceiver(model.Receiver{ IMEI: req.Code, Pastureid: req.Pastureid, BarId: req.BarId, //BarName: req.BarName, SimID: req.SimId, Status: req.Status, //Location: req.Location, UpdateDate: time.Now(), }) if err != nil { err = errors.Wrap(err, "AddReceiver-error") log.Error(err) appG.Response(app.StatusInternalServerError, app.ERROR, err) return } appG.Response(app.StatusOK, app.SUCCESS, true) } //EditReceiver //@Summary 编辑接收器 //@Description add by json account //@Tags 接收器管理 //@Accept json //@Produce json //@Param account body v1.EditReceiverReq true "Add account" //@Router /receiver/edit [post] func EditReceiver(r *ghttp.Request) { var req *v1.EditReceiverReq appG := app.Ghttp{C: r} err := appG.C.Parse(&req) if err != nil { err = errors.Wrap(err, "GetNeckRingListing-error") log.Error(err) appG.Response(app.StatusInternalServerError, app.ERROR, err) return } exist, err := srv.CheckReceiverCode(req.Id, req.Code) if err != nil { err = errors.Wrap(err, "EditReceiver-error") log.Error(err) appG.Response(app.StatusInternalServerError, app.ERROR, err) return } if exist { appG.Response(app.StatusInternalServerError, app.ERROR, "IMEI卡号已经存在!!!") return } err = srv.EditReceiver(model.Receiver{ Id: req.Id, IMEI: req.Code, Pastureid: req.Pastureid, BarId: req.BarId, SimID: req.SimId, Status: req.Status, //Location: req.Location, UpdateDate: time.Now(), }) if err != nil { return } //err = srv.EditSimReceiverid(model.Sim{ // Id: req.SimId, // ReceiverId: req.Id, //}) //if err != nil { // return //} appG.Response(app.StatusOK, app.SUCCESS, true) } // GetReceiverPull // @Summary 接口器下拉 // @Description add by json account // @Tags 公共接口 // @Accept json // @Produce json // @Success 200 {object} v1.GetReceiverRes // @Router /receiver/pull [get] func GetReceiverPull(r *ghttp.Request) { appG := app.Ghttp{C: r} receiverList, err := srv.GetReceiverPull() if err != nil { appG.Response(app.StatusInternalServerError, app.ERROR, err) return } resp := v1.GetReceiverPullRes{} dataList := make([]*v1.ReceiverPull, 0) for _, item := range receiverList { var arg v1.ReceiverPull arg.Id = item.Id arg.Code = item.IMEI dataList = append(dataList, &arg) } resp.List = dataList appG.Response(app.StatusOK, app.SUCCESS, resp) } // GetReceiverInfo // @Summary 接收器位置信息 // @Description add by json account // @Tags 接收器管理 // @Accept json // @Produce json // @Param barid path int true "string 栏舍" // @Success 200 {object} v1.GetReceiverRes // @Router /receiver/coordinates [get] func GetReceiverInfo(r *ghttp.Request) { appG := app.Ghttp{C: r} barid := r.Get("barid").String() receiverList, err := srv.GetReceiverByPHYHOUSEUUID(barid) if err != nil { appG.Response(app.StatusInternalServerError, app.ERROR, err) return } resp := v1.GetReceiverInfoRes{} dataList := make([]*v1.CoordinatesInfo, 0) for _, item := range receiverList { var arg v1.CoordinatesInfo arg.ID = item.Id arg.Pastureid = item.Pastureid arg.BarId = item.BarId arg.Status = item.Status coordinatesmap := make(map[string]string) style := new(v1.Style) if item.Coordinates != "" { err := json.Unmarshal([]byte(item.Coordinates), &coordinatesmap) if err == nil { if _, ok := coordinatesmap["left"]; ok { if coordinatesmap["left"] != "" { style.Left = coordinatesmap["left"] } } if _, ok := coordinatesmap["top"]; ok { if coordinatesmap["top"] != "" { style.Top = coordinatesmap["top"] } } } } arg.Style = style dataList = append(dataList, &arg) } resp.List = dataList appG.Response(app.StatusOK, app.SUCCESS, resp) } //EditReceiverCoordinates //@Summary 编辑接收器坐标 //@Description add by json account //@Tags 接收器管理 //@Accept json //@Produce json //@Param account body v1.EditReceiverCoordinatesReq true "Add account" //@Router /receiver/coordinates/edit [post] func EditReceiverCoordinates(r *ghttp.Request) { //var req *v1.EditReceiverCoordinatesReq reqList := make([]*v1.CoordinatesInfo, 0) appG := app.Ghttp{C: r} err := appG.C.Parse(&reqList) if err != nil { err = errors.Wrap(err, "EditReceiverCoordinates-error") log.Error(err) appG.Response(app.StatusInternalServerError, app.ERROR, err) return } dataList := make([]*model.Receiver, 0) for _, item := range reqList { var arg model.Receiver arg.Id = item.ID arg.Status = item.Status if item.Left == "" || item.Top == "" { arg.Coordinates = "" } else { coordinatesMap := make(map[string]string) coordinatesMap["left"] = item.Left coordinatesMap["top"] = item.Top coordinatesByte, _ := json.Marshal(coordinatesMap) arg.Coordinates = string(coordinatesByte) } dataList = append(dataList, &arg) } err = srv.UpdateReceiverById(dataList) if err != nil { err = errors.Wrap(err, "EditReceiverCoordinates-error") log.Error(err) appG.Response(app.StatusInternalServerError, app.ERROR, err) return } appG.Response(app.StatusOK, app.SUCCESS, true) }