spillage.go 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. package api
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "time"
  13. "../../pkg/app"
  14. "../../pkg/e"
  15. "../../pkg/logging"
  16. "../../pkg/setting"
  17. "../../routers/restful"
  18. "github.com/Anderson-Lu/gofasion/gofasion"
  19. "github.com/gin-gonic/gin"
  20. "github.com/xormplus/xorm"
  21. )
  22. type feedtemplet struct {
  23. Id int64 `xorm:"id"`
  24. Ccname string `xorm:"ccname"`
  25. Cname string `xorm:"tname"`
  26. }
  27. type tmrStruct struct {
  28. Id int64 `xorm:"id"`
  29. Tname string `xorm:"tname"`
  30. Maxstirfeed float64 `xorm:"maxstirfeed"`
  31. }
  32. type lpplanStruct struct {
  33. Id int64 `xorm:"id"`
  34. Tmrname string `xorm:"tmrname"`
  35. Tmrid int64 `xorm:"tmrid"`
  36. Sort int64 `xorm:"sort"`
  37. Sel int64 `xorm:"sel"`
  38. Times int64 `xorm:"times"`
  39. Ftname string `xorm:"ftname"`
  40. Ftid int64 `xorm:"ftid"`
  41. Maxweight float64 `xorm:"maxweight"`
  42. Sumweight float64 `xorm:"sumweight"`
  43. Sumcowcount int64 `xorm:"sumcowcount"`
  44. Pastureid string `xorm:"pastureid"`
  45. Display string `xorm:"display"`
  46. Issplit int64 `xorm:"issplit"`
  47. BeginTime string `xorm:"begintime"`
  48. }
  49. type sysopt struct {
  50. Inforvalue int `xorm:"inforvalue"`
  51. }
  52. type lpplanQueryInfo struct {
  53. Pastureid int64 `xorm:"pastureid"`
  54. Lppid int64 `xorm:"id"`
  55. Maxweight float64 `xorm:"maxweight"`
  56. Lweight float64 `xorm:"lweight"`
  57. Lweighthis float64 `xorm:"lweighthis"`
  58. Barid int64 `xorm:"barid"`
  59. Barname string `xorm:"barname"`
  60. Sort int64 `xorm:"sort"`
  61. Tmrid int64 `xorm:"tmrid"`
  62. Tmrname string `xorm:"tmrname"`
  63. Cowcount int64 `xorm:"cowcount"`
  64. Ccountradio float64 `xorm:"ccountradio"`
  65. Background string `xorm:"background"`
  66. Times int64 `xorm:"times"`
  67. Ftid int64 `xorm:"ftid"`
  68. }
  69. type fpdetailQueryInfo struct {
  70. Fpdid int64 `xorm:"fpdid"`
  71. Ptsrate float64 `xorm:"ptsrate"`
  72. Ptsuse float64 `xorm:"ptsuse"`
  73. Ptuse float64 `xorm:"ptuse"`
  74. Weight float64 `xorm:"weight"`
  75. Barid int64 `xorm:"barid"`
  76. Barname string `xorm:"barname"`
  77. Cowcount int64 `xorm:"cowcount"`
  78. Ccountradio float64 `xorm:"ccountradio"`
  79. Fttypeid int64 `xorm:"fttypeid"`
  80. Times int64 `xorm:"times"`
  81. Ptid int64 `xorm:"ptid"`
  82. Background string `xorm:"background"`
  83. }
  84. type lpplandtl1 struct {
  85. Pastureid int64 `xorm:"pastureid"`
  86. Lppid int64 `xorm:"lppid"`
  87. Barid int64 `xorm:"barid"`
  88. Barname string `xorm:"barname"`
  89. Fpdid int64 `xorm:"fpdid"`
  90. Fttype int64 `xorm:"fttype"`
  91. Lweight float64 `xorm:"lweight"`
  92. Sort int64 `xorm:"sort"`
  93. Tmrid int64 `xorm:"tmrid"`
  94. Tmrname string `xorm:"tmrname"`
  95. Cowcount int64 `xorm:"cowcount"`
  96. Ccountradio float64 `xorm:"ccountradio"`
  97. Background string `xorm:"background"`
  98. Lweighthis float64 `xorm:"lweighthis"`
  99. }
  100. //撒料计划 自动生成
  101. func Autogeneration(c *gin.Context) {
  102. appG := app.Gin{C: c}
  103. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  104. fsion := gofasion.NewFasion(string(dataByte))
  105. pastureid := fsion.Get("pastureid").ValueStr()
  106. typeIN := fsion.Get("type").ValueInt64()
  107. log.Println(time.Now())
  108. tx := restful.Engine.NewSession()
  109. defer tx.Close()
  110. var times int
  111. var wg sync.WaitGroup
  112. var tmrlist *[]*tmrStruct
  113. var feedtempletlist *[]*feedtemplet
  114. var err error
  115. var lpplanlist *[]*lpplanList
  116. var fpdetailWeightList *[]*fpdetailWeight
  117. wg.Add(1)
  118. go func() {
  119. defer wg.Done()
  120. feedtempletlist, err = getfeedtemplet(pastureid, tx)
  121. if err != nil {
  122. appG.Response(http.StatusOK, e.ERROR, false)
  123. return
  124. }
  125. }()
  126. wg.Wait()
  127. wg.Add(1)
  128. go func() {
  129. defer wg.Done()
  130. tmrlist, err = gettmr(pastureid, tx)
  131. if err != nil {
  132. appG.Response(http.StatusOK, e.ERROR, false)
  133. return
  134. }
  135. }()
  136. wg.Wait()
  137. wg.Add(1)
  138. go func() {
  139. defer wg.Done()
  140. times, err = gettimes(pastureid, tx)
  141. if err != nil {
  142. appG.Response(http.StatusOK, e.ERROR, false)
  143. return
  144. }
  145. }()
  146. wg.Wait()
  147. wg.Add(1)
  148. go func() {
  149. defer wg.Done()
  150. lpplanlist, err = getLpplanlist(pastureid, tx)
  151. if err != nil {
  152. appG.Response(http.StatusOK, e.ERROR, false)
  153. return
  154. }
  155. }()
  156. wg.Wait()
  157. wg.Add(1)
  158. go func() {
  159. defer wg.Done()
  160. fpdetailWeightList, err = getFpdetail(pastureid, tx)
  161. if err != nil {
  162. appG.Response(http.StatusOK, e.ERROR, false)
  163. return
  164. }
  165. for _, fpd := range *fpdetailWeightList {
  166. for _, lpp := range *lpplanlist {
  167. if (lpp.Ftid == fpd.Ptid || lpp.Ftid == fpd.Ptsid) && lpp.Times == fpd.Times {
  168. fpd.Weight = fpd.Weight - (lpp.Maxweight - lpp.Lweight)
  169. }
  170. }
  171. }
  172. }()
  173. wg.Wait()
  174. // 避免车次数量为0造成死循环
  175. max := false
  176. for _, tmr := range *tmrlist {
  177. if tmr.Maxstirfeed > 0 {
  178. max = true
  179. break
  180. }
  181. }
  182. for i := 1; i <= times; i++ {
  183. count, err := getLpplanCount(tx, i, pastureid)
  184. if err != nil {
  185. appG.Response(http.StatusOK, e.ERROR, false)
  186. return
  187. }
  188. for _, tem := range *feedtempletlist {
  189. for _, fpd := range *fpdetailWeightList {
  190. if tem.Id == fpd.Temid && i == int(fpd.Times) {
  191. var ftid int64
  192. if fpd.Ptid != 0 {
  193. ftid = fpd.Ptid
  194. } else {
  195. ftid = fpd.Ptsid
  196. }
  197. for fpd.Weight > 0 && max {
  198. for _, tmr := range *tmrlist {
  199. if tmr.Maxstirfeed > 0 {
  200. count++
  201. ids, err := setting.SnowIds.NextId()
  202. if err != nil {
  203. ids = time.Now().UnixNano()
  204. logging.Info("create SnowIds err", err)
  205. }
  206. display := tem.Cname
  207. if i == 1 {
  208. display += "第一班"
  209. } else if i == 2 {
  210. display += "第二班"
  211. } else if i == 3 {
  212. display += "第三班"
  213. } else if i == 4 {
  214. display += "第四班"
  215. }
  216. lpplan := &lpplanStruct{
  217. Id: ids,
  218. Tmrname: tmr.Tname,
  219. Tmrid: tmr.Id,
  220. Sort: count,
  221. Sel: 1,
  222. Times: int64(i),
  223. Ftname: tem.Cname,
  224. Ftid: ftid,
  225. Maxweight: tmr.Maxstirfeed,
  226. Sumweight: 0,
  227. Sumcowcount: 0,
  228. Pastureid: pastureid,
  229. Display: display,
  230. }
  231. err = addLpplan(lpplan, tx)
  232. if err != nil {
  233. appG.Response(http.StatusOK, e.ERROR, false)
  234. return
  235. }
  236. fpd.Weight = fpd.Weight - tmr.Maxstirfeed
  237. }
  238. if fpd.Weight < 0 {
  239. break
  240. }
  241. }
  242. }
  243. }
  244. }
  245. }
  246. }
  247. // wg.Wait()
  248. err = autogeneration(typeIN, pastureid, tx)
  249. if err != nil {
  250. appG.Response(http.StatusOK, e.ERROR, false)
  251. return
  252. }
  253. appG.Response(http.StatusOK, e.SUCCESS, true)
  254. }
  255. //查看配方模板
  256. func getfeedtemplet(pastureid string, tx *xorm.Session) (*[]*feedtemplet, error) {
  257. data := make([]*feedtemplet, 0)
  258. err := tx.Table("feedtemplet").Select("id,ccname,tname").Where("pastureid = ? ", pastureid).Find(&data)
  259. if err != nil {
  260. log.Println("getfeedtemplet-error", err)
  261. return nil, err
  262. }
  263. return &data, nil
  264. }
  265. //查看tmr设备
  266. func gettmr(pastureid string, tx *xorm.Session) (*[]*tmrStruct, error) {
  267. data := make([]*tmrStruct, 0)
  268. session := tx.Table("tmr")
  269. session.Select("TRIM(id) id,tname,maxstirfeed")
  270. session.Where("pastureid = ? ", pastureid)
  271. session.Where("enable = 1")
  272. session.In("tclassid", "1,2,3")
  273. err := session.Find(&data)
  274. if err != nil {
  275. log.Println("gettmr-error", err)
  276. return nil, err
  277. }
  278. return &data, nil
  279. }
  280. //判断计划种是否有该车次
  281. func getLpplanExist(tmrid, ftid int64, times int, pastureid string, tx *xorm.Session) (bool, error) {
  282. session := tx.Table("lpplan")
  283. session.Where("pastureid = ? ", pastureid)
  284. session.Where("times = ? ", times)
  285. session.Where("tmrid = ? ", tmrid)
  286. session.Where("ftid = ? ", ftid)
  287. exist, err := session.Exist()
  288. if err != nil {
  289. log.Println("getLpplanExist-error", err)
  290. return false, err
  291. }
  292. return exist, nil
  293. }
  294. type fpdetailWeight struct {
  295. Times int64 `xorm:"times"`
  296. Weight float64 `xorm:"weight"`
  297. Pastureid int64 `xorm:"pastureid"`
  298. Ptid int64 `xorm:"ptid"`
  299. Ptsid int64 `xorm:"ptsid"`
  300. Cowcount int64 `xorm:"cowcount"`
  301. Temid int64 `xorm:"Temid"`
  302. }
  303. //获取料数量
  304. func getFpdetail(pastureid string, tx *xorm.Session) (*[]*fpdetailWeight, error) {
  305. fpdetaillist := make([]*fpdetailWeight, 0)
  306. err := tx.SQL(` select t1.temid,t1.times,ifnull(t2.weight,0)+sum(t1.weight) as weight ,TRIM(t1.pastureid) pastureid,TRIM(t1.ptid) ptid,TRIM(t1.ptsid ) ptsid ,t1.cowcount from (SELECT fpd.id,ft.id as temid,fpd.times, ROUND((1-fpd.ptsrate)*fpd.weight,2)-fpd.ptuse weight,TRIM(fpd.pastureid) pastureid,TRIM(fpd.ptid) ptid,TRIM(fpd.ptsid ) ptsid ,fpd.cowcount FROM fpdetail fpd
  307. join feedp fp on fp.ftid = fpd.ptid and fp.pastureid = fpd.pastureid
  308. inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
  309. WHERE fpd.pastureid = ? AND fpd.ptuse<ROUND((1-fpd.ptsrate)*fpd.weight,2) and fpd.ptid > 0
  310. group by fpd.id,fpd.times) t1
  311. left join ( select t1.id,t1.temid as temid, t1.times,sum(t1.weight) weight,TRIM(t1.pastureid) pastureid,TRIM(t1.ptid) ptid,TRIM(t1.ptsid ) ptsid ,
  312. t1.cowcount from (SELECT fpd.id,ft.id as temid, fpd.times,ROUND(fpd.ptsrate*fpd.weight,2)-fpd.ptsuse weight,TRIM(fpd.pastureid) pastureid,TRIM(fpd.ptid) ptid,TRIM(fpd.ptsid ) ptsid ,
  313. cowcount FROM fpdetail fpd
  314. join feedp fp on fp.ptsfid = fpd.ptsid and fp.pastureid = fpd.pastureid
  315. inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
  316. WHERE fpd.pastureid = ? AND fpd.ptsuse< ROUND(fpd.ptsrate*fpd.weight,2) and fpd.ptsid > 0
  317. group by fpd.id,fpd.times) t1
  318. group by t1.temid,t1.times) t2 on t2.temid = t1.temid and t2.times = t1.times
  319. group by t1.temid,t1.times
  320. `, pastureid, pastureid).Find(&fpdetaillist)
  321. if err != nil {
  322. log.Println("getFpdetail-error", err)
  323. tx.Rollback()
  324. return nil, err
  325. }
  326. return &fpdetaillist, nil
  327. }
  328. type lpplanList struct {
  329. Tmrid int64 `xorm:"tmrid"`
  330. Times int64 `xorm:"times"`
  331. Ftid int64 `xorm:"ftid"`
  332. Maxweight float64 `xorm:"maxweight"`
  333. Lweight float64 `xorm:"lweight"`
  334. }
  335. func getLpplanlist(patsureid string, tx *xorm.Session) (*[]*lpplanList, error) {
  336. session := tx.SQL(` select t1.times,t1.tmrid,t1.ftid,t1.maxweight ,ifnull(t2.lweight,0)as lweight from lpplan t1
  337. left join ( select ifnull(sum(lweight),0) as lweight,lppid from lpplandtl1 where pastureid =? GROUP BY lppid) t2 on t1.id = t2.lppid
  338. where t1.pastureid = ? `, patsureid, patsureid)
  339. data := make([]*lpplanList, 0)
  340. err := session.Find(&data)
  341. if err != nil {
  342. log.Println("getLpplanlist-error", err)
  343. return nil, err
  344. }
  345. return &data, nil
  346. }
  347. // 添加车次计划
  348. func addLpplan(lpplan *lpplanStruct, tx *xorm.Session) error {
  349. _, err := tx.Table("lpplan").Insert(lpplan)
  350. if err != nil {
  351. log.Println("addLpplan-error", err)
  352. return err
  353. }
  354. return nil
  355. }
  356. //获取车次数量
  357. func getLpplanCount(tx *xorm.Session, times int, pastureid string) (int64, error) {
  358. count, err := tx.Table("lpplan").Where("times = ?", times).And("pastureid = ?", pastureid).Count()
  359. if err != nil {
  360. log.Println("getLpplanCount-error", err)
  361. return 0, err
  362. }
  363. return count, err
  364. }
  365. //获取班次信息
  366. func gettimes(pastureid string, tx *xorm.Session) (int, error) {
  367. session := tx.Table("sysopt")
  368. session.Where("pastureid = ? ", pastureid)
  369. session.Where("inforname = 'times' ")
  370. data := new(sysopt)
  371. _, err := session.Get(data)
  372. if err != nil {
  373. log.Println("gettimes-error", err)
  374. return 0, err
  375. }
  376. return data.Inforvalue, nil
  377. }
  378. type upfpdetail struct {
  379. insertlweight float64
  380. fttypeid int64
  381. fpdid int64
  382. }
  383. func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
  384. tx.Begin()
  385. if typeIN == 1 {
  386. _, err := tx.SQL(`DELETE FROM lpplandtl1 WHERE pastureid = ?`, pastureid).Execute()
  387. if err != nil {
  388. log.Println("autogeneration-error-1", err)
  389. tx.Rollback()
  390. return err
  391. }
  392. _, err = tx.SQL(`update fpdetail
  393. set ptuse = 0,ptsuse=0
  394. WHERE pastureid = ?`, pastureid).Execute()
  395. if err != nil {
  396. log.Println("autogeneration-error-2", err)
  397. tx.Rollback()
  398. return err
  399. }
  400. }
  401. lpplansession := tx.SQL(`select
  402. t1.sort,t1.tmrid,t1.tmrname,t1.ftid,
  403. t1.pastureid,t1.id ,t1.maxweight,ifnull(t2.lweight,0) as lweight,t1.times from lpplan t1
  404. left join ( select ifnull(sum(lweight),0) as lweight,lppid from lpplandtl1 where pastureid =? GROUP BY lppid) t2 on t1.id = t2.lppid
  405. where t1.pastureid = ? and t1.maxweight > ifnull(t2.lweight,0) `, pastureid, pastureid)
  406. lpplanlist := make([]*lpplanQueryInfo, 0)
  407. err := lpplansession.Find(&lpplanlist)
  408. if err != nil {
  409. log.Println("autogeneration-error-3", err)
  410. tx.Rollback()
  411. return err
  412. }
  413. fpdetaillist := make([]*fpdetailQueryInfo, 0)
  414. err = tx.SQL(`SELECT ft.tcolor background,fpd.ptid,ft.fttypeid,fpd.cowcount,fpd.ccountradio,fpd.id as fpdid,times,tratio,ROUND((1-fpd.ptsrate)*fpd.weight,2)-fpd.ptuse weight,TRIM(fpd.barid) barid,TRIM(fpd.pastureid) pastureid,TRIM(fpd.ptid) ptid,TRIM(fpd.ptsid ) ptsid ,
  415. (select bname from bar where pastureid =fpd.pastureid and id = fpd.barid ) barname,cowcount,ccountradio,0 isfill FROM fpdetail fpd
  416. join feedp fp on fp.ftid = fpd.ptid and fp.pastureid = fpd.pastureid
  417. inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
  418. WHERE fpd.pastureid = ? AND ptuse<ROUND((1-fpd.ptsrate)*fpd.weight,2)
  419. group by fpd.id , fpd.times `, pastureid).Find(&fpdetaillist)
  420. if err != nil {
  421. log.Println("autogeneration-error-4", err)
  422. tx.Rollback()
  423. return err
  424. }
  425. log.Println(time.Now(), "333333333333")
  426. var wg sync.WaitGroup
  427. for _, lpplan := range lpplanlist {
  428. lpplandtl1List := make([]*lpplandtl1, 0)
  429. upfpdetailList := make([]*upfpdetail, 0)
  430. for _, fpdetail := range fpdetaillist {
  431. if fpdetail.Ptid == lpplan.Ftid && fpdetail.Times == lpplan.Times {
  432. var weight float64
  433. weight = fpdetail.Weight
  434. if lpplan.Maxweight <= lpplan.Lweight || weight <= 0 {
  435. continue
  436. }
  437. var insertlweight float64
  438. if lpplan.Maxweight-lpplan.Lweight > weight {
  439. insertlweight = weight
  440. } else {
  441. insertlweight = lpplan.Maxweight - lpplan.Lweight
  442. }
  443. lpplan.Lweight += insertlweight
  444. lpplan.Lweighthis = insertlweight
  445. exist := false
  446. for _, up := range upfpdetailList {
  447. if up.fpdid == fpdetail.Fpdid {
  448. up.insertlweight += insertlweight
  449. exist = true
  450. break
  451. }
  452. }
  453. if !exist {
  454. upfpdetailList = append(upfpdetailList, &upfpdetail{
  455. fpdid: fpdetail.Fpdid,
  456. insertlweight: insertlweight,
  457. fttypeid: fpdetail.Fttypeid,
  458. })
  459. }
  460. fpdetail.Weight = fpdetail.Weight - insertlweight
  461. exist = false
  462. for _, lpplandtl1 := range lpplandtl1List {
  463. if lpplandtl1.Lppid == lpplan.Lppid && lpplan.Pastureid == lpplandtl1.Pastureid && lpplandtl1.Fttype == fpdetail.Fttypeid && lpplandtl1.Fpdid == fpdetail.Fpdid {
  464. lpplandtl1.Lweight += insertlweight
  465. lpplandtl1.Lweighthis = insertlweight
  466. exist = true
  467. break
  468. }
  469. }
  470. if !exist {
  471. lpplandtl1List = append(lpplandtl1List, &lpplandtl1{
  472. Pastureid: lpplan.Pastureid,
  473. Lppid: lpplan.Lppid,
  474. Barid: fpdetail.Barid,
  475. Barname: fpdetail.Barname,
  476. Fpdid: fpdetail.Fpdid,
  477. Fttype: fpdetail.Fttypeid,
  478. Lweight: insertlweight,
  479. Sort: lpplan.Sort,
  480. Tmrid: lpplan.Tmrid,
  481. Tmrname: lpplan.Tmrname,
  482. Cowcount: lpplan.Cowcount,
  483. Ccountradio: lpplan.Ccountradio,
  484. Background: fpdetail.Background,
  485. Lweighthis: insertlweight,
  486. })
  487. }
  488. }
  489. }
  490. for _, lpplpplandtl1 := range lpplandtl1List {
  491. wg.Add(1)
  492. go func(lpplpplandtl1 *lpplandtl1) {
  493. defer wg.Done()
  494. _, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  495. cowcount,ccountradio,background,lweighthis)
  496. value(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
  497. ON DUPLICATE KEY UPDATE lweight = lweight + lweight, lweighthis=lweighthis`, lpplpplandtl1.Pastureid,
  498. lpplpplandtl1.Lppid,
  499. lpplpplandtl1.Barid,
  500. lpplpplandtl1.Barname,
  501. lpplpplandtl1.Fpdid,
  502. lpplpplandtl1.Fttype,
  503. lpplpplandtl1.Lweight,
  504. lpplpplandtl1.Sort,
  505. lpplpplandtl1.Tmrid,
  506. lpplpplandtl1.Tmrname,
  507. lpplpplandtl1.Cowcount,
  508. lpplpplandtl1.Ccountradio,
  509. lpplpplandtl1.Background,
  510. lpplpplandtl1.Lweighthis).Execute()
  511. if err != nil {
  512. log.Println("autogeneration-error-6", err)
  513. tx.Rollback()
  514. return
  515. }
  516. }(lpplpplandtl1)
  517. }
  518. wg.Wait()
  519. for _, up := range upfpdetailList {
  520. wg.Add(1)
  521. go func(up *upfpdetail) {
  522. defer wg.Done()
  523. _, err = tx.SQL(` UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  524. WHERE pastureid=? AND id=? `, up.fttypeid, up.insertlweight, up.insertlweight, up.fttypeid, up.insertlweight, up.insertlweight, pastureid, up.fpdid).Execute()
  525. if err != nil {
  526. log.Println("autogeneration-error-5", err)
  527. tx.Rollback()
  528. return
  529. }
  530. }(up)
  531. }
  532. wg.Wait()
  533. }
  534. if typeIN == 1 {
  535. _, err = tx.SQL("update lpplan set sel = 1 where pastureid = ?", pastureid).Execute()
  536. if err != nil {
  537. log.Println("autogeneration-error-10", err)
  538. return err
  539. }
  540. }
  541. supplement := tx.Table("fpdetail").Alias("fpd")
  542. supplement.Select("ft.tcolor background,fpd.id as fpdid,fpd.barid,ft.id as temid, fpd.times,ROUND(fpd.ptsrate*fpd.weight,2)-fpd.ptsuse weight,TRIM(fpd.pastureid) pastureid,TRIM(fpd.ptid) ptid,TRIM(fpd.ptsid ) ptsid ,cowcount")
  543. supplement.Join("inner", []string{"feedp", "fp"}, "fp.ptsfid = fpd.ptsid").And("fp.pastureid = fpd.pastureid")
  544. supplement.Join("inner", []string{"feedtemplet", "ft"}, "ft.id = fp.ftid").And("ft.pastureid = fpd.pastureid")
  545. supplement.Where("fpd.pastureid = ?", pastureid).And("fpd.ptsuse< ROUND(fpd.ptsrate*fpd.weight,2)").And("fpd.ptsid > 0")
  546. supplement.GroupBy("fpd.id,fpd.times")
  547. supplementList := make([]*fpdetailQueryInfo, 0)
  548. err = supplement.Find(&supplementList)
  549. if err != nil {
  550. log.Println("autogeneration-error-11", err)
  551. return err
  552. }
  553. if len(supplementList) > 0 {
  554. for _, lpplan := range lpplanlist {
  555. if lpplan.Maxweight <= lpplan.Lweight {
  556. continue
  557. }
  558. lppids := []string{strconv.FormatInt(lpplan.Lppid, 10)}
  559. for _, lpp := range lpplanlist {
  560. if lpp.Ftid == lpplan.Ftid && lpp.Times == lpplan.Times {
  561. lppids = append(lppids, strconv.FormatInt(lpp.Lppid, 10))
  562. }
  563. }
  564. lpplandtl1List := make([]*lpplandtl1, 0)
  565. upfpdetailList := make([]*upfpdetail, 0)
  566. lpplandtllist := make([]*lpplandtl1, 0)
  567. err = tx.Table("lpplandtl1").Select("barid").Where(fmt.Sprintf("lppid in (%s)", strings.Join(lppids, ","))).And("").Find(&lpplandtllist)
  568. if err != nil {
  569. log.Println("autogeneration-error-11", err)
  570. return err
  571. }
  572. for _, fpdetail := range supplementList {
  573. if lpplan.Times != fpdetail.Times {
  574. continue
  575. }
  576. exist := false
  577. for _, bar := range lpplandtllist {
  578. if bar.Barid == fpdetail.Barid {
  579. exist = true
  580. break
  581. }
  582. }
  583. if !exist {
  584. continue
  585. }
  586. var weight float64
  587. weight = fpdetail.Weight
  588. if weight <= 0 {
  589. continue
  590. }
  591. var insertlweight float64
  592. if lpplan.Maxweight-lpplan.Lweight > weight {
  593. insertlweight = weight
  594. } else {
  595. insertlweight = lpplan.Maxweight - lpplan.Lweight
  596. }
  597. lpplan.Lweight += insertlweight
  598. lpplan.Lweighthis = insertlweight
  599. exist = false
  600. for _, up := range upfpdetailList {
  601. if up.fpdid == fpdetail.Fpdid {
  602. up.insertlweight += insertlweight
  603. exist = true
  604. break
  605. }
  606. }
  607. if !exist {
  608. upfpdetailList = append(upfpdetailList, &upfpdetail{
  609. fpdid: fpdetail.Fpdid,
  610. insertlweight: insertlweight,
  611. fttypeid: 0,
  612. })
  613. }
  614. fpdetail.Weight = fpdetail.Weight - insertlweight
  615. exist = false
  616. for _, lpplandtl1 := range lpplandtl1List {
  617. if lpplandtl1.Lppid == lpplan.Lppid && lpplan.Pastureid == lpplandtl1.Pastureid && lpplandtl1.Fttype == fpdetail.Fttypeid && lpplandtl1.Fpdid == fpdetail.Fpdid {
  618. lpplandtl1.Lweight += insertlweight
  619. lpplandtl1.Lweighthis = insertlweight
  620. exist = true
  621. break
  622. }
  623. }
  624. if !exist {
  625. lpplandtl1List = append(lpplandtl1List, &lpplandtl1{
  626. Pastureid: lpplan.Pastureid,
  627. Lppid: lpplan.Lppid,
  628. Barid: fpdetail.Barid,
  629. Barname: fpdetail.Barname,
  630. Fpdid: fpdetail.Fpdid,
  631. Fttype: 0,
  632. Lweight: insertlweight,
  633. Sort: lpplan.Sort,
  634. Tmrid: lpplan.Tmrid,
  635. Tmrname: lpplan.Tmrname,
  636. Cowcount: lpplan.Cowcount,
  637. Ccountradio: lpplan.Ccountradio,
  638. Background: fpdetail.Background,
  639. Lweighthis: insertlweight,
  640. })
  641. }
  642. }
  643. for _, lpplpplandtl1 := range lpplandtl1List {
  644. wg.Add(1)
  645. go func(lpplpplandtl1 *lpplandtl1) {
  646. defer wg.Done()
  647. _, err = tx.SQL(`insert into lpplandtl1(pastureid,lppid,barid,barname,fpdid,fttype,lweight,sort,tmrid,tmrname,
  648. cowcount,ccountradio,background,lweighthis)
  649. value(?,?,?,?,?,?,?,?,?,?,?,?,?,?)
  650. ON DUPLICATE KEY UPDATE lweight = lweight + lweight, lweighthis=lweighthis`, lpplpplandtl1.Pastureid,
  651. lpplpplandtl1.Lppid,
  652. lpplpplandtl1.Barid,
  653. lpplpplandtl1.Barname,
  654. lpplpplandtl1.Fpdid,
  655. lpplpplandtl1.Fttype,
  656. lpplpplandtl1.Lweight,
  657. lpplpplandtl1.Sort,
  658. lpplpplandtl1.Tmrid,
  659. lpplpplandtl1.Tmrname,
  660. lpplpplandtl1.Cowcount,
  661. lpplpplandtl1.Ccountradio,
  662. lpplpplandtl1.Background,
  663. lpplpplandtl1.Lweighthis).Execute()
  664. if err != nil {
  665. log.Println("autogeneration-error-6", err)
  666. tx.Rollback()
  667. return
  668. }
  669. }(lpplpplandtl1)
  670. }
  671. wg.Wait()
  672. for _, up := range upfpdetailList {
  673. wg.Add(1)
  674. go func(up *upfpdetail) {
  675. defer wg.Done()
  676. _, err = tx.SQL(` UPDATE fpdetail SET ptuse=IF(?=1,IF(ptuse+? <0,0,ptuse+?),ptuse),ptsuse=IF(?=0,IF(ptsuse+?<0,0,ptsuse+?),ptsuse)
  677. WHERE pastureid=? AND id=? `, up.fttypeid, up.insertlweight, up.insertlweight, up.fttypeid, up.insertlweight, up.insertlweight, pastureid, up.fpdid).Execute()
  678. if err != nil {
  679. log.Println("autogeneration-error-5", err)
  680. tx.Rollback()
  681. // return err
  682. return
  683. }
  684. }(up)
  685. }
  686. wg.Wait()
  687. }
  688. }
  689. err = tx.Commit()
  690. if err != nil {
  691. log.Println("autogeneration-error-11", err)
  692. return err
  693. }
  694. return nil
  695. }
  696. func GetSpillage(c *gin.Context) {
  697. appG := app.Gin{C: c}
  698. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  699. fsion := gofasion.NewFasion(string(dataByte))
  700. parammaps := fsion.Get("parammaps")
  701. pastureid := parammaps.Get("pastureid").ValueInt64()
  702. times := parammaps.Get("times").ValueInt64()
  703. ftid := parammaps.Get("ftid").ValueDefaultInt64(0)
  704. // refresh 0 全部 , 1 车次信息, 2 班次
  705. refresh := parammaps.Get("refresh").ValueInt64()
  706. tx := restful.Engine.NewSession()
  707. defer tx.Close()
  708. data := make(map[string]interface{})
  709. if refresh == 0 || refresh == 2 {
  710. sqlStr := `SELECT trim(feedp.pastureid) pastureid,barname,trim(barid) barid,trim(ftid) arrid,1 as type,? as times,ft.tname ftname,ft.tcolor background,0 isfill FROM feedp
  711. inner join feedtemplet ft
  712. on ft.id = feedp.ftid and ft.pastureid=?
  713. WHERE feedp.pastureid = ? %s
  714. GROUP BY feedp.ftid
  715. UNION
  716. SELECT trim(feedp.pastureid) pastureid,barname,barid,trim(ptsfid) arrid,0 as type,? as times,ft.tname ptsfname ,ft.tcolor background,1 isfill FROM feedp
  717. inner join feedtemplet ft
  718. on ft.id = feedp.ptsfid and ft.pastureid=?
  719. WHERE feedp.pastureid = ? %s and (SELECT inforvalue FROM sysopt WHERE sysopt.pastureid=feedp.pastureid AND sysopt.inforname= 'isEnableSupplyFeed') = 1
  720. GROUP BY feedp.ptsfid`
  721. if ftid != 0 {
  722. sqlStr = fmt.Sprintf(sqlStr, fmt.Sprintf(" and ft.id = %d ", ftid), fmt.Sprintf(" and ft.id = %d ", ftid))
  723. } else {
  724. sqlStr = fmt.Sprintf(sqlStr, "", "")
  725. }
  726. ftList, err := tx.SQL(sqlStr, times, pastureid, pastureid, times, pastureid, pastureid).Query().List()
  727. if err != nil {
  728. log.Println("GetSpillage-error-1: ", err)
  729. appG.Response(http.StatusOK, e.ERROR, nil)
  730. return
  731. }
  732. ftDetailList, err := tx.SQL(`SELECT TRIM(id) id,times,tratio,ROUND(ptsrate*weight,2)-ptsuse weight,TRIM(barid) barid,TRIM(pastureid) pastureid,TRIM(ptid) ptid,TRIM(ptsid ) ptsid ,
  733. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,0 AS fttype,cowcount,ccountradio,1 isfill FROM fpdetail
  734. WHERE fpdetail.pastureid = ? AND times=? AND ptsuse< ROUND(ptsrate*weight,2)
  735. UNION
  736. SELECT TRIM(id) id,times,tratio,ROUND((1-ptsrate)*weight,2)-ptuse weight,TRIM(barid) barid,TRIM(pastureid) pastureid,TRIM(ptid) ptid,TRIM(ptsid ) ptsid ,
  737. (select bname from bar where pastureid =fpdetail.pastureid and id = fpdetail.barid ) barname,1 AS fttype,cowcount,ccountradio,0 isfill FROM fpdetail
  738. WHERE fpdetail.pastureid = ? AND times=? AND ptuse<ROUND((1-ptsrate)*weight,2)
  739. ORDER BY barid`, pastureid, times, pastureid, times).Query().List()
  740. if err != nil {
  741. log.Println("GetSpillage-error-2: ", err)
  742. appG.Response(http.StatusOK, e.ERROR, nil)
  743. return
  744. }
  745. for _, ft := range ftList {
  746. arrList := make([]map[string]interface{}, 0)
  747. arrid := ft["arrid"].(string)
  748. for _, detail := range ftDetailList {
  749. if _, ok := detail["ptid"]; ok {
  750. if arrid == detail["ptid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  751. detail["background"] = ft["background"]
  752. arrList = append(arrList, detail)
  753. }
  754. } else if _, ok := detail["ptsid"]; ok {
  755. if arrid == detail["ptsid"].(string) && ft["isfill"].(int64) == detail["isfill"].(int64) {
  756. detail["background"] = ft["background"]
  757. arrList = append(arrList, detail)
  758. }
  759. }
  760. }
  761. if len(arrList) > 0 {
  762. ft["arrList"] = arrList
  763. }
  764. }
  765. data["ftlist"] = ftList
  766. }
  767. if refresh == 0 || refresh == 1 {
  768. sqlstr := `SELECT
  769. ifnull((select eqcode from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),lpplan.tmrname) tmrname,trim(tmrid) tmrid,sort,sel,
  770. CASE times
  771. WHEN 1 THEN
  772. '第一班'
  773. WHEN 2 THEN
  774. '第二班'
  775. WHEN 3 THEN
  776. '第三班'
  777. WHEN 4 THEN
  778. '第四班'
  779. END timesstr,issplit,
  780. times,display,begintime,
  781. (select tname from feedtemplet where pastureid =lpplan.pastureid and id = lpplan.ftid ) ftname,sumcowcount,
  782. IFNULL(round((SELECT SUM(lpplandtl1.lweight) FROM lpplandtl1 WHERE lpplandtl1.lppid=lpplan.id),2),0) sumweight ,
  783. ifnull((select maxstirfeed from tmr where pastureid =lpplan.pastureid and id = lpplan.tmrid ),100000) maxweight,
  784. TRIM(ftid) ftid,
  785. TRIM(id) id,
  786. TRIM(id) lppid,
  787. TRIM(pastureid) pastureid
  788. FROM
  789. lpplan
  790. WHERE pastureid = ? and times <= (SELECT sysopt.inforvalue FROM sysopt
  791. WHERE sysopt.pastureid= lpplan.pastureid AND sysopt.inforname='times')
  792. `
  793. arrsql := `SELECT
  794. trim(lpplandtl1.lppid) lppid,lweight,
  795. lpplandtl1.lweight weight,lpplandtl1.sort,
  796. (select bname from bar where pastureid =lpplandtl1.pastureid and id = lpplandtl1.barid ) barname,
  797. TRIM(lpplandtl1.fpdid) fpdid,
  798. TRIM(lpplandtl1.barid) barid,
  799. TRIM(lpplandtl1.id) id,
  800. TRIM(lpplandtl1.pastureid) pastureid,
  801. (SELECT tcolor FROM feedtemplet WHERE pastureid =lpplandtl1.pastureid AND id = IF(lpplandtl1.fttype=1,fpdetail.ptid,fpdetail.ptsid) ) background,
  802. lpplandtl1.fttype,
  803. trim(lpplandtl1.tmrid) tmrid,
  804. ifnull((select eqcode from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),lpplandtl1.tmrname) tmrname,
  805. ifnull((select tcolor from tmr where pastureid =lpplandtl1.pastureid and id = lpplandtl1.tmrid ),'#ccc') tbackground,
  806. fpdetail.ptid,
  807. fpdetail.ptsid,
  808. fpdetail.times,
  809. fpdetail.cowcount,
  810. fpdetail.ccountradio
  811. FROM
  812. lpplandtl1
  813. inner join fpdetail
  814. on lpplandtl1.fpdid= fpdetail.id and lpplandtl1.pastureid = fpdetail.pastureid
  815. WHERE lpplandtl1.pastureid = ? and lpplandtl1.lweight>0
  816. `
  817. var args []interface{}
  818. args = append(args, pastureid)
  819. if times != 0 {
  820. sqlstr += " and times = ? "
  821. arrsql += " and times = ? "
  822. args = append(args, times)
  823. }
  824. sqlstr += "ORDER BY times,lpplan.sort"
  825. session := tx.SQL(sqlstr, args...)
  826. lppList, err := session.Query().List()
  827. if err != nil {
  828. log.Println("GetSpillage-error-3: ", err)
  829. appG.Response(http.StatusOK, e.ERROR, nil)
  830. return
  831. }
  832. arrsql += " ORDER BY lpplandtl1.sort"
  833. arrList, err := tx.SQL(arrsql, args...).QueryString() //获取
  834. if err != nil {
  835. log.Println("GetSpillage-error-4: ", err)
  836. appG.Response(http.StatusOK, e.ERROR, nil)
  837. return
  838. }
  839. for _, query := range lppList {
  840. list := make([]map[string]string, 0)
  841. for _, arr := range arrList {
  842. if query["lppid"].(string) == arr["lppid"] {
  843. list = append(list, arr)
  844. }
  845. }
  846. query["arrList"] = list
  847. }
  848. data["list"] = lppList
  849. data["total"] = len(lppList)
  850. }
  851. appG.Response(http.StatusOK, e.SUCCESS, data)
  852. }
  853. type lpplanTrains struct {
  854. Id string `xorm:"id"`
  855. Sort int64 `xorm:"sort"`
  856. Pastureid string `xorm:"pastureid"`
  857. }
  858. //修改车次顺序
  859. func UpdateTrains(c *gin.Context) {
  860. appG := app.Gin{C: c}
  861. lppList := make([]*lpplanTrains, 0)
  862. err := c.Bind(&lppList)
  863. if err != nil {
  864. log.Println("UpdateTrains-error-1: ", err)
  865. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, err)
  866. return
  867. }
  868. tx := restful.Engine.NewSession()
  869. defer tx.Close()
  870. tx.Begin()
  871. for _, item := range lppList {
  872. _, err = tx.Table("lpplan").Where("id = ?", item.Id).And("pastureid = ? ", item.Pastureid).Update(item)
  873. if err != nil {
  874. log.Println("UpdateTrains-error-2: ", err)
  875. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  876. tx.Rollback()
  877. return
  878. }
  879. }
  880. err = tx.Commit()
  881. if err != nil {
  882. log.Println("UpdateTrains-error-2: ", err)
  883. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  884. tx.Rollback()
  885. return
  886. }
  887. appG.Response(http.StatusOK, e.SUCCESS, true)
  888. }
  889. type ReqAddLpplan struct {
  890. Id string `xorm:"id"`
  891. Tmrid string `xorm:"tmrid"`
  892. TmrName string `xorm:"tmrname"`
  893. Sort int64 `xorm:"sort"`
  894. Sel int64 `xorm:"sel"`
  895. Times int64 `xorm:"times"`
  896. Ftname string `xorm:"ftname"`
  897. Ftid string `xorm:"ftid"`
  898. Maxweight float64 `xorm:"maxweight"`
  899. // Sumweight int64 `xorm:"sumweight"`
  900. Sumcowcount int64 `xorm:"sumcowcount"`
  901. Pastureid string `xorm:"pastureid"`
  902. Display string `xorm:"display"`
  903. Issplit int64 `xorm:"issplit"`
  904. BeginTime string `xorm:"begintime"`
  905. }
  906. //添加车次并返回添加的车次信息
  907. func AddLpplan(c *gin.Context) {
  908. appG := app.Gin{C: c}
  909. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  910. fsion := gofasion.NewFasion(string(dataByte))
  911. parammaps := fsion.Get("parammaps")
  912. req := new(ReqAddLpplan)
  913. err := json.Unmarshal([]byte(parammaps.Json()), req)
  914. if err != nil {
  915. log.Println("AddLpplan-error-1: ", err)
  916. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  917. return
  918. }
  919. if req.Times == 0 {
  920. log.Println("AddLpplan-error-1: ", err)
  921. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, errors.New(" 班次错误 times = 0 !!!"))
  922. return
  923. }
  924. tx := restful.Engine.NewSession()
  925. defer tx.Close()
  926. ids, err := setting.SnowIds.NextId()
  927. if err != nil {
  928. ids = time.Now().UnixNano()
  929. logging.Info("AddLpplan-error-1: ", err)
  930. }
  931. req.Id = strconv.FormatInt(ids, 10)
  932. count, err := tx.Table("lpplan").Where("pastureid = ?", req.Pastureid).And(" times = ?", req.Times).Count()
  933. if err != nil {
  934. log.Println("AddLpplan-error-2: ", err)
  935. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  936. return
  937. }
  938. req.Sort = count + 1
  939. _, err = tx.Table("lpplan").Insert(req)
  940. if err != nil {
  941. log.Println("AddLpplan-error-3: ", err)
  942. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  943. return
  944. }
  945. appG.Response(http.StatusOK, e.SUCCESS, req)
  946. }
  947. //修改车次信息,如果修改tmrid信息则删除栏舍信息
  948. func UpdateLpplan(c *gin.Context) {
  949. appG := app.Gin{C: c}
  950. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  951. fsion := gofasion.NewFasion(string(dataByte))
  952. parammaps := fsion.Get("parammaps")
  953. lpplanid := parammaps.Get("id").ValueStr()
  954. pastureid := parammaps.Get("pastureid").ValueStr()
  955. tmrid := parammaps.Get("tmrid").ValueStr()
  956. tmrname := parammaps.Get("tmrname").ValueStr()
  957. begintime := parammaps.Get("begintime").ValueStr()
  958. times := parammaps.Get("times").ValueInt64()
  959. ftid := parammaps.Get("ftid").ValueStr()
  960. ftname := parammaps.Get("ftname").ValueStr()
  961. sel := parammaps.Get("sel").ValueInt64()
  962. issplit := parammaps.Get("issplit").ValueInt64()
  963. lpplan := new(ReqAddLpplan)
  964. lpplan.Tmrid = tmrid
  965. lpplan.TmrName = tmrname
  966. lpplan.BeginTime = begintime
  967. lpplan.Sel = sel
  968. lpplan.Issplit = issplit
  969. lpplan.Times = times
  970. lpplan.Ftid = ftid
  971. lpplan.Ftname = ftname
  972. tx := restful.Engine.NewSession()
  973. defer tx.Close()
  974. tx.Begin()
  975. lpplanData := new(lpplanStruct)
  976. _, err := tx.Table("lpplan").Select("tmrid").Where(" id = ? ", lpplanid).And(" pastureid = ? ", pastureid).Get(lpplanData)
  977. if err != nil {
  978. log.Println("UpdateLpplan-error-1: ", err)
  979. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  980. tx.Rollback()
  981. return
  982. }
  983. _, err = tx.Table("lpplan").Where(" id = ? ", lpplanid).And(" pastureid = ? ", pastureid).
  984. MustCols("tmrid,tmrname,begintime,sel,issplit,times,ftid,ftname").Update(lpplan)
  985. if err != nil {
  986. log.Println("UpdateLpplan-error-1: ", err)
  987. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  988. tx.Rollback()
  989. return
  990. }
  991. // if tmrid != lpplan.Tmrid {
  992. // _, err = tx.SQL(`delete from lpplandtl1 where lppid = ? and pastureid = ? `, lpplanid, pastureid).Execute()
  993. // if err != nil {
  994. // log.Println("UpdateLpplan-error-2: ", err)
  995. // appG.Response(http.StatusInternalServerError, e.ERROR, err)
  996. // tx.Rollback()
  997. // return
  998. // }
  999. // }
  1000. err = tx.Commit()
  1001. if err != nil {
  1002. log.Println("UpdateLpplan-error-3: ", err)
  1003. appG.Response(http.StatusInternalServerError, e.ERROR, err)
  1004. tx.Rollback()
  1005. return
  1006. }
  1007. appG.Response(http.StatusOK, e.SUCCESS, true)
  1008. }