pusher.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "tmr-watch/http/handle/restful"
  11. "tmr-watch/pkg/app"
  12. "tmr-watch/pkg/e"
  13. "github.com/Anderson-Lu/gofasion/gofasion"
  14. "github.com/gin-gonic/gin"
  15. )
  16. // 三个接口,车 顺序 时间 描述 牛舍
  17. // 执行接口 会 哪些牛舍 具体那个牛舍列出来
  18. // 第三个 gps
  19. type Pusher struct {
  20. Id int `xorm:"id" json:"id"`
  21. EqCode string `xorm:"eqcode" json:"eqCode"`
  22. Sort int64 `xorm:"sort" json:"sort"`
  23. Date string `xorm:"date" json:"date"`
  24. Remark string `xorm:"remark" json:"remark"`
  25. BarId string `xorm:"barId" json:"barId"`
  26. BarName string `xorm:"barName" json:"barName"`
  27. BarList []*PusherBar `json:"barList"`
  28. }
  29. // Id int64 `xorm:"id" json:"id"`
  30. // TmrId int64 `xorm:"tmrId" json:"tmrId"`
  31. // PastureId int64 `xorm:"pastureId" json:"pastureId"`
  32. // Date string `xorm:"date" json:"date"`
  33. // Remark string `xorm:"remark" json:"remark"`
  34. // Enable int64 `xorm:"enable" json:"enable"`
  35. // BarId string `xorm:"barId" json:"barId"`
  36. // BName string `xorm:"bname" json:"bname"`
  37. // TName string `xorm:"tname" json:"tname"`
  38. type PusherBar struct {
  39. BarId int64 `json:"barId"`
  40. BarName string `json:"barName"`
  41. }
  42. func GetPusherList(c *gin.Context) {
  43. appG := app.Gin{C: c}
  44. // dataByte, _ := ioutil.ReadAll(c.Request.Body)
  45. // fsion := gofasion.NewFasion(string(dataByte))
  46. // date := fsion.Get("date").ValueDefaultStr(time.Now().Format("2006-01-02"))
  47. // eqcode := fsion.Get("eqCode").ValueDefaultStr("")
  48. date := c.Query("date")
  49. eqCode := c.Query("eqCode")
  50. if date == "" {
  51. date = time.Now().Format("2006-01-02")
  52. } else {
  53. _, err := time.Parse("2006-01-02", date)
  54. if err != nil {
  55. appG.Response(http.StatusInternalServerError, e.ERROR, fmt.Sprintf("%s 日期解析错误!", date))
  56. return
  57. }
  58. }
  59. tx := restful.Engine.NewSession()
  60. defer tx.Close()
  61. tmrEquipmentList := make([]*Pusher, 0)
  62. err := tx.SQL(` select te.*,t.eqcode from tmr_equipment te join tmr t on t.id = te.tmrId where (t.eqcode = ? or ? = '' )
  63. and te.id not in(select id from tmr_equipment_date where plandate = ? ) and te.enable = 1 order by te.date`, eqCode, eqCode, date).Find(&tmrEquipmentList)
  64. if err != nil {
  65. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  66. return
  67. }
  68. for i, tmr := range tmrEquipmentList {
  69. tmr.Sort = int64(i + 1)
  70. barList := make([]*Bar, 0)
  71. err := tx.Table("bar").In("id", strings.Split(tmr.BarId, ",")).Find(&barList)
  72. if err != nil {
  73. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  74. return
  75. }
  76. tmr.Date = fmt.Sprintf("%s %s", date, tmr.Date)
  77. var barNameList []string
  78. for _, bar := range barList {
  79. barNameList = append(barNameList, bar.BName)
  80. }
  81. tmr.BarName = strings.Join(barNameList, ",")
  82. }
  83. appG.Response(http.StatusOK, e.SUCCESS, tmrEquipmentList)
  84. }
  85. type PusherGPS struct {
  86. Id int `json:"id"`
  87. EndDate string `json:"endDate"`
  88. StartDate string `json:"startDate"`
  89. GPS []*GPS `json:"GPS"`
  90. }
  91. type GPS struct {
  92. A string `json:"A"`
  93. N string `json:"N"`
  94. }
  95. func EditPusherGPS(c *gin.Context) {
  96. appG := app.Gin{C: c}
  97. pusherGPS := new(PusherGPS)
  98. if err := c.ShouldBind(&pusherGPS); err != nil {
  99. fmt.Println(err)
  100. appG.Response(http.StatusInternalServerError, e.ERROR, false)
  101. return
  102. }
  103. tx := restful.Engine.NewSession()
  104. defer tx.Close()
  105. gpsByte, _ := json.Marshal(pusherGPS.GPS)
  106. _, err := tx.Exec(` INSERT INTO tmr_equipment_date (pid,tmrId,pastureId,date,remark,enable,barId,plandate,startdate,enddate)
  107. select id,tmrId,pastureId,date,remark,enable,barId,now(),?,? from tmr_equipment where id = ? ON DUPLICATE KEY UPDATE startdate = ? ,enddate = ? `,
  108. pusherGPS.StartDate, pusherGPS.EndDate, pusherGPS.Id, pusherGPS.StartDate, pusherGPS.EndDate)
  109. if err != nil {
  110. fmt.Println(err)
  111. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  112. return
  113. }
  114. data := new(TMREquipmentDate)
  115. _, err = tx.SQL(` select id,pid,plandate from tmr_equipment_date where pid = ? and plandate = ? `, pusherGPS.Id, time.Now().Format("2006-01-02")).Get(data)
  116. if err != nil {
  117. fmt.Println(err)
  118. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  119. return
  120. }
  121. _, err = tx.Exec(` INSERT INTO tmr_equipment_muster (pid,gptjson)
  122. values(?,?) `,
  123. data.Id, string(gpsByte))
  124. if err != nil {
  125. fmt.Println(err)
  126. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  127. return
  128. }
  129. appG.Response(http.StatusOK, e.SUCCESS, true)
  130. }
  131. type TMREquipment struct {
  132. Id string `xorm:"id" json:"id"`
  133. TmrId string `xorm:"tmrId" json:"tmrId"`
  134. PastureId string `xorm:"pastureId" json:"pastureId"`
  135. Date string `xorm:"date" json:"date"`
  136. Remark string `xorm:"remark" json:"remark"`
  137. Enable int64 `xorm:"enable" json:"enable"`
  138. BarId string `xorm:"barId" json:"barId"`
  139. BName string `xorm:"bname" json:"bname"`
  140. TName string `xorm:"tname" json:"tname"`
  141. }
  142. type TMREquipmentDate struct {
  143. Id string `xorm:"id" json:"id"`
  144. Pid string `xorm:"pid" json:"pid"`
  145. Date string `xorm:"plandate" json:"plandate"`
  146. }
  147. func AddTmrEquipment(c *gin.Context) {
  148. appG := app.Gin{C: c}
  149. req := new(TMREquipment)
  150. if err := c.ShouldBind(&req); err != nil {
  151. appG.Response(http.StatusInternalServerError, e.ERROR, false)
  152. return
  153. }
  154. tx := restful.Engine.NewSession()
  155. defer tx.Close()
  156. if req.Id == "" {
  157. _, err := tx.SQL(` INSERT INTO tmr_equipment (tmrId, pastureId, date, remark, enable, barId) VALUES ( ?, ?, ?, ?, ?, ?) `,
  158. req.TmrId, req.PastureId, req.Date, req.Remark, req.Enable, req.BarId).Execute()
  159. if err != nil {
  160. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  161. return
  162. }
  163. } else {
  164. _, err := tx.SQL(`UPDATE tmr_equipment SET tmrId = ? , pastureId = ? , date = ? , remark = ? , enable = ? , barId = ? WHERE id = ? `,
  165. req.TmrId, req.PastureId, req.Date, req.Remark, req.Enable, req.BarId, req.Id).Execute()
  166. if err != nil {
  167. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  168. return
  169. }
  170. }
  171. appG.Response(http.StatusOK, e.SUCCESS, true)
  172. }
  173. type GetTmrEquipmentResp struct {
  174. TmrEquipmentList []*TMREquipment `json:"data"`
  175. Count int64 `json:"count"`
  176. }
  177. func GetTmrEquipment(c *gin.Context) {
  178. appG := app.Gin{C: c}
  179. pastureId := c.Query("pastureId")
  180. offsetStr := c.Query("offset")
  181. pageStr := c.Query("page")
  182. offset, _ := strconv.Atoi(offsetStr)
  183. page, _ := strconv.Atoi(pageStr)
  184. tx := restful.Engine.NewSession()
  185. defer tx.Close()
  186. tmrEquipmentList := make([]*TMREquipment, 0)
  187. err := tx.SQL(` select te.*,t.tname from tmr_equipment te join tmr t on t.id = te.tmrId where te.pastureId = ? order by te.date limit ?,? `, pastureId, offset-1, page).Find(&tmrEquipmentList)
  188. if err != nil {
  189. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  190. return
  191. }
  192. for _, tmr := range tmrEquipmentList {
  193. barList := make([]*Bar, 0)
  194. err := tx.Table("bar").In("id", strings.Split(tmr.BarId, ",")).Find(&barList)
  195. if err != nil {
  196. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  197. return
  198. }
  199. var barNameList []string
  200. for _, bar := range barList {
  201. barNameList = append(barNameList, bar.BName)
  202. }
  203. tmr.BName = strings.Join(barNameList, ",")
  204. }
  205. resp := new(GetTmrEquipmentResp)
  206. count, err := tx.SQL(`select count(1) from tmr_equipment te join tmr t on t.id = te.tmrId where te.pastureId = ?`, pastureId).Count()
  207. if err != nil {
  208. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  209. return
  210. }
  211. resp.Count = count
  212. resp.TmrEquipmentList = tmrEquipmentList
  213. appG.Response(http.StatusOK, e.SUCCESS, resp)
  214. }
  215. func DelTmrEquipment(c *gin.Context) {
  216. appG := app.Gin{C: c}
  217. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  218. fsion := gofasion.NewFasion(string(dataByte))
  219. id := fsion.Get("id").ValueStr()
  220. tx := restful.Engine.NewSession()
  221. defer tx.Close()
  222. _, err := tx.SQL(`delete from tmr_equipment WHERE id = ? `, id).Execute()
  223. if err != nil {
  224. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  225. return
  226. }
  227. appG.Response(http.StatusOK, e.SUCCESS, true)
  228. }
  229. type Bar struct {
  230. Id int64 `xorm:"id" json:"id"`
  231. BName string `xorm:"bname" json:"bname"`
  232. }