|
@@ -7,6 +7,7 @@ import (
|
|
|
"fmt"
|
|
|
"io/ioutil"
|
|
|
"net/http"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
"tmr-watch/conf/setting"
|
|
|
"tmr-watch/http/handle/restful"
|
|
@@ -218,3 +219,256 @@ func feedPull(url, data string, msgtype int, tx *xorm.Session, pastureId, token
|
|
|
tx.Exec(` insert into saplog(pastureId,request,response,status,msgtext,createTime,msgtype,dataDate,url)
|
|
|
values(?,?,?,?,?,now(),?,now(),?)`, pastureId, data, string(body), "", "", msgtype, url)
|
|
|
}
|
|
|
+
|
|
|
+func YmyFeedtempletPush(pastureId, farmId, date string) error {
|
|
|
+ tx := restful.Engine.NewSession()
|
|
|
+ defer tx.Close()
|
|
|
+
|
|
|
+ var now string
|
|
|
+ if date != "" {
|
|
|
+ now = date
|
|
|
+ } else {
|
|
|
+ now = time.Now().AddDate(0, 0, -1).Format("2006-01-02")
|
|
|
+ }
|
|
|
+
|
|
|
+ dataList, err := tx.SQL(`SELECT
|
|
|
+ ft.tcode AS recipeId,
|
|
|
+ ft.TNAME AS recipeName,
|
|
|
+ f.feedcode AS ingId,
|
|
|
+ ifnull(f.udname,f.fname) AS ingName,
|
|
|
+ round( f.dry * ftd.FWEIGHT, 2 ) AS dmQty,
|
|
|
+ ftd.FWEIGHT AS afQty,
|
|
|
+ ftd.SORT AS mixNo,
|
|
|
+ round( f.Uprice * ftd.FWEIGHT, 2 ) AS recipeCost,
|
|
|
+ fc.fcname AS ingType,
|
|
|
+ f.AllowRatio AS allowableError ,fc.fcname ingType
|
|
|
+FROM
|
|
|
+ feedtempletdate ft
|
|
|
+ JOIN ftdetaildate ftd ON ft.id = ftd.FTID and ft.date = ftd.date
|
|
|
+ JOIN feed f ON ftd.FID = f.id
|
|
|
+ JOIN feedclass fc ON f.fclassid = fc.id
|
|
|
+WHERE
|
|
|
+ ft.ENABLE = 1
|
|
|
+ AND ft.isDelete != 1 and ft.date = ? `, now).Query().List()
|
|
|
+ if err != nil {
|
|
|
+ logging.Error(err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ dataByte, _ := json.Marshal(&dataList)
|
|
|
+
|
|
|
+ reqJson := `{
|
|
|
+ "reqMethod": "getfeedtempletinfo",
|
|
|
+ "platForm": "EYIMU",
|
|
|
+ "regCode": "123543",
|
|
|
+ "command ": "202cb962ac590",
|
|
|
+ "apiId": "getCptData ",
|
|
|
+ "param": {
|
|
|
+ "farmId": "%s",
|
|
|
+ "method": "getfeedtempletinfo ",
|
|
|
+ "code": "1",
|
|
|
+ "errMessage": "",
|
|
|
+ "rowCount": %d,
|
|
|
+ "resultData": %s
|
|
|
+ }
|
|
|
+}`
|
|
|
+ reqJson = fmt.Sprintf(reqJson, farmId, len(dataList), string(dataByte))
|
|
|
+ fmt.Println(reqJson)
|
|
|
+ postPush(reqJson, 4, tx, pastureId)
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+//搅拌
|
|
|
+func YmyUtirPush(pastureId, farmId, date string) error {
|
|
|
+ tx := restful.Engine.NewSession()
|
|
|
+ defer tx.Close()
|
|
|
+ var now string
|
|
|
+ if date != "" {
|
|
|
+ now = date
|
|
|
+ } else {
|
|
|
+ now = time.Now().AddDate(0, 0, -1).Format("2006-01-02")
|
|
|
+ }
|
|
|
+
|
|
|
+ dataList, err := tx.SQL(`select date_format(d1.date,'%Y-%m-%d') dropDate,date_format(d1.date,'%Y-%m-%d') loadDate,d.times loadShift,d.datacaptureno tmrNo,ft.tcode recipeId,ft.ccname recipeName,if(d1.feedcode = -1 ,d1.fid,f.feedcode) ingId,ifnull(f.fname,ftd.fname) ingName,
|
|
|
+ ifnull(ifnull(fc.fcname,(select fcname from feedclass fc1 join feed f1 on f1.fclassid = fc1.id where f1.id =ftd.fid )),'饲料') ingType,
|
|
|
+ ifnull(f.dry * d1.actualweightminus,0) dmPct,d1.sort mixNo,ifnull(d1.feedallowratio,0) allowableError , if(f.fname is not null, ifnull(d1.lweight,''), ifnull(ftd.fweight / (select sum(fweight) from ftdetaildate where date = d1.date and ftid = d1.fid ) * d1.lweight,"") ) expWeight,
|
|
|
+
|
|
|
+ if(f.fname is not null, ifnull(d1.actualweightminus,''), ifnull(ftd.fweight / (select sum(fweight) from ftdetaildate where date = d1.date and ftid = d1.fid ) * d1.actualweightminus,"") ) actualWeight,
|
|
|
+ DATE_FORMAT(IFNULL((SELECT MAX(intime) FROM downloadplandtl1 d2
|
|
|
+WHERE d1.pid=d2.pid AND d1.pastureid = d2.pastureid AND d2.intime<d1.intime)
|
|
|
+, (SELECT MAX(intime) FROM downloadedplan d2
|
|
|
+WHERE d1.date=d2.mydate AND d1.pastureid = d2.pastureid AND d2.id=d1.pid)
|
|
|
+), '%Y-%m-%d %H:%i:%s')
|
|
|
+ startTime ,
|
|
|
+ date_format(d1.intime,'%Y-%m-%d %H:%i:%s') endTime, ifnull(driver.drivername,'') tmrName ,d1.feedallowratio allowable_error,d.tmrtname equipmentId
|
|
|
+ from downloadplandtl1 d1 join downloadedplan d on d.id = d1.pid
|
|
|
+ left join feedtempletdate ft on ft.id = d.tempid and ft.date = d1.date
|
|
|
+ left join feed f on f.feedcode = d1.feedcode
|
|
|
+ left join feedclass fc on fc.id = f.fclassid
|
|
|
+ left join ftdetaildate ftd on ftd.date = d1.date and ftd.ftid = d1.fid
|
|
|
+ join tmr t on t.id = d.tmrid
|
|
|
+ left join driver on driver.drivercode = t.eqcode
|
|
|
+ where d1.date = ? and d.havebutton = 1 and d1.type = 0 `, now).Query().List()
|
|
|
+ if err != nil {
|
|
|
+ logging.Error(err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ dataByte, _ := json.Marshal(&dataList)
|
|
|
+
|
|
|
+ reqJson := `{
|
|
|
+ "reqMethod": "uploadadddata",
|
|
|
+ "platform": "EYIMU",
|
|
|
+ "regCode": "123543",
|
|
|
+ "command": "202cb962ac590",
|
|
|
+ "apiId": "getCptData",
|
|
|
+ "param": {
|
|
|
+ "farmId": '%s',
|
|
|
+ "method": "uploadadddata",
|
|
|
+ "code": "1",
|
|
|
+ "errMessage": "",
|
|
|
+ "rowCount": %d,
|
|
|
+ "resultData": %s
|
|
|
+ }
|
|
|
+ }`
|
|
|
+
|
|
|
+ reqJson = fmt.Sprintf(reqJson, farmId, len(dataList), string(dataByte))
|
|
|
+ postPush(reqJson, 0, tx, pastureId)
|
|
|
+ fmt.Println(reqJson)
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func YmyMaterialIssuePush(pastureId, farmId, date string) error {
|
|
|
+ tx := restful.Engine.NewSession()
|
|
|
+ defer tx.Close()
|
|
|
+
|
|
|
+ var now string
|
|
|
+ if date != "" {
|
|
|
+ now = date
|
|
|
+ } else {
|
|
|
+ now = time.Now().AddDate(0, 0, -1).Format("2006-01-02")
|
|
|
+ }
|
|
|
+
|
|
|
+ dataList, err := tx.SQL(`SELECT d1.sort feedSort, ifnull(f.feedcode,d1.fid) feedId,ifnull(ifnull(f.udname,f.fname),d1.fname) feedName,
|
|
|
+ d.id,IFNULL(ft.tcode,d2.feedtempletid ) recipeId,IFNULL(ft.tcode,d2.feedtempletid ) feedtempletId,ifnull(ft.tname,d.templetName) feedtempletName ,
|
|
|
+ IFNULL(d.templetName,d2.feedtempletName ) recipeName,
|
|
|
+ date_format(d.mydate,'%Y-%m-%d') as dropDate,ifnull((select sort from downloadedplan where pid=d.pid and lpplanType !=d.lpplanType and mydate=d.mydate),d.sort) as tmrNo,
|
|
|
+ d.Times as dropShift,ifnull(b.bcode,b.id) as penId,b.bname as penName,fp.CCOUNT as cowCount, d2.SORT as feedingNo,
|
|
|
+ifnull(ROUND(d2.lweight * (d1.actualweightminus/ (select sum(actualweightminus) from downloadplandtl1 where pid = d1.pid and type = 0) ),2) ,0)as expWeight,
|
|
|
+ ifnull(d2.ActualWeightMinus,0) as actualWeight, ifnull(date_format(timestamp(d2.InTime,CONCAT('-',d2.processTime)),'%Y-%m-%d %H:%i:%s'),d.mydate) as startTime,
|
|
|
+ ifnull(date_format(d2.InTime,'%Y-%m-%d %H:%i:%s'),d.mydate) as endTime,ifnull((SELECT dr.driver FROM dutyrecord dr
|
|
|
+WHERE dr.pastureid = d.pastureid AND dr.eqid = d.tmrid AND dr.times= d.times AND dr.operatetime <=d.mydate
|
|
|
+ORDER BY dr.operatetime DESC LIMIT 1),"") as tmrName ,d.tmrtname equipmentId ,
|
|
|
+d1.fname,
|
|
|
+ifnull(TRIM(if(d1.feedcode != '-1', if ( (SELECT SUM(actualweightminus) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date) > 0 ,
|
|
|
+d2.actualweightminus/(SELECT SUM(actualweightminus) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date)*
|
|
|
+d1.lweight*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetaildate ftd2 WHERE ftd2.date = d1.date AND ftd2.version = ftd.version AND ftd2.ftid = d1.fid )),
|
|
|
+
|
|
|
+ d2.lweight/(SELECT SUM(lweight) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date)*
|
|
|
+d1.lweight*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetaildate ftd2 WHERE ftd2.date = d1.date AND ftd2.version = ftd.version AND ftd2.ftid = d1.fid ))
|
|
|
+ ),'')),0) feedWeight
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ ,ifnull(ftd.sort,"") premixFeedSort ,
|
|
|
+ ifnull((select feedcode from feed where id = ftd.fid),"") premixFeedId,ifnull(ftd.fname,"") premixFeedName,
|
|
|
+
|
|
|
+
|
|
|
+ ifnull(if(d1.feedcode = '-1', if ( (SELECT SUM(actualweightminus) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date) > 0 ,
|
|
|
+ d2.actualweightminus/(SELECT SUM(actualweightminus) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date)*
|
|
|
+ d1.lweight*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetaildate ftd2 WHERE ftd2.date = d1.date AND ftd2.version = ftd.version AND ftd2.ftid = d1.fid )),
|
|
|
+
|
|
|
+ d2.lweight/(SELECT SUM(lweight) FROM downloadplandtl2 d2t WHERE d2t.pastureid = d2.pastureid AND d2t.flpid = d2.flpid AND d2t.type = 0 AND d2t.date = d2.date)*
|
|
|
+ d1.lweight*IF(ftd.id IS NULL,1,ftd.fweight/(SELECT SUM(ftd2.fweight) FROM ftdetaildate ftd2 WHERE ftd2.date = d1.date AND ftd2.version = ftd.version AND ftd2.ftid = d1.fid ))
|
|
|
+ ),''),'0')
|
|
|
+ premixFeedWeight
|
|
|
+
|
|
|
+ from downloadedplan d
|
|
|
+ LEFT JOIN downloadplandtl2 d2 on d.id=d2.PID
|
|
|
+ LEFT JOIN (select dd.pid as lppid,dd1.* from downloadedplan dd join downloadplandtl1 dd1 on dd1.pid = dd.id and dd1.type = 0
|
|
|
+ where dd.mydate= date_format(?,'%Y-%m-%d') and dd.lpplanType in (0,1) ) d1 on d1.lppid = d.pid
|
|
|
+
|
|
|
+ LEFT JOIN feedp fp on d2.FBarID=fp.barid
|
|
|
+ join bar b on fp.barid = b.id and d.pastureid = b.pastureid
|
|
|
+ left join feed f on f.feedcode = d1.feedcode
|
|
|
+ left JOIN feedtempletdate ft on d.tempid=ft.id and ft.date = d1.date and d.tversion = ft.version
|
|
|
+ left join feedtempletdate ft1 on d1.feedcode = '-1' and ft1.date = d1.date and ft1.id = d1.fid
|
|
|
+ left join ftdetaildate ftd on ftd.date = d1.date and ftd.ftid = d1.fid AND d1.feedcode = '-1' and ftd.version = ft1.version
|
|
|
+
|
|
|
+ where d.mydate= date_format(?,'%Y-%m-%d') and d2.type = 0 and d.havebutton = 1 and d.pastureid = ? and d1.fname is not null order by tmrno `, now, now, pastureId).Query().List()
|
|
|
+ if err != nil {
|
|
|
+ logging.Error(err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ dataByte, _ := json.Marshal(&dataList)
|
|
|
+
|
|
|
+ reqJson := `{
|
|
|
+ "reqMethod": "uploaddiliverdata",
|
|
|
+ "platForm": "EYIMU",
|
|
|
+ "regCode": "123543",
|
|
|
+ "command": "202cb962ac590",
|
|
|
+ "apiId": "uploaddiliverdata",
|
|
|
+ "param": {
|
|
|
+ "farmId": "%s",
|
|
|
+ "method": "getCptData",
|
|
|
+ "code": "1",
|
|
|
+ "errMessage": "",
|
|
|
+ "rowCount": %d,
|
|
|
+ "resultData": %s
|
|
|
+ }
|
|
|
+ }`
|
|
|
+ reqJson = fmt.Sprintf(reqJson, farmId, len(dataList), string(dataByte))
|
|
|
+ // '0饲喂 1剩料 2原料 3栏舍 4配方 5栏舍牛头数 6库存',
|
|
|
+ postPush(reqJson, 0, tx, pastureId)
|
|
|
+ fmt.Println(reqJson)
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func postPush(data string, msgtype int, tx *xorm.Session, pastureId string) {
|
|
|
+
|
|
|
+ // forwardingReq := new(ForwardingReq)
|
|
|
+ // forwardingReq.Url = "https://fnhbdairyfarmlive.fn.com.my:8380/copartner_uploads/"
|
|
|
+ // forwardingReq.Method = "POST"
|
|
|
+ // forwardingReq.Body = data
|
|
|
+ // jsonStr, _ := json.Marshal(forwardingReq)
|
|
|
+ // fmt.Println(setting.ServerSetting.UDForwardingSvc)
|
|
|
+ // req, err := http.NewRequest("POST", setting.ServerSetting.UDForwardingSvc+":8080/forwarding", bytes.NewBuffer(jsonStr))
|
|
|
+ req, err := http.NewRequest("POST", "http://dapp-web.yimucloud.com:8080/webapps/api.html", bytes.NewBuffer([]byte(data)))
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ // return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ req.Header.Set("Content-Type", "application/json")
|
|
|
+ tr := &http.Transport{
|
|
|
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
+ }
|
|
|
+ client := &http.Client{Transport: tr}
|
|
|
+
|
|
|
+ resp, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ body, _ := ioutil.ReadAll(resp.Body)
|
|
|
+ fmt.Println(string(body))
|
|
|
+
|
|
|
+ if strings.Index(resp.Status, "200") > -1 {
|
|
|
+ bodyData := make(map[string]string)
|
|
|
+ json.Unmarshal(body, &bodyData)
|
|
|
+ if _, ok := bodyData["data"]; ok {
|
|
|
+ tx.Exec(` insert into saplog(pastureId,request,response,status,msgtext,createTime,msgtype,dataDate,url)
|
|
|
+ values(?,?,?,?,?,now(),?,now(),?)`, pastureId, data, bodyData["data"], "", "", msgtype, forwardingReq.Url)
|
|
|
+ } else {
|
|
|
+ tx.Exec(` insert into saplog(pastureId,request,response,status,msgtext,createTime,msgtype,dataDate,url)
|
|
|
+ values(?,?,?,?,?,now(),?,now(),?)`, pastureId, data, string(body), "", "", msgtype, forwardingReq.Url)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ tx.Exec(` insert into saplog(pastureId,request,response,status,msgtext,createTime,msgtype,dataDate,url)
|
|
|
+ values(?,?,?,?,?,now(),?,now(),?)`, pastureId, data, string(body), "", "", msgtype, forwardingReq.Url)
|
|
|
+ }
|
|
|
+
|
|
|
+}
|