package api import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http" "tmr-watch/http/handle/restful" "tmr-watch/pkg/app" "tmr-watch/pkg/e" "github.com/Anderson-Lu/gofasion/gofasion" "github.com/gin-gonic/gin" ) func GetLpplandtl(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) parammaps := fsion.Get("parammaps") lpplanid := parammaps.Get("id").ValueStr() pastureid := parammaps.Get("pastureid").ValueStr() // times := parammaps.Get("times").ValueInt64() sqlStr := `SELECT trim(lpplandtl1.lppid) lppid,lweight, lpplandtl1.lweight weight,lpplandtl1.sort, (select bname from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname, TRIM(lpplandtl1.fpdid) fpdid, TRIM(lpplandtl1.barid) barid, TRIM(lpplandtl1.id) id, TRIM(lpplandtl1.pastureid) pastureid, (SELECT tcolor FROM feedtemplet WHERE pastureid =lpplandtl1.pastureid AND id = IF(lpplandtl1.fttype=1,fpdetail.ptid,fpdetail.ptsid) ) background, lpplandtl1.fttype, trim(lpplandtl1.tmrid) tmrid, ifnull((select eqcode from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname, ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground, fpdetail.ptid, fpdetail.ptsid, fpdetail.times, fpdetail.cowcount, fpdetail.ccountradio,ifnull(tmr.volume,0)volume FROM lpplandtl1 inner join fpdetail on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid left join tmr on tmr.id =lpplandtl1.tmrid WHERE lpplandtl1.pastureid = ? and lpplandtl1.lweight>0 and lpplandtl1.lppid = ? ` // sqlStr += " and times = ? " sqlStr += " ORDER BY lpplandtl1.sort" tx := restful.Engine.NewSession() defer tx.Close() data, err := tx.SQL(sqlStr, pastureid, lpplanid).QueryString() if err != nil { log.Println("GetLpplandtl-error-1: ", err) appG.Response(http.StatusOK, e.ERROR, err) return } appG.Response(http.StatusOK, e.SUCCESS, data) } func GetLpplandtlDay(c *gin.Context) { appG := app.Gin{C: c} dataByte, _ := ioutil.ReadAll(c.Request.Body) fsion := gofasion.NewFasion(string(dataByte)) parammaps := fsion.Get("parammaps") lpplanid := parammaps.Get("id").ValueStr() pastureid := parammaps.Get("pastureid").ValueStr() date := parammaps.Get("date").ValueStr() // times := parammaps.Get("times").ValueInt64() sqlStr := `SELECT trim(lpplandtl1.lppid) lppid,lweight, lpplandtl1.lweight weight,lpplandtl1.sort, (select bname from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname, TRIM(lpplandtl1.fpdid) fpdid, TRIM(lpplandtl1.barid) barid, TRIM(lpplandtl1.id) id, TRIM(lpplandtl1.pastureid) pastureid, (SELECT tcolor FROM feedtemplet WHERE pastureid =lpplandtl1.pastureid AND id = IF(lpplandtl1.fttype=1,fpdetail.ptid,fpdetail.ptsid) ) background, lpplandtl1.fttype, trim(lpplandtl1.tmrid) tmrid, ifnull((select eqcode from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname, ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground, fpdetail.ptid, fpdetail.ptsid, fpdetail.times, fpdetail.cowcount, fpdetail.ccountradio,ifnull(tmr.volume,0)volume FROM lpplandtl1date as lpplandtl1 inner join fpdetail on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid left join tmr on tmr.id =lpplandtl1.tmrid WHERE lpplandtl1.pastureid = ? and lpplandtl1.lweight>0 and lpplandtl1.lppid = ? and lpplandtl1.date = ? ` // sqlStr += " and times = ? " sqlStr += " ORDER BY lpplandtl1.sort" tx := restful.Engine.NewSession() defer tx.Close() data, err := tx.SQL(sqlStr, pastureid, lpplanid, date).QueryString() if err != nil { log.Println("GetLpplandtl-error-1: ", err) appG.Response(http.StatusOK, e.ERROR, err) return } appG.Response(http.StatusOK, e.SUCCESS, data) } type ReqDelLpplandtl struct { PastureId string `xorm:"pastureid"` LpplandtlId string `xorm:"lpplandtlid"` FtType string `xorm:"fttype"` Lweight string `xorm:"lweight"` FpdId string `xorm:"fpdid"` } func DelLpplandtl(c *gin.Context) { appG := app.Gin{C: c} reqList := make([]*ReqDelLpplandtl, 0) // if err := c.Bind(&reqList); err != nil { // log.Println("DelLpplandtl-error-1: ", err) // appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err) // return // } dataByte, _ := ioutil.ReadAll(c.Request.Body) if err := json.Unmarshal(dataByte, &reqList); err != nil { log.Println("DelLpplandtl-error-1: ", err) appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err) return } tx := restful.Engine.NewSession() defer tx.Close() tx.Begin() for _, req := range reqList { if len(req.Lweight) > 0 { req.Lweight = fmt.Sprintf("-%s", req.Lweight) } _, err := tx.SQL(`delete from lpplandtl1 where id = ? and pastureid = ? `, req.LpplandtlId, req.PastureId).Execute() if err != nil { log.Println("DelLpplandtl-error-2: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) tx.Rollback() return } fmt.Println(req.PastureId, req.FpdId, req.Lweight, req.FtType) _, err = tx.SQL(`UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse), ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse) WHERE pastureid=? AND id=? `, req.FtType, req.Lweight, req.Lweight, req.FtType, req.Lweight, req.Lweight, req.PastureId, req.FpdId).Execute() if err != nil { log.Println("DelLpplandtl-error-3: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) tx.Rollback() return } } err := tx.Commit() if err != nil { log.Println("DelLpplandtl-error-4: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) tx.Rollback() return } appG.Response(http.StatusOK, e.SUCCESS, true) } func DelLpplandtlDay(c *gin.Context) { appG := app.Gin{C: c} reqList := make([]*ReqDelLpplandtl, 0) dataByte, _ := ioutil.ReadAll(c.Request.Body) if err := json.Unmarshal(dataByte, &reqList); err != nil { log.Println("DelLpplandtlDay-error-1: ", err) appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err) return } tx := restful.Engine.NewSession() defer tx.Close() tx.Begin() for _, req := range reqList { if len(req.Lweight) > 0 { req.Lweight = fmt.Sprintf("-%s", req.Lweight) } _, err := tx.SQL(`delete from lpplandtl1date where id = ? and pastureid = ? `, req.LpplandtlId, req.PastureId).Execute() if err != nil { log.Println("DelLpplandtlDay-error-2: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) tx.Rollback() return } fmt.Println(req.PastureId, req.FpdId, req.Lweight, req.FtType) _, err = tx.SQL(`UPDATE fpdetaildate SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse), ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse) WHERE pastureid=? AND id=? `, req.FtType, req.Lweight, req.Lweight, req.FtType, req.Lweight, req.Lweight, req.PastureId, req.FpdId).Execute() if err != nil { log.Println("DelLpplandtlDay-error-3: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) tx.Rollback() return } } err := tx.Commit() if err != nil { log.Println("DelLpplandtlDay-error-3: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) tx.Rollback() return } appG.Response(http.StatusOK, e.SUCCESS, true) } type lpplandtl struct { Id string `xorm:"id"` Pastureid string `xorm:"pastureid"` Lppid string `xorm:"lppid"` Barid string `xorm:"barid"` Barname string `xorm:"barname"` Fpdid string `xorm:"fpdid"` Fttype string `xorm:"fttype"` Lweight string `xorm:"lweight"` Sort string `xorm:"sort"` Tmrid string `xorm:"tmrid"` Tmrname string `xorm:"tmrname"` Background string `xorm:"background"` Ccountradio string `xorm:"ccountradio"` Date string `xorm:"date"` } type ReqRestoreLpplandtl struct { Old []*lpplandtl `json:"old"` New []*lpplandtl `json:"new"` } func RestoreLpplandtl(c *gin.Context) { appG := app.Gin{C: c} req := new(ReqRestoreLpplandtl) dataByte, _ := ioutil.ReadAll(c.Request.Body) if err := json.Unmarshal(dataByte, req); err != nil { log.Println("RestoreLpplandtl-error-1: ", err) appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err) return } tx := restful.Engine.NewSession() defer tx.Close() tx.Begin() for _, item := range req.New { if len(item.Lweight) > 0 { item.Lweight = fmt.Sprintf("-%s", item.Lweight) } _, err := tx.SQL(`delete from lpplandtl1 where id = ? and pastureid = ? `, item.Id, item.Pastureid).Execute() if err != nil { log.Println("RestoreLpplandtl-error-2: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) tx.Rollback() return } _, err = tx.SQL(`UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse), ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse) WHERE pastureid=? AND id=? `, item.Fttype, item.Lweight, item.Lweight, item.Fttype, item.Lweight, item.Lweight, item.Pastureid, item.Fpdid).Execute() if err != nil { log.Println("RestoreLpplandtl-error-3: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) tx.Rollback() return } } for _, item := range req.Old { _, err := tx.SQL(` UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse) WHERE pastureid=? AND id=? `, item.Fttype, item.Lweight, item.Lweight, item.Fttype, item.Lweight, item.Lweight, item.Pastureid, item.Fpdid).Execute() if err != nil { log.Println("RestoreLpplandtl-error-4", err) tx.Rollback() return } // tx.Table("lpplandtl1").Insert(item) _, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,ccountradio,background) value(?,?,?,?,?,?,?,?,?,?,?,?)`, item.Pastureid, item.Lppid, item.Barid, item.Barname, item.Fpdid, item.Fttype, item.Lweight, item.Sort, item.Tmrid, item.Tmrname, item.Ccountradio, item.Background).Execute() if err != nil { log.Println("RestoreLpplandtl-error-5", err) tx.Rollback() return } } err := tx.Commit() if err != nil { log.Println("RestoreLpplandtl-error-6: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) tx.Rollback() return } appG.Response(http.StatusOK, e.SUCCESS, true) } func RestoreLpplandtlDay(c *gin.Context) { appG := app.Gin{C: c} req := new(ReqRestoreLpplandtl) dataByte, _ := ioutil.ReadAll(c.Request.Body) if err := json.Unmarshal(dataByte, req); err != nil { log.Println("RestoreLpplandtlDay-error-1: ", err) appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err) return } tx := restful.Engine.NewSession() defer tx.Close() tx.Begin() for _, item := range req.New { if len(item.Lweight) > 0 { item.Lweight = fmt.Sprintf("-%s", item.Lweight) } _, err := tx.SQL(`delete from lpplandtl1date where id = ? and pastureid = ? `, item.Id, item.Pastureid).Execute() if err != nil { log.Println("RestoreLpplandtl-error-2: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) tx.Rollback() return } _, err = tx.SQL(`UPDATE fpdetaildate SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse), ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse) WHERE pastureid=? AND id=? `, item.Fttype, item.Lweight, item.Lweight, item.Fttype, item.Lweight, item.Lweight, item.Pastureid, item.Fpdid).Execute() if err != nil { log.Println("RestoreLpplandtl-error-3: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) tx.Rollback() return } } for _, item := range req.Old { _, err := tx.SQL(` UPDATE fpdetaildate SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse) WHERE pastureid=? AND id=? `, item.Fttype, item.Lweight, item.Lweight, item.Fttype, item.Lweight, item.Lweight, item.Pastureid, item.Fpdid).Execute() if err != nil { log.Println("RestoreLpplandtl-error-4", err) tx.Rollback() return } // tx.Table("lpplandtl1").Insert(item) _, err = tx.SQL(`insert into lpplandtl1date(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,ccountradio,background,date) value(?,?,?,?,?,?,?,?,?,?,?,?,?)`, item.Pastureid, item.Lppid, item.Barid, item.Barname, item.Fpdid, item.Fttype, item.Lweight, item.Sort, item.Tmrid, item.Tmrname, item.Ccountradio, item.Background, item.Date).Execute() if err != nil { log.Println("RestoreLpplandtl-error-5", err) tx.Rollback() return } } err := tx.Commit() if err != nil { log.Println("RestoreLpplandtl-error-6: ", err) appG.Response(http.StatusInternalServerError, e.ERROR, err) tx.Rollback() return } appG.Response(http.StatusOK, e.SUCCESS, true) }