| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- package ymy
- import (
- "bytes"
- "crypto/tls"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "net/http"
- "time"
- "tmr-watch/conf/setting"
- "tmr-watch/http/handle/restful"
- "tmr-watch/models"
- "tmr-watch/pkg/logging"
- "github.com/astaxie/beego/logs"
- "github.com/xormplus/xorm"
- )
- func YmyCron() {
- // pastureinfo := new(udPastureInfo)
- // err := tx.SQL(`select column_default as pastureid from information_schema.COLUMNS
- // WHERE table_name = 'recweight' AND table_schema = ? AND column_name = 'pastureid'`, setting.DatabaseSetting.Name).GetFirst(pastureinfo).Error
- // if err != nil {
- // logs.Error(err)
- // return err
- // }
- }
- func YmySyncBar(farmId, pastureId string) {
- tx := restful.Engine.NewSession()
- defer tx.Close()
- reqJson := `{
- "methodKey": "getFeedPenList",
- "param": {
- "farmId": "%s",
- "parlorType": "CPT"
- },
- "platform": "CPT",
- "regCode": "cpt180511",
- "secret": "67a74fd5748054ac67b6fc433e9249b0"}`
- barPull("http://dairy.yimucloud.com/dairy/external/api", fmt.Sprintf(reqJson, farmId), 3, tx, pastureId)
- }
- func barPull(url, data string, msgtype int, tx *xorm.Session, pastureId string) {
- req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data)))
- if err != nil {
- logs.Error(err)
- }
- req.Header.Set("Content-Type", "application/json")
- // client := &http.Client{}
- 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)
- respData := new(models.BarResponse)
- json.Unmarshal(body, &respData)
- tx.Exec(` insert into saplog(pastureId,request,response,status,msgtext,createTime,msgtype,dataDate,url)
- values(?,?,?,?,?,now(),?,now(),?)`, pastureId, data, string(body), "", "", msgtype, url)
- for _, v := range respData.Result {
- count, _ := tx.Table("bar").Where("pastureId = ? ", pastureId).Count()
- tx.Exec(`insert into bar(pastureid,bcode,bname,sort)values(?,?,?,?) ON DUPLICATE KEY UPDATE bname = ? `, pastureId, v.PenId, v.PenName, count+1, v.PenName)
- }
- }
- func YmySyncFeed(farmId, pastureId string) {
- tx := restful.Engine.NewSession()
- defer tx.Close()
- reqJson := `{
- "data":{
- "page": "0",
- "size": "1000",
- "modifiedAfter":"2025-01-01 00:00:00",
- "MATERIAL_BASIC_CLASSIFICATION_CODE":"",
- "MATERIAL_CODE": ""
- }}`
- // reqJson = fmt.Sprintf(reqJson, time.Now().Format("2006-01-02 15:04:05"))
- feedPull("http://113.250.182.238:9000/ierp/kapi/v2/iscb/route/script_cusapi_deepexi_material_query", reqJson, 2, tx, pastureId, getApiToken())
- }
- type udPastureInfo struct {
- Token string `xorm:"token"`
- Pastureid string `xorm:"pastureid"`
- Werks string `xorm:"werks"`
- UpdateDate string `xorm:"updatedate"`
- }
- func getApiToken() string {
- tx := restful.Engine.NewSession()
- defer tx.Close()
- pastureinfo := new(udPastureInfo)
- err := tx.SQL(`SELECT (SELECT pastureid FROM pasture WHERE pastureid = column_default) pastureid,
- IF( TIMESTAMPDIFF(MINUTE,
- (SELECT updatedate FROM pasture WHERE pastureid = column_default),
- NOW()
- ) > 60, -- 110分钟 = 1小时50分钟
- '', -- 超过时间返回空字符串
- (SELECT token FROM pasture WHERE pastureid = column_default) -- 否则返回 pastureid
- ) AS token,
- (SELECT werks FROM pasture WHERE pastureid = column_default) AS werks,
- DATE_FORMAT(
- (SELECT updatedate FROM pasture WHERE pastureid = column_default),
- '%Y-%m-%d %H:%i:%s'
- ) AS updatedate
- FROM information_schema.COLUMNS
- WHERE table_name = 'recweight'
- AND table_schema = ?
- AND column_name = 'pastureid' `, setting.DatabaseSetting.Name).GetFirst(pastureinfo).Error
- if err != nil {
- logs.Error(err)
- return pastureinfo.Token
- }
- if pastureinfo.Token != "" {
- return pastureinfo.Token
- }
- reqJson := `{
- "client_id": "Cosmic_deepexi_user",
- "client_secret": "Cosmic_zkm_deepexi_openapi@user01",
- "username": "dipujiekouyonghu",
- "accountId": "2174058657413595136",
- "nonce": "a42c1a91-56ad-4c6d-95d1-40bcb8b5ec89",
- "timestamp": "%s",
- "language": "zh_CN"
- }`
- reqJson = fmt.Sprintf(reqJson, time.Now().Format("2006-01-02 15:04:05"))
- req, err := http.NewRequest("POST", "http://113.250.182.238:9000/ierp/kapi/oauth2/getToken", bytes.NewBuffer([]byte(reqJson)))
- if err != nil {
- logs.Error(err)
- }
- 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 pastureinfo.Token
- }
- defer resp.Body.Close()
- body, _ := ioutil.ReadAll(resp.Body)
- fmt.Println(string(body))
- token := new(models.TokenResponse)
- json.Unmarshal(body, &token)
- tx.Exec(" update pasture set token = ?,updatedate = now() where pastureid = ? ", token.Data.AccessToken, pastureinfo.Pastureid)
- return token.Data.AccessToken
- }
- func feedPull(url, data string, msgtype int, tx *xorm.Session, pastureId, token string) {
- req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data)))
- if err != nil {
- logs.Error(err)
- }
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("access_token", token)
- client := &http.Client{}
- 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))
- feedData := new(models.FeedResponse)
- json.Unmarshal(body, &feedData)
- for _, v := range feedData.Data.Content {
- feedname := v.MaterialName
- feedcode := v.MaterialCode
- feedClassExist, err := tx.Table("feedclass").Where("fccode = ? ", v.MaterialClassification).Where(" pastureId = ? ", pastureId).Exist()
- fmt.Println(err)
- if !feedClassExist {
- count, _ := tx.Table("feedclass").Where("pastureId = ? ", pastureId).Count()
- ids, err := setting.SnowIds.NextId()
- if err != nil {
- ids = time.Now().UnixNano()
- }
- _, err = tx.Exec(`insert into feedclass(id,pastureId,fcname,fccode,bigfeedclassname,sort,bigfeedclassid) values(?,?,?,?,?,?,?)`,
- ids, pastureId, v.MaterialClassification, v.MaterialClassification, v.MaterialClassification, count+1, ids)
- fmt.Println(err)
- }
- ids, err := setting.SnowIds.NextId()
- if err != nil {
- ids = time.Now().UnixNano()
- logging.Info("create SnowIds err", err)
- }
- tx.Exec(`insert into feed(id,pastureId,feedcode,udname,fclassid,fclass,fname) values(?,?,?,?,(select id from feedclass where fccode = ? and pastureId = ? ),?,if(char_length(?) > 7, left(?,7),?) )
- ON DUPLICATE KEY UPDATE udname = ? ,fclassid = (select id from feedclass where fccode = ? and pastureId = ? ) ,fclass = ?`,
- ids, pastureId, feedcode, feedname, v.MaterialClassification, pastureId, v.MaterialClassification, feedname, feedname, feedname, feedname,
- v.MaterialClassification, pastureId, v.MaterialClassification)
- }
- tx.Exec(` insert into saplog(pastureId,request,response,status,msgtext,createTime,msgtype,dataDate,url)
- values(?,?,?,?,?,now(),?,now(),?)`, pastureId, data, string(body), "", "", msgtype, url)
- }
|