123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- package api
- import (
- "encoding/json"
- "fmt"
- "io/ioutil"
- "log"
- "net/http"
- "../../pkg/app"
- "../../pkg/e"
- "../../routers/restful"
- "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)
- }
- 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-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"`
- }
- type ReqRestoreLpplandtl struct {
- Old []*lpplandtl `json:"old"`
- New []*lpplandtl `json:"new"`
- }
- func RestoreLpplandtl(c *gin.Context) {
-
- appG := app.Gin{C: c}
- req := new(ReqRestoreLpplandtl)
- // if err := c.Bind(req); err != nil {
- // log.Println("RestoreLpplandtl-error-1: ", err)
- // appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
- // return
- // }
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- if err := json.Unmarshal(dataByte, req); err != nil {
- log.Println("DelLpplandtl-error-1: ", err)
- appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
- return
- }
- fmt.Println(req)
- // exist := false
- // for _, new := range req.New {
- // for _, old := range req.Old {
- // if old.Id == new.Id {
- // if old.Lweight != new.Lweight {
- // exist = true
- // break
- // }
- // }
- // }
- // if exist {
- // break
- // }
- // }
- // if !exist {
- // appG.Response(http.StatusOK, e.SUCCESS, true)
- // }
- 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)
- }
|