123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- package api
- import (
- "io/ioutil"
- "log"
- "net/http"
- "time"
- "../../pkg/app"
- "../../pkg/e"
- "../../pkg/logging"
- "../../pkg/setting"
- "../../routers/restful"
- "github.com/Anderson-Lu/gofasion/gofasion"
- "github.com/gin-gonic/gin"
- "github.com/xormplus/xorm"
- )
- type feedtemplet struct {
- Id int64 `xorm:"id"`
- Ccname string `xorm:"ccname"`
- Cname string `xorm:"tname"`
- }
- type tmrStruct struct {
- Id int64 `xorm:"id"`
- Tname string `xorm:"tname"`
- Maxstirfeed float64 `xorm:"maxstirfeed"`
- }
- type lpplanStruct struct {
- Id int64 `xorm:"id"`
- Tmrname string `xorm:"tmrname"`
- Tmrid int64 `xorm:"tmrid"`
- Sort int64 `xorm:"sort"`
- Sel int64 `xorm:"sel"`
- Times int64 `xorm:"times"`
- Ftname string `xorm:"ftname"`
- Ftid int64 `xorm:"ftid"`
- Maxweight float64 `xorm:"maxweight"`
- Sumweight float64 `xorm:"sumweight"`
- Sumcowcount int64 `xorm:"sumcowcount"`
- Pastureid string `xorm:"pastureid"`
- }
- type sysopt struct {
- Inforvalue int `xorm:"inforvalue"`
- }
- type lpplanQueryInfo struct {
- Pastureid int64 `xorm:"pastureid"`
- Lppid int64 `xorm:"id"`
- Maxweight float64 `xorm:"maxweight"`
- Lweight float64 `xorm:"lweight"`
- Lweighthis float64 `xorm:"lweighthis"`
- Barid int64 `xorm:"barid"`
- Barname string `xorm:"barname"`
- Sort int64 `xorm:"sort"`
- Tmrid int64 `xorm:"tmrid"`
- Tmrname string `xorm:"tmrname"`
- Cowcount int64 `xorm:"cowcount"`
- Ccountradio float64 `xorm:"ccountradio"`
- Background string `xorm:"background"`
- Times int64 `xorm:"times"`
- Ftid int64 `xorm:"ftid"`
- }
- type fpdetailQueryInfo struct {
- Fpdid int64 `xorm:"fpdid"`
- Ptsrate float64 `xorm:"ptsrate"`
- Ptsuse float64 `xorm:"ptsuse"`
- Ptuse float64 `xorm:"ptuse"`
- Weight float64 `xorm:"weight"`
- Barid int64 `xorm:"barid"`
- Barname string `xorm:"barname"`
- Cowcount int64 `xorm:"cowcount"`
- Ccountradio float64 `xorm:"ccountradio"`
- Fttypeid int64 `xorm:"fttypeid"`
- }
- type lpplandtl1 struct {
- Pastureid int64 `xorm:"pastureid"`
- Lppid int64 `xorm:"lppid"`
- Barid int64 `xorm:"barid"`
- Barname string `xorm:"barname"`
- Fpdid int64 `xorm:"fpdid"`
- Fttype int64 `xorm:"fttype"`
- Lweight float64 `xorm:"lweight"`
- Sort int64 `xorm:"sort"`
- Tmrid int64 `xorm:"tmrid"`
- Tmrname string `xorm:"tmrname"`
- Cowcount int64 `xorm:"cowcount"`
- Ccountradio float64 `xorm:"ccountradio"`
- Background string `xorm:"background"`
- Lweighthis float64 `xorm:"lweighthis"`
- }
- //撒料计划 自动生成
- func Autogeneration(c *gin.Context) {
- appG := app.Gin{C: c}
- dataByte, _ := ioutil.ReadAll(c.Request.Body)
- fsion := gofasion.NewFasion(string(dataByte))
- pastureid := fsion.Get("pastureid").ValueStr()
- typeIN := fsion.Get("type").ValueInt64()
- tx := restful.Engine.NewSession()
- feedtempletlist, err := getfeedtemplet(pastureid, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, false)
- return
- }
- tmrlist, err := gettmr(pastureid, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, false)
- return
- }
- times, err := gettimes(pastureid, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, false)
- return
- }
- lpplanlist, err := getLpplanlist(pastureid, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, false)
- return
- }
- fpdetailWeightList, err := getFpdetail(pastureid, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, false)
- return
- }
- for _, tem := range feedtempletlist {
- count, err := getLpplanCount(tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, false)
- return
- }
- for _, tmr := range tmrlist {
- for i := 1; i <= times; i++ {
- exist := false
- var lppLweight, maxweight float64
- for _, lpp := range lpplanlist {
- if lpp.Tmrid == tmr.Id && lpp.Times == int64(i) && lpp.Ftid == tem.Id {
- exist = true
- lppLweight += lpp.Lweight
- maxweight += lpp.Maxweight
- }
- }
- lppLweight = maxweight - lppLweight
- for _, fpd := range fpdetailWeightList {
- if (tem.Id == fpd.Ptid || tem.Id == fpd.Ptsid) && i == int(fpd.Times) {
- var ftid int64
- if fpd.Ptid != 0 {
- ftid = fpd.Ptid
- } else {
- ftid = fpd.Ptsid
- }
- if tmr.Maxstirfeed <= 0 {
- if !exist {
- count++
- ids, err := setting.SnowIds.NextId()
- if err != nil {
- ids = time.Now().UnixNano()
- logging.Info("create SnowIds err", err)
- }
- lpplan := &lpplanStruct{
- Id: ids,
- Tmrname: tmr.Tname,
- Tmrid: tmr.Id,
- Sort: count,
- Sel: 1,
- Times: int64(i),
- Ftname: tem.Cname,
- Ftid: ftid,
- Maxweight: tmr.Maxstirfeed,
- Sumweight: 0,
- Sumcowcount: 0,
- Pastureid: pastureid,
- }
- err = addLpplan(lpplan, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, false)
- return
- }
- }
- } else {
- var weight float64
- weight = fpd.Weight - lppLweight
- for weight > 0 {
- count++
- ids, err := setting.SnowIds.NextId()
- if err != nil {
- ids = time.Now().UnixNano()
- logging.Info("create SnowIds err", err)
- }
- lpplan := &lpplanStruct{
- Id: ids,
- Tmrname: tmr.Tname,
- Tmrid: tmr.Id,
- Sort: count,
- Sel: 1,
- Times: int64(i),
- Ftname: tem.Cname,
- Ftid: ftid,
- Maxweight: tmr.Maxstirfeed,
- Sumweight: 0,
- Sumcowcount: 0,
- Pastureid: pastureid,
- }
- err = addLpplan(lpplan, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, false)
- return
- }
- weight = weight - tmr.Maxstirfeed
- }
- }
- break
- }
- // }
- }
- }
- }
- // }(tem)
- }
- // go func(typeIN int64, pastureid string, tx *xorm.Session) {
- err = autogeneration(typeIN, pastureid, tx)
- if err != nil {
- appG.Response(http.StatusOK, e.ERROR, false)
- return
- }
- // }(typeIN, pastureid, tx)
- appG.Response(http.StatusOK, e.SUCCESS, true)
- }
- //查看配方模板
- func getfeedtemplet(pastureid string, tx *xorm.Session) ([]*feedtemplet, error) {
- data := make([]*feedtemplet, 0)
- err := tx.Table("feedtemplet").Select("id,ccname,tname").Where("pastureid = ? ", pastureid).Find(&data)
- if err != nil {
- log.Println("getfeedtemplet-error", err)
- return nil, err
- }
- return data, nil
- }
- //查看tmr设备
- func gettmr(pastureid string, tx *xorm.Session) ([]*tmrStruct, error) {
- data := make([]*tmrStruct, 0)
- session := tx.Table("tmr")
- session.Select("TRIM(id) id,tname,maxstirfeed")
- session.Where("pastureid = ? ", pastureid)
- session.Where("enable = 1")
- session.In("tclassid", "1,2,3")
- err := session.Find(&data)
- if err != nil {
- log.Println("gettmr-error", err)
- return nil, err
- }
- return data, nil
- }
- //判断计划种是否有该车次
- func getLpplanExist(tmrid, ftid int64, times int, pastureid string, tx *xorm.Session) (bool, error) {
- session := tx.Table("lpplan")
- session.Where("pastureid = ? ", pastureid)
- session.Where("times = ? ", times)
- session.Where("tmrid = ? ", tmrid)
- session.Where("ftid = ? ", ftid)
- exist, err := session.Exist()
- if err != nil {
- log.Println("getLpplanExist-error", err)
- return false, err
- }
- return exist, nil
- }
- type fpdetailWeight struct {
- Times int64 `xorm:"times"`
- Weight float64 `xorm:"weight"`
- Pastureid int64 `xorm:"pastureid"`
- Ptid int64 `xorm:"ptid"`
- Ptsid int64 `xorm:"ptsid"`
- Cowcount int64 `xorm:"cowcount"`
- }
- //获取料数量
- func getFpdetail(pastureid string, tx *xorm.Session) ([]*fpdetailWeight, error) {
- fpdetaillist := make([]*fpdetailWeight, 0)
- err := tx.SQL(`SELECT times,sum(ROUND(ptsrate*weight,2)-ptsuse) weight,TRIM(pastureid) pastureid,TRIM(ptid) ptid,TRIM(ptsid ) ptsid ,
- cowcount FROM fpdetail
- WHERE fpdetail.pastureid = ? AND ptsuse< ROUND(ptsrate*weight,2) and ptsid !=0
- group by ptid,times,ptsid
- UNION
- SELECT times,sum(ROUND((1-ptsrate)*weight,2)-ptuse)weight,TRIM(pastureid) pastureid,TRIM(ptid) ptid,TRIM(ptsid ) ptsid ,cowcount FROM fpdetail
- WHERE fpdetail.pastureid = ? AND ptuse<ROUND((1-ptsrate)*weight,2) and ptid !=0
- group by ptid,times,ptsid
- `, pastureid, pastureid).Find(&fpdetaillist)
- if err != nil {
- log.Println("getFpdetail-error", err)
- tx.Rollback()
- return nil, err
- }
- return fpdetaillist, nil
- }
- type lpplanList struct {
- Tmrid int64 `xorm:"tmrid"`
- Times int64 `xorm:"times"`
- Ftid int64 `xorm:"ftid"`
- Maxweight float64 `xorm:"maxweight"`
- Lweight float64 `xorm:"lweight"`
- }
- func getLpplanlist(patsureid string, tx *xorm.Session) ([]*lpplanList, error) {
- session := tx.SQL(` select t1.times,t1.tmrid,t1.ftid,t1.maxweight ,ifnull(t2.lweight,0)as lweight from lpplan t1
- left join ( select ifnull(sum(lweight),0) as lweight,lppid from lpplandtl1 where pastureid =? GROUP BY lppid) t2 on t1.id = t2.lppid
- where t1.pastureid = ? `, patsureid, patsureid)
- data := make([]*lpplanList, 0)
- err := session.Find(&data)
- if err != nil {
- log.Println("getLpplanlist-error", err)
- return nil, err
- }
- return data, nil
- }
- // 添加车次计划
- func addLpplan(lpplan *lpplanStruct, tx *xorm.Session) error {
- _, err := tx.Table("lpplan").Insert(lpplan)
- if err != nil {
- log.Println("addLpplan-error", err)
- return err
- }
- return nil
- }
- //获取车次数量
- func getLpplanCount(tx *xorm.Session) (int64, error) {
- count, err := tx.Table("lpplan").Count()
- if err != nil {
- log.Println("getLpplanCount-error", err)
- return 0, err
- }
- return count, err
- }
- //获取班次信息
- func gettimes(pastureid string, tx *xorm.Session) (int, error) {
- session := tx.Table("sysopt")
- session.Where("pastureid = ? ", pastureid)
- session.Where("inforname = 'times' ")
- data := new(sysopt)
- _, err := session.Get(data)
- if err != nil {
- log.Println("gettimes-error", err)
- return 0, err
- }
- return data.Inforvalue, nil
- }
- func autogeneration(typeIN int64, pastureid string, tx *xorm.Session) error {
- tx.Begin()
- if typeIN == 1 {
- _, err := tx.SQL(`DELETE FROM lpplandtl1 WHERE pastureid = ?`, pastureid).Execute()
- if err != nil {
- log.Println("autogeneration-error-1", err)
- tx.Rollback()
- return err
- }
- _, err = tx.SQL(`update fpdetail
- set ptuse = 0,ptsuse=0
- WHERE pastureid = ?`, pastureid).Execute()
- if err != nil {
- log.Println("autogeneration-error-2", err)
- tx.Rollback()
- return err
- }
- }
- lpplansession := tx.SQL(`select
- t1.sort,t1.tmrid,t1.tmrname,t1.ftid,
- t1.pastureid,t1.id ,t1.maxweight,ifnull(t2.lweight,0) as lweight,t1.times from lpplan t1
- left join ( select ifnull(sum(lweight),0) as lweight,lppid from lpplandtl1 where pastureid =? GROUP BY lppid) t2 on t1.id = t2.lppid
- where t1.pastureid = ? and t1.maxweight > ifnull(t2.lweight,0) `, pastureid, pastureid)
- lpplanlist := make([]*lpplanQueryInfo, 0)
- err := lpplansession.Find(&lpplanlist)
- if err != nil {
- log.Println("autogeneration-error-3", err)
- tx.Rollback()
- return err
- }
- for _, lpplan := range lpplanlist {
- // fpdetailsession := tx.Table("fpdetail").Alias("fpd")
- // fpdetailsession.Select("fpd.barid,fpd.barname,fpd.id as fpdid ,fpd.weight as weight,ifnull(fpd.ptuse,0) as ptuse,ifnull(fpd.ptsuse,0) as ptsuse,fpd.barid,fpd.barname,fpd.cowcount,fpd.ccountradio,fpd.ptsrate as ptsrate")
- // fpdetailsession.Where(" fpd.ptid = ? ", lpplan.Ftid)
- // fpdetailsession.Where(" fpd.times = ? ", lpplan.Times)
- // fpdetailsession.Where("fpd.pastureid = ? ", pastureid)
- // fpdetailsession.GroupBy("fpd.id")
- fpdetaillist := make([]*fpdetailQueryInfo, 0)
- err = tx.SQL(`SELECT 0 as fttypeid,fpd.cowcount,fpd.ccountradio,fpd.id as fpdid,fpd.times,fpd.tratio,ROUND(fpd.ptsrate*fpd.weight,2)-fpd.ptsuse weight,
- TRIM(fpd.barid) barid,TRIM(fpd.pastureid) pastureid,TRIM(fpd.ptid) ptid,TRIM(fpd.ptsid ) ptsid ,
- (select bname from bar where pastureid =fpd.pastureid and id = fpd.barid ) barname,
- cowcount,ccountradio,1 isfill FROM fpdetail fpd
- join feedp fp on fp.ptsfid = fpd.ptsid and fp.pastureid = fpd.pastureid
- inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
- WHERE fpd.pastureid = ? AND ptuse<ROUND((1-fpd.ptsrate)*fpd.weight,2) and fpd.ptsid = ?
- and fpd.times = ?
- UNION
- SELECT 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 ,
- (select bname from bar where pastureid =fpd.pastureid and id = fpd.barid ) barname,cowcount,ccountradio,0 isfill FROM fpdetail fpd
- join feedp fp on fp.ftid = fpd.ptid and fp.pastureid = fpd.pastureid
- inner join feedtemplet ft on ft.id = fp.ftid and ft.pastureid = fpd.pastureid
- WHERE fpd.pastureid = ? AND ptuse<ROUND((1-fpd.ptsrate)*fpd.weight,2) and fpd.ptid = ?
- and fpd.times = ?`, pastureid, lpplan.Ftid, lpplan.Times, pastureid, lpplan.Ftid, lpplan.Times).Find(&fpdetaillist)
- // err = fpdetailsession
- if err != nil {
- log.Println("autogeneration-error-4", err)
- tx.Rollback()
- return err
- }
- for _, fpdetail := range fpdetaillist {
- // //剩余料
- var weight float64
- weight = fpdetail.Weight
- if lpplan.Maxweight <= lpplan.Lweight || weight < 0 {
- continue
- }
- var insertlweight float64
- if lpplan.Maxweight-lpplan.Lweight > weight {
- insertlweight = weight
- } else {
- insertlweight = lpplan.Maxweight - lpplan.Lweight
- }
- lpplan.Lweight += insertlweight
- lpplan.Lweighthis = insertlweight
- // fpdetail.Ptsuse += insertlweight
- _, 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=? `, fpdetail.Fttypeid, fpdetail.Weight, fpdetail.Weight, fpdetail.Fttypeid, fpdetail.Weight, fpdetail.Weight, pastureid, fpdetail.Fpdid).Execute()
- if err != nil {
- log.Println("autogeneration-error-5", err)
- tx.Rollback()
- return err
- }
- fpdetail.Weight = fpdetail.Weight - insertlweight
- lpplandtlExecute, err := tx.SQL(`select id from lpplandtl1 where pastureid= ?
- and lppid = ? and fpdid =? and fttype = ? `, pastureid, lpplan.Lppid, fpdetail.Fpdid, fpdetail.Fttypeid).Exist()
- if err != nil {
- log.Println("autogeneration-error-6", err)
- tx.Rollback()
- return err
- }
- if lpplandtlExecute {
- lpplandtl1query := new(lpplandtl1)
- query := tx.Table("lpplandtl1")
- query.Where("pastureid = ?", pastureid)
- query.Where("lppid = ?", lpplan.Lppid)
- query.Where("fpdid = ?", fpdetail.Fpdid)
- query.Where("fttype = 1")
- _, err = query.Get(lpplandtl1query)
- if err != nil {
- log.Println("autogeneration-error-7", err)
- tx.Rollback()
- return err
- }
- upLpplandtl1 := tx.Table("lpplandtl1")
- data := &lpplandtl1{
- Pastureid: lpplan.Pastureid,
- Lppid: lpplan.Lppid,
- Barid: fpdetail.Barid,
- Barname: fpdetail.Barname,
- Fpdid: fpdetail.Fpdid,
- Fttype: fpdetail.Fttypeid,
- Lweight: lpplandtl1query.Lweight + insertlweight,
- Sort: lpplan.Sort,
- Tmrid: lpplan.Tmrid,
- Tmrname: lpplan.Tmrname,
- Cowcount: fpdetail.Cowcount,
- Ccountradio: fpdetail.Ccountradio,
- // Background: lpplan.Background,
- Lweighthis: insertlweight,
- }
- upLpplandtl1.Where("pastureid = ?", pastureid)
- upLpplandtl1.Where("lppid = ?", lpplan.Lppid)
- upLpplandtl1.Where("fpdid = ?", fpdetail.Fpdid)
- upLpplandtl1.Where("fttype = 1")
- _, err = upLpplandtl1.Update(data)
- if err != nil {
- log.Println("autogeneration-error-8", err)
- tx.Rollback()
- return err
- }
- } else {
- data := &lpplandtl1{
- Pastureid: lpplan.Pastureid,
- Lppid: lpplan.Lppid,
- Barid: lpplan.Barid,
- Barname: lpplan.Barname,
- Fpdid: fpdetail.Fpdid,
- Fttype: fpdetail.Fttypeid,
- Lweight: insertlweight,
- Sort: lpplan.Sort,
- Tmrid: lpplan.Tmrid,
- Tmrname: lpplan.Tmrname,
- Cowcount: lpplan.Cowcount,
- Ccountradio: lpplan.Ccountradio,
- Background: lpplan.Background,
- Lweighthis: insertlweight,
- }
- addLpplandtl1 := tx.Table("lpplandtl1")
- _, err = addLpplandtl1.Insert(data)
- if err != nil {
- log.Println("autogeneration-error-9", err)
- tx.Rollback()
- return err
- }
- }
- }
- if typeIN == 1 {
- _, err = tx.SQL("update lpplan set sel = 1 where id = ?", lpplan.Lppid).Execute()
- if err != nil {
- log.Println("autogeneration-error-10", err)
- return err
- }
- }
- }
- err = tx.Commit()
- if err != nil {
- log.Println("autogeneration-error-11", err)
- return err
- }
- return nil
- }
|