main.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. package main
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "fmt"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "./models"
  11. "./pkg/setting"
  12. "github.com/astaxie/beego/logs"
  13. "github.com/pkg/errors"
  14. //"unsafe"
  15. )
  16. var (
  17. strMethod string // 输入的方法名称
  18. strOutFilePath string //保存路径
  19. strDate string // 操作日期
  20. FarmCode string // 牧场编号
  21. )
  22. type DataXML struct {
  23. XMLName xml.Name `xml:"DataXML"`
  24. Head XMLHead `xml:"Head"`
  25. Record []XMLRecord `xml:"Record"`
  26. }
  27. type DataXMLMix struct {
  28. XMLName xml.Name `xml:"DataXML"`
  29. Head XMLHead `xml:"Head"`
  30. Record []XMLRecordMix `xml:"Record"`
  31. }
  32. type DataXMLResi struct {
  33. XMLName xml.Name `xml:"DataXML"`
  34. Head XMLHead `xml:"Head"`
  35. Record []XMLRecordResi `xml:"Record"`
  36. }
  37. type XMLHead struct {
  38. SystemCode string `xml:"SystemCode"`
  39. MappingID string `xml:"MappingID"`
  40. Type string `xml:"Type"`
  41. FarmCode string `xml:"FarmCode"`
  42. DataNum string `xml:"DataNum"`
  43. Version string `xml:"Version"`
  44. }
  45. type XMLRecord struct {
  46. FeedCode string
  47. FeedName string
  48. FarmCode string
  49. FeedDate string
  50. Banci string
  51. BarName string
  52. GroupTypeCode string
  53. GroupTypeName string
  54. CowAmount string
  55. PCows string
  56. RecipeCode string
  57. RecipeName string
  58. MixBatch string
  59. BarStartTime string
  60. BarEndTime string
  61. BarRecipePlanWeight string
  62. BarRecipeRealWeight string
  63. BarFeedPlanWeight string
  64. BarFeedRealWeight string
  65. FDate string
  66. DStatus string
  67. UserName string
  68. UserTrueName string
  69. }
  70. type XMLRecordMix struct {
  71. FarmCode string
  72. MixDate string
  73. RecipeCode string
  74. RecipeName string
  75. FeedCode string
  76. FeedName string
  77. GroupTypeCode string
  78. GroupTypeName string
  79. Banci string
  80. MixBatch string
  81. MixStartTime string
  82. MixEndTime string
  83. MixRound string
  84. OutFeedTime string
  85. FeedWeightTotal string
  86. FeedOrder string
  87. FeedDryMatter string
  88. FeedAddTime string
  89. RecipeWeight string
  90. SystemWeight string
  91. FDate string
  92. DStatus string
  93. UserName string
  94. UserTrueName string
  95. }
  96. type XMLRecordResi struct {
  97. FarmCode string
  98. FeedDate string
  99. ResiDate string
  100. BarName string
  101. GroupTypeCode string
  102. GroupTypeName string
  103. ResiWeight string
  104. ResiGroupTypeCode string
  105. ResiGroupTypeName string
  106. FDate string
  107. DStatus string
  108. UserName string
  109. UserTrueName string
  110. }
  111. type Senddatarecipe struct {
  112. XMLName xml.Name `xml:"DataXML"`
  113. Head XMLHead `xml:"Head"`
  114. Record []Xnr_senddatarecipe `xml:"Record"`
  115. }
  116. type Senddatafeed struct {
  117. XMLName xml.Name `xml:"DataXML"`
  118. Head XMLHead `xml:"Head"`
  119. Record []Xnr_senddatafeed `xml:"Record"`
  120. }
  121. type Feedgdailycost struct {
  122. XMLName xml.Name `xml:"DataXML"`
  123. Head XMLHead `xml:"Head"`
  124. Record []Xnr_feedgdailycost `xml:"Record"`
  125. }
  126. type Xnr_senddatarecipe struct {
  127. getDate string
  128. getTime string
  129. FarmCode string
  130. RecipeCode string
  131. RecipeName string
  132. RecipeVersion string
  133. GroupTypeCode string
  134. GroupTypeName string
  135. RecipeType string
  136. FeedCode string
  137. FeedName string
  138. DryMatter string
  139. JiaobanOrder string
  140. JiaobanTime string
  141. BDWeight string
  142. BDTime string
  143. FeedWeight string
  144. FeedWeightRate string
  145. StartDate string
  146. LastAdjDate string
  147. FDate string
  148. DStatus string
  149. UserName string
  150. UserTrueName string
  151. }
  152. type Xnr_senddatafeed struct {
  153. getDate string
  154. getTime string
  155. FarmCode string
  156. FeedCode string
  157. FeedName string
  158. Goods_Spec string
  159. Goods_Unit string
  160. Class2_Name string
  161. FDate string
  162. DStatus string
  163. UserName string
  164. UserTrueName string
  165. }
  166. type Xnr_feedgdailycost struct {
  167. GDailyCostListID string
  168. getDate string
  169. getTime string
  170. FarmCode string
  171. FeedName string
  172. FeedTMR_Date string
  173. DeptGroupType_Code string
  174. DeptGroupType_Name string
  175. Group_Name string
  176. RealCowAmount string
  177. PCows string
  178. FeedCowAmount string
  179. FeedRecipe_Code string
  180. FeedRecipe_Verson string
  181. FeedRecipe_Name string
  182. FeedWeight string
  183. AverageDMIasRecipe string
  184. FeedBanciPlan string
  185. Banci1Rate string
  186. Banci2Rate string
  187. Banci3Rate string
  188. Banci4Rate string
  189. StatusID string
  190. StatusText string
  191. OrderNum string
  192. Remark string
  193. Creator string
  194. CreateDate string
  195. }
  196. func main() {
  197. if len(os.Args) < 2 {
  198. logs.Error("参数输入错误:例 : " + `PrconsoleV2.4.exe -a getData -p d:\202101011getData.txt -d 2021-01-01`)
  199. return
  200. }
  201. paraMap := make(map[string]string, 0)
  202. for i := 1; i < len(os.Args); i++ {
  203. if len(os.Args) > i+1 {
  204. paraMap[strings.ToLower(os.Args[i])] = strings.Trim(strings.ToLower(os.Args[i+1]), " ")
  205. } else {
  206. paraMap[strings.ToLower(os.Args[i])] = ""
  207. }
  208. }
  209. strMethod = paraMap["-a"]
  210. strDate = paraMap["-d"]
  211. err := checkDate()
  212. if err != nil {
  213. logs.Error("日期格式错误 例 : 2021-01-01")
  214. return
  215. }
  216. strOutFilePath = strings.ToLower(paraMap["-p"])
  217. FarmCode = setting.DatabaseSetting.FarmCode
  218. setting.Setup("")
  219. models.Setup()
  220. FarmCode = setting.DatabaseSetting.FarmCode
  221. //logs.Error(strMethod,strOutFilePath,strDate,FarmCode)
  222. SelectApi()
  223. }
  224. func SelectApi() {
  225. var err error
  226. switch strMethod {
  227. case "getstate":
  228. err = getstate()
  229. case "getdatamix":
  230. err = checkpathCreat()
  231. if err != nil {
  232. return
  233. }
  234. err = getDataMix()
  235. case "getdata":
  236. err = checkpathCreat()
  237. if err != nil {
  238. return
  239. }
  240. err = getData()
  241. case "getdataresi":
  242. err = checkpathCreat()
  243. if err != nil {
  244. return
  245. }
  246. err = getDataResi()
  247. case "senddatarecipe":
  248. err = checkpathExist()
  249. if err != nil {
  250. return
  251. }
  252. err = sendDataRecipe()
  253. case "senddatafeed":
  254. err = checkpathExist()
  255. if err != nil {
  256. return
  257. }
  258. err = sendDataFeed()
  259. case "feedgdailycost":
  260. err = checkpathExist()
  261. if err != nil {
  262. return
  263. }
  264. err = feedGDailyCost()
  265. default:
  266. fmt.Println("1")
  267. //logs.Info("1")
  268. }
  269. if err == nil {
  270. fmt.Println("1")
  271. //logs.Info("1")
  272. } else {
  273. logs.Error("fail", err)
  274. }
  275. }
  276. func getstate() error {
  277. tx := models.Engine.NewSession()
  278. err := tx.Begin()
  279. defer func() {
  280. switch {
  281. case err != nil:
  282. //logs.Error("__error:", err)
  283. if tx != nil {
  284. tx.Rollback()
  285. }
  286. default:
  287. if tx != nil {
  288. err = tx.Commit()
  289. }
  290. }
  291. if tx != nil {
  292. tx.Close()
  293. }
  294. }()
  295. // 修改栏舍配方
  296. sqlstr := `
  297. ALTER TABLE feed ADD COLUMN xnrFeedName VARCHAR(50) DEFAULT NULL COMMENT '伊启牛饲料名称'
  298. `
  299. _, err = tx.Exec(sqlstr)
  300. if err != nil {
  301. // logs.Error("err", err, )
  302. }
  303. sqlstr = `
  304. ALTER TABLE feedp ADD COLUMN xnrFeedpName VARCHAR(50) DEFAULT NULL COMMENT '伊启牛栏舍名称'
  305. `
  306. _, err = tx.Exec(sqlstr)
  307. if err != nil {
  308. // logs.Error("err", err, )
  309. }
  310. sqlstr = `
  311. ALTER TABLE feedtemplet ADD COLUMN feedtempletCode VARCHAR(50) DEFAULT NULL COMMENT '伊启牛配方模板编码'
  312. `
  313. _, err = tx.Exec(sqlstr)
  314. if err != nil {
  315. // logs.Error("err", err, )
  316. }
  317. sqlstr = `
  318. ALTER TABLE feedtemplet ADD COLUMN xnrFeedtempletName VARCHAR(50) DEFAULT NULL COMMENT '伊启牛配方模板名称'
  319. `
  320. _, err = tx.Exec(sqlstr)
  321. if err != nil {
  322. //logs.Error("err", err, )
  323. }
  324. return nil
  325. }
  326. func getDataMix() error {
  327. sqlstr := `SELECT ? AS FarmCode, DATE_FORMAT(MixDate,'%Y-%m-%d') MixDate,RecipeCode,RecipeName,GroupTypeCode,GroupTypeName,Banci,MixBatch,
  328. DATE_FORMAT(MixStartTime,'%Y-%m-%d %H:%i:%s')MixStartTime, DATE_FORMAT(MixEndTime,'%Y-%m-%d %H:%i:%s') MixEndTime,MixRound, DATE_FORMAT(OutFeedTime,'%Y-%m-%d %H:%i:%s') OutFeedTime,
  329. SUM(FeedWeightTotal) AS FeedWeightTotal,FeedOrder,FeedCode,FeedName,
  330. FeedDryMatter, DATE_FORMAT(FeedAddTime,'%Y-%m-%d %H:%i:%s') FeedAddTime ,SUM(RecipeWeight) AS RecipeWeight,SUM(SystemWeight) AS SystemWeight, DATE_FORMAT(FDate,'%Y-%m-%d %H:%i:%s') FDate,DStatus,UserName,UserTrueName
  331. FROM(
  332. SELECT d.Mydate AS MixDate,
  333. IFNULL((SELECT feedtempletCode FROM feedtemplet WHERE id = d2.feedtempletId),'') AS RecipeCode,
  334. d2.feedtempletName AS RecipeName,
  335. (SELECT GroupTypeCode FROM xnr_senddatarecipe WHERE RecipeName=d2.feedtempletName ORDER BY id DESC LIMIT 1) AS GroupTypeCode,
  336. (SELECT GroupTypeName FROM xnr_senddatarecipe WHERE RecipeName=d2.feedtempletName ORDER BY id DESC LIMIT 1) AS GroupTypeName,
  337. CASE WHEN (d.Times = 1) OR (d.Times = 2) THEN 1 WHEN (d.Times = 3) OR (d.Times = 4) THEN 2 WHEN (d.Times = 5) OR (d.Times = 6) THEN 3 END AS Banci,
  338. CEIL(SUBSTRING_INDEX(d.ProjName,'.', -1)) AS MixBatch,
  339. (SELECT MIN(downloadplandtl1_exec.InTime) FROM downloadplandtl1_exec WHERE downloadplandtl1_exec.PID = d.id GROUP BY d.id) AS MixStartTime,
  340. (SELECT MAX(downloadplandtl1_exec.InTime) FROM downloadplandtl1_exec WHERE downloadplandtl1_exec.PID = d.id GROUP BY d.id) AS MixEndTime,'' AS MixRound,
  341. d1exe.InTime AS OutFeedTime,
  342. (SELECT SUM(downloadplandtl1.ActualWeightMinus) FROM downloadplandtl1 WHERE downloadplandtl1.PID = d.id GROUP BY d.id) AS FeedWeightTotal,d1exe.sort AS FeedOrder,
  343. feed.feedcode AS FeedCode,d1.Fname AS FeedName,feed.dry AS FeedDryMatter,
  344. IFNULL(DATE_SUB(d1exe.Intime, INTERVAL d1exe.processTime HOUR_SECOND),d1exe.Intime) AS FeedAddTime,d1.LWEIGHT AS RecipeWeight,
  345. d1.ActualWeightMinus AS SystemWeight,NOW() AS FDate,1 AS DStatus,driver.ID AS UserName,driver.drivername AS UserTrueName
  346. FROM downloadplandtl1 d1 LEFT JOIN downloadedplan d ON d.id = d1.PID LEFT JOIN downloadplandtl1_exec d1exe ON d1.PID = d1exe.PID AND d1.SORT=d1exe.SORT
  347. LEFT JOIN driver ON d.DriverID = driver.ID
  348. LEFT JOIN feed ON d1.FID = feed.ID
  349. LEFT JOIN (SELECT downloadplandtl2.* FROM downloadplandtl2 JOIN downloadedplan ON downloadplandtl2.pid=downloadedplan.id
  350. WHERE downloadedplan.lpplanType!=1 AND downloadedplan.Mydate= ?
  351. GROUP BY pid) d2 ON d2.pid=d.id
  352. WHERE d.Mydate = ? AND d.IsCompleted = 1
  353. ) a
  354. WHERE FeedName != '需加料' GROUP BY RecipeName,Banci,MixBatch,feedname
  355. UNION ALL
  356. SELECT ? AS FarmCode, DATE_FORMAT(MixDate,'%Y-%m-%d') MixDate,RecipeCode,RecipeName,GroupTypeCode,GroupTypeName,Banci,MixBatch,
  357. DATE_FORMAT(MixStartTime,'%Y-%m-%d %H:%i:%s')MixStartTime, DATE_FORMAT(MixEndTime,'%Y-%m-%d %H:%i:%s') MixEndTime,MixRound, DATE_FORMAT(OutFeedTime,'%Y-%m-%d %H:%i:%s') OutFeedTime,
  358. SUM(FeedWeightTotal) AS FeedWeightTotal,FeedOrder,FeedCode,FeedName,
  359. FeedDryMatter, DATE_FORMAT(FeedAddTime,'%Y-%m-%d %H:%i:%s') FeedAddTime ,SUM(RecipeWeight) AS RecipeWeight,SUM(SystemWeight) AS SystemWeight, DATE_FORMAT(FDate,'%Y-%m-%d %H:%i:%s') FDate,DStatus,UserName,UserTrueName FROM(
  360. SELECT d.Mydate AS MixDate,
  361. IFNULL((SELECT feedtempletCode FROM feedtemplet WHERE id = d2.feedtempletId),'') AS RecipeCode,
  362. d2.feedtempletName AS RecipeName,
  363. (SELECT GroupTypeCode FROM xnr_senddatarecipe WHERE RecipeName=d2.feedtempletName ORDER BY id DESC LIMIT 1) AS GroupTypeCode,
  364. (SELECT GroupTypeName FROM xnr_senddatarecipe WHERE RecipeName=d2.feedtempletName ORDER BY id DESC LIMIT 1) AS GroupTypeName,
  365. CASE WHEN (d.Times = 1) OR (d.Times = 2) THEN 1 WHEN (d.Times = 3) OR (d.Times = 4) THEN 2 WHEN (d.Times = 5) OR (d.Times = 6) THEN 3 END AS Banci,
  366. CEIL(SUBSTRING_INDEX(d.ProjName, '.', -1)) AS MixBatch,
  367. (SELECT MIN(downloadplandtl1_exec.InTime) FROM downloadplandtl1_exec WHERE downloadplandtl1_exec.PID = d.id GROUP BY d.id) AS MixStartTime,
  368. (SELECT MAX(downloadplandtl1_exec.InTime) FROM downloadplandtl1_exec WHERE downloadplandtl1_exec.PID = d.id GROUP BY d.id) AS MixEndTime, ''AS MixRound,
  369. d1exe.InTime AS OutFeedTime,
  370. (SELECT SUM(downloadplandtl1.ActualWeightMinus) FROM downloadplandtl1 WHERE downloadplandtl1.PID = d.id GROUP BY d.id) AS FeedWeightTotal,d1exe.sort AS FeedOrder,
  371. feed.feedcode AS FeedCode,d1.Fname AS FeedName,feed.dry AS FeedDryMatter,
  372. IFNULL(DATE_SUB(d1exe.Intime, INTERVAL d1exe.processTime HOUR_SECOND),d1exe.Intime) AS FeedAddTime,d1.LWEIGHT AS RecipeWeight,
  373. d1.ActualWeightMinus AS SystemWeight,NOW() AS FDate,1 AS DStatus,driver.ID AS UserName,driver.drivername AS UserTrueName
  374. FROM downloadplandtl1 d1 LEFT JOIN downloadedplan d ON d.id = d1.PID LEFT JOIN downloadplandtl1_exec d1exe ON d1.PID = d1exe.PID AND d1.SORT=d1exe.SORT
  375. LEFT JOIN driver ON d.DriverID = driver.ID
  376. LEFT JOIN feed ON d1.FID = feed.ID
  377. LEFT JOIN (SELECT downloadplandtl2.* FROM downloadplandtl2 JOIN downloadedplan ON downloadplandtl2.pid=downloadedplan.id
  378. WHERE downloadedplan.lpplanType!=1 AND downloadedplan.Mydate= ?
  379. GROUP BY pid) d2 ON d2.flpid=d.id
  380. WHERE d.Mydate = ? AND d.IsCompleted = 1 AND d2.FLPID IS NOT NULL
  381. ) a GROUP BY RecipeName,Banci,MixBatch,feedname`
  382. valuesMap, err := models.Engine.SQL(sqlstr, FarmCode, strDate, strDate, FarmCode, strDate, strDate).QueryString()
  383. if err != nil {
  384. logs.Error("err", err, len(valuesMap))
  385. return err
  386. }
  387. W := DataXMLMix{}
  388. W.Head = XMLHead{
  389. SystemCode: "TMR",
  390. MappingID: FarmCode,
  391. Type: "TMRDataMix",
  392. FarmCode: FarmCode,
  393. DataNum: strconv.Itoa(len(valuesMap)),
  394. Version: "3",
  395. }
  396. var records []XMLRecordMix
  397. strtem, err := json.Marshal(valuesMap)
  398. if err != nil {
  399. logs.Error("json.Marshal err", err)
  400. }
  401. err = json.Unmarshal(strtem, &records)
  402. if err != nil {
  403. logs.Error("json.Unmarshal err", err)
  404. }
  405. W.Record = append(W.Record, records...)
  406. output1, _ := xml.MarshalIndent(W, " ", " ")
  407. f, err := os.Create(strOutFilePath)
  408. if err != nil {
  409. logs.Error("os.Open err", err)
  410. return err
  411. }
  412. output1 = append([]byte(xml.Header), output1...)
  413. _, err = f.Write(output1)
  414. if err != nil {
  415. logs.Error("f.Write err", err)
  416. return err
  417. }
  418. f.Close()
  419. return nil
  420. }
  421. func getData() error {
  422. sqlstr := ` SELECT ? AS FarmCode,id,DATE_FORMAT(FeedDate,'%Y-%m-%d') AS FeedDate,Banci,BarName,GroupTypeCode,GroupTypeName,CowAmount,
  423. PCows,RecipeCode,RecipeName,MixBatch,
  424. DATE_FORMAT(BarStartTime,'%Y-%m-%d %H:%i:%s') BarStartTime,DATE_FORMAT(BarEndTime,'%Y-%m-%d %H:%i:%s') BarEndTime,
  425. SUM(BarRecipePlanWeight) AS BarRecipePlanWeight,
  426. SUM(BarRecipeRealWeight) AS BarRecipeRealWeight,
  427. FeedCode,FeedName,
  428. SUM(ROUND(BarFeedPlanWeight / CCountRatio * BarRecipePlanWeight,2)) AS BarFeedPlanWeight,
  429. SUM(ROUND(BarFeedRealWeight / aCCountRatio * BarRecipeRealWeight,2)) AS BarFeedRealWeight,
  430. DATE_FORMAT(FDate,'%Y-%m-%d %H:%i:%s') FDate,IF(d1fname <> 0,d1fname,IF(ismodify=0,1,3)) DStatus,UserName,UserTrueName
  431. FROM (
  432. SELECT d.id,d.Mydate AS FeedDate,
  433. IF(
  434. (SELECT COUNT(*) FROM recweight r WHERE d2.Intime = r.MTime AND r.Weight = d2.ActualWeight )>0,
  435. IF((SELECT COUNT(*) FROM recweight r WHERE r.MTime = (SELECT dee.intime FROM downloadplandtl1_exec dee WHERE dee.pid = d1.pid AND dee.sort= d1.sort)
  436. AND r.Weight = (SELECT dee.ActualWeight FROM downloadplandtl1_exec dee WHERE dee.pid = d1.pid AND dee.sort= d1.sort) ) >0,0,3),3) ismodify,
  437. IF(ABS((SELECT SUM(ActualWeightMinus) FROM downloadplandtl1_exec de WHERE de.PID = d1.PID AND de.sort = d1.sort ) -
  438. (SELECT SUM(ActualWeightMinus) FROM downloadplandtl1 de WHERE de.PID = d1.PID AND de.sort = d1.sort ))<1,0,2 ) d1fname,
  439. CASE WHEN (d.Times = 1) OR (d.Times = 2) THEN 1 WHEN (d.Times = 3) OR (d.Times = 4) THEN 2 WHEN (d.Times = 5) OR (d.Times = 6) THEN 3 END AS Banci,
  440. d2.Fname AS BarName,
  441. IFNULL((SELECT DeptGroupType_Code FROM xnr_feedgdailycost WHERE Group_Name=d2.Fname ORDER BY id DESC LIMIT 1),
  442. (SELECT GroupTypeCode FROM xnr_senddatarecipe WHERE RecipeName =d2.feedtempletName ORDER BY id DESC LIMIT 1)
  443. ) AS GroupTypeCode,
  444. IFNULL( (SELECT DeptGroupType_Name FROM xnr_feedgdailycost WHERE Group_Name=d2.Fname ORDER BY id DESC LIMIT 1),
  445. (SELECT GroupTypeName FROM xnr_senddatarecipe WHERE RecipeName =d2.feedtempletName ORDER BY id DESC LIMIT 1)
  446. ) AS GroupTypeName,
  447. d2.cowcount AS CowAmount,
  448. d2.cowcount*CCountRatio/100 AS PCows,
  449. (SELECT feedtempletCode FROM feedtemplet WHERE id = d2.feedtempletId) AS RecipeCode,
  450. d2.feedtempletName AS RecipeName,
  451. CEIL(SUBSTRING_INDEX(d.ProjName, '.', -1)) AS MixBatch,
  452. IFNULL(DATE_SUB(d2.Intime, INTERVAL d2.processTime HOUR_SECOND),d2.intime) AS BarStartTime,d2.Intime AS BarEndTime,
  453. d2.LWEIGHT AS BarRecipePlanWeight,d2.ActualWeightMinus AS BarRecipeRealWeight,feed.feedcode AS FeedCode,d1.Fname AS FeedName,
  454. d1.LWEIGHT AS BarFeedPlanWeight,d1.ActualWeightMinus AS BarFeedRealWeight,NOW() AS FDate,1 AS DStatus,driver.ID AS UserName,driver.drivername AS UserTrueName,
  455. (SELECT SUM(dd1.lweight) FROM downloadplandtl1 dd1 INNER JOIN downloadedplan dd ON dd1.PID = dd.id WHERE dd1.PID = d.ID AND dd.IsCompleted = 1) AS CCountRatio,
  456. (SELECT SUM(ActualWeightMinus) FROM downloadplandtl1 dd1 INNER JOIN downloadedplan dd ON dd1.PID = dd.id WHERE dd1.PID = d.ID AND dd.IsCompleted = 1) AS aCCountRatio
  457. FROM downloadedplan d INNER JOIN downloadplandtl1 d1 ON d.id = d1.PID
  458. INNER JOIN downloadplandtl2 d2 ON d.id = d2.PID INNER JOIN feed ON d1.FID=feed.id
  459. left JOIN driver ON d.DriverID = driver.ID
  460. WHERE d.Mydate = ? AND d.IsCompleted = 1
  461. ) a GROUP BY BarName,FeedName,banci
  462. UNION ALL
  463. SELECT ? AS FarmCode,id,DATE_FORMAT(FeedDate,'%Y-%m-%d') FeedDate,Banci,BarName,GroupTypeCode,GroupTypeName,CowAmount,
  464. PCows,RecipeCode,RecipeName,MixBatch,
  465. DATE_FORMAT(BarStartTime,'%Y-%m-%d %H:%i:%s') BarStartTime,DATE_FORMAT(BarEndTime,'%Y-%m-%d %H:%i:%s')BarEndTime,
  466. SUM(BarRecipePlanWeight) AS BarRecipePlanWeight,
  467. SUM(BarRecipeRealWeight) AS BarRecipeRealWeight,
  468. FeedCode,FeedName,
  469. SUM(ROUND(BarFeedPlanWeight / CCountRatio * BarRecipePlanWeight,2)) AS BarFeedPlanWeight,
  470. SUM(ROUND(BarFeedRealWeight / aCCountRatio * BarRecipeRealWeight,2)) AS BarFeedRealWeight,
  471. DATE_FORMAT(FDate,'%Y-%m-%d %H:%i:%s') FDate, IF(d1fname <> 0,d1fname,IF(ismodify=0,1,3)) DStatus,UserName,UserTrueName
  472. FROM (
  473. SELECT d.id,d.Mydate AS FeedDate,
  474. IF(
  475. (SELECT COUNT(*) FROM recweight r WHERE d2.Intime = r.MTime AND r.Weight = d2.ActualWeight )>0,
  476. IF((SELECT COUNT(*) FROM recweight r WHERE r.MTime = (SELECT dee.intime FROM downloadplandtl1_exec dee WHERE dee.pid = d1.pid AND dee.sort= d1.sort)
  477. AND r.Weight = (SELECT dee.ActualWeight FROM downloadplandtl1_exec dee WHERE dee.pid = d1.pid AND dee.sort= d1.sort) ) >0,0,3),3) ismodify,
  478. IF((SELECT SUM(ActualWeightMinus) FROM downloadplandtl1_exec de WHERE de.PID = d1.PID AND de.sort = d1.sort ) =
  479. (SELECT SUM(ActualWeightMinus) FROM downloadplandtl1 de WHERE de.PID = d1.PID AND de.sort = d1.sort ),0,2 ) d1fname,
  480. CASE WHEN (d.Times = 1) OR (d.Times = 2) THEN 1 WHEN (d.Times = 3) OR (d.Times = 4) THEN 2 WHEN (d.Times = 5) OR (d.Times = 6) THEN 3 END AS Banci,
  481. d2.Fname AS BarName,
  482. IFNULL((SELECT DeptGroupType_Code FROM xnr_feedgdailycost WHERE Group_Name=d2.Fname ORDER BY id DESC LIMIT 1),
  483. (SELECT GroupTypeCode FROM xnr_senddatarecipe WHERE RecipeName =d2.Fname ORDER BY id DESC LIMIT 1)
  484. ) AS GroupTypeCode,
  485. IFNULL( (SELECT DeptGroupType_Name FROM xnr_feedgdailycost WHERE Group_Name=d2.Fname ORDER BY id DESC LIMIT 1),
  486. (SELECT GroupTypeCode FROM xnr_senddatarecipe WHERE RecipeName =d2.Fname ORDER BY id DESC LIMIT 1)
  487. ) AS GroupTypeName,
  488. d2.cowcount AS CowAmount,
  489. d2.cowcount*CCountRatio/100 AS PCows,
  490. (SELECT feedtempletCode FROM feedtemplet WHERE id = d2.feedtempletId) AS RecipeCode,
  491. d2.feedtempletName AS RecipeName,
  492. CEIL(SUBSTRING_INDEX(d.ProjName, '.', -1)) AS MixBatch,
  493. IFNULL(DATE_SUB(d2.Intime, INTERVAL d2.processTime HOUR_SECOND),d2.intime) AS BarStartTime,d2.Intime AS BarEndTime,
  494. d2.LWEIGHT AS BarRecipePlanWeight,d2.ActualWeightMinus AS BarRecipeRealWeight,feed.feedcode AS FeedCode,d1.Fname AS FeedName,
  495. d1.LWEIGHT AS BarFeedPlanWeight,d1.ActualWeightMinus AS BarFeedRealWeight,NOW() AS FDate,1 AS DStatus,driver.ID AS UserName,driver.drivername AS UserTrueName,
  496. (SELECT SUM(dd1.lweight) FROM downloadplandtl1 dd1 INNER JOIN downloadedplan dd ON dd1.PID = dd.id WHERE dd1.PID = d.ID AND dd.IsCompleted = 1) AS CCountRatio,
  497. (SELECT SUM(ActualWeightMinus) FROM downloadplandtl1 dd1 INNER JOIN downloadedplan dd ON dd1.PID = dd.id WHERE dd1.PID = d.ID AND dd.IsCompleted = 1) AS aCCountRatio
  498. FROM downloadedplan d INNER JOIN downloadplandtl1 d1 ON d.id = d1.PID
  499. INNER JOIN downloadplandtl2 d2 ON d.id = d2.FLPID INNER JOIN feed ON d1.FID=feed.id
  500. left JOIN driver ON d.DriverID = driver.ID
  501. WHERE d.Mydate = ? AND d.IsCompleted = 1 AND d2.FLPID IS NOT NULL
  502. ) a GROUP BY BarName,FeedName,banci `
  503. valuesMap, err := models.Engine.SQL(sqlstr, FarmCode, strDate, FarmCode, strDate).QueryString()
  504. //valuesMap, err := models.Engine.SQL(sqlstr).QueryString()
  505. if err != nil {
  506. logs.Error("err", err, len(valuesMap))
  507. logs.Error("err", err, len(valuesMap))
  508. return err
  509. }
  510. W := DataXML{}
  511. W.Head = XMLHead{
  512. SystemCode: "TMR",
  513. MappingID: FarmCode,
  514. Type: "TMRData",
  515. FarmCode: FarmCode,
  516. DataNum: strconv.Itoa(len(valuesMap)),
  517. Version: "3",
  518. }
  519. var records []XMLRecord
  520. strtem, err := json.Marshal(valuesMap)
  521. if err != nil {
  522. logs.Error("json.Marshal err", err)
  523. logs.Error("json.Marshal err", err)
  524. }
  525. err = json.Unmarshal(strtem, &records)
  526. if err != nil {
  527. logs.Error("json.Unmarshal err", err)
  528. logs.Error("json.Unmarshal err", err)
  529. }
  530. W.Record = append(W.Record, records...)
  531. output1, _ := xml.MarshalIndent(W, " ", " ")
  532. f, err := os.Create(strOutFilePath)
  533. if err != nil {
  534. logs.Error("os.Open err", err)
  535. logs.Error("os.Open err", err)
  536. return err
  537. }
  538. output1 = append([]byte(xml.Header), output1...)
  539. _, err = f.Write(output1)
  540. if err != nil {
  541. logs.Error("f.Write err", err)
  542. return err
  543. }
  544. f.Close()
  545. return nil
  546. }
  547. func getDataResi() error {
  548. sqlstr := ` SELECT ? AS FarmCode, DATE_FORMAT(DATE_SUB(d.Mydate, INTERVAL 1 DAY),'%Y-%m-%d') AS FeedDate,DATE_FORMAT(d.Mydate,'%Y-%m-%d') AS ResiDate,d2.Fname AS BarName,
  549. '' AS GroupTypeCode,d2.cowclassname AS GroupTypeName,barfeedremain.Remain AS ResiWeight,'' AS ResiGroupTypeCode,bar.class AS ResiGroupTypeName,
  550. DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s') AS FDate,1 AS DStatus,driver.ID AS UserName,driver.drivername AS UserTrueName
  551. FROM downloadedplan d INNER JOIN downloadplandtl2 d2 ON d.id = d2.PID
  552. JOIN barfeedremain ON d2.FBarID = barfeedremain.barid
  553. AND DATE_ADD(d.Mydate,INTERVAL -1 DAY) = barfeedremain.RemainDate
  554. LEFT JOIN feedp ON feedp.barid = barfeedremain.barid
  555. LEFT JOIN bar ON feedp.barid = bar.id
  556. left JOIN driver ON d.DriverID = driver.ID
  557. WHERE d.Mydate = ? AND d.IsCompleted = 1 `
  558. valuesMap, err := models.Engine.SQL(sqlstr, FarmCode, strDate).QueryString()
  559. if err != nil {
  560. logs.Error("select downloadedplan err", err, len(valuesMap))
  561. return err
  562. }
  563. W := DataXMLResi{}
  564. W.Head = XMLHead{
  565. SystemCode: "TMR",
  566. MappingID: FarmCode,
  567. Type: "TMRDataResi",
  568. FarmCode: FarmCode,
  569. DataNum: strconv.Itoa(len(valuesMap)),
  570. Version: "3",
  571. }
  572. var records []XMLRecordResi
  573. strtem, err := json.Marshal(valuesMap)
  574. if err != nil {
  575. logs.Error("json.Marshal err", err)
  576. }
  577. err = json.Unmarshal(strtem, &records)
  578. if err != nil {
  579. logs.Error("json.Unmarshal err", err)
  580. }
  581. W.Record = append(W.Record, records...)
  582. output1, _ := xml.MarshalIndent(W, " ", " ")
  583. f, err := os.Create(strOutFilePath)
  584. if err != nil {
  585. logs.Error("os.Open err", err)
  586. return err
  587. }
  588. output1 = append([]byte(xml.Header), output1...)
  589. _, err = f.Write(output1)
  590. if err != nil {
  591. logs.Error("f.Write err", err)
  592. return err
  593. }
  594. f.Close()
  595. return nil
  596. }
  597. // 判断集合是否包含某个字符串
  598. func contains(slice []string, str string) bool {
  599. for _, s := range slice {
  600. if s == str {
  601. return true
  602. }
  603. }
  604. return false
  605. }
  606. func sendDataRecipe() error {
  607. tx := models.Engine.NewSession()
  608. err := tx.Begin()
  609. defer func() {
  610. switch {
  611. case err != nil:
  612. logs.Error("__error:", err)
  613. if tx != nil {
  614. tx.Rollback()
  615. }
  616. default:
  617. if tx != nil {
  618. err = tx.Commit()
  619. }
  620. }
  621. if tx != nil {
  622. tx.Close()
  623. }
  624. }()
  625. _, err = tx.Exec("delete from xnr_senddatarecipe where getDate = ?", strDate)
  626. if err != nil {
  627. logs.Error("delete xnr_senddatarecipe err", err)
  628. }
  629. f, err := os.Open(strOutFilePath)
  630. if err != nil {
  631. logs.Error("os.Open err", err)
  632. }
  633. input := make([]byte, 100000)
  634. _, err = f.Read(input)
  635. if err != nil {
  636. logs.Error("f.Read err", err)
  637. }
  638. W := Senddatarecipe{}
  639. err = xml.Unmarshal(input, &W)
  640. if err != nil {
  641. //logs.Error("xml.Unmarshal err", err,W)
  642. }
  643. sqlstr := `INSERT INTO xnr_senddatarecipe (
  644. getDate,getTime,FarmCode,RecipeCode,RecipeName,RecipeVersion,GroupTypeCode,GroupTypeName,RecipeType,FeedCode,FeedName,DryMatter,JiaobanTime,JiaobanOrder,BDWeight,BDTime,FeedWeight,FeedWeightRate,StartDate,LastAdjDate,FDate,DStatus,UserName,UserTrueName
  645. ) VALUES ( ?,NOW(),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ) `
  646. // 新增新的明细
  647. for _, v := range W.Record {
  648. _, err = tx.Exec(sqlstr, strDate, v.FarmCode, v.RecipeCode, v.RecipeName, transtr(v.RecipeVersion), v.GroupTypeCode, v.GroupTypeName,
  649. transtr(v.RecipeVersion), v.FeedCode, v.FeedName, transtr(v.DryMatter), transtr(v.JiaobanTime), transtr(v.JiaobanOrder), transtr(v.BDWeight),
  650. transtr(v.BDTime), transtr(v.FeedWeight), transtr(v.FeedWeightRate), trandate(v.StartDate), trandate(v.LastAdjDate), trandate(v.FDate), v.DStatus, v.UserName, v.UserTrueName)
  651. if err != nil {
  652. logs.Error("insert xnr_senddatarecipe err"+sqlstr, err)
  653. }
  654. }
  655. // 修改饲料
  656. sqlstr = `
  657. UPDATE feedtemplet SET TNAME=CONCAT(LEFT(TNAME,4),'_del'),
  658. feedtempletCode=NULL,xnrFeedtempletName=NULL WHERE isDelete=1
  659. `
  660. _, _ = tx.Exec(sqlstr)
  661. // 修改饲料
  662. sqlstr = `
  663. UPDATE feed
  664. JOIN xnr_senddatarecipe xf ON feed.feedCode = xf.FeedCode
  665. SET
  666. feed.xnrFeedName=xf.FeedName,feed.ENABLE=1
  667. WHERE xf.getDate=? `
  668. _, err = tx.Exec(sqlstr, strDate)
  669. if err != nil {
  670. logs.Error("UPDATE feed1 err", err)
  671. }
  672. // 修改饲料
  673. // sqlstr = `
  674. // UPDATE feed
  675. // JOIN xnr_senddatarecipe xf ON feed.xnrFeedName = xf.FeedName OR feed.FNAME = xf.FeedName
  676. // SET
  677. // feed.xnrFeedName=xf.FeedName,feed.feedCode=xf.FeedCode,feed.ENABLE=1
  678. // WHERE xf.getDate=?
  679. //`
  680. // _, err =tx.Exec(sqlstr,strDate)
  681. // if err != nil {
  682. // logs.Error(" update feed2 err", err)
  683. // }
  684. //新增饲料
  685. sqlstr = `
  686. INSERT INTO feed(feedcode,FNAME,xnrFeedName,FCLASS,ENABLE)
  687. SELECT xf.FeedCode,xf.FeedName,xf.FeedName,
  688. IFNULL((SELECT ID FROM feedclass WHERE FCNAME='其它' OR FCNAME='其他' LIMIT 1),0)ww,1
  689. FROM xnr_senddatarecipe xf
  690. WHERE xf.getDate=? AND (SELECT COUNT(*) FROM feed WHERE feed.feedCode = xf.FeedCode) = 0
  691. GROUP BY xf.FeedCode `
  692. _, err = tx.Exec(sqlstr, strDate)
  693. if err != nil {
  694. logs.Error(" INSERT feed err", err)
  695. }
  696. // 修改饲料干物质
  697. sqlstr = `
  698. UPDATE feed
  699. JOIN xnr_senddatarecipe xf ON feed.feedCode = xf.FeedCode
  700. SET
  701. feed.dry=ROUND(xf.DryMatter/100,4)
  702. WHERE xf.getDate=? AND xf.DryMatter IS NOT NULL
  703. `
  704. _, err = tx.Exec(sqlstr, strDate)
  705. if err != nil {
  706. logs.Error("update feed3 err", err)
  707. }
  708. // 修改配方模板
  709. sqlstr = `
  710. UPDATE feedtemplet ft
  711. JOIN xnr_senddatarecipe xf ON ft.feedtempletCode = xf.RecipeCode OR ft.xnrFeedtempletName = xf.RecipeName
  712. OR ft.TNAME = xf.RecipeName
  713. SET
  714. ft.TNAME=xf.RecipeName,ft.feedtempletCode=xf.RecipeCode,
  715. ft.xnrFeedtempletName=xf.RecipeName,
  716. ft.OWNER = xf.UserTrueName,
  717. ft.REMARK = '伊启牛配方',
  718. ft.CCID = IFNULL((SELECT id FROM cowclass WHERE CLASSNAME= xf.GroupTypeName LIMIT 1),3)
  719. WHERE xf.getDate=? `
  720. _, err = tx.Exec(sqlstr, strDate)
  721. if err != nil {
  722. logs.Error(" update feedtemplet1 err", err)
  723. }
  724. //新增配方模板
  725. sqlstr = `
  726. INSERT INTO feedtemplet(TNAME,feedtempletCode,xnrFeedtempletName,OWNER,CCID,REMARK)
  727. SELECT xf.RecipeName,xf.RecipeCode,xf.RecipeName,xf.UserTrueName,
  728. IFNULL((SELECT id FROM cowclass WHERE CLASSNAME= xf.GroupTypeName LIMIT 1),3),'伊启牛配方'
  729. FROM xnr_senddatarecipe xf
  730. LEFT JOIN feedtemplet ft ON ft.feedtempletCode = xf.RecipeCode OR ft.xnrFeedtempletName = xf.RecipeName
  731. OR ft.TNAME = xf.RecipeName
  732. WHERE xf.getDate=? AND ft.id IS NULL GROUP BY xf.RecipeCode
  733. `
  734. _, err = tx.Exec(sqlstr, strDate)
  735. if err != nil {
  736. logs.Error(" INSERT feedtemplet2 err", err)
  737. }
  738. //修改配方模板子表
  739. sqlstr = ` DELETE ftdetail
  740. FROM feedtemplet
  741. JOIN ftdetail ON ftdetail.FTID = feedtemplet.id
  742. WHERE feedtemplet.feedtempletCode IN(
  743. SELECT RecipeCode FROM xnr_senddatarecipe WHERE getDate=? GROUP BY RecipeCode
  744. ) `
  745. _, err = tx.Exec(sqlstr, strDate)
  746. if err != nil {
  747. logs.Error("UPDATE ftdetail err", err)
  748. }
  749. //新增配方模板子表
  750. // sqlstr = `
  751. // DELETE ftd
  752. // FROM ftdetail ftd
  753. // LEFT JOIN feed ON feed.id = ftd.FID
  754. // LEFT JOIN feedtemplet ft ON ftd.FTID = ft.id
  755. // LEFT JOIN xnr_senddatarecipe xf
  756. // ON ft.feedtempletCode = xf.RecipeCode AND feed.feedCode = xf.FeedCode
  757. // AND xf.getDate = ?
  758. // WHERE xf.id IS NULL AND ft.feedtempletCode IN(SELECT RecipeCode FROM xnr_senddatarecipe xf1 WHERE xf1.getDate = ? GROUP BY RecipeCode)
  759. // `
  760. // _, err = tx.Exec(sqlstr, strDate, strDate)
  761. // if err != nil {
  762. // logs.Error("delete ftdetail err", err)
  763. // }
  764. //新增配方模板子表
  765. sqlstr = `
  766. INSERT INTO ftdetail(FTID,FID,FWEIGHT,packageSumWeight,sort,autosecond,autosecondname,feedgroup,fname)
  767. SELECT ft.id,(SELECT id FROM feed WHERE feed.feedCode = xf.FeedCode LIMIT 1) feedcode,xf.FeedWeight,0,xf.JiaobanOrder,0,'禁用',
  768. IF((SELECT COUNT(1) FROM ftdetail WHERE ftid = ft.id AND sort = xf.JiaobanOrder ) > 0 ,
  769. (SELECT feedgroup FROM ftdetail WHERE ftid = ft.id AND sort = xf.JiaobanOrder ) ,
  770. (SELECT fname FROM feed WHERE feed.feedCode =xf.FeedCode LIMIT 1)),
  771. (SELECT fname FROM feed WHERE feed.feedCode =xf.FeedCode LIMIT 1)
  772. FROM xnr_senddatarecipe xf
  773. LEFT JOIN feedtemplet ft ON ft.feedtempletCode =xf.RecipeCode OR ft.xnrFeedtempletName = xf.RecipeName OR ft.TNAME = xf.RecipeName
  774. WHERE xf.getDate=? GROUP BY ft.id,feedcode`
  775. _, err = tx.Exec(sqlstr, strDate)
  776. if err != nil {
  777. logs.Error(" INSERT ftdetail err", err)
  778. }
  779. return nil
  780. }
  781. func sendDataFeed() error {
  782. tx := models.Engine.NewSession()
  783. err := tx.Begin()
  784. defer func() {
  785. switch {
  786. case err != nil:
  787. logs.Error("__error:", err)
  788. if tx != nil {
  789. tx.Rollback()
  790. }
  791. default:
  792. if tx != nil {
  793. err = tx.Commit()
  794. }
  795. }
  796. if tx != nil {
  797. tx.Close()
  798. }
  799. }()
  800. _, err = tx.Exec("delete from xnr_senddatafeed where getDate = ?", strDate)
  801. if err != nil {
  802. logs.Error("delete err", err)
  803. }
  804. f, err := os.Open(strOutFilePath)
  805. if err != nil {
  806. logs.Error("os.Open err", err)
  807. }
  808. input := make([]byte, 100000)
  809. _, err = f.Read(input)
  810. if err != nil {
  811. logs.Error("f.Read err", err)
  812. }
  813. W := Senddatafeed{}
  814. err = xml.Unmarshal(input, &W)
  815. if err != nil {
  816. //logs.Error("xml.Unmarshal err", err,W)
  817. }
  818. sqlstr := `INSERT INTO xnr_senddatafeed (
  819. getDate,getTime,FarmCode,FeedCode,FeedName,Goods_Spec,Goods_Unit,Class2_Name,FDate,DStatus,UserName,UserTrueName
  820. )
  821. VALUES
  822. (
  823. ?,NOW(),?,?,?,?,?,?,?,?,?,?
  824. )
  825. `
  826. for _, v := range W.Record {
  827. _, err = tx.Exec(sqlstr, strDate, v.FarmCode, v.FeedCode, v.FeedName, v.Goods_Spec, v.Goods_Unit, v.Class2_Name, trandate(v.FDate), transtr(v.DStatus), v.UserName, v.UserTrueName)
  828. if err != nil {
  829. logs.Error("insert err", err)
  830. }
  831. }
  832. _, err = tx.Exec("call xnr_synfeeddata(?)", strDate)
  833. if err != nil {
  834. logs.Error("call xnr_synfeeddata err", err)
  835. }
  836. return nil
  837. }
  838. func feedGDailyCost() error {
  839. tx := models.Engine.NewSession()
  840. err := tx.Begin()
  841. defer func() {
  842. //switch {
  843. //case err != nil:
  844. // logs.Error("__error:", err)
  845. // if tx != nil {
  846. // tx.Rollback()
  847. // }
  848. //default:
  849. // if tx != nil {
  850. // err = tx.Commit()
  851. // }
  852. //}
  853. err = tx.Commit()
  854. if tx != nil {
  855. tx.Close()
  856. }
  857. }()
  858. _, err = tx.Exec("delete from xnr_feedgdailycost where getDate = ?", strDate)
  859. if err != nil {
  860. logs.Error("delete err", err)
  861. }
  862. f, err := os.Open(strOutFilePath)
  863. if err != nil {
  864. logs.Error("os.Open err", err)
  865. }
  866. input := make([]byte, 100000)
  867. _, err = f.Read(input)
  868. if err != nil {
  869. logs.Error("f.Read err", err)
  870. }
  871. W := Feedgdailycost{}
  872. err = xml.Unmarshal(input, &W)
  873. if err != nil {
  874. logs.Error("xml.Unmarshal err", err, W)
  875. }
  876. sqlstr := `INSERT INTO xnr_feedgdailycost (
  877. getDate,getTime,FarmCode,GDailyCostListID,FeedName,FeedTMR_Date,DeptGroupType_Code,DeptGroupType_Name,Group_Name,RealCowAmount,
  878. PCows,FeedCowAmount,FeedRecipe_Code,FeedRecipe_Verson,FeedRecipe_Name,FeedWeight,AverageDMIasRecipe,
  879. FeedBanciPlan,Banci1Rate,Banci2Rate,Banci3Rate,Banci4Rate,StatusID,StatusText,OrderNum,Remark,Creator,CreateDate
  880. )
  881. VALUES
  882. (
  883. ?,NOW(),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?
  884. )
  885. `
  886. for _, v := range W.Record {
  887. _, err = tx.Exec(sqlstr, strDate, v.FarmCode, transtr(v.GDailyCostListID), v.FeedName, trandate(v.FeedTMR_Date),
  888. v.DeptGroupType_Code, v.DeptGroupType_Name, v.Group_Name, transtr(v.RealCowAmount), transtr(v.PCows),
  889. transtr(v.FeedCowAmount), v.FeedRecipe_Code, v.FeedRecipe_Verson, v.FeedRecipe_Name,
  890. transtr(v.FeedWeight), transtr(v.AverageDMIasRecipe), transtr(v.FeedBanciPlan), transtr(v.Banci1Rate), transtr(v.Banci2Rate),
  891. transtr(v.Banci3Rate), transtr(v.Banci4Rate), transtr(v.StatusID), v.StatusText, transtr(v.OrderNum), v.Remark, v.Creator, trandate(v.CreateDate))
  892. if err != nil {
  893. logs.Error("insert err", err)
  894. }
  895. }
  896. // 修改栏舍配方
  897. sqlstr = `
  898. UPDATE feedp
  899. JOIN xnr_feedgdailycost xf ON feedp.xnrFeedpName = xf.Group_Name OR feedp.BNAME = xf.Group_Name
  900. SET
  901. feedp.softCowCount=xf.RealCowAmount,feedp.xnrFeedpName= xf.Group_Name,feedp.ENABLE=1
  902. WHERE xf.getDate=?
  903. `
  904. _, err = tx.Exec(sqlstr, strDate)
  905. if err != nil {
  906. logs.Error(" update feedp err", err)
  907. }
  908. //新增栏舍配方
  909. sqlstr = `
  910. INSERT INTO feedp (BNAME,xnrFeedpName,softCowCount,ENABLE)
  911. SELECT xf.Group_Name,xf.Group_Name,xf.RealCowAmount,1
  912. FROM xnr_feedgdailycost xf
  913. LEFT JOIN feedp ON feedp.xnrFeedpName = xf.Group_Name OR feedp.BNAME = xf.Group_Name
  914. WHERE xf.getDate=? AND feedp.id IS NULL
  915. `
  916. _, err = tx.Exec(sqlstr, strDate)
  917. if err != nil {
  918. logs.Error(" insert feedp err", err)
  919. }
  920. return nil
  921. }
  922. func transtr(str string) string {
  923. if str == "" {
  924. str = "0"
  925. }
  926. return str
  927. }
  928. func trandate(str string) string {
  929. if str == "" {
  930. str = time.Now().Format("2006-01-02 15:04:05")
  931. }
  932. return str
  933. }
  934. func checkpathCreat() error {
  935. if strOutFilePath == "" {
  936. strOutFilePath = setting.CurrentPath + strMethod + strDate
  937. }
  938. f, err := os.Create(strOutFilePath)
  939. if err != nil {
  940. logs.Error("输入的保存路径不存在,请核对", err.Error())
  941. return err
  942. }
  943. f.Close()
  944. return nil
  945. }
  946. func checkpathExist() error {
  947. if strOutFilePath == "" {
  948. logs.Error("输入的路径不存在")
  949. return errors.New("输入的路径不存在")
  950. }
  951. f, err := os.Open(strOutFilePath)
  952. if err != nil {
  953. logs.Error("输入的保存路径不存在,请核对", err.Error())
  954. return err
  955. }
  956. f.Close()
  957. return nil
  958. }
  959. func checkDate() error {
  960. if strDate == "" {
  961. strDate = time.Now().Format("2006-01-02")
  962. return nil
  963. }
  964. _, err := time.Parse("2006-01-02", strDate)
  965. if err != nil {
  966. _, err = time.Parse("2006/01/02", strDate)
  967. if err != nil {
  968. return err
  969. }
  970. }
  971. return nil
  972. }