package api import ( "encoding/json" "fmt" "io/ioutil" "net/http" "strconv" "strings" "time" "tmr-watch/http/handle/restful" "tmr-watch/pkg/app" "tmr-watch/pkg/e" "github.com/Anderson-Lu/gofasion/gofasion" "github.com/gin-gonic/gin" ) // 三个接口,车 顺序 时间 描述 牛舍 // 执行接口 会 哪些牛舍 具体那个牛舍列出来 // 第三个 gps type Pusher struct { Id int `xorm:"id" json:"id"` EqCode string `xorm:"eqcode" json:"eqCode"` Sort int64 `xorm:"sort" json:"sort"` Date string `xorm:"date" json:"date"` Remark string `xorm:"remark" json:"remark"` BarId string `xorm:"barId" json:"barId"` BarName string `xorm:"barName" json:"barName"` BarList []*PusherBar `json:"barList"` } // Id int64 `xorm:"id" json:"id"` // TmrId int64 `xorm:"tmrId" json:"tmrId"` // PastureId int64 `xorm:"pastureId" json:"pastureId"` // Date string `xorm:"date" json:"date"` // Remark string `xorm:"remark" json:"remark"` // Enable int64 `xorm:"enable" json:"enable"` // BarId string `xorm:"barId" json:"barId"` // BName string `xorm:"bname" json:"bname"` // TName string `xorm:"tname" json:"tname"` type PusherBar struct { BarId int64 `json:"barId"` BarName string `json:"barName"` } func GetPusherList(c *gin.Context) { appG := app.Gin{C: c} // dataByte, _ := ioutil.ReadAll(c.Request.Body) // fsion := gofasion.NewFasion(string(dataByte)) // date := fsion.Get("date").ValueDefaultStr(time.Now().Format("2006-01-02")) // eqcode := fsion.Get("eqCode").ValueDefaultStr("") date := c.Query("date") eqCode := c.Query("eqCode") if date == "" { date = time.Now().Format("2006-01-02") } else { _, err := time.Parse("2006-01-02", date) if err != nil { appG.Response(http.StatusInternalServerError, e.ERROR, fmt.Sprintf("%s 日期解析错误!", date)) return } } tx := restful.Engine.NewSession() defer tx.Close() tmrEquipmentList := make([]*Pusher, 0) err := tx.SQL(` select te.*,t.eqcode from tmr_equipment te join tmr t on t.id = te.tmrId where (t.eqcode = ? or ? = '' ) 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) if err != nil { appG.Response(http.StatusInternalServerError, e.ERROR, err) return } for i, tmr := range tmrEquipmentList { tmr.Sort = int64(i + 1) barList := make([]*Bar, 0) err := tx.Table("bar").In("id", strings.Split(tmr.BarId, ",")).Find(&barList) if err != nil { appG.Response(http.StatusInternalServerError, e.ERROR, err) return } tmr.Date = fmt.Sprintf("%s %s", date, tmr.Date) var barNameList []string for _, bar := range barList { barNameList = append(barNameList, bar.BName) } tmr.BarName = strings.Join(barNameList, ",") } appG.Response(http.StatusOK, e.SUCCESS, tmrEquipmentList) } type PusherGPS struct { Id int `json:"id"` EndDate string `json:"endDate"` StartDate string `json:"startDate"` GPS []*GPS `json:"GPS"` } type GPS struct { A string `json:"A"` N string `json:"N"` } func EditPusherGPS(c *gin.Context) { appG := app.Gin{C: c} pusherGPS := new(PusherGPS) if err := c.ShouldBind(&pusherGPS); err != nil { fmt.Println(err) appG.Response(http.StatusInternalServerError, e.ERROR, false) return } tx := restful.Engine.NewSession() defer tx.Close() gpsByte, _ := json.Marshal(pusherGPS.GPS) _, err := tx.Exec(` INSERT INTO tmr_equipment_date (pid,tmrId,pastureId,date,remark,enable,barId,plandate,startdate,enddate) select id,tmrId,pastureId,date,remark,enable,barId,now(),?,? from tmr_equipment where id = ? ON DUPLICATE KEY UPDATE startdate = ? ,enddate = ? `, pusherGPS.StartDate, pusherGPS.EndDate, pusherGPS.Id, pusherGPS.StartDate, pusherGPS.EndDate) if err != nil { fmt.Println(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } data := new(TMREquipmentDate) _, 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) if err != nil { fmt.Println(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } _, err = tx.Exec(` INSERT INTO tmr_equipment_muster (pid,gptjson) values(?,?) `, data.Id, string(gpsByte)) if err != nil { fmt.Println(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } appG.Response(http.StatusOK, e.SUCCESS, true) } type TMREquipment struct { Id string `xorm:"id" json:"id"` TmrId string `xorm:"tmrId" json:"tmrId"` PastureId string `xorm:"pastureId" json:"pastureId"` Date string `xorm:"date" json:"date"` Remark string `xorm:"remark" json:"remark"` Enable int64 `xorm:"enable" json:"enable"` BarId string `xorm:"barId" json:"barId"` BName string `xorm:"bname" json:"bname"` TName string `xorm:"tname" json:"tname"` } type TMREquipmentDate struct { Id string `xorm:"id" json:"id"` Pid string `xorm:"pid" json:"pid"` Date string `xorm:"plandate" json:"plandate"` } func AddTmrEquipment(c *gin.Context) { appG := app.Gin{C: c} req := new(TMREquipment) if err := c.ShouldBind(&req); err != nil { appG.Response(http.StatusInternalServerError, e.ERROR, false) return } tx := restful.Engine.NewSession() defer tx.Close() if req.Id == "" { _, err := tx.SQL(` INSERT INTO tmr_equipment (tmrId, pastureId, date, remark, enable, barId) VALUES ( ?, ?, ?, ?, ?, ?) `, req.TmrId, req.PastureId, req.Date, req.Remark, req.Enable, req.BarId).Execute() if err != nil { appG.Response(http.StatusInternalServerError, e.ERROR, err) return } } else { _, err := tx.SQL(`UPDATE tmr_equipment SET tmrId = ? , pastureId = ? , date = ? , remark = ? , enable = ? , barId = ? WHERE id = ? `, req.TmrId, req.PastureId, req.Date, req.Remark, req.Enable, req.BarId, req.Id).Execute() if err != nil { appG.Response(http.StatusInternalServerError, e.ERROR, err) return } } appG.Response(http.StatusOK, e.SUCCESS, true) } type GetTmrEquipmentResp struct { TmrEquipmentList []*TMREquipment `json:"data"` Count int64 `json:"count"` } func GetTmrEquipment(c *gin.Context) { appG := app.Gin{C: c} pastureId := c.Query("pastureId") offsetStr := c.Query("offset") pageStr := c.Query("page") offset, _ := strconv.Atoi(offsetStr) page, _ := strconv.Atoi(pageStr) tx := restful.Engine.NewSession() defer tx.Close() tmrEquipmentList := make([]*TMREquipment, 0) 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) if err != nil { appG.Response(http.StatusInternalServerError, e.ERROR, err) return } for _, tmr := range tmrEquipmentList { barList := make([]*Bar, 0) err := tx.Table("bar").In("id", strings.Split(tmr.BarId, ",")).Find(&barList) if err != nil { appG.Response(http.StatusInternalServerError, e.ERROR, err) return } var barNameList []string for _, bar := range barList { barNameList = append(barNameList, bar.BName) } tmr.BName = strings.Join(barNameList, ",") } resp := new(GetTmrEquipmentResp) count, err := tx.SQL(`select count(1) from tmr_equipment te join tmr t on t.id = te.tmrId where te.pastureId = ?`, pastureId).Count() if err != nil { appG.Response(http.StatusInternalServerError, e.ERROR, err) return } resp.Count = count resp.TmrEquipmentList = tmrEquipmentList appG.Response(http.StatusOK, e.SUCCESS, resp) } func DelTmrEquipment(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) id := fsion.Get("id").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() _, err := tx.SQL(`delete from tmr_equipment WHERE id = ? `, id).Execute() if err != nil { appG.Response(http.StatusInternalServerError, e.ERROR, err) return } appG.Response(http.StatusOK, e.SUCCESS, true) } type Bar struct { Id int64 `xorm:"id" json:"id"` BName string `xorm:"bname" json:"bname"` }