zc.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. package zc
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "tmr-watch/http/handle/restful"
  9. "tmr-watch/pkg/app"
  10. "tmr-watch/pkg/e"
  11. "github.com/astaxie/beego/logs"
  12. "github.com/gin-gonic/gin"
  13. "github.com/robfig/cron"
  14. )
  15. func SyncZC() {
  16. duetimecst, _ := time.ParseInLocation("15:04:05", "00:00:15", time.Local)
  17. duetimecst1, _ := time.ParseInLocation("15:04:05", "00:00:00", time.Local)
  18. spec := fmt.Sprintf("@every %v", duetimecst.Sub(duetimecst1))
  19. c := cron.New()
  20. c.AddFunc(spec, func() {
  21. updateTmrStatus()
  22. // count1, err := restful.SqlServerEngine.Table("PFGL").QueryString()
  23. // fmt.Println(err, count1)
  24. })
  25. c.Start()
  26. }
  27. // 同步配方到正诚
  28. func SyncSCJH(c *gin.Context) {
  29. appG := app.Gin{C: c}
  30. count1, err := restful.SqlServerEngine.Table("COMM").Where(` Status = 1 `).Count()
  31. fmt.Println(err)
  32. if count1 == 0 {
  33. appG.Response(http.StatusOK, e.SUCCESS, "本都生产状态,禁止更新生产计划与配方!")
  34. return
  35. }
  36. now := time.Now().Format("2006-01-02 15:04:05")
  37. feedtempletList := GetFeedtempletList(time.Now().Format("2006-01-02"))
  38. feedtempletMapList := make(map[string][]map[string]string)
  39. for _, ft := range feedtempletList {
  40. feedtempletMapList[ft["tname"]] = append(feedtempletMapList[ft["tname"]], ft)
  41. }
  42. // n := 0
  43. restful.SqlServerEngine.SQL(` delete from PFGL`).Execute()
  44. for k, ftList := range feedtempletMapList {
  45. // count, _ := restful.SqlServerEngine.Table("PFGL").Where(` PFMC = ? `, k).Count()
  46. // n++
  47. // if count > 0 {
  48. // var wlmcstr []string
  49. // var wlmc []interface{}
  50. // wlmc = append(wlmc, now, ftList[0]["tcode"])
  51. // var i, n int
  52. // for _, ft := range ftList {
  53. // if strings.Index(ft["fclass"], "小料") > -1 {
  54. // wlmcstr = append(wlmcstr, fmt.Sprintf("WLMC_%d", 40+n+1))
  55. // wlmcstr = append(wlmcstr, fmt.Sprintf("WLZL_%d", 40+n+1))
  56. // wlmcstr = append(wlmcstr, fmt.Sprintf("WLSX_%d", 40+n+1))
  57. // n++
  58. // } else {
  59. // wlmcstr = append(wlmcstr, fmt.Sprintf("WLMC_%d", i+1))
  60. // wlmcstr = append(wlmcstr, fmt.Sprintf("WLZL_%d", i+1))
  61. // wlmcstr = append(wlmcstr, fmt.Sprintf("WLSX_%d", i+1))
  62. // i++
  63. // }
  64. // // wlmcstr = append(wlmcstr, fmt.Sprintf("WLMC_%d = ? ", i+1))
  65. // // wlmcstr = append(wlmcstr, fmt.Sprintf("WLZL_%d = ? ", i+1))
  66. // // wlmcstr = append(wlmcstr, fmt.Sprintf("WLSX_%d = ?", i+1))
  67. // if _, ok := ft["fname"]; ok {
  68. // wlmc = append(wlmc, ft["fname"])
  69. // } else {
  70. // wlmc = append(wlmc, "")
  71. // }
  72. // if _, ok := ft["fweight"]; ok {
  73. // wlmc = append(wlmc, ft["fweight"])
  74. // } else {
  75. // wlmc = append(wlmc, "0")
  76. // }
  77. // wlmc = append(wlmc, i)
  78. // }
  79. // for i := len(ftList); i < 50; i++ {
  80. // wlmcstr = append(wlmcstr, fmt.Sprintf("WLMC_%d = ? ", i+1))
  81. // wlmcstr = append(wlmcstr, fmt.Sprintf("WLZL_%d = ? ", i+1))
  82. // wlmcstr = append(wlmcstr, fmt.Sprintf("WLSX_%d = ?", i+1))
  83. // wlmc = append(wlmc, "", "0", "0")
  84. // }
  85. // wlmc = append(wlmc, k)
  86. // _, err := restful.SqlServerEngine.SQL(fmt.Sprintf("update PFGL set XGRQ = ?,PFBM = ? ,%s where PFMC = ? ", strings.Join(wlmcstr, ",")), wlmc...).Execute()
  87. // fmt.Println(err, wlmc)
  88. // } else {
  89. // // PFBM := ""
  90. var vlues, wlmcstr []string
  91. var wlmc []interface{}
  92. wlmc = append(wlmc, k, now, ftList[0]["tcode"])
  93. vlues = append(vlues, "?", "?", "?")
  94. var i, n int
  95. for _, ft := range ftList {
  96. if strings.Index(ft["fclass"], "小料") > -1 {
  97. wlmcstr = append(wlmcstr, fmt.Sprintf("WLMC_%d", 40+n+1))
  98. wlmcstr = append(wlmcstr, fmt.Sprintf("WLZL_%d", 40+n+1))
  99. wlmcstr = append(wlmcstr, fmt.Sprintf("WLSX_%d", 40+n+1))
  100. n++
  101. } else {
  102. wlmcstr = append(wlmcstr, fmt.Sprintf("WLMC_%d", i+1))
  103. wlmcstr = append(wlmcstr, fmt.Sprintf("WLZL_%d", i+1))
  104. wlmcstr = append(wlmcstr, fmt.Sprintf("WLSX_%d", i+1))
  105. i++
  106. }
  107. if _, ok := ft["fname"]; ok {
  108. wlmc = append(wlmc, ft["fname"])
  109. } else {
  110. wlmc = append(wlmc, "")
  111. }
  112. if _, ok := ft["fweight"]; ok {
  113. wlmc = append(wlmc, ft["fweight"])
  114. } else {
  115. wlmc = append(wlmc, "0")
  116. }
  117. if strings.Index(ft["fclass"], "小料") > -1 {
  118. wlmc = append(wlmc, n)
  119. } else {
  120. wlmc = append(wlmc, i)
  121. }
  122. vlues = append(vlues, "?", "?", "?")
  123. }
  124. restful.SqlServerEngine.SQL(fmt.Sprintf("insert into PFGL(PFMC,XGRQ,PFBM,%s)values(%s)", strings.Join(wlmcstr, ","),
  125. strings.Join(vlues, ",")), wlmc...).Execute()
  126. }
  127. downloadedplanList, err := restful.Engine.SQL(`
  128. select t.* from
  129. (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 ,
  130. round((d2.lweight/d.lweight*100),0)PFBL,f.ccount
  131. from downloadedplan d
  132. join downloadplandtl2 d2 on d2.pid = d.id
  133. join feedp f on f.barid = d2.fbarid
  134. 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
  135. UNION all
  136. 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 ,
  137. round((d2.lweight/d.lweight*100),0)PFBL ,f.ccount
  138. from downloadedplan d
  139. join downloadplandtl2 d2 on d2.pid = (select id from downloadedplan where pid = d.pid and mydate = d.mydate and lpplantype= 2)
  140. join feedp f on f.barid = d2.fbarid
  141. 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
  142. order by t.sort,t.barsort `, time.Now().Format("2006-01-02"), time.Now().Format("2006-01-02")).QueryString()
  143. if err != nil {
  144. appG.Response(http.StatusOK, e.SUCCESS, "计划查询失败!!!")
  145. return
  146. }
  147. var list []string
  148. //把推送过的计划id保存到列表中
  149. zcpushList, _ := restful.Engine.SQL(`select * from zcpush where date_format(date ,'%Y-%m-%d') = ? `, time.Now().Format("2006-01-02")).QueryString()
  150. for _, item := range zcpushList {
  151. list = append(list, item["downloadedplan"])
  152. }
  153. if len(list) > 0 {
  154. noWxz, _ := restful.SqlServerEngine.Table("SCJH").Where(` Replace(WCZT,' ','') != N'未选择' `).Count()
  155. if noWxz == 0 {
  156. restful.Engine.SQL(`delete from zcpush where date_format(date ,'%Y-%m-%d') = ? `, time.Now().Format("2006-01-02")).Execute()
  157. } else if noWxz > 0 {
  158. appG.Response(http.StatusOK, e.SUCCESS, "上传成功!")
  159. return
  160. }
  161. } else {
  162. restful.SqlServerEngine.SQL(` delete from SCJH `).Execute()
  163. }
  164. restful.SqlServerEngine.SQL(` delete from SCJH where Replace(WCZT,' ','') = N'未选择' `).Execute()
  165. num := make(map[string]int, 0)
  166. tmrmap := make(map[string]string, 0)
  167. for _, item := range downloadedplanList {
  168. num[item["id"]] = 0
  169. }
  170. for _, item := range downloadedplanList {
  171. exist := false
  172. for _, id := range list {
  173. if id == item["id"] {
  174. exist = true
  175. break
  176. }
  177. }
  178. //如果已经推送过直接返回
  179. if exist {
  180. continue
  181. }
  182. //执行完成的直接返回
  183. if item["iscompleted"] == "1" {
  184. continue
  185. }
  186. if _, ok := tmrmap[item["datacaptureno"]]; ok {
  187. //判断同tmr车是否已经有计划推送过
  188. if tmrmap[item["datacaptureno"]] != item["id"] {
  189. continue
  190. }
  191. } else {
  192. // 首次推送
  193. tmrmap[item["datacaptureno"]] = item["id"]
  194. restful.Engine.SQL(` insert into zcpush(downloadedplan,sort,date,datacaptureno)values(?,?,now(),?)`, item["id"], item["sort"], item["datacaptureno"]).Execute()
  195. }
  196. num[item["id"]]++
  197. restful.SqlServerEngine.SQL(`insert SCJH(SXCH,PFMC,LSMC,LSTS,PFBL,WCZT,TMRS)values(?,?,?,?,?,?,?)`, item["sort"], item["templetname"], item["fname"],
  198. item["ccount"], item["PFBL"], "未完成", item["datacaptureno"]).Execute()
  199. }
  200. restful.SqlServerEngine.SQL(` update COMM set int_1 = 1 `).Execute()
  201. appG.Response(http.StatusOK, e.SUCCESS, "上传成功!")
  202. }
  203. //获取当天精料配方
  204. func GetFeedtempletList(now string) []map[string]string {
  205. 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
  206. join feed fd on fd.id = ft.fid where fd.smtmrid >0 and date = ? order by f.sort,ft.sort`, now).QueryString()
  207. if err != nil {
  208. logs.Error(err)
  209. }
  210. return feedtempletList
  211. }
  212. func updateTmrStatus() {
  213. commList, err := restful.SqlServerEngine.Table("COMM").QueryString()
  214. if err != nil {
  215. logs.Error(err)
  216. return
  217. }
  218. comm := commList[0]
  219. restful.Engine.SQL(` update zccomm set int_2 = ? where id = 1 `, comm["Int_2"]).Execute()
  220. if comm["Status"] == "1" {
  221. now := time.Now().Format("2006-01-02")
  222. count, _ := restful.Engine.Table("downloadedplan").Where(` mydate = ?`, now).Where(`lpplantype in(0,1)`).Count()
  223. // total, _ := restful.Engine.Table("downloadedplan").Where(` mydate = ?`, now).Where(`lpplantype in(0,1)`).Where(`havebutton = 1 `).Count()
  224. havebuttontotal, _ := restful.Engine.Table("downloadedplan").Where(` mydate = ?`, now).Where(`lpplantype in(0,1,2)`).Where(`havebutton = 1 `).Count()
  225. iscompletedtotal, _ := restful.Engine.Table("downloadedplan").Where(` mydate = ?`, now).Where(`lpplantype in(0,1,2)`).Where(`iscompleted = 1 `).Count()
  226. if count == 0 {
  227. restful.SqlServerEngine.SQL(` update COMM set int_1 = ? `, 0).Execute()
  228. } else if havebuttontotal != iscompletedtotal {
  229. restful.SqlServerEngine.SQL(` update COMM set int_1 = ? `, 2).Execute()
  230. } else if havebuttontotal > 0 && iscompletedtotal > 0 {
  231. restful.SqlServerEngine.SQL(` update COMM set int_1 = ? `, 4).Execute()
  232. } else {
  233. restful.SqlServerEngine.SQL(` update COMM set int_1 = ? `, 1).Execute()
  234. }
  235. downloadedplanList, err := restful.Engine.SQL(`
  236. select t.* from
  237. (select d.id,d.havebutton,d.sort,datacaptureno from downloadedplan d
  238. 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
  239. UNION all
  240. select d.id,d.havebutton,d.sort,datacaptureno from downloadedplan d
  241. 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
  242. order by t.sort `, now, now).QueryString()
  243. if err != nil {
  244. logs.Error(err)
  245. return
  246. }
  247. tmrMap := make(map[string][]map[string]string, 0)
  248. for _, item := range downloadedplanList {
  249. tmrMap[item["datacaptureno"]] = append(tmrMap[item["datacaptureno"]], item)
  250. }
  251. for datacaptureno, tmrList := range tmrMap {
  252. 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()
  253. zcpush := make(map[string]string)
  254. if len(zcpushList) == 0 {
  255. continue
  256. } else {
  257. zcpush = zcpushList[0]
  258. }
  259. data := make(map[string]string, 0)
  260. exist := true
  261. for _, item := range tmrList {
  262. itemsort, _ := strconv.Atoi(item["sort"])
  263. zcsort, _ := strconv.Atoi(zcpush["sort"])
  264. //按顺序下推相同tmr如果还没有开始发料 就不更新下一个
  265. if zcpush["downloadedplan"] == item["id"] && item["havebutton"] == "0" {
  266. break
  267. } else if zcsort < itemsort {
  268. data = item
  269. exist = false
  270. break
  271. }
  272. }
  273. if !exist {
  274. restful.Engine.SQL(` insert into zcpush(downloadedplan,sort,date,datacaptureno)values(?,?,now(),?)`, data["id"], data["sort"], data["datacaptureno"]).Execute()
  275. list, err := restful.Engine.SQL(`
  276. select t.* from
  277. (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 ,
  278. round((d2.lweight/d.lweight*100),0)PFBL,f.ccount
  279. from downloadedplan d
  280. join downloadplandtl2 d2 on d2.pid = d.id
  281. join feedp f on f.barid = d2.fbarid
  282. 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
  283. UNION all
  284. 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 ,
  285. round((d2.lweight/d.lweight*100),0)PFBL ,f.ccount
  286. from downloadedplan d
  287. join downloadplandtl2 d2 on d2.pid = (select id from downloadedplan where pid = d.pid and mydate = d.mydate and lpplantype= 2)
  288. join feedp f on f.barid = d2.fbarid
  289. 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
  290. order by t.sort,t.barsort `, data["id"], data["id"]).QueryString()
  291. if err != nil {
  292. logs.Error(err)
  293. return
  294. }
  295. for _, arg := range list {
  296. restful.SqlServerEngine.SQL(`insert SCJH(SXCH,PFMC,LSMC,LSTS,PFBL,WCZT,TMRS)values(?,?,?,?,?,?,?)`, arg["sort"], arg["templetname"], arg["fname"],
  297. arg["ccount"], arg["PFBL"], "未完成", arg["datacaptureno"]).Execute()
  298. }
  299. restful.SqlServerEngine.SQL(` update COMM set int_1 = 1 `).Execute()
  300. }
  301. }
  302. }
  303. }