| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 | package zcimport (	"fmt"	"net/http"	"strconv"	"strings"	"time"	"tmr-watch/http/handle/restful"	"tmr-watch/pkg/app"	"tmr-watch/pkg/e"	"github.com/astaxie/beego/logs"	"github.com/gin-gonic/gin"	"github.com/robfig/cron")func SyncZC() {	duetimecst, _ := time.ParseInLocation("15:04:05", "00:00:15", 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() {		updateTmrStatus()		// count1, err := restful.SqlServerEngine.Table("PFGL").QueryString()		// fmt.Println(err, count1)	})	c.Start()}// 同步配方到正诚func SyncSCJH(c *gin.Context) {	appG := app.Gin{C: c}	count1, err := restful.SqlServerEngine.Table("COMM").Where(` Status = 1 `).Count()	fmt.Println(err)	if count1 == 0 {		appG.Response(http.StatusOK, e.SUCCESS, "本都生产状态,禁止更新生产计划与配方!")		return	}	now := time.Now().Format("2006-01-02 15:04:05")	feedtempletList := GetFeedtempletList(time.Now().Format("2006-01-02"))	feedtempletMapList := make(map[string][]map[string]string)	for _, ft := range feedtempletList {		feedtempletMapList[ft["tname"]] = append(feedtempletMapList[ft["tname"]], ft)	}	// n := 0	restful.SqlServerEngine.SQL(`  delete from PFGL`).Execute()	for k, ftList := range feedtempletMapList {		// count, _ := restful.SqlServerEngine.Table("PFGL").Where(` PFMC = ? `, k).Count()		// n++		// if count > 0 {		// 	var wlmcstr []string		// 	var wlmc []interface{}		// 	wlmc = append(wlmc, now, ftList[0]["tcode"])		// 	var i, n int		// 	for _, ft := range ftList {		// 		if strings.Index(ft["fclass"], "小料") > -1 {		// 			wlmcstr = append(wlmcstr, fmt.Sprintf("WLMC_%d", 40+n+1))		// 			wlmcstr = append(wlmcstr, fmt.Sprintf("WLZL_%d", 40+n+1))		// 			wlmcstr = append(wlmcstr, fmt.Sprintf("WLSX_%d", 40+n+1))		// 			n++		// 		} else {		// 			wlmcstr = append(wlmcstr, fmt.Sprintf("WLMC_%d", i+1))		// 			wlmcstr = append(wlmcstr, fmt.Sprintf("WLZL_%d", i+1))		// 			wlmcstr = append(wlmcstr, fmt.Sprintf("WLSX_%d", i+1))		// 			i++		// 		}		// 		// wlmcstr = append(wlmcstr, fmt.Sprintf("WLMC_%d = ? ", i+1))		// 		// wlmcstr = append(wlmcstr, fmt.Sprintf("WLZL_%d = ? ", i+1))		// 		// wlmcstr = append(wlmcstr, fmt.Sprintf("WLSX_%d = ?", i+1))		// 		if _, ok := ft["fname"]; ok {		// 			wlmc = append(wlmc, ft["fname"])		// 		} else {		// 			wlmc = append(wlmc, "")		// 		}		// 		if _, ok := ft["fweight"]; ok {		// 			wlmc = append(wlmc, ft["fweight"])		// 		} else {		// 			wlmc = append(wlmc, "0")		// 		}		// 		wlmc = append(wlmc, i)		// 	}		// 	for i := len(ftList); i < 50; i++ {		// 		wlmcstr = append(wlmcstr, fmt.Sprintf("WLMC_%d = ? ", i+1))		// 		wlmcstr = append(wlmcstr, fmt.Sprintf("WLZL_%d = ? ", i+1))		// 		wlmcstr = append(wlmcstr, fmt.Sprintf("WLSX_%d = ?", i+1))		// 		wlmc = append(wlmc, "", "0", "0")		// 	}		// 	wlmc = append(wlmc, k)		// 	_, err := restful.SqlServerEngine.SQL(fmt.Sprintf("update PFGL set XGRQ = ?,PFBM = ? ,%s where PFMC = ? ", strings.Join(wlmcstr, ",")), wlmc...).Execute()		// 	fmt.Println(err, wlmc)		// } else {		// 	// PFBM := ""		var vlues, wlmcstr []string		var wlmc []interface{}		wlmc = append(wlmc, k, now, ftList[0]["tcode"])		vlues = append(vlues, "?", "?", "?")		var i, n int		for _, ft := range ftList {			if strings.Index(ft["fclass"], "小料") > -1 {				wlmcstr = append(wlmcstr, fmt.Sprintf("WLMC_%d", 40+n+1))				wlmcstr = append(wlmcstr, fmt.Sprintf("WLZL_%d", 40+n+1))				wlmcstr = append(wlmcstr, fmt.Sprintf("WLSX_%d", 40+n+1))				n++			} else {				wlmcstr = append(wlmcstr, fmt.Sprintf("WLMC_%d", i+1))				wlmcstr = append(wlmcstr, fmt.Sprintf("WLZL_%d", i+1))				wlmcstr = append(wlmcstr, fmt.Sprintf("WLSX_%d", i+1))				i++			}			if _, ok := ft["fname"]; ok {				wlmc = append(wlmc, ft["fname"])			} else {				wlmc = append(wlmc, "")			}			if _, ok := ft["fweight"]; ok {				wlmc = append(wlmc, ft["fweight"])			} else {				wlmc = append(wlmc, "0")			}			if strings.Index(ft["fclass"], "小料") > -1 {				wlmc = append(wlmc, n)			} else {				wlmc = append(wlmc, i)			}			vlues = append(vlues, "?", "?", "?")		}		restful.SqlServerEngine.SQL(fmt.Sprintf("insert into PFGL(PFMC,XGRQ,PFBM,%s)values(%s)", strings.Join(wlmcstr, ","),			strings.Join(vlues, ",")), wlmc...).Execute()	}	downloadedplanList, err := restful.Engine.SQL(`  select t.* from (select d.id,d.datacaptureno,d.tmrtname,d2.havebuttom,d.iscompleted,d2.fname,d2.lweight,d.lweight allweight,d.projname,d.sort,d2.sort barsort,d.templetname,d2.intime , 	round((d2.lweight/d.lweight*100),0)PFBL,f.ccount  from downloadedplan d join downloadplandtl2 d2 on d2.pid = d.idjoin feedp f on f.barid = d2.fbarid   where d.mydate = ? and d.enable = 1 and d.lpplantype= 0 and (select count(1) from downloadplandtl1  where pid = d.id  and type = 1) >0  UNION  all select d.id, d.datacaptureno,d.tmrtname,d2.havebuttom,d.iscompleted,d2.fname,d2.lweight,d.lweight allweight,d.projname,d.sort,d2.sort barsort,d.templetname,d2.intime , round((d2.lweight/d.lweight*100),0)PFBL  ,f.ccountfrom downloadedplan d  join downloadplandtl2 d2 on d2.pid = (select id from downloadedplan  where pid = d.pid  and mydate = d.mydate and  lpplantype= 2)join feedp f on f.barid = d2.fbarid where d.mydate = ? and d.enable = 1 and d.lpplantype= 1  and (select count(1) from downloadplandtl1  where pid = d.id  and type = 1) >0    ) t order by t.sort,t.barsort `, time.Now().Format("2006-01-02"), time.Now().Format("2006-01-02")).QueryString()	if err != nil {		appG.Response(http.StatusOK, e.SUCCESS, "计划查询失败!!!")		return	}	var list []string	//把推送过的计划id保存到列表中	zcpushList, _ := restful.Engine.SQL(`select * from zcpush where  date_format(date ,'%Y-%m-%d') = ?   `, time.Now().Format("2006-01-02")).QueryString()	for _, item := range zcpushList {		list = append(list, item["downloadedplan"])	}	if len(list) > 0 {		noWxz, _ := restful.SqlServerEngine.Table("SCJH").Where(`  Replace(WCZT,' ','')  != N'未选择' `).Count()		if noWxz == 0 {			restful.Engine.SQL(`delete from zcpush where  date_format(date ,'%Y-%m-%d') = ?   `, time.Now().Format("2006-01-02")).Execute()		} else if noWxz > 0 {			appG.Response(http.StatusOK, e.SUCCESS, "上传成功!")			return		}	} else {		restful.SqlServerEngine.SQL(` delete from SCJH  `).Execute()	}	restful.SqlServerEngine.SQL(` delete from SCJH  where Replace(WCZT,' ','')  = N'未选择' `).Execute()	num := make(map[string]int, 0)	tmrmap := make(map[string]string, 0)	for _, item := range downloadedplanList {		num[item["id"]] = 0	}	for _, item := range downloadedplanList {		exist := false		for _, id := range list {			if id == item["id"] {				exist = true				break			}		}		//如果已经推送过直接返回		if exist {			continue		}		//执行完成的直接返回		if item["iscompleted"] == "1" {			continue		}		if _, ok := tmrmap[item["datacaptureno"]]; ok {			//判断同tmr车是否已经有计划推送过			if tmrmap[item["datacaptureno"]] != item["id"] {				continue			}		} else {			// 首次推送			tmrmap[item["datacaptureno"]] = item["id"]			restful.Engine.SQL(` insert into zcpush(downloadedplan,sort,date,datacaptureno)values(?,?,now(),?)`, item["id"], item["sort"], item["datacaptureno"]).Execute()		}		num[item["id"]]++		restful.SqlServerEngine.SQL(`insert SCJH(SXCH,PFMC,LSMC,LSTS,PFBL,WCZT,TMRS)values(?,?,?,?,?,?,?)`, item["sort"], item["templetname"], item["fname"],			item["ccount"], item["PFBL"], "未完成", item["datacaptureno"]).Execute()	}	restful.SqlServerEngine.SQL(` update COMM set int_1 = 1 `).Execute()	appG.Response(http.StatusOK, e.SUCCESS, "上传成功!")}//获取当天精料配方func GetFeedtempletList(now string) []map[string]string {	feedtempletList, err := restful.Engine.SQL(`select fd.fclass,f.tname,(select tcode from feedtemplet where id = f.id) tcode,ft.fname,ft.fweight,ft.sort  from feedtempletdate f join ftdetail ft on ft.ftid = f.id  	join feed fd on fd.id = ft.fid   where fd.smtmrid >0  and date = ?   order by f.sort,ft.sort`, now).QueryString()	if err != nil {		logs.Error(err)	}	return feedtempletList}func updateTmrStatus() {	commList, err := restful.SqlServerEngine.Table("COMM").QueryString()	if err != nil {		logs.Error(err)		return	}	comm := commList[0]	restful.Engine.SQL(` update zccomm set  int_2 = ?  where id = 1 `, comm["Int_2"]).Execute()	if comm["Status"] == "1" {		now := time.Now().Format("2006-01-02")		count, _ := restful.Engine.Table("downloadedplan").Where(` mydate = ?`, now).Where(`lpplantype in(0,1)`).Count()		// total, _ := restful.Engine.Table("downloadedplan").Where(` mydate = ?`, now).Where(`lpplantype in(0,1)`).Where(`havebutton = 1 `).Count()		havebuttontotal, _ := restful.Engine.Table("downloadedplan").Where(` mydate = ?`, now).Where(`lpplantype in(0,1,2)`).Where(`havebutton = 1 `).Count()		iscompletedtotal, _ := restful.Engine.Table("downloadedplan").Where(` mydate = ?`, now).Where(`lpplantype in(0,1,2)`).Where(`iscompleted = 1 `).Count()		if count == 0 {			restful.SqlServerEngine.SQL(` update COMM set  int_1 = ? `, 0).Execute()		} else if havebuttontotal != iscompletedtotal {			restful.SqlServerEngine.SQL(` update COMM set  int_1 = ? `, 2).Execute()		} else if havebuttontotal > 0 && iscompletedtotal > 0 {			restful.SqlServerEngine.SQL(` update COMM set int_1 = ? `, 4).Execute()		} else {			restful.SqlServerEngine.SQL(` update COMM set int_1 = ? `, 1).Execute()		}		downloadedplanList, err := restful.Engine.SQL(`	select t.* from	(select  d.id,d.havebutton,d.sort,datacaptureno   from downloadedplan d	  where d.mydate = ? and d.enable = 1 and d.lpplantype= 0 and (select count(1) from downloadplandtl1  where pid = d.id  and type = 1) >0  	UNION  all	select d.id,d.havebutton,d.sort,datacaptureno from downloadedplan d	where d.mydate = ? and d.enable = 1 and d.lpplantype= 1  and (select count(1) from downloadplandtl1  where pid = d.id  and type = 1) >0   ) t	order by t.sort `, now, now).QueryString()		if err != nil {			logs.Error(err)			return		}		tmrMap := make(map[string][]map[string]string, 0)		for _, item := range downloadedplanList {			tmrMap[item["datacaptureno"]] = append(tmrMap[item["datacaptureno"]], item)		}		for datacaptureno, tmrList := range tmrMap {			zcpushList, _ := restful.Engine.SQL(`select * from zcpush where  date_format(date ,'%Y-%m-%d') = ? and datacaptureno = ? order by date desc limit 1  `, now, datacaptureno).QueryString()			zcpush := make(map[string]string)			if len(zcpushList) == 0 {				continue			} else {				zcpush = zcpushList[0]			}			data := make(map[string]string, 0)			exist := true			for _, item := range tmrList {				itemsort, _ := strconv.Atoi(item["sort"])				zcsort, _ := strconv.Atoi(zcpush["sort"])				//按顺序下推相同tmr如果还没有开始发料 就不更新下一个				if zcpush["downloadedplan"] == item["id"] && item["havebutton"] == "0" {					break				} else if zcsort < itemsort {					data = item					exist = false					break				}			}			if !exist {				restful.Engine.SQL(` insert into zcpush(downloadedplan,sort,date,datacaptureno)values(?,?,now(),?)`, data["id"], data["sort"], data["datacaptureno"]).Execute()				list, err := restful.Engine.SQL(`				select t.* from 				(select d.id,d.datacaptureno,d.tmrtname,d2.havebuttom,d.iscompleted,d2.fname,d2.lweight,d.lweight allweight,d.projname,d.sort,d2.sort barsort,d.templetname,d2.intime , 					round((d2.lweight/d.lweight*100),0)PFBL,f.ccount				  from downloadedplan d 				join downloadplandtl2 d2 on d2.pid = d.id				join feedp f on f.barid = d2.fbarid 				  where d.id = ? and d.enable = 1 and d.lpplantype= 0 and (select count(1) from downloadplandtl1  where pid = d.id  and type = 1) >0  				UNION  all 				select d.id, d.datacaptureno,d.tmrtname,d2.havebuttom,d.iscompleted,d2.fname,d2.lweight,d.lweight allweight,d.projname,d.sort,d2.sort barsort,d.templetname,d2.intime , 				round((d2.lweight/d.lweight*100),0)PFBL  ,f.ccount				from downloadedplan d  				join downloadplandtl2 d2 on d2.pid = (select id from downloadedplan  where pid = d.pid  and mydate = d.mydate and  lpplantype= 2)				join feedp f on f.barid = d2.fbarid 				where d.id = ? and d.enable = 1 and d.lpplantype= 1  and (select count(1) from downloadplandtl1  where pid = d.id  and type = 1) >0    ) t 				order by t.sort,t.barsort  `, data["id"], data["id"]).QueryString()				if err != nil {					logs.Error(err)					return				}				for _, arg := range list {					restful.SqlServerEngine.SQL(`insert SCJH(SXCH,PFMC,LSMC,LSTS,PFBL,WCZT,TMRS)values(?,?,?,?,?,?,?)`, arg["sort"], arg["templetname"], arg["fname"],						arg["ccount"], arg["PFBL"], "未完成", arg["datacaptureno"]).Execute()				}				restful.SqlServerEngine.SQL(` update COMM set int_1 = 1 `).Execute()			}		}	}}
 |