lpplandtl.go 13 KB

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