| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 | 
							- 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"`
 
- }
 
 
  |