lpplandtl.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. func GetLpplandtlDay(c *gin.Context) {
  59. appG := app.Gin{C: c}
  60. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  61. fsion := gofasion.NewFasion(string(dataByte))
  62. parammaps := fsion.Get("parammaps")
  63. lpplanid := parammaps.Get("id").ValueStr()
  64. pastureid := parammaps.Get("pastureid").ValueStr()
  65. date := parammaps.Get("date").ValueStr()
  66. // times := parammaps.Get("times").ValueInt64()
  67. sqlStr := `SELECT
  68. trim(lpplandtl1.lppid) lppid,lweight,
  69. lpplandtl1.lweight weight,lpplandtl1.sort,
  70. (select bname from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
  71. TRIM(lpplandtl1.fpdid) fpdid,
  72. TRIM(lpplandtl1.barid) barid,
  73. TRIM(lpplandtl1.id) id,
  74. TRIM(lpplandtl1.pastureid) pastureid,
  75. (SELECT tcolor FROM feedtemplet WHERE pastureid =lpplandtl1.pastureid AND id = IF(lpplandtl1.fttype=1,fpdetail.ptid,fpdetail.ptsid) ) background,
  76. lpplandtl1.fttype,
  77. trim(lpplandtl1.tmrid) tmrid,
  78. ifnull((select eqcode from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
  79. ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
  80. fpdetail.ptid,
  81. fpdetail.ptsid,
  82. fpdetail.times,
  83. fpdetail.cowcount,
  84. fpdetail.ccountradio
  85. FROM
  86. lpplandtl1date as lpplandtl1
  87. inner join fpdetail
  88. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
  89. WHERE lpplandtl1.pastureid = ? and lpplandtl1.lweight>0 and lpplandtl1.lppid = ? and lpplandtl1.date = ?
  90. `
  91. // sqlStr += " and times = ? "
  92. sqlStr += " ORDER BY lpplandtl1.sort"
  93. tx := restful.Engine.NewSession()
  94. defer tx.Close()
  95. data, err := tx.SQL(sqlStr, pastureid, lpplanid, date).QueryString()
  96. if err != nil {
  97. log.Println("GetLpplandtl-error-1: ", err)
  98. appG.Response(http.StatusOK, e.ERROR, err)
  99. return
  100. }
  101. appG.Response(http.StatusOK, e.SUCCESS, data)
  102. }
  103. type ReqDelLpplandtl struct {
  104. PastureId string `xorm:"pastureid"`
  105. LpplandtlId string `xorm:"lpplandtlid"`
  106. FtType string `xorm:"fttype"`
  107. Lweight string `xorm:"lweight"`
  108. FpdId string `xorm:"fpdid"`
  109. }
  110. func DelLpplandtl(c *gin.Context) {
  111. appG := app.Gin{C: c}
  112. reqList := make([]*ReqDelLpplandtl, 0)
  113. // if err := c.Bind(&reqList); err != nil {
  114. // log.Println("DelLpplandtl-error-1: ", err)
  115. // appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  116. // return
  117. // }
  118. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  119. if err := json.Unmarshal(dataByte, &reqList); err != nil {
  120. log.Println("DelLpplandtl-error-1: ", err)
  121. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  122. return
  123. }
  124. tx := restful.Engine.NewSession()
  125. defer tx.Close()
  126. tx.Begin()
  127. for _, req := range reqList {
  128. if len(req.Lweight) > 0 {
  129. req.Lweight = fmt.Sprintf("-%s", req.Lweight)
  130. }
  131. _, err := tx.SQL(`delete from lpplandtl1 where id = ? and pastureid = ? `, req.LpplandtlId, req.PastureId).Execute()
  132. if err != nil {
  133. log.Println("DelLpplandtl-error-2: ", err)
  134. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  135. tx.Rollback()
  136. return
  137. }
  138. fmt.Println(req.PastureId, req.FpdId, req.Lweight, req.FtType)
  139. _, err = tx.SQL(`UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),
  140. ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  141. WHERE pastureid=? AND id=? `, req.FtType, req.Lweight, req.Lweight, req.FtType, req.Lweight, req.Lweight, req.PastureId, req.FpdId).Execute()
  142. if err != nil {
  143. log.Println("DelLpplandtl-error-3: ", err)
  144. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  145. tx.Rollback()
  146. return
  147. }
  148. }
  149. err := tx.Commit()
  150. if err != nil {
  151. log.Println("DelLpplandtl-error-4: ", err)
  152. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  153. tx.Rollback()
  154. return
  155. }
  156. appG.Response(http.StatusOK, e.SUCCESS, true)
  157. }
  158. func DelLpplandtlDay(c *gin.Context) {
  159. appG := app.Gin{C: c}
  160. reqList := make([]*ReqDelLpplandtl, 0)
  161. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  162. if err := json.Unmarshal(dataByte, &reqList); err != nil {
  163. log.Println("DelLpplandtlDay-error-1: ", err)
  164. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  165. return
  166. }
  167. tx := restful.Engine.NewSession()
  168. defer tx.Close()
  169. tx.Begin()
  170. for _, req := range reqList {
  171. if len(req.Lweight) > 0 {
  172. req.Lweight = fmt.Sprintf("-%s", req.Lweight)
  173. }
  174. _, err := tx.SQL(`delete from lpplandtl1date where id = ? and pastureid = ? `, req.LpplandtlId, req.PastureId).Execute()
  175. if err != nil {
  176. log.Println("DelLpplandtlDay-error-2: ", err)
  177. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  178. tx.Rollback()
  179. return
  180. }
  181. fmt.Println(req.PastureId, req.FpdId, req.Lweight, req.FtType)
  182. _, err = tx.SQL(`UPDATE fpdetaildate SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),
  183. ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  184. WHERE pastureid=? AND id=? `, req.FtType, req.Lweight, req.Lweight, req.FtType, req.Lweight, req.Lweight, req.PastureId, req.FpdId).Execute()
  185. if err != nil {
  186. log.Println("DelLpplandtlDay-error-3: ", err)
  187. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  188. tx.Rollback()
  189. return
  190. }
  191. }
  192. err := tx.Commit()
  193. if err != nil {
  194. log.Println("DelLpplandtlDay-error-3: ", err)
  195. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  196. tx.Rollback()
  197. return
  198. }
  199. appG.Response(http.StatusOK, e.SUCCESS, true)
  200. }
  201. type lpplandtl struct {
  202. Id string `xorm:"id"`
  203. Pastureid string `xorm:"pastureid"`
  204. Lppid string `xorm:"lppid"`
  205. Barid string `xorm:"barid"`
  206. Barname string `xorm:"barname"`
  207. Fpdid string `xorm:"fpdid"`
  208. Fttype string `xorm:"fttype"`
  209. Lweight string `xorm:"lweight"`
  210. Sort string `xorm:"sort"`
  211. Tmrid string `xorm:"tmrid"`
  212. Tmrname string `xorm:"tmrname"`
  213. Background string `xorm:"background"`
  214. Ccountradio string `xorm:"ccountradio"`
  215. Date string `xorm:"date"`
  216. }
  217. type ReqRestoreLpplandtl struct {
  218. Old []*lpplandtl `json:"old"`
  219. New []*lpplandtl `json:"new"`
  220. }
  221. func RestoreLpplandtl(c *gin.Context) {
  222. appG := app.Gin{C: c}
  223. req := new(ReqRestoreLpplandtl)
  224. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  225. if err := json.Unmarshal(dataByte, req); err != nil {
  226. log.Println("RestoreLpplandtl-error-1: ", err)
  227. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  228. return
  229. }
  230. tx := restful.Engine.NewSession()
  231. defer tx.Close()
  232. tx.Begin()
  233. for _, item := range req.New {
  234. if len(item.Lweight) > 0 {
  235. item.Lweight = fmt.Sprintf("-%s", item.Lweight)
  236. }
  237. _, err := tx.SQL(`delete from lpplandtl1 where id = ? and pastureid = ? `, item.Id, item.Pastureid).Execute()
  238. if err != nil {
  239. log.Println("RestoreLpplandtl-error-2: ", err)
  240. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  241. tx.Rollback()
  242. return
  243. }
  244. _, err = tx.SQL(`UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),
  245. ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  246. WHERE pastureid=? AND id=? `, item.Fttype, item.Lweight, item.Lweight, item.Fttype, item.Lweight, item.Lweight, item.Pastureid, item.Fpdid).Execute()
  247. if err != nil {
  248. log.Println("RestoreLpplandtl-error-3: ", err)
  249. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  250. tx.Rollback()
  251. return
  252. }
  253. }
  254. for _, item := range req.Old {
  255. _, err := tx.SQL(` UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  256. WHERE pastureid=? AND id=? `, item.Fttype, item.Lweight, item.Lweight, item.Fttype, item.Lweight, item.Lweight, item.Pastureid, item.Fpdid).Execute()
  257. if err != nil {
  258. log.Println("RestoreLpplandtl-error-4", err)
  259. tx.Rollback()
  260. return
  261. }
  262. // tx.Table("lpplandtl1").Insert(item)
  263. _, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,ccountradio,background)
  264. value(?,?,?,?,?,?,?,?,?,?,?,?)`, item.Pastureid,
  265. item.Lppid,
  266. item.Barid,
  267. item.Barname,
  268. item.Fpdid,
  269. item.Fttype,
  270. item.Lweight,
  271. item.Sort,
  272. item.Tmrid,
  273. item.Tmrname,
  274. item.Ccountradio,
  275. item.Background).Execute()
  276. if err != nil {
  277. log.Println("RestoreLpplandtl-error-5", err)
  278. tx.Rollback()
  279. return
  280. }
  281. }
  282. err := tx.Commit()
  283. if err != nil {
  284. log.Println("RestoreLpplandtl-error-6: ", err)
  285. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  286. tx.Rollback()
  287. return
  288. }
  289. appG.Response(http.StatusOK, e.SUCCESS, true)
  290. }
  291. func RestoreLpplandtlDay(c *gin.Context) {
  292. appG := app.Gin{C: c}
  293. req := new(ReqRestoreLpplandtl)
  294. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  295. if err := json.Unmarshal(dataByte, req); err != nil {
  296. log.Println("RestoreLpplandtlDay-error-1: ", err)
  297. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  298. return
  299. }
  300. tx := restful.Engine.NewSession()
  301. defer tx.Close()
  302. tx.Begin()
  303. for _, item := range req.New {
  304. if len(item.Lweight) > 0 {
  305. item.Lweight = fmt.Sprintf("-%s", item.Lweight)
  306. }
  307. _, err := tx.SQL(`delete from lpplandtl1date where id = ? and pastureid = ? `, item.Id, item.Pastureid).Execute()
  308. if err != nil {
  309. log.Println("RestoreLpplandtl-error-2: ", err)
  310. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  311. tx.Rollback()
  312. return
  313. }
  314. _, err = tx.SQL(`UPDATE fpdetaildate SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),
  315. ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  316. WHERE pastureid=? AND id=? `, item.Fttype, item.Lweight, item.Lweight, item.Fttype, item.Lweight, item.Lweight, item.Pastureid, item.Fpdid).Execute()
  317. if err != nil {
  318. log.Println("RestoreLpplandtl-error-3: ", err)
  319. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  320. tx.Rollback()
  321. return
  322. }
  323. }
  324. for _, item := range req.Old {
  325. _, err := tx.SQL(` UPDATE fpdetaildate SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  326. WHERE pastureid=? AND id=? `, item.Fttype, item.Lweight, item.Lweight, item.Fttype, item.Lweight, item.Lweight, item.Pastureid, item.Fpdid).Execute()
  327. if err != nil {
  328. log.Println("RestoreLpplandtl-error-4", err)
  329. tx.Rollback()
  330. return
  331. }
  332. // tx.Table("lpplandtl1").Insert(item)
  333. _, err = tx.SQL(`insert into lpplandtl1date(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,ccountradio,background,date)
  334. value(?,?,?,?,?,?,?,?,?,?,?,?,?)`, item.Pastureid,
  335. item.Lppid,
  336. item.Barid,
  337. item.Barname,
  338. item.Fpdid,
  339. item.Fttype,
  340. item.Lweight,
  341. item.Sort,
  342. item.Tmrid,
  343. item.Tmrname,
  344. item.Ccountradio,
  345. item.Background,
  346. item.Date).Execute()
  347. if err != nil {
  348. log.Println("RestoreLpplandtl-error-5", err)
  349. tx.Rollback()
  350. return
  351. }
  352. }
  353. err := tx.Commit()
  354. if err != nil {
  355. log.Println("RestoreLpplandtl-error-6: ", err)
  356. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  357. tx.Rollback()
  358. return
  359. }
  360. appG.Response(http.StatusOK, e.SUCCESS, true)
  361. }