lpplandtl.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "../../pkg/app"
  9. "../../pkg/e"
  10. "../../routers/restful"
  11. "github.com/Anderson-Lu/gofasion/gofasion"
  12. "github.com/gin-gonic/gin"
  13. )
  14. func GetLpplandtl(c *gin.Context) {
  15. appG := app.Gin{C: c}
  16. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  17. fsion := gofasion.NewFasion(string(dataByte))
  18. parammaps := fsion.Get("parammaps")
  19. lpplanid := parammaps.Get("id").ValueStr()
  20. pastureid := parammaps.Get("pastureid").ValueStr()
  21. // times := parammaps.Get("times").ValueInt64()
  22. sqlStr := `SELECT
  23. trim(lpplandtl1.lppid) lppid,lweight,
  24. lpplandtl1.lweight weight,lpplandtl1.sort,
  25. (select bname from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
  26. TRIM(lpplandtl1.fpdid) fpdid,
  27. TRIM(lpplandtl1.barid) barid,
  28. TRIM(lpplandtl1.id) id,
  29. TRIM(lpplandtl1.pastureid) pastureid,
  30. (SELECT tcolor FROM feedtemplet WHERE pastureid =lpplandtl1.pastureid AND id = IF(lpplandtl1.fttype=1,fpdetail.ptid,fpdetail.ptsid) ) background,
  31. lpplandtl1.fttype,
  32. trim(lpplandtl1.tmrid) tmrid,
  33. ifnull((select eqcode from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
  34. ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
  35. fpdetail.ptid,
  36. fpdetail.ptsid,
  37. fpdetail.times,
  38. fpdetail.cowcount,
  39. fpdetail.ccountradio
  40. FROM
  41. lpplandtl1
  42. inner join fpdetail
  43. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
  44. WHERE lpplandtl1.pastureid = ? and lpplandtl1.lweight>0 and lpplandtl1.lppid = ?
  45. `
  46. // sqlStr += " and times = ? "
  47. sqlStr += " ORDER BY lpplandtl1.sort"
  48. tx := restful.Engine.NewSession()
  49. defer tx.Close()
  50. data, err := tx.SQL(sqlStr, pastureid, lpplanid).QueryString()
  51. if err != nil {
  52. log.Println("GetLpplandtl-error-1: ", err)
  53. appG.Response(http.StatusOK, e.ERROR, err)
  54. return
  55. }
  56. appG.Response(http.StatusOK, e.SUCCESS, data)
  57. }
  58. type ReqDelLpplandtl struct {
  59. PastureId string `xorm:"pastureid"`
  60. LpplandtlId string `xorm:"lpplandtlid"`
  61. FtType string `xorm:"fttype"`
  62. Lweight string `xorm:"lweight"`
  63. FpdId string `xorm:"fpdid"`
  64. }
  65. func DelLpplandtl(c *gin.Context) {
  66. appG := app.Gin{C: c}
  67. reqList := make([]*ReqDelLpplandtl, 0)
  68. // if err := c.Bind(&reqList); err != nil {
  69. // log.Println("DelLpplandtl-error-1: ", err)
  70. // appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  71. // return
  72. // }
  73. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  74. if err := json.Unmarshal(dataByte, &reqList); err != nil {
  75. log.Println("DelLpplandtl-error-1: ", err)
  76. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  77. return
  78. }
  79. tx := restful.Engine.NewSession()
  80. defer tx.Close()
  81. tx.Begin()
  82. for _, req := range reqList {
  83. if len(req.Lweight) > 0 {
  84. req.Lweight = fmt.Sprintf("-%s", req.Lweight)
  85. }
  86. _, err := tx.SQL(`delete from lpplandtl1 where id = ? and pastureid = ? `, req.LpplandtlId, req.PastureId).Execute()
  87. if err != nil {
  88. log.Println("DelLpplandtl-error-2: ", err)
  89. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  90. tx.Rollback()
  91. return
  92. }
  93. fmt.Println(req.PastureId, req.FpdId, req.Lweight, req.FtType)
  94. _, err = tx.SQL(`UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),
  95. ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  96. WHERE pastureid=? AND id=? `, req.FtType, req.Lweight, req.Lweight, req.FtType, req.Lweight, req.Lweight, req.PastureId, req.FpdId).Execute()
  97. if err != nil {
  98. log.Println("DelLpplandtl-error-3: ", err)
  99. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  100. tx.Rollback()
  101. return
  102. }
  103. }
  104. err := tx.Commit()
  105. if err != nil {
  106. log.Println("DelLpplandtl-error-3: ", err)
  107. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  108. tx.Rollback()
  109. return
  110. }
  111. appG.Response(http.StatusOK, e.SUCCESS, true)
  112. }
  113. type lpplandtl struct {
  114. Id string `xorm:"id"`
  115. Pastureid string `xorm:"pastureid"`
  116. Lppid string `xorm:"lppid"`
  117. Barid string `xorm:"barid"`
  118. Barname string `xorm:"barname"`
  119. Fpdid string `xorm:"fpdid"`
  120. Fttype string `xorm:"fttype"`
  121. Lweight string `xorm:"lweight"`
  122. Sort string `xorm:"sort"`
  123. Tmrid string `xorm:"tmrid"`
  124. Tmrname string `xorm:"tmrname"`
  125. Background string `xorm:"background"`
  126. Ccountradio string `xorm:"ccountradio"`
  127. }
  128. type ReqRestoreLpplandtl struct {
  129. Old []*lpplandtl `json:"old"`
  130. New []*lpplandtl `json:"new"`
  131. }
  132. func RestoreLpplandtl(c *gin.Context) {
  133. appG := app.Gin{C: c}
  134. req := new(ReqRestoreLpplandtl)
  135. // if err := c.Bind(req); err != nil {
  136. // log.Println("RestoreLpplandtl-error-1: ", err)
  137. // appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  138. // return
  139. // }
  140. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  141. if err := json.Unmarshal(dataByte, req); err != nil {
  142. log.Println("DelLpplandtl-error-1: ", err)
  143. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  144. return
  145. }
  146. fmt.Println(req)
  147. // exist := false
  148. // for _, new := range req.New {
  149. // for _, old := range req.Old {
  150. // if old.Id == new.Id {
  151. // if old.Lweight != new.Lweight {
  152. // exist = true
  153. // break
  154. // }
  155. // }
  156. // }
  157. // if exist {
  158. // break
  159. // }
  160. // }
  161. // if !exist {
  162. // appG.Response(http.StatusOK, e.SUCCESS, true)
  163. // }
  164. tx := restful.Engine.NewSession()
  165. defer tx.Close()
  166. tx.Begin()
  167. for _, item := range req.New {
  168. if len(item.Lweight) > 0 {
  169. item.Lweight = fmt.Sprintf("-%s", item.Lweight)
  170. }
  171. _, err := tx.SQL(`delete from lpplandtl1 where id = ? and pastureid = ? `, item.Id, item.Pastureid).Execute()
  172. if err != nil {
  173. log.Println("RestoreLpplandtl-error-2: ", err)
  174. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  175. tx.Rollback()
  176. return
  177. }
  178. _, err = tx.SQL(`UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),
  179. ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  180. WHERE pastureid=? AND id=? `, item.Fttype, item.Lweight, item.Lweight, item.Fttype, item.Lweight, item.Lweight, item.Pastureid, item.Fpdid).Execute()
  181. if err != nil {
  182. log.Println("RestoreLpplandtl-error-3: ", err)
  183. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  184. tx.Rollback()
  185. return
  186. }
  187. }
  188. for _, item := range req.Old {
  189. _, err := tx.SQL(` UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  190. WHERE pastureid=? AND id=? `, item.Fttype, item.Lweight, item.Lweight, item.Fttype, item.Lweight, item.Lweight, item.Pastureid, item.Fpdid).Execute()
  191. if err != nil {
  192. log.Println("RestoreLpplandtl-error-4", err)
  193. tx.Rollback()
  194. return
  195. }
  196. // tx.Table("lpplandtl1").Insert(item)
  197. _, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,ccountradio,background)
  198. value(?,?,?,?,?,?,?,?,?,?,?,?)`, item.Pastureid,
  199. item.Lppid,
  200. item.Barid,
  201. item.Barname,
  202. item.Fpdid,
  203. item.Fttype,
  204. item.Lweight,
  205. item.Sort,
  206. item.Tmrid,
  207. item.Tmrname,
  208. item.Ccountradio,
  209. item.Background).Execute()
  210. if err != nil {
  211. log.Println("RestoreLpplandtl-error-5", err)
  212. tx.Rollback()
  213. return
  214. }
  215. }
  216. err := tx.Commit()
  217. if err != nil {
  218. log.Println("RestoreLpplandtl-error-6: ", err)
  219. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  220. tx.Rollback()
  221. return
  222. }
  223. appG.Response(http.StatusOK, e.SUCCESS, true)
  224. }