receiver.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package controller
  2. import (
  3. v1 "demo/api/v1"
  4. "demo/internal/app"
  5. "demo/internal/model"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/gogf/gf/v2/net/ghttp"
  9. "github.com/pkg/errors"
  10. "github.com/siddontang/go/log"
  11. "time"
  12. )
  13. // GetReceiver
  14. // @Summary 接收器管理查看
  15. // @Description add by json account
  16. // @Tags 接收器管理
  17. // @Accept json
  18. // @Produce json
  19. // @Param index path int true "int "
  20. // @Param pagesize path int true "int "
  21. // @Param code path string true "string 接收器编码"
  22. // @Param status path int true "string 状态"
  23. // @Success 200 {object} v1.GetReceiverRes
  24. // @Router /receiver/list [get]
  25. func GetReceiver(r *ghttp.Request) {
  26. var req *v1.GetReceiverReq
  27. appG := app.Ghttp{C: r}
  28. err := appG.C.Parse(&req)
  29. if err != nil {
  30. err = errors.Wrap(err, "GetNeckRingListing-error")
  31. log.Error(err)
  32. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  33. return
  34. }
  35. receiverList, count, err := srv.GetReceiverList(req.Index, req.PageSize, req.Code, req.Status)
  36. if err != nil {
  37. return
  38. }
  39. resp := v1.GetReceiverRes{}
  40. dataList := make([]*v1.Receiver, 0)
  41. for _, item := range receiverList {
  42. var data v1.Receiver
  43. data.ReceiverId = item.Receiver.Id
  44. data.Code = item.IMEI
  45. data.Pastureid = item.Receiver.Pastureid
  46. data.BarId = item.Receiver.BarId
  47. data.BarName = item.BarName
  48. data.SimId = item.Receiver.Id
  49. data.Status = item.Receiver.Status
  50. data.Location = item.BarName
  51. if !item.UpdateDate.IsZero() {
  52. data.UpdateDate = item.Receiver.UpdateDate.Format("2006-01-02 15:04:05")
  53. }
  54. data.Card = item.Sim.Card
  55. data.PastureName = item.PastureName
  56. data.PastureCode = item.PastureCode
  57. data.Id = item.Receiver.Id
  58. if item.Image != "" {
  59. data.Image = fmt.Sprintf("image/%s", item.Image)
  60. }
  61. style := new(v1.Style)
  62. coordinatesmap := make(map[string]string)
  63. if item.Coordinates != "" {
  64. err := json.Unmarshal([]byte(item.Coordinates), &coordinatesmap)
  65. if err == nil {
  66. if _, ok := coordinatesmap["left"]; ok {
  67. if coordinatesmap["left"] != "" {
  68. style.Left = coordinatesmap["left"]
  69. }
  70. }
  71. if _, ok := coordinatesmap["top"]; ok {
  72. if coordinatesmap["top"] != "" {
  73. style.Top = coordinatesmap["top"]
  74. }
  75. }
  76. }
  77. }
  78. data.Style = style
  79. dataList = append(dataList, &data)
  80. }
  81. resp.List = dataList
  82. resp.Total = count
  83. appG.Response(app.StatusOK, app.SUCCESS, resp)
  84. }
  85. //AddReceiver
  86. //@Summary 添加接收器
  87. //@Description add by json account
  88. //@Tags 接收器管理
  89. //@Accept json
  90. //@Produce json
  91. //@Param account body v1.AddReceiverReq true "Add account"
  92. //@Router /receiver/add [post]
  93. func AddReceiver(r *ghttp.Request) {
  94. var req *v1.AddReceiverReq
  95. appG := app.Ghttp{C: r}
  96. err := appG.C.Parse(&req)
  97. if err != nil {
  98. err = errors.Wrap(err, "AddReceiver-error")
  99. log.Error(err)
  100. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  101. return
  102. }
  103. exist, err := srv.CheckReceiverCode(0, req.Code)
  104. if err != nil {
  105. err = errors.Wrap(err, "AddReceiver-error")
  106. log.Error(err)
  107. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  108. return
  109. }
  110. if exist {
  111. appG.Response(app.StatusInternalServerError, app.ERROR, "IMEI卡号已经存在!!!")
  112. return
  113. }
  114. err = srv.AddReceiver(model.Receiver{
  115. IMEI: req.Code,
  116. Pastureid: req.Pastureid,
  117. BarId: req.BarId,
  118. //BarName: req.BarName,
  119. SimID: req.SimId,
  120. Status: req.Status,
  121. //Location: req.Location,
  122. UpdateDate: time.Now(),
  123. })
  124. if err != nil {
  125. err = errors.Wrap(err, "AddReceiver-error")
  126. log.Error(err)
  127. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  128. return
  129. }
  130. appG.Response(app.StatusOK, app.SUCCESS, true)
  131. }
  132. //EditReceiver
  133. //@Summary 编辑接收器
  134. //@Description add by json account
  135. //@Tags 接收器管理
  136. //@Accept json
  137. //@Produce json
  138. //@Param account body v1.EditReceiverReq true "Add account"
  139. //@Router /receiver/edit [post]
  140. func EditReceiver(r *ghttp.Request) {
  141. var req *v1.EditReceiverReq
  142. appG := app.Ghttp{C: r}
  143. err := appG.C.Parse(&req)
  144. if err != nil {
  145. err = errors.Wrap(err, "GetNeckRingListing-error")
  146. log.Error(err)
  147. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  148. return
  149. }
  150. exist, err := srv.CheckReceiverCode(req.Id, req.Code)
  151. if err != nil {
  152. err = errors.Wrap(err, "EditReceiver-error")
  153. log.Error(err)
  154. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  155. return
  156. }
  157. if exist {
  158. appG.Response(app.StatusInternalServerError, app.ERROR, "IMEI卡号已经存在!!!")
  159. return
  160. }
  161. err = srv.EditReceiver(model.Receiver{
  162. Id: req.Id,
  163. IMEI: req.Code,
  164. Pastureid: req.Pastureid,
  165. BarId: req.BarId,
  166. SimID: req.SimId,
  167. Status: req.Status,
  168. //Location: req.Location,
  169. UpdateDate: time.Now(),
  170. })
  171. if err != nil {
  172. return
  173. }
  174. //err = srv.EditSimReceiverid(model.Sim{
  175. // Id: req.SimId,
  176. // ReceiverId: req.Id,
  177. //})
  178. //if err != nil {
  179. // return
  180. //}
  181. appG.Response(app.StatusOK, app.SUCCESS, true)
  182. }
  183. // GetReceiverPull
  184. // @Summary 接口器下拉
  185. // @Description add by json account
  186. // @Tags 公共接口
  187. // @Accept json
  188. // @Produce json
  189. // @Success 200 {object} v1.GetReceiverRes
  190. // @Router /receiver/pull [get]
  191. func GetReceiverPull(r *ghttp.Request) {
  192. appG := app.Ghttp{C: r}
  193. receiverList, err := srv.GetReceiverPull()
  194. if err != nil {
  195. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  196. return
  197. }
  198. resp := v1.GetReceiverPullRes{}
  199. dataList := make([]*v1.ReceiverPull, 0)
  200. for _, item := range receiverList {
  201. var arg v1.ReceiverPull
  202. arg.Id = item.Id
  203. arg.Code = item.IMEI
  204. dataList = append(dataList, &arg)
  205. }
  206. resp.List = dataList
  207. appG.Response(app.StatusOK, app.SUCCESS, resp)
  208. }
  209. // GetReceiverInfo
  210. // @Summary 接收器位置信息
  211. // @Description add by json account
  212. // @Tags 接收器管理
  213. // @Accept json
  214. // @Produce json
  215. // @Param barid path int true "string 栏舍"
  216. // @Success 200 {object} v1.GetReceiverRes
  217. // @Router /receiver/coordinates [get]
  218. func GetReceiverInfo(r *ghttp.Request) {
  219. appG := app.Ghttp{C: r}
  220. barid := r.Get("barid").String()
  221. receiverList, err := srv.GetReceiverByPHYHOUSEUUID(barid)
  222. if err != nil {
  223. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  224. return
  225. }
  226. resp := v1.GetReceiverInfoRes{}
  227. dataList := make([]*v1.CoordinatesInfo, 0)
  228. for _, item := range receiverList {
  229. var arg v1.CoordinatesInfo
  230. arg.ID = item.Id
  231. arg.Pastureid = item.Pastureid
  232. arg.BarId = item.BarId
  233. arg.Status = item.Status
  234. coordinatesmap := make(map[string]string)
  235. style := new(v1.Style)
  236. if item.Coordinates != "" {
  237. err := json.Unmarshal([]byte(item.Coordinates), &coordinatesmap)
  238. if err == nil {
  239. if _, ok := coordinatesmap["left"]; ok {
  240. if coordinatesmap["left"] != "" {
  241. style.Left = coordinatesmap["left"]
  242. }
  243. }
  244. if _, ok := coordinatesmap["top"]; ok {
  245. if coordinatesmap["top"] != "" {
  246. style.Top = coordinatesmap["top"]
  247. }
  248. }
  249. }
  250. }
  251. arg.Style = style
  252. dataList = append(dataList, &arg)
  253. }
  254. resp.List = dataList
  255. appG.Response(app.StatusOK, app.SUCCESS, resp)
  256. }
  257. //EditReceiverCoordinates
  258. //@Summary 编辑接收器坐标
  259. //@Description add by json account
  260. //@Tags 接收器管理
  261. //@Accept json
  262. //@Produce json
  263. //@Param account body v1.EditReceiverCoordinatesReq true "Add account"
  264. //@Router /receiver/coordinates/edit [post]
  265. func EditReceiverCoordinates(r *ghttp.Request) {
  266. //var req *v1.EditReceiverCoordinatesReq
  267. reqList := make([]*v1.CoordinatesInfo, 0)
  268. appG := app.Ghttp{C: r}
  269. err := appG.C.Parse(&reqList)
  270. if err != nil {
  271. err = errors.Wrap(err, "EditReceiverCoordinates-error")
  272. log.Error(err)
  273. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  274. return
  275. }
  276. dataList := make([]*model.Receiver, 0)
  277. for _, item := range reqList {
  278. var arg model.Receiver
  279. arg.Id = item.ID
  280. arg.Status = item.Status
  281. if item.Left == "" || item.Top == "" {
  282. arg.Coordinates = ""
  283. } else {
  284. coordinatesMap := make(map[string]string)
  285. coordinatesMap["left"] = item.Left
  286. coordinatesMap["top"] = item.Top
  287. coordinatesByte, _ := json.Marshal(coordinatesMap)
  288. arg.Coordinates = string(coordinatesByte)
  289. }
  290. dataList = append(dataList, &arg)
  291. }
  292. err = srv.UpdateReceiverById(dataList)
  293. if err != nil {
  294. err = errors.Wrap(err, "EditReceiverCoordinates-error")
  295. log.Error(err)
  296. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  297. return
  298. }
  299. appG.Response(app.StatusOK, app.SUCCESS, true)
  300. }