|
|
@@ -0,0 +1,907 @@
|
|
|
+package api
|
|
|
+
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "context"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "io/ioutil"
|
|
|
+ "log"
|
|
|
+ "net/http"
|
|
|
+ "os"
|
|
|
+ "reflect"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "../../pkg/app"
|
|
|
+ "../../pkg/e"
|
|
|
+ "../../pkg/logging"
|
|
|
+ "../../pkg/setting"
|
|
|
+ "../../routers/restful"
|
|
|
+ "github.com/Anderson-Lu/gofasion/gofasion"
|
|
|
+ "github.com/astaxie/beego/logs"
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "github.com/robfig/cron"
|
|
|
+ "github.com/xormplus/xorm"
|
|
|
+)
|
|
|
+
|
|
|
+func CronScheduled(ctx context.Context) {
|
|
|
+ tx := restful.Engine.NewSession()
|
|
|
+ defer tx.Close()
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+ duetimecst, _ := time.ParseInLocation("15:04:05", "00:01:00", time.Local)
|
|
|
+ duetimecst1, _ := time.ParseInLocation("15:04:05", "00:00:00", time.Local)
|
|
|
+ spec := fmt.Sprintf("@every %v", duetimecst.Sub(duetimecst1))
|
|
|
+
|
|
|
+ c := cron.New()
|
|
|
+ c.AddFunc(spec, func() {
|
|
|
+ tx1 := restful.Engine.NewSession()
|
|
|
+ defer tx1.Close()
|
|
|
+ exist, err := tx1.Table("notice").Where("status = 1").And("content = ? ", "downloadedplan_log").Exist()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error("CronScheduled-error-1:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Println(exist)
|
|
|
+ if exist {
|
|
|
+ _, err := tx.SQL(`update notice set status = 0 where content = ? `, "downloadedplan_log").Execute()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error("CronScheduled-error-2:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ Scheduled(ctx, tx1, pastureinfo)
|
|
|
+ }
|
|
|
+
|
|
|
+ })
|
|
|
+ c.Start()
|
|
|
+
|
|
|
+ // duetimecst2, _ := time.ParseInLocation("15:04:05", "00:01:01", time.Local)
|
|
|
+ // duetimecst3, _ := time.ParseInLocation("15:04:05", "00:00:00", time.Local)
|
|
|
+ // spec1 := fmt.Sprintf("@every %v", duetimecst2.Sub(duetimecst3))
|
|
|
+
|
|
|
+ // downloadplandtl1 := cron.New()
|
|
|
+ // downloadplandtl1.AddFunc(spec1, func() {
|
|
|
+ // tx2 := restful.Engine.NewSession()
|
|
|
+ // defer tx2.Close()
|
|
|
+
|
|
|
+ // exist, err := tx2.Table("notice").Where("status = 1").And("content = ? ", "downloadplandtl1").Exist()
|
|
|
+ // if err != nil {
|
|
|
+ // logs.Error("CronScheduled-error-4:", err)
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ // fmt.Println(exist)
|
|
|
+ // if exist {
|
|
|
+ // _, err := tx2.SQL(`update notice set status = 0 where content = ? `, "downloadplandtl1").Execute()
|
|
|
+ // if err != nil {
|
|
|
+ // logs.Error("CronScheduled-error-5:", err)
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+
|
|
|
+ // dataList, err := tx2.SQL("select * from downloadplandtl1_log where date = date_format(now(),'%Y-%m-%d') ").Query().List()
|
|
|
+ // if err != nil {
|
|
|
+ // logs.Error("CronScheduled-error-6:", err)
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ // plandtl1SlIdMap := make(map[string][]int64, 0)
|
|
|
+ // // plandtl1HlIdMap := make(map[string][]string, 0)
|
|
|
+ // for _, data := range dataList {
|
|
|
+ // if data["type"].(int64) == 0 && data["plandtl1"] != nil {
|
|
|
+ // plandtl1SlIdMap["planid"] = append(plandtl1SlIdMap["planid"], data["plandtl1"].(int64))
|
|
|
+ // }
|
|
|
+
|
|
|
+ // }
|
|
|
+
|
|
|
+ // for _, data := range dataList {
|
|
|
+ // if data["type"].(int64) == 0 {
|
|
|
+ // plandtl1List, err := tx2.SQL(`select d1.fname,d.tmrtname,d1.intime,d1.id,d1.feedallowratio,abs(d1.actualweightminus -d1.lweight ) errorvalue ,d1.actualweightminus,d1.lweight,
|
|
|
+ // if(d1.actualweightminus <= d1.lweight,ROUND(d1.actualweightminus/d1.lweight* 100,2),ROUND(d1.lweight/d1.actualweightminus* 100,2) ) as accuracy from
|
|
|
+ // downloadplandtl1 d1
|
|
|
+ // join downloadedplan d on d.id = d1.pid
|
|
|
+ // where d1.pid = ? and d1.sort = ? and d1.pastureid = ? and d1.intime is not null and d1.type = 0 `,
|
|
|
+ // data["planid"], data["sort"], data["pastureid"]).Query().List()
|
|
|
+
|
|
|
+ // if err != nil {
|
|
|
+ // logs.Error("CronScheduled-error-7:", err)
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ // idexist := false
|
|
|
+ // for _, plandtl1 := range plandtl1List {
|
|
|
+ // //防止同顺序饲料多次推送
|
|
|
+ // for _, plandtl1Id := range plandtl1SlIdMap["planid"] {
|
|
|
+ // if plandtl1Id == plandtl1["id"].(int64) {
|
|
|
+ // idexist = true
|
|
|
+ // break
|
|
|
+ // }
|
|
|
+
|
|
|
+ // }
|
|
|
+ // if idexist {
|
|
|
+ // continue
|
|
|
+ // }
|
|
|
+
|
|
|
+ // errorvalue, _ := strconv.ParseFloat(plandtl1["errorvalue"].(string), 64)
|
|
|
+ // if plandtl1["feedallowratio"].(float64) < errorvalue {
|
|
|
+ // // if plandtl1["feedallowratio"].(float64) < plandtl1["errorvalue"].(float64) {
|
|
|
+ // fmt.Println(plandtl1, "推送")
|
|
|
+ // target := []string{"13916925233"}
|
|
|
+ // var sendList []interface{}
|
|
|
+ // sendStr := fmt.Sprintf("tmr名称:%v\n饲料名称:%v\n计划重量(kg):%v\n实际重量(kg):%v\n误差值(kg):%v\n准确率:%v",
|
|
|
+ // plandtl1["tmrtname"], plandtl1["fname"], plandtl1["lweight"], plandtl1["actualweightminus"], plandtl1["errorvalue"], plandtl1["accuracy"])
|
|
|
+ // // content = append(content, "混料", plandtl1["begintime"], "kpt", sendStr, "搅拌预警")
|
|
|
+
|
|
|
+ // sendMap1 := make(map[string]interface{}, 0)
|
|
|
+ // sendMap1["value"] = "搅拌"
|
|
|
+ // sendMap1["color"] = "#173177"
|
|
|
+ // sendList = append(sendList, sendMap1)
|
|
|
+ // sendMap2 := make(map[string]interface{}, 0)
|
|
|
+ // sendMap2["value"] = plandtl1["intime"]
|
|
|
+ // sendMap2["color"] = "#173177"
|
|
|
+ // sendList = append(sendList, sendMap2)
|
|
|
+ // sendMap3 := make(map[string]interface{}, 0)
|
|
|
+ // sendMap3["value"] = "kpt"
|
|
|
+ // sendMap3["color"] = "#173177"
|
|
|
+ // sendList = append(sendList, sendMap3)
|
|
|
+ // sendMap4 := make(map[string]interface{}, 0)
|
|
|
+ // sendMap4["value"] = sendStr
|
|
|
+ // sendMap4["color"] = "#173177"
|
|
|
+ // sendList = append(sendList, sendMap4)
|
|
|
+
|
|
|
+ // sendMap5 := make(map[string]interface{}, 0)
|
|
|
+ // sendMap5["value"] = "搅拌预警"
|
|
|
+ // sendMap5["color"] = "#173177"
|
|
|
+ // sendList = append(sendList, sendMap5)
|
|
|
+ // wxPush(target, sendList)
|
|
|
+ // }
|
|
|
+
|
|
|
+ // plandtl1SlIdMap["planid"] = append(plandtl1SlIdMap["planid"], plandtl1["id"].(int64))
|
|
|
+ // _, err := tx2.SQL(" update downloadplandtl1_log set plandtl1 = ? where id = ? and type = 0 ", plandtl1["id"], data["id"]).Execute()
|
|
|
+ // if err != nil {
|
|
|
+ // logs.Error("CronScheduled-error-8:", err)
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+
|
|
|
+ // }
|
|
|
+ // } else {
|
|
|
+ // plandtl1List, err := tx2.SQL(`select d2.fname,d.tmrtname,d2.id,ifnull(d2.allowratio,0) allowratio ,abs(d2.actualweightminus -d2.lweight ) errorvalue ,d2.intime,d2.actualweightminus,d2.lweight,
|
|
|
+ // if(d2.actualweightminus <= d2.lweight,ROUND(d2.actualweightminus/d2.lweight* 100,2),ROUND(d2.lweight/d2.actualweightminus* 100,2) ) as accuracy
|
|
|
+ // from downloadplandtl2 d2
|
|
|
+ // join downloadedplan d on d.id = d2.pid
|
|
|
+ // where d2.pid = ? and d2.sort = ? and d2.pastureid = ? and d2.intime is not null and d2.type = 0 `,
|
|
|
+ // data["planid"], data["sort"], data["pastureid"]).Query().List()
|
|
|
+ // // Where(" pid = ? ", data["planid"]).And("sort = ? ", data["sort"]).And(" pastureid = ? ", data["pastureid"]).And(" intime is not null").And("type = 0 ").Query().List()
|
|
|
+ // if err != nil {
|
|
|
+ // logs.Error("CronScheduled-error-7:", err)
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ // for _, plandtl1 := range plandtl1List {
|
|
|
+ // errorvalue, _ := strconv.ParseFloat(plandtl1["errorvalue"].(string), 64)
|
|
|
+ // if float64(plandtl1["allowratio"].(int64)) < errorvalue && float64(plandtl1["allowratio"].(int64)) != 0 {
|
|
|
+ // fmt.Println(plandtl1, "推送")
|
|
|
+ // target := []string{"13916925233"}
|
|
|
+ // // var content []interface{}
|
|
|
+
|
|
|
+ // var sendList []interface{}
|
|
|
+ // sendStr := fmt.Sprintf("tmr名称:%v\n栏舍名称:%v\n计划重量(kg):%v\n实际重量(kg):%v\n误差值(kg):%v\n准确率:%v",
|
|
|
+ // plandtl1["tmrtname"], plandtl1["fname"], plandtl1["lweight"], plandtl1["actualweightminus"], plandtl1["errorvalue"], plandtl1["accuracy"])
|
|
|
+ // sendMap1 := make(map[string]interface{}, 0)
|
|
|
+ // sendMap1["value"] = "撒料"
|
|
|
+ // sendMap1["color"] = "#173177"
|
|
|
+ // sendList = append(sendList, sendMap1)
|
|
|
+ // sendMap2 := make(map[string]interface{}, 0)
|
|
|
+ // sendMap2["value"] = plandtl1["intime"]
|
|
|
+ // sendMap2["color"] = "#173177"
|
|
|
+ // sendList = append(sendList, sendMap2)
|
|
|
+ // sendMap3 := make(map[string]interface{}, 0)
|
|
|
+ // sendMap3["value"] = "kpt"
|
|
|
+ // sendMap3["color"] = "#173177"
|
|
|
+ // sendList = append(sendList, sendMap3)
|
|
|
+ // sendMap4 := make(map[string]interface{}, 0)
|
|
|
+ // sendMap4["value"] = sendStr
|
|
|
+ // sendMap4["color"] = "#173177"
|
|
|
+ // sendList = append(sendList, sendMap4)
|
|
|
+
|
|
|
+ // sendMap5 := make(map[string]interface{}, 0)
|
|
|
+ // sendMap5["value"] = "撒料预警"
|
|
|
+ // sendMap5["color"] = "#173177"
|
|
|
+ // sendList = append(sendList, sendMap5)
|
|
|
+
|
|
|
+ // wxPush(target, sendList)
|
|
|
+ // }
|
|
|
+
|
|
|
+ // plandtl1SlIdMap["planid"] = append(plandtl1SlIdMap["planid"], plandtl1["id"].(int64))
|
|
|
+ // _, err := tx2.SQL(" update downloadplandtl1_log set plandtl1 = ? where id = ? and type = 1 ", plandtl1["id"], data["id"]).Execute()
|
|
|
+ // if err != nil {
|
|
|
+ // logs.Error("CronScheduled-error-8:", err)
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+
|
|
|
+ // downloadplandtl1.Start()
|
|
|
+}
|
|
|
+
|
|
|
+type ScheduledInfo struct {
|
|
|
+ Id int64 `xorm:"id"`
|
|
|
+ Sname string `xorm:"sname"`
|
|
|
+ Action int64 `xorm:"action"`
|
|
|
+ Childid int64 `xorm:"childid"`
|
|
|
+ Times string `xorm:"times"`
|
|
|
+ Enable string `xorm:"enable"`
|
|
|
+}
|
|
|
+type ScheduledUpInfo struct {
|
|
|
+ Id int64 `xorm:"id"`
|
|
|
+ Company string `xorm:"company"`
|
|
|
+ Addres string `xorm:"addres"`
|
|
|
+ Datatype int64 `xorm:"datatype"`
|
|
|
+ Package string `xorm:"Package"`
|
|
|
+ Datasql string `xorm:"datasql"`
|
|
|
+ Automatic int64 `xorm:"automatic"`
|
|
|
+ Manual int64 `xorm:"manual"`
|
|
|
+ Targetdata string `xorm:"targetdata"`
|
|
|
+}
|
|
|
+
|
|
|
+type ScheduledDownInfo struct {
|
|
|
+ Id int64 `xorm:"id"`
|
|
|
+ Datatype int64 `xorm:"datatype"`
|
|
|
+ Addres string `xorm:"addres"`
|
|
|
+ Adressparam string `xorm:"adressparam"`
|
|
|
+ Targetdata string `xorm:"targetdata"`
|
|
|
+ Manual int64 `xorm:"manual"`
|
|
|
+ Methods string `xorm:"methods"`
|
|
|
+}
|
|
|
+
|
|
|
+type ScheduledDownChildInfo struct {
|
|
|
+ Id int64 `xorm:"id"`
|
|
|
+ Parentid int64 `xorm:"parentid"`
|
|
|
+ Fieldname string `xorm:"fieldname"`
|
|
|
+ Checksql string `xorm:"checksql"`
|
|
|
+ Dosql string `xorm:"dosql"`
|
|
|
+}
|
|
|
+
|
|
|
+func Scheduled(ctx context.Context, tx *xorm.Session, pastureinfo *udPastureInfo) {
|
|
|
+ times := new(ScheduledInfo)
|
|
|
+ err := tx.SQL(" select times from scheduled where action = 0 group by times").GetFirst(times).Error
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if times.Times == "0" {
|
|
|
+ downloadedplanLogList, err := tx.SQL(` select id from downloadedplan_log where date = date_format(now(),'%Y-%m-%d') `).Query().List()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ids := []string{}
|
|
|
+ for _, item := range downloadedplanLogList {
|
|
|
+ ids = append(ids, strconv.FormatInt(item["id"].(int64), 10))
|
|
|
+ }
|
|
|
+ fmt.Println(ids, time.Now())
|
|
|
+ err = UpdatePush(ctx, tx, true, ids, pastureinfo, "")
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if times.Times == "1" {
|
|
|
+ downloadedplanLogList, err := tx.SQL(` select times from downloadedplan_log where date = date_format(now(),'%Y-%m-%d') and status = 0 group by times`).Query().List()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, item := range downloadedplanLogList {
|
|
|
+ count, err := tx.SQL(` select count(1) from downloadedplan where pastureid = ? and mydate = date_format(now(),'%Y-%m-%d')
|
|
|
+ and iscompleted = 0 and times = ? and enable = 1 `,
|
|
|
+ pastureinfo.Pastureid, item["times"]).Count()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if count == 0 {
|
|
|
+ //推送
|
|
|
+ downloadedplanLogList, err := tx.SQL(` select id from downloadedplan_log where date = date_format(now(),'%Y-%m-%d')
|
|
|
+ and times = ? `, item["times"]).Query().List()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ids := []string{}
|
|
|
+ for _, item := range downloadedplanLogList {
|
|
|
+ ids = append(ids, strconv.FormatInt(item["id"].(int64), 10))
|
|
|
+ }
|
|
|
+ err = UpdatePush(ctx, tx, true, ids, pastureinfo, "")
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if times.Times == "2" {
|
|
|
+ count, err := tx.SQL(" select count(1) from downloadedplan where pastureid = ? and mydate = date_format(now(),'%Y-%m-%d') and iscompleted = 0 and enable = 1 ",
|
|
|
+ pastureinfo.Pastureid).Count()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if count == 0 {
|
|
|
+ //推送
|
|
|
+ downloadedplanLogList, err := tx.SQL(` select id from downloadedplan_log where date = date_format(now(),'%Y-%m-%d') `).Query().List()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ids := []string{}
|
|
|
+ for _, item := range downloadedplanLogList {
|
|
|
+ ids = append(ids, strconv.FormatInt(item["id"].(int64), 10))
|
|
|
+ }
|
|
|
+ err = UpdatePush(ctx, tx, true, ids, pastureinfo, "")
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func UpdateScheduledStatus(c *gin.Context) {
|
|
|
+ appG := app.Gin{C: c}
|
|
|
+ dataByte, _ := ioutil.ReadAll(c.Request.Body)
|
|
|
+ fsion := gofasion.NewFasion(string(dataByte))
|
|
|
+
|
|
|
+ idList := fsion.Get("id").Array()
|
|
|
+ automatic := fsion.Get("automatic").ValueStr()
|
|
|
+ manual := fsion.Get("manual").ValueStr()
|
|
|
+ company := fsion.Get("company").ValueStr()
|
|
|
+ //type 0 上传,1 下载
|
|
|
+ typee := fsion.Get("type").ValueStr()
|
|
|
+
|
|
|
+ ids := []string{}
|
|
|
+ for _, item := range idList {
|
|
|
+ ids = append(ids, item.ValueStr())
|
|
|
+ }
|
|
|
+
|
|
|
+ tx := restful.Engine.NewSession()
|
|
|
+ defer tx.Close()
|
|
|
+ tx.Begin()
|
|
|
+
|
|
|
+ sqlstr := ""
|
|
|
+ sqlstr1 := ""
|
|
|
+ // var args []interface{}
|
|
|
+ if typee == "0" {
|
|
|
+ if len(ids) > 0 {
|
|
|
+ sqlstr = `update scheduled s join scheduled_up su on su.id = s.childid set `
|
|
|
+ sqlstr1 = `update scheduled s join scheduled_up su on su.id = s.childid set `
|
|
|
+ if automatic != "" {
|
|
|
+ sqlstr += " su.automatic = 1 "
|
|
|
+ sqlstr1 += " su.automatic = 0 "
|
|
|
+ } else if manual != "" {
|
|
|
+ sqlstr += " su.manual = 1 "
|
|
|
+ sqlstr1 += " su.manual = 0 "
|
|
|
+ }
|
|
|
+ id := strings.Join(ids, ",")
|
|
|
+ sqlstr += fmt.Sprintf(" where s.id in (%s) and s.action = 0 and su.company = '%s' ", id, company)
|
|
|
+ sqlstr1 += fmt.Sprintf(" where s.id not in (%s) and s.action = 0 and su.company ='%s' ", id, company)
|
|
|
+ } else {
|
|
|
+ sqlstr = `update scheduled s join scheduled_up su on su.id = s.childid set `
|
|
|
+ if automatic != "" {
|
|
|
+ sqlstr += " su.automatic = 0 "
|
|
|
+ } else if manual != "" {
|
|
|
+ sqlstr += " su.manual = 0 "
|
|
|
+ }
|
|
|
+ sqlstr += fmt.Sprintf(" where s.action = 0 and su.company = '%s' ", company)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if len(ids) > 0 {
|
|
|
+ sqlstr = `update scheduled s join scheduled_down sd on sd.id = s.childid set `
|
|
|
+ sqlstr1 = `update scheduled s join scheduled_down sd on sd.id = s.childid set `
|
|
|
+ sqlstr += " sd.manual = 1 "
|
|
|
+ sqlstr1 += " sd.manual = 0 "
|
|
|
+
|
|
|
+ id := strings.Join(ids, ",")
|
|
|
+ sqlstr += fmt.Sprintf(" where s.id in (%s) and s.action = 1 and sd.company = '%s' ", id, company)
|
|
|
+ sqlstr1 += fmt.Sprintf(" where s.id not in (%s) and s.action = 1 and sd.company = '%s' ", id, company)
|
|
|
+ } else {
|
|
|
+ sqlstr = `update scheduled s join scheduled_down sd on sd.id = s.childid set `
|
|
|
+ sqlstr += " sd.manual = 0 "
|
|
|
+ sqlstr += fmt.Sprintf(" where s.action = 1 and sd.company = '%s' ", company)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err := tx.SQL(sqlstr).Execute()
|
|
|
+ if err != nil {
|
|
|
+ log.Println("UpdateScheduledStatus-error-1: ", err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, err)
|
|
|
+ tx.Rollback()
|
|
|
+ }
|
|
|
+ if len(sqlstr1) > 0 {
|
|
|
+ _, err = tx.SQL(sqlstr1).Execute()
|
|
|
+ if err != nil {
|
|
|
+ log.Println("UpdateScheduledStatus-error-2: ", err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, err)
|
|
|
+ tx.Rollback()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ err = tx.Commit()
|
|
|
+ if err != nil {
|
|
|
+ log.Println("UpdateScheduledStatus-error-3: ", err)
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, err)
|
|
|
+ tx.Rollback()
|
|
|
+ }
|
|
|
+
|
|
|
+ appG.Response(http.StatusOK, e.SUCCESS, true)
|
|
|
+}
|
|
|
+
|
|
|
+func SynchronizeNow(c *gin.Context) {
|
|
|
+ appG := app.Gin{C: c}
|
|
|
+ dataByte, _ := ioutil.ReadAll(c.Request.Body)
|
|
|
+ fsion := gofasion.NewFasion(string(dataByte))
|
|
|
+ company := fsion.Get("company").ValueStr()
|
|
|
+
|
|
|
+ tx := restful.Engine.NewSession()
|
|
|
+ defer tx.Close()
|
|
|
+
|
|
|
+ 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 {
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ dataList, err := tx.SQL(`select id from downloadedplan_log where date = date_format(now(),'%Y-%m-%d') `).Query().List()
|
|
|
+ if err != nil {
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var idList []string
|
|
|
+ for _, data := range dataList {
|
|
|
+ idList = append(idList, strconv.FormatInt(data["id"].(int64), 10))
|
|
|
+ }
|
|
|
+
|
|
|
+ err = UpdatePush(c, tx, false, idList, pastureinfo, company)
|
|
|
+ if err != nil {
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ UpdateDown(c, tx, nil, pastureinfo, company)
|
|
|
+ if err != nil {
|
|
|
+ appG.Response(http.StatusInternalServerError, e.ERROR, false)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ appG.Response(http.StatusOK, e.SUCCESS, true)
|
|
|
+}
|
|
|
+
|
|
|
+func UpdatePush(ctx context.Context, tx *xorm.Session, auto bool, idList []string, pastureinfo *udPastureInfo, company string) error {
|
|
|
+ upList := make([]*ScheduledUpInfo, 0)
|
|
|
+ up := tx.Table("scheduled_up")
|
|
|
+ if company != "" {
|
|
|
+ up.Where("company = ? ", company)
|
|
|
+ }
|
|
|
+ if auto {
|
|
|
+ up.Where("automatic = 1")
|
|
|
+ } else {
|
|
|
+ up.Where("manual = 1")
|
|
|
+ }
|
|
|
+ err := up.Find(&upList)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("UpdataPush-error-1: ", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, item := range upList {
|
|
|
+ if item.Datasql != "" {
|
|
|
+ if item.Datatype == 3 || item.Datatype == 4 {
|
|
|
+ targetdataList := strings.Split(item.Targetdata, ",")
|
|
|
+ var args []interface{}
|
|
|
+ for _, targetdata := range targetdataList {
|
|
|
+ if targetdata == "pastureid" {
|
|
|
+ args = append(args, pastureinfo.Pastureid)
|
|
|
+ } else if targetdata == "idlist" {
|
|
|
+ // args = append(args, strings.Join(idList, ","))
|
|
|
+ item.Datasql = strings.ReplaceAll(item.Datasql, "idlist", strings.Join(idList, ","))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ data, err := tx.SQL(item.Datasql, args...).Query().List()
|
|
|
+ if err != nil {
|
|
|
+ log.Println("UpdataPush-error-2: ", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if len(data) > 0 {
|
|
|
+ databyte, err := json.Marshal(data)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("UpdataPush-error-3: ", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ UDPostPush1(fmt.Sprintf(item.Package, string(databyte), len(data)))
|
|
|
+ }
|
|
|
+ if len(idList) > 0 {
|
|
|
+ _, err := tx.SQL(fmt.Sprintf(" update downloadedplan_log set status = 1 where id in (%s)", strings.Join(idList, ","))).Execute()
|
|
|
+ if err != nil {
|
|
|
+ log.Println("UpdataPush-error-4: ", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ targetdataList := strings.Split(item.Targetdata, ",")
|
|
|
+ var args []interface{}
|
|
|
+ for _, targetdata := range targetdataList {
|
|
|
+ if targetdata == "pastureid" {
|
|
|
+ args = append(args, pastureinfo.Pastureid)
|
|
|
+ } else if targetdata == "idlist" {
|
|
|
+ // args = append(args, strings.Join(idList, ","))
|
|
|
+ item.Datasql = strings.ReplaceAll(item.Datasql, "idlist", strings.Join(idList, ","))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ data, err := tx.SQL(item.Datasql, args...).Query().List()
|
|
|
+ if err != nil {
|
|
|
+ log.Println("UpdataPush-error-5: ", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if len(data) > 0 {
|
|
|
+ databyte, err := json.Marshal(data)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("UpdataPush-error-6: ", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ // d, err := OpenFile("a.txt")
|
|
|
+ // d.WriteString()
|
|
|
+ // UDPostPush(fmt.Sprintf(item.Package, string(databyte), len(data)), "application/json")
|
|
|
+ UDPostPush1(fmt.Sprintf(item.Package, string(databyte), len(data)))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// OpenFile 判断文件是否存在 存在则OpenFile 不存在则Create
|
|
|
+func OpenFile(filename string) (*os.File, error) {
|
|
|
+ if _, err := os.Stat(filename); os.IsNotExist(err) {
|
|
|
+ fmt.Println("文件不存在")
|
|
|
+ return os.Create(filename) //创建文件
|
|
|
+ }
|
|
|
+ fmt.Println("文件存在")
|
|
|
+ return os.OpenFile(filename, os.O_APPEND, 0666) //打开文件
|
|
|
+}
|
|
|
+
|
|
|
+func UpdateDown(ctx context.Context, tx *xorm.Session, idList []string, pastureinfo *udPastureInfo, company string) error {
|
|
|
+
|
|
|
+ downList := make([]*ScheduledDownInfo, 0)
|
|
|
+ down := tx.Table("scheduled_down")
|
|
|
+ if company != "" {
|
|
|
+ down.Where("company = ? ", company)
|
|
|
+ }
|
|
|
+
|
|
|
+ down.Where("manual = 1")
|
|
|
+ err := down.Find(&downList)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("UpdateDown-error-1: ", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, item := range downList {
|
|
|
+ list := httpGetMC(item.Addres, item.Targetdata)
|
|
|
+ if item.Methods != "" {
|
|
|
+ s := ScheduledDown{}
|
|
|
+ value := reflect.ValueOf(&s)
|
|
|
+ f := value.MethodByName(item.Methods)
|
|
|
+ f.Call([]reflect.Value{reflect.ValueOf(pastureinfo.Pastureid), reflect.ValueOf(list)})
|
|
|
+
|
|
|
+ // value := reflect.ValueOf(&s)
|
|
|
+ // f := value.MethodByName("Ccaa")
|
|
|
+ // f.Call([]reflect.Value{reflect.ValueOf(list)})
|
|
|
+
|
|
|
+ } else {
|
|
|
+ childList := make([]*ScheduledDownChildInfo, 0)
|
|
|
+ child := tx.Table("scheduled_down")
|
|
|
+ child.Where("parentid = ? ", item.Id)
|
|
|
+ err = child.Find(&childList)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("UpdateDown-error-2: ", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(childList) > 0 {
|
|
|
+ tx.Begin()
|
|
|
+ for _, c := range childList {
|
|
|
+ fieldnames := strings.Split(c.Fieldname, ",")
|
|
|
+ for _, data := range list {
|
|
|
+ var args []interface{}
|
|
|
+ dataMap := data.(map[string]interface{})
|
|
|
+ for _, fieldname := range fieldnames {
|
|
|
+ args = append(args, dataMap[fieldname])
|
|
|
+ }
|
|
|
+ _, err = tx.SQL(c.Dosql, args...).Execute()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ log.Println("UpdateDown-error-3: ", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ err := tx.Commit()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ log.Println("UpdateDown-error-4: ", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func httpGetMC(url, targetdata string) []interface{} {
|
|
|
+ // url := fmt.Sprintf("https://wdc.unidairy.cn/copartner_downloads/?farmId=%s&method=%s", farmId, method)
|
|
|
+ res, err := http.Get(url)
|
|
|
+ if err != nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ robots, err := ioutil.ReadAll(res.Body)
|
|
|
+ res.Body.Close()
|
|
|
+ if err != nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ var data map[string][]interface{}
|
|
|
+ json.Unmarshal(robots, &data)
|
|
|
+ return data[targetdata]
|
|
|
+}
|
|
|
+
|
|
|
+type ScheduledDown struct {
|
|
|
+}
|
|
|
+
|
|
|
+func (h *ScheduledDown) SyncFeed(pastureid string, feedList []interface{}) {
|
|
|
+
|
|
|
+ tx := restful.Engine.NewSession()
|
|
|
+ defer tx.Close()
|
|
|
+ err := tx.Begin()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error("syncFeed-error-1:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, f := range feedList {
|
|
|
+ feed := f.(map[string]interface{})
|
|
|
+
|
|
|
+ var feedcode, fname, fclass, fclassid, dry interface{}
|
|
|
+
|
|
|
+ if _, ok := feed["feedcode"]; ok {
|
|
|
+ feedcode = feed["feedcode"]
|
|
|
+ }
|
|
|
+ if _, ok := feed["feedname"]; ok {
|
|
|
+ fname = feed["feedname"]
|
|
|
+ }
|
|
|
+ if _, ok := feed["feedclass"]; ok {
|
|
|
+ fclass = feed["feedclass"]
|
|
|
+ }
|
|
|
+ if _, ok := feed["drymatter"]; ok {
|
|
|
+ dry = feed["drymatter"]
|
|
|
+ }
|
|
|
+
|
|
|
+ fclassExist, err := tx.SQL(" select id from feedclass where pastureid = ? and fcname = ?", pastureid, fclass).Exist()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error("syncFeed-error-2:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if fclassExist {
|
|
|
+ fclassDataList, err := tx.SQL(" select id from feedclass where pastureid = ? and fcname = ?", pastureid, fclass).QueryString()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error("syncFeed-error-3:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, fclassData := range fclassDataList {
|
|
|
+ fclassid = fclassData["id"]
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ ids, err := setting.SnowIds.NextId()
|
|
|
+ if err != nil {
|
|
|
+ ids = time.Now().UnixNano()
|
|
|
+ logging.Info("create SnowIds err", err)
|
|
|
+ }
|
|
|
+ _, err = tx.SQL("insert into feedclass(id,pastureid,fccode,fcname,bigfeedclassname,bigfeedclassid,sort)VALUES(?,?,?,?,?,?,(select ifnull(max(f.sort),0) +1 from feedclass f where f.pastureid =? ))",
|
|
|
+ ids, pastureid, fclass, fclass, fclass, ids, pastureid).Execute()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error("syncFeed-error-4:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ fclassid = ids
|
|
|
+ }
|
|
|
+
|
|
|
+ ids, err := setting.SnowIds.NextId()
|
|
|
+ if err != nil {
|
|
|
+ ids = time.Now().UnixNano()
|
|
|
+ logging.Info("create SnowIds err", err)
|
|
|
+ }
|
|
|
+ insertSql := `insert into feed(id,pastureid,feedcode,fname,fclassid,fclass,dry)VALUES(?,?,?,?,?,?,?)
|
|
|
+ ON DUPLICATE KEY UPDATE fname = ? ,dry = ? `
|
|
|
+ _, err = tx.SQL(insertSql, ids, pastureid, feedcode, fname, fclassid, fclass, dry, fname, dry).Execute()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error("syncFeed-error-5:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ err = tx.Commit()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error("syncFeed-error-6:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func (h *ScheduledDown) SyncFeedp(pastureid string, feedpList []interface{}) error {
|
|
|
+
|
|
|
+ tx := restful.Engine.NewSession()
|
|
|
+ defer tx.Close()
|
|
|
+ err := tx.Begin()
|
|
|
+ if err != nil {
|
|
|
+ logs.Error("syncFeedp-error-1:", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, f := range feedpList {
|
|
|
+ feedp := f.(map[string]interface{})
|
|
|
+ var barName, barCode, feedtempletCode, barId interface{}
|
|
|
+ var ftId, tname interface{}
|
|
|
+ var cowCount interface{}
|
|
|
+ if _, ok := feedp["barname"]; ok {
|
|
|
+ barName = feedp["barname"]
|
|
|
+ }
|
|
|
+ if _, ok := feedp["barcode"]; ok {
|
|
|
+ barCode = feedp["barcode"]
|
|
|
+ }
|
|
|
+ if _, ok := feedp["cowcount"]; ok {
|
|
|
+ cowCount = feedp["cowcount"]
|
|
|
+ }
|
|
|
+ if _, ok := feedp["feedtempletcode"]; ok {
|
|
|
+ feedtempletCode = feedp["feedtempletCode"]
|
|
|
+ }
|
|
|
+
|
|
|
+ barCount, err := tx.SQL(" select count(1) from bar where pastureid = ? and bcode = ? ", pastureid, barCode).Count()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error("syncFeedp-error-2:", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if barCount > 0 {
|
|
|
+ barDataList, err := tx.SQL(" select id from bar where pastureid = ? and bcode = ?", pastureid, barCode).QueryString()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error("syncFeedp-error-3:", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ for _, barData := range barDataList {
|
|
|
+ barId = barData["id"]
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ barReq, err := tx.SQL("insert into bar(pastureid,bname,bcode)VALUES(?,?,?)", pastureid, barName, barCode).Execute()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error("syncFeedp-error-4:", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ id, err := barReq.LastInsertId()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error("syncFeedp-error-5:", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ barId = strconv.FormatInt(id, 10)
|
|
|
+ }
|
|
|
+
|
|
|
+ if feedtempletCode != "" {
|
|
|
+ feedtempletDataList, err := tx.SQL(" select id,tname from feedtemplet where pastureid = ? and tcode = ?", pastureid, feedtempletCode).QueryString()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error("syncFeedp-error-6:", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ for _, feedtemplet := range feedtempletDataList {
|
|
|
+ ftId = feedtemplet
|
|
|
+ tname = feedtemplet
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ insertSql := `insert into feedp(pastureid,barname,barid,ccount,ftid,ftname)VALUES(?,?,?,?,?,?)
|
|
|
+ ON DUPLICATE KEY UPDATE ccount = ? ,ftid = ? ,ftname = ? `
|
|
|
+ _, err = tx.SQL(insertSql, pastureid, barName, barId, cowCount, ftId, tname, cowCount, ftId, tname).Execute()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error("syncFeedp-error-7:", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ err = tx.Commit()
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ logs.Error("syncFeedp-error-8:", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func wxPush(target []string, content []interface{}) {
|
|
|
+ url := "http://tmrwatch.cn/notice/message"
|
|
|
+
|
|
|
+ dataStr := `{
|
|
|
+ "sys_name": "tmrwatch",
|
|
|
+ "remind_type": "1",
|
|
|
+ "template_id": "BtkN1rWKOJtKP0C64lGxIrPzLRFsYFas-4gupX2-pFo",
|
|
|
+ "target": %s,
|
|
|
+ "miniprogram": {
|
|
|
+ "appid": "xiaochengxuappid12345",
|
|
|
+ "pagepath": "index?foo=bar"
|
|
|
+ },
|
|
|
+ "keys": [
|
|
|
+ "first",
|
|
|
+ "keyword1",
|
|
|
+ "keyword2",
|
|
|
+ "keyword3",
|
|
|
+ "remark"
|
|
|
+ ],
|
|
|
+ "content":%s
|
|
|
+ }`
|
|
|
+
|
|
|
+ targetStr, _ := json.Marshal(target)
|
|
|
+ contentStr, _ := json.Marshal(content)
|
|
|
+ dataStr = fmt.Sprintf(dataStr, string(targetStr), string(contentStr))
|
|
|
+ var jsonStr = []byte(dataStr)
|
|
|
+ req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
|
|
|
+
|
|
|
+ req.Header.Set("Content-Type", "application/json")
|
|
|
+ client := &http.Client{}
|
|
|
+ resp, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ defer resp.Body.Close()
|
|
|
+ body, _ := ioutil.ReadAll(resp.Body)
|
|
|
+ fmt.Println("response Body:", string(body))
|
|
|
+}
|
|
|
+
|
|
|
+func UDPostPush1(data string) {
|
|
|
+ url := "https://wdc.unidairy.cn/copartner_uploads/"
|
|
|
+ // 超时时间:5秒
|
|
|
+ fmt.Println(data)
|
|
|
+ var jsonStr = []byte(data)
|
|
|
+ req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
|
|
|
+
|
|
|
+ req.Header.Set("Content-Type", "application/json")
|
|
|
+ client := &http.Client{}
|
|
|
+ resp, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ defer resp.Body.Close()
|
|
|
+ body, _ := ioutil.ReadAll(resp.Body)
|
|
|
+ fmt.Println("response Body:", string(body))
|
|
|
+}
|