123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- 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
- FROM
- lpplandtl1
- inner join fpdetail
- on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
- 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
- FROM
- lpplandtl1date as lpplandtl1
- inner join fpdetail
- on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
- 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)
- }
|