package api import ( "encoding/json" "io/ioutil" "net/http" "strconv" "tmr-watch/http/handle/restful" "tmr-watch/pkg/app" "tmr-watch/pkg/e" "github.com/Anderson-Lu/gofasion/gofasion" "github.com/astaxie/beego/logs" "github.com/gin-gonic/gin" ) type downloadedplan struct { Id int64 `json:"id"` Mydate string `json:"mydate"` Projname string `json:"projname"` Datacaptureno string `json:"datacaptureno"` Lweight string `json:"lweight"` Havebutton string `json:"havebutton"` Feedpname string `json:"feedpname"` Iscompleted string `json:"iscompleted"` Tmrtname string `json:"tmrtname"` Remark string `json:"remark"` Lpplantype string `json:"lpplantype"` Sort string `json:"sort"` Shifts string `json:"shifts"` } type ReqGetDownloadedplanMaterial struct { Pastureid int64 `json:"pastureid"` Datacaptureno int64 `json:"datacaptureno"` Date string `json:"date"` } func GetDownloadedplanMaterial1(c *gin.Context) { // var reqInfo ReqGetDownloadedplanMaterial // err := c.Bind(&reqInfo) appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) date := fsion.Get("date").ValueStr() pastureid := fsion.Get("pastureid").ValueStr() datacaptureno := fsion.Get("datacaptureno").ValueStr() id := fsion.Get("id").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() tclassid := "" tmrList, err := tx.SQL(` select tclassid from tmr where datacaptureno = ? `, datacaptureno).QueryString() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } for _, tmr := range tmrList { tclassid = tmr["tclassid"] } downloadedplanInfo := new(downloadedplan) err = tx.SQL(` select d.id,date_format(d.mydate ,'%Y-%m-%d') as mydate ,d.projname,d.datacaptureno,d.lweight,d.havebutton,d.feedpname,d.iscompleted,d.remark,d.tmrtname from downloadedplan d where d.mydate = ? and d.pastureid = ? and (d.datacaptureno is null or d.datacaptureno = ?) and d.iscompleted = 0 and ( (? IN(1,2) AND d.lpplanType IN (0,1)) OR (? IN (1,3) AND d.lpplanType IN (0,2))) and if(d.lpplantype = 2,if((select count(1) from downloadplandtl1 d1 where d1.pid = (select id from downloadedplan dd where dd.mydate = d.mydate and dd.pastureId = d.pastureid and dd.lpplanType IN (0,1) and dd.pid = d.pid and dd.times = d.times ) and d1.type = 0 and d1.havebuttom = 0 ) >0 ,1,0 ),0) = 0 and (?= '' or d.id = ? ) order by d.sort `, date, pastureid, datacaptureno, tclassid, tclassid, id, id).GetFirst(downloadedplanInfo).Error if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } if downloadedplanInfo.Id == 0 { appG.Response(http.StatusOK, e.THROUGHOUt_THE_DAY_TO_END, nil) return } _, err = tx.SQL(` update downloadedplan set havebutton = 1 ,intime = now() where id = ? and havebutton != 1 `, downloadedplanInfo.Id).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } _, err = tx.SQL(` update tmr set projid = ? ,lastbuttontime =? , nextsort = ifnull((select sort from downloadplandtl1 where pid = ? and intime is null order by sort limit 0,1),0 ) where datacaptureno = ? and pastureid = ? `, downloadedplanInfo.Id, date, downloadedplanInfo.Id, datacaptureno, pastureid).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } appG.Response(http.StatusOK, e.SUCCESS, downloadedplanInfo) } func GetSubPlan(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) parammaps := gofasion.NewFasion(string(dataByte)) pid := parammaps.Get("pid").ValueStr() pastureid := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() downloadplandtl1List, err := tx.SQL(` select DATE_FORMAT(d1.begintime, '%Y-%m-%d %H:%i:%S') as begintime, DATE_FORMAT(d1.intime, '%Y-%m-%d %H:%i:%S') as intime ,f.id as feedid,d1.fname,d1.feedcode,f.autosecond, f.autozone,d1.lweight,d1.sort,d1.buttontype,d1.actualweightminus,f.confirmstart,f.jmp from downloadplandtl1 d1 left join feed f on f.feedcode = d1.feedcode and f.pastureid = d1.pastureid where d1.pid = ? and d1.pastureid = ? `, pid, pastureid).Query().List() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } downloadplandtl2List, err := tx.SQL(` select d2.sort,DATE_FORMAT(d2.begintime, '%Y-%m-%d %H:%i:%S') as begintime ,DATE_FORMAT(d2.intime, '%Y-%m-%d %H:%i:%S') as intime ,d2.id,d2.pid,d2.fbarid,d2.fname as fbarname,d2.lweight,d.lpplantype,d2.actualweightminus ,ifnull(b.autozone,0) as autozone,ifnull(b.autosecond,"") as autosecond,ifnull(b.isstart,1) as isstart ,ifnull(b.jmp,0) as jmp from downloadplandtl2 d2 join downloadedplan d on d.id = d2.pid left join bar b on d2.fbarid = b.id where d2.pid = ? and d2.pastureid = ? `, pid, pastureid).Query().List() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } d1List := make([]map[string]interface{}, 0) d2List := make([]map[string]interface{}, 0) for _, d2item := range downloadplandtl2List { data := make(map[string]interface{}, 0) data["fbarid"] = d2item["fbarid"] data["fbarname"] = d2item["fbarname"] data["lweight"] = d2item["lweight"] data["lpplantype"] = d2item["lpplantype"] data["id"] = d2item["id"] data["actualweightminus"] = d2item["actualweightminus"] data["intime"] = d2item["intime"] data["begintime"] = d2item["begintime"] data["sort"] = d2item["sort"] data["autozone"] = d2item["autozone"] data["autosecond"] = d2item["autosecond"] data["isstart"] = d2item["isstart"] data["jmp"] = d2item["jmp"] d2List = append(d2List, data) } for _, d1item := range downloadplandtl1List { d1 := make(map[string]interface{}, 0) // lweight, _ := strconv.ParseFloat(d1item["lweight"].(string), 64) d1["fname"] = d1item["fname"] d1["feedcode"] = d1item["feedcode"] d1["autosecond"] = d1item["autosecond"] d1["autozone"] = d1item["autozone"] d1["sort"] = d1item["sort"] d1["buttontype"] = d1item["buttontype"] d1["feedid"] = d1item["feedid"] d1["lweight"] = d1item["lweight"] d1["intime"] = d1item["intime"] d1["begintime"] = d1item["begintime"] d1["actualweightminus"] = d1item["actualweightminus"] d1["confirmstart"] = d1item["confirmstart"] d1["jmp"] = d1item["jmp"] d1List = append(d1List, d1) } resData := make(map[string]interface{}, 0) resData["mixing"] = d1List resData["spillage"] = d2List // data["plan"] = d1List // dataList = append(dataList, data) appG.Response(http.StatusOK, e.SUCCESS, resData) } func CompletePlan(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) parammaps := gofasion.NewFasion(string(dataByte)) pid := parammaps.Get("pid").ValueStr() feedid := parammaps.Get("feedid").ValueStr() lastactualweight := parammaps.Get("lastactualweight").ValueDefaultFloat64(0) actualweightminus := parammaps.Get("actualweightminus").ValueDefaultFloat64(0) actualweight := parammaps.Get("actualweight").ValueDefaultFloat64(0) datacaptureno := parammaps.Get("datacaptureno").ValueStr() begintime := parammaps.Get("begintime").ValueStr() intime := parammaps.Get("intime").ValueStr() sort := parammaps.Get("sort").ValueStr() buttontype := parammaps.Get("buttontype").ValueStr() lpplantype := parammaps.Get("lpplantype").ValueDefaultInt64(0) d2id := parammaps.Get("d2id").ValueStr() pastureid := parammaps.Get("pastureid").ValueStr() tx := restful.Engine.NewSession() defer tx.Close() _, err := tx.SQL(` update downloadedplan set tmrid = ( select id from tmr where datacaptureno = ? and pastureid = ?) , tmrtname = ( select eqcode from tmr where datacaptureno = ? and pastureid = ?) where id = ? and tmrid != ( select id from tmr where datacaptureno = ? and pastureid = ?)`, datacaptureno, pastureid, datacaptureno, pastureid, pid, datacaptureno, pastureid).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } if lpplantype == 0 { _, err := tx.SQL(` update downloadplandtl1 set begintime = ? , actualweightminus = ifnull(actualweightminus,0) +? , intime = ? ,actualweight = ? ,optdevice = ( select id from tmr where datacaptureno = ? and pastureid = ?),buttontype = ?, havebuttom = 1,lastactualweight = ? where pid = ? and sort = ? and feedcode = (select feedcode from feed where id = ? ) and (intime is null )`, begintime, actualweightminus, intime, actualweight, datacaptureno, pastureid, buttontype, lastactualweight, pid, sort, feedid).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } _, err = tx.SQL(` update downloadplandtl1_exec set begintime = ? ,actualweightminus = ifnull(actualweightminus,0)+ ? , intime = ? ,actualweight = ? ,optdevice = ( select id from tmr where datacaptureno = ? and pastureid = ?),buttontype = ?,havebuttom = 1,lastactualweight = ? where pid = ? and sort = ? and (intime is null ) `, begintime, actualweightminus, intime, actualweight, datacaptureno, pastureid, buttontype, lastactualweight, pid, sort).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } d2List, err := tx.SQL(` select * from downloadplandtl2 where pid = ? order by sort `, pid).Query().List() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } var sumlweight float64 for _, d2 := range d2List { lweight, _ := strconv.ParseFloat(d2["lweight"].(string), 64) sumlweight += lweight } for _, d2 := range d2List { if actualweightminus <= 0 { break } lweight, _ := strconv.ParseFloat(d2["lweight"].(string), 64) actualweight := lweight / sumlweight * actualweightminus last := lweight / sumlweight * lastactualweight _, err := tx.SQL(` update downloadplandtl2 set actualweightminus = ifnull(actualweightminus,0)+?, actualweight = ?, lastactualweight = ? , intime = ?,begintime = ? ,havebuttom = 1,buttontype = ? where id = ? and (intime is null ) `, actualweight, actualweight, last, intime, begintime, buttontype, d2["id"]).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } } execExist, err := tx.SQL(` select id from downloadplandtl2 where pid = ? and (intime is null ) `, pid).Exist() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } if !execExist { _, err := tx.SQL(` update downloadedplan set iscompleted = 1, completedtime = ? where id = ? `, intime, pid).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } } _, err = tx.SQL(` update tmr set projid = ? ,lastbuttontime =? , nextsort = (select sort from downloadplandtl1 where pid = ? and intime is null order by sort limit 0,1 ) where datacaptureno = ? and pastureid = ? `, pid, intime, pid, datacaptureno, pastureid).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } } else if lpplantype == 1 { _, err := tx.SQL(` update downloadplandtl2 set actualweightminus =ifnull(actualweightminus,0)+?, actualweight = ?, lastactualweight = ? , intime = ? ,begintime = ?,havebuttom = 1,buttontype = ? where id = ? and (intime is null ) `, actualweightminus, actualweight, lastactualweight, intime, begintime, buttontype, d2id).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } _, err = tx.SQL(` update downloadplandtl1_exec set actualweightminus =ifnull(actualweightminus,0)+?, actualweight = ?, lastactualweight = ? , intime = ?,begintime= ? ,havebuttom = 1,buttontype= ? where pid = ? and (intime is null ) `, actualweightminus, actualweight, lastactualweight, intime, begintime, buttontype, pid).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } _, err = tx.SQL(` update downloadplandtl1 set actualweightminus =ifnull(actualweightminus,0)+?, actualweight = ?, lastactualweight = ? , intime = ?,begintime=? ,havebuttom = 1,buttontype = ? where pid = ? and (intime is null ) `, actualweightminus, actualweight, lastactualweight, intime, begintime, buttontype, pid).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } exist, err := tx.SQL(` select id from downloadplandtl2 where pid = ? and intime is null `, pid).Exist() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } if !exist { _, err := tx.SQL(` update downloadedplan set iscompleted = 1, completedtime = ? where id = ? `, intime, pid).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } } _, err = tx.SQL(` update tmr set projid = ? ,lastbuttontime =? , nextsort = (select sort from downloadplandtl2 where pid = ? and intime is null order by sort limit 0,1 ) where datacaptureno = ? and pastureid = ? `, pid, intime, pid, datacaptureno, pastureid).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } } else if lpplantype == 2 { _, err := tx.SQL(` update downloadplandtl1 set begintime = ? , actualweightminus =ifnull(actualweightminus,0)+ ? , intime = ? ,actualweight = ? ,optdevice = ( select id from tmr where datacaptureno = ? and pastureid = ?),buttontype = ?,havebuttom = 1,lastactualweight = ?,begintime = ? where pid = ? and sort = ? and feedcode = (select feedcode from feed where id = ? ) and (intime is null )`, begintime, actualweightminus, intime, actualweight, datacaptureno, pastureid, buttontype, lastactualweight, begintime, pid, sort, feedid).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } _, err = tx.SQL(` update downloadplandtl1_exec set begintime = ? ,actualweightminus =ifnull(actualweightminus,0)+ ? , intime = ? ,actualweight = ? ,optdevice = ( select id from tmr where datacaptureno = ? and pastureid = ?),buttontype = ?,havebuttom = 1,lastactualweight = ?,begintime = ? where pid = ? and sort = ? and (intime is null ) `, begintime, actualweightminus, intime, actualweight, datacaptureno, pastureid, buttontype, lastactualweight, begintime, pid, sort).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } _, err = tx.SQL(` update tmr set projid = ? ,lastbuttontime =? , nextsort = (select sort from downloadplandtl1 where pid = ? and intime is null order by sort limit 0,1 ) where datacaptureno = ? and pastureid = ? `, pid, intime, pid, datacaptureno, pastureid).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } } else { _, err := tx.SQL(` update downloadplandtl2 set actualweightminus =ifnull(actualweightminus,0)+?, actualweight = ?, lastactualweight = ? , intime = ? ,havebuttom = 1,buttontype = ?,begintime = ? where id = ? and (intime is null ) `, actualweightminus, actualweight, lastactualweight, intime, buttontype, begintime, d2id).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } exist, err := tx.SQL(` select id from downloadplandtl2 where pid = ? and intime is null`, pid).Exist() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } if !exist { _, err := tx.SQL(` update downloadedplan set iscompleted = 1, completedtime = ? where id = ? `, intime, pid).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } } _, err = tx.SQL(` update tmr set projid = ? ,lastbuttontime =? , nextsort = (select sort from downloadplandtl2 where pid = ? and intime is null order by sort limit 0,1 ) where datacaptureno = ? and pastureid = ? `, pid, intime, pid, datacaptureno, pastureid).Execute() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } } appG.Response(http.StatusOK, e.SUCCESS, true) } func AddRealTimeWeight(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) pid := fsion.Get("pid").ValueStr() pastureid := fsion.Get("pastureid").ValueStr() weightjson := fsion.Get("weightjson").ValueStr() // weightjson tx := restful.Engine.NewSession() defer tx.Close() dataList, err := tx.SQL(` select weightjson from recweightmuster where pid = ? and pastureid = ? `, pid, pastureid).QueryString() if err != nil { logs.Error("AddRealTimeWeight-error1: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } weightList := make([]map[string]interface{}, 0) if len(dataList) > 0 { weight := "" for _, item := range dataList { weight = item["weightjson"] } list := make([]map[string]interface{}, 0) err = json.Unmarshal([]byte(weight), &weightList) if err != nil { logs.Error("AddRealTimeWeight-error2: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } err = json.Unmarshal([]byte(weightjson), &list) if err != nil { logs.Error("AddRealTimeWeight-error3: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } weightList = append(weightList, list...) } else { json.Unmarshal([]byte(weightjson), &weightList) } weightbyte, err := json.Marshal(weightList) if err != nil { logs.Error("AddRealTimeWeight-error4: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } _, err = tx.SQL(`REPLACE INTO recweightmuster (pid, pastureid, weightjson)values (?,?,?)`, pid, pastureid, string(weightbyte)).Execute() if err != nil { logs.Error("AddRealTimeWeight-error5: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } appG.Response(http.StatusOK, e.SUCCESS, true) } func GetDownloadedplanMaterialList(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) pastureid := fsion.Get("pastureid").ValueStr() date := fsion.Get("date").ValueStr() datacaptureno := fsion.Get("datacaptureno").ValueStr() // date := reqInfo.Date // datacaptureno := reqInfo.Datacaptureno tx := restful.Engine.NewSession() defer tx.Close() tclassid := "" tmrList, err := tx.SQL(` select tclassid from tmr where datacaptureno = ? `, datacaptureno).QueryString() if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } for _, tmr := range tmrList { tclassid = tmr["tclassid"] } downloadedplanInfo := make([]*downloadedplan, 0) err = tx.SQL(` select d.lpplantype,d.times,d.sort,d.times shifts,d.id,date_format(d.mydate ,'%Y-%m-%d') as mydate ,d.projname,d.datacaptureno,d.lweight,d.havebutton,d.feedpname,d.iscompleted,d.remark,d.tmrtname from downloadedplan d where d.mydate = ? and d.pastureid = ? and (d.datacaptureno is null or d.datacaptureno = ?) and ( (? IN(1,2) AND d.lpplanType IN (0,1)) OR (? IN (1,3) AND d.lpplanType IN (0,2))) and if(d.lpplantype = 2,if((select count(1) from downloadplandtl1 d1 where d1.pid = (select id from downloadedplan dd where dd.mydate = d.mydate and dd.pastureId = d.pastureid and dd.lpplanType IN (0,1) and dd.pid = d.pid and dd.times = d.times ) and d1.type = 0 and d1.havebuttom = 0 ) >0 ,1,0 ),0) = 0 order by d.sort `, date, pastureid, datacaptureno, tclassid, tclassid).Find(&downloadedplanInfo) if err != nil { logs.Error(err) appG.Response(http.StatusInternalServerError, e.ERROR, err) return } appG.Response(http.StatusOK, e.SUCCESS, downloadedplanInfo) }