c3144974acaba40419456d65226e3d730cdcb55e.svn-base 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. package api
  2. import (
  3. "../../pkg/app"
  4. "../../pkg/e"
  5. "../../pkg/logging"
  6. "github.com/Knetic/govaluate"
  7. "math"
  8. "../../routers/restful"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "github.com/Anderson-Lu/gofasion/gofasion"
  13. "github.com/gin-gonic/gin"
  14. "github.com/xormplus/xorm"
  15. "io/ioutil"
  16. "net/http"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. //配方评估相关
  22. type Nur struct {
  23. Avgmonthage float64 //月龄
  24. Bw float64 //体重
  25. Dayw float64 //日增重
  26. Fetal float64 //胎次
  27. Avgdim float64 //平均泌乳天数
  28. Dayspre float64 //怀孕天数
  29. Product float64 //产奶量
  30. Fat float64 //乳脂率
  31. Pro float64 //乳蛋白率
  32. Lactose float64 //乳糖率
  33. Source string //来源
  34. Tem float64 //温度
  35. Cowsum float64 //牛头数
  36. Cowclass string //牲畜类别
  37. Cowclassid string //牲畜类别id
  38. Bigcowclass string //牲畜大类
  39. }
  40. //栏舍配方,配方评估
  41. func GETNurJudge(c *gin.Context) {
  42. appG := app.Gin{C: c}
  43. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  44. fsion := gofasion.NewFasion(string(dataByte))
  45. nurstr := fsion.Get("parammaps").Json()
  46. parammaps := make(map[string]interface{})
  47. nur := Nur{}
  48. _ = json.Unmarshal([]byte(nurstr), &nur) //需要json 的类型相同
  49. _ = json.Unmarshal([]byte(nurstr), &parammaps) //需要json 的类型相同
  50. tx := restful.Engine.NewSession()
  51. err := tx.Begin()
  52. defer func() {
  53. switch {
  54. case err != nil:
  55. fmt.Println("__error:", err)
  56. if tx != nil {
  57. tx.Rollback()
  58. }
  59. default:
  60. if tx != nil {
  61. err = tx.Commit()
  62. }
  63. }
  64. if tx != nil {
  65. tx.Close()
  66. }
  67. }()
  68. formulas, _ := tx.SQL("select * from tb_formula_d where cowclass = ? "+
  69. "and if (?='',''='', nutrition=? ) ORDER BY nutrition", nur.Bigcowclass, nur.Source, nur.Source).Query().List()
  70. resnur, err := CalNUR(formulas, &nur)
  71. if err != nil {
  72. logging.Error("GETNurJudge err: ", err)
  73. msg := geterrmsg(err.Error())
  74. appG.Response(http.StatusOK, e.ERROR, msg)
  75. }
  76. resf, err := FeedproviderV2(parammaps, tx)
  77. reslist := make([]map[string]interface{}, 21)
  78. if len(resf) > 1 {
  79. for key, _ := range resf[0] {
  80. //println(key)
  81. str, sort := switchname(key)
  82. res := make(map[string]interface{}, 0)
  83. res["standard"] = str
  84. res["cowneed"] = resnur[key] // 奶牛需要
  85. res["ftpro"] = resf[0][key] // 配方提供
  86. res["hprov"] = resf[1][key] // tmr料
  87. res["sprov"] = resf[2][key] //采食量
  88. reslist[sort] = res
  89. }
  90. }
  91. if err != nil {
  92. logging.Error("GETNurJudge err: ", err)
  93. msg := geterrmsg(err.Error())
  94. appG.Response(http.StatusOK, e.ERROR, msg)
  95. } else {
  96. appG.Response(http.StatusOK, e.SUCCESS, reslist)
  97. }
  98. }
  99. func GETNurJudgeRport(c *gin.Context) {
  100. appG := app.Gin{C: c}
  101. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  102. fsion := gofasion.NewFasion(string(dataByte))
  103. nurstr := fsion.Get("parammaps").Json()
  104. parammaps := make(map[string]interface{})
  105. _ = json.Unmarshal([]byte(nurstr), &parammaps)
  106. //startTime, err := time.Parse("2006-01-02", parammaps["startTime"].(string))
  107. //if err != nil {
  108. // appG.Response(http.StatusOK, e.ERROR, err.Error())
  109. // return
  110. //}
  111. //stopTime, err := time.Parse("2006-01-02", parammaps["stopTime"].(string))
  112. //
  113. //if err != nil {
  114. // appG.Response(http.StatusOK, e.ERROR, err.Error())
  115. // return
  116. //}
  117. //if stopTime.Sub(startTime) > 31*24*time.Hour {
  118. // appG.Response(http.StatusOK, e.ERROR, errors.New("日期范围过大"))
  119. // return
  120. //}
  121. tx := restful.Engine.NewSession()
  122. err := tx.Begin()
  123. defer func() {
  124. switch {
  125. case err != nil:
  126. fmt.Println("__error:", err)
  127. if tx != nil {
  128. tx.Rollback()
  129. }
  130. default:
  131. if tx != nil {
  132. err = tx.Commit()
  133. }
  134. }
  135. if tx != nil {
  136. tx.Close()
  137. }
  138. }()
  139. //CulNurReportCow("getbmdetaillist",parammaps,tx)
  140. res, err := CulNurReportFT(parammaps, tx)
  141. if err != nil {
  142. logging.Error("GETNurJudgeRport err: ", err)
  143. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  144. msg := geterrmsg(err.Error())
  145. appG.Response(http.StatusOK, e.ERROR, msg)
  146. } else {
  147. appG.Response(http.StatusOK, e.SUCCESS, res)
  148. }
  149. }
  150. //奶牛需要
  151. func CulNurReportCow(sqlname string, parammaps map[string]interface{}, tx *xorm.Session) (map[string]interface{}, error) {
  152. var nur Nur
  153. sql, p := restful.GetSqlByNameDBT(sqlname, tx)
  154. s_params := make([]interface{}, 0)
  155. paramslist := strings.Split(p, ",")
  156. if len(paramslist) > 0 && p != "" {
  157. for _, value := range paramslist {
  158. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  159. }
  160. }
  161. res, err := tx.SQL(sql, s_params...).Query().List()
  162. if len(res) > 0 {
  163. resstr, _ := json.Marshal(res[0])
  164. err = json.Unmarshal(resstr, &nur)
  165. if err != nil {
  166. return nil, err
  167. }
  168. }
  169. formulas, _ := tx.SQL("select * from tb_formula_d where cowclass = ? and if (?='',''='', nutrition=? ) ORDER BY nutrition", nur.Bigcowclass, nur.Source, nur.Source).Query().List()
  170. resnur, err := CalNUR(formulas, &nur)
  171. if err != nil {
  172. return nil, err
  173. }
  174. return resnur, err
  175. }
  176. func CulNurReportFT(parammaps map[string]interface{}, tx *xorm.Session) (res []map[string]interface{}, err error) {
  177. // 获取该配方日期范围内的所有栏舍
  178. //barids, err := tx.SQL("SELECT trim(fp.barid) barid,fp.ccount,DATE_FORMAT(fp.date, '%Y-%m-%d')date,fp.barname FROM feedpdate fp WHERE fp.pastureid = ? AND fp.date BETWEEN ? AND ? AND fp.`ftid` = ? GROUP BY fp.`barid` order by fp.barid",
  179. // parammaps["pastureid"], parammaps["startTime"], parammaps["stopTime"], parammaps["ftid"]).Query().List()
  180. barids, err := tx.SQL(` SELECT TRIM(fp.barid) barid,IFNULL((SELECT bm.cowsum FROM barmilk bm WHERE pastureid = fp.pastureid AND barid = fp.barid AND productdate <= ? ORDER BY productdate LIMIT 1),fp.ccount) ccount,DATE_FORMAT(fp.date, '%Y-%m-%d')date,
  181. fp.barname FROM feedpdate fp
  182. WHERE fp.pastureid = ? AND fp.date BETWEEN ? AND ? AND fp.ftid = ? GROUP BY fp.barid ORDER BY fp.barid`,
  183. parammaps["stopTime"],parammaps["pastureid"], parammaps["startTime"], parammaps["stopTime"], parammaps["ftid"]).Query().List()
  184. if err != nil {
  185. return nil, err
  186. }
  187. bartem := ""
  188. // 获取日期,以及日期范围校验
  189. dates := make([]string, 0)
  190. startTime, err := time.Parse("2006-01-02", parammaps["startTime"].(string))
  191. if err != nil {
  192. return nil, err
  193. }
  194. stopTime, err := time.Parse("2006-01-02", parammaps["stopTime"].(string))
  195. if err != nil {
  196. return nil, err
  197. }
  198. if stopTime.Sub(startTime) > 31*24*time.Hour {
  199. return nil, errors.New("日期范围过大")
  200. }
  201. for stopTime.Sub(startTime) >= 0 {
  202. dates = append(dates, startTime.Format("2006-01-02"))
  203. startTime = startTime.Add(24 * time.Hour)
  204. }
  205. parammaps["date"] = parammaps["stopTime"]
  206. for key, value := range barids { // 遍历栏舍
  207. if key == 0 {
  208. bartem = value["barid"].(string)
  209. parammaps["barid"] = value["barid"]
  210. }
  211. if bartem != value["barid"].(string) || key == len(barids)-1 || key == 0{
  212. parammaps["barid"] = value["barid"]
  213. barres, err := FeedproviderV3(parammaps, dates, tx)
  214. if err != nil {
  215. return nil, err
  216. }
  217. //
  218. resnur, err := CulNurReportCow("getbmdetaillist", parammaps, tx)
  219. if err != nil {
  220. return nil, err
  221. }
  222. restem := make(map[string]interface{}, 0)
  223. for k, _ := range barres[0] {
  224. //println("wwwwwww",k)
  225. restem["barid"] = value["barid"].(string)
  226. restem["barname"] = value["barname"].(string)
  227. restem["ccount"] = value["ccount"].(int64)
  228. restem[k+"-nur"] = resnur[k]
  229. restem[k+"-FT"] = barres[0][k]
  230. restem[k+"-H"] = barres[1][k]
  231. restem[k+"-S"] = barres[2][k]
  232. }
  233. res = append(res, restem)
  234. if err != nil {
  235. return nil, err
  236. }
  237. bartem = value["barid"].(string)
  238. dates = make([]string, 0)
  239. parammaps["barid"] = value["barid"]
  240. }
  241. dates = append(dates, value["date"].(string))
  242. }
  243. return res, err
  244. }
  245. //排序
  246. func switchname(str string) (string, int) {
  247. tstr := ""
  248. sort := 0
  249. switch str {
  250. case "dry":
  251. tstr = "干物质(kg)"
  252. sort = 0
  253. case "nm":
  254. tstr = "产奶净能(MJ)"
  255. sort = 1
  256. case "nuint":
  257. tstr = "奶牛能量单位(NND)"
  258. sort = 2
  259. case "cp":
  260. tstr = "粗蛋白(g)"
  261. sort = 3
  262. case "ca":
  263. tstr = " 钙(g)"
  264. sort = 4
  265. case "p":
  266. tstr = "磷(g)"
  267. sort = 5
  268. case "nmd":
  269. tstr = "产奶净能(MCal/DM)"
  270. sort = 6
  271. case "cpd":
  272. tstr = "粗蛋白(%DM)"
  273. sort = 7
  274. case "fat":
  275. tstr = "脂肪"
  276. sort = 8
  277. case "starch":
  278. tstr = "淀粉"
  279. sort = 9
  280. case "ndf":
  281. tstr = "NDF(%DM)"
  282. sort = 10
  283. case "cndf":
  284. tstr = "粗料中的NDF(%DM)"
  285. sort = 11
  286. case "adf":
  287. tstr = "ADF(%DM)"
  288. sort = 12
  289. case "cad":
  290. tstr = " 钙(%DM)"
  291. sort = 13
  292. case "pd":
  293. tstr = "磷(%DM)"
  294. sort = 14
  295. case "trans35f":
  296. tstr = "(3.5)饲料转化效率(kg/DM)"
  297. sort = 15
  298. case "trans4f":
  299. tstr = "(4.0)饲料转化效率(kg/DM)"
  300. sort = 16
  301. case "jcrate":
  302. tstr = "精粗比"
  303. sort = 17
  304. case "uprice":
  305. tstr = "日粮成本(元)"
  306. sort = 18
  307. case "upriced":
  308. tstr = "干物质成本(元/公斤)"
  309. sort = 19
  310. case "kprice":
  311. tstr = "公斤奶成本(元) "
  312. sort = 20
  313. default:
  314. tstr = str
  315. sort = 21
  316. }
  317. return tstr, sort
  318. }
  319. func FeedproviderV2(parammaps map[string]interface{}, tx *xorm.Session) ([]map[string]float64, error) {
  320. startTime, err := time.Parse("2006-01-02", parammaps["startTime"].(string))
  321. if err != nil {
  322. return nil, err
  323. }
  324. stopTime, err := time.Parse("2006-01-02", parammaps["stopTime"].(string))
  325. if err != nil {
  326. return nil, err
  327. }
  328. if stopTime.Sub(startTime) > 31*24*time.Hour {
  329. return nil, errors.New("日期范围过大")
  330. }
  331. dates := make([]string, 0)
  332. for stopTime.Sub(startTime) >= 0 {
  333. dates = append(dates, startTime.Format("2006-01-02"))
  334. startTime = startTime.Add(24 * time.Hour)
  335. }
  336. resall, err := FeedproviderV3(parammaps, dates, tx)
  337. return resall, err
  338. }
  339. func FeedproviderV3(parammaps map[string]interface{}, dates []string, tx *xorm.Session) ([]map[string]float64, error) {
  340. res := make(map[string]float64, 0)
  341. resH := make(map[string]float64, 0)
  342. resS := make(map[string]float64, 0)
  343. i := 0
  344. for _, date := range dates {
  345. i++
  346. //println(date)
  347. parammaps["date"] = date
  348. restem, err := Feedprovider(parammaps, tx)
  349. if err != nil {
  350. return nil, err
  351. }
  352. restemSH, err := SHprovider(parammaps, tx)
  353. if err != nil {
  354. return nil, err
  355. }
  356. var h, s float64
  357. if len(restemSH) > 1 {
  358. switch restemSH[0]["actualweightminus"].(type) {
  359. case string:
  360. h, _ = strconv.ParseFloat(restemSH[0]["actualweightminus"].(string), 64)
  361. }
  362. switch restemSH[1]["actualweightminus"].(type) {
  363. case string:
  364. s, _ = strconv.ParseFloat(restemSH[1]["actualweightminus"].(string), 64)
  365. }
  366. }
  367. if len(restem) > 0 {
  368. b, _ := strconv.ParseFloat(restem[0]["jcrate"].(string), 64)
  369. res["jcrate"] = res["jcrate"] + b
  370. resH["jcrate"] = resH["jcrate"] + h*b
  371. resS["jcrate"] = resS["jcrate"] + s*b
  372. b, _ = strconv.ParseFloat(restem[0]["cpd"].(string), 64)
  373. res["cpd"] = res["cpd"] + b
  374. resH["cpd"] = resH["cpd"] + h*b
  375. resS["cpd"] = resS["cpd"] + s*b
  376. b, _ = strconv.ParseFloat(restem[0]["adf"].(string), 64)
  377. res["adf"] = res["adf"] + b
  378. resH["adf"] = resH["adf"] + h*b
  379. resS["adf"] = resS["adf"] + s*b
  380. b, _ = strconv.ParseFloat(restem[0]["ca"].(string), 64)
  381. res["ca"] = res["ca"] + b
  382. resH["ca"] = resH["ca"] + h*b
  383. resS["ca"] = resS["ca"] + s*b
  384. b, _ = strconv.ParseFloat(restem[0]["cad"].(string), 64)
  385. res["cad"] = res["cad"] + b
  386. resH["cad"] = resH["cad"] + h*b
  387. resS["cad"] = resS["cad"] + s*b
  388. //b,_ =strconv.ParseFloat(restem[0]["cdry"].(string),64)
  389. //res["cdry"] = res["cdry"]+b
  390. //
  391. //resH["cdry"] = resH["cdry"]+ h*b
  392. //resS["cdry"] = resS["cdry"]+ s*b
  393. b, _ = strconv.ParseFloat(restem[0]["cndf"].(string), 64)
  394. res["cndf"] = res["cndf"] + b
  395. resH["cndf"] = resH["cndf"] + h*b
  396. resS["cndf"] = resS["cndf"] + s*b
  397. b, _ = strconv.ParseFloat(restem[0]["cp"].(string), 64)
  398. res["cp"] = res["cp"] + b
  399. resH["cp"] = resH["cp"] + h*b
  400. resS["cp"] = resS["cp"] + s*b
  401. b, _ = strconv.ParseFloat(restem[0]["dry"].(string), 64)
  402. res["dry"] = res["dry"] + b
  403. resH["dry"] = resH["dry"] + h*b
  404. resS["dry"] = resS["dry"] + s*b
  405. b, _ = strconv.ParseFloat(restem[0]["fat"].(string), 64)
  406. res["fat"] = res["fat"] + b
  407. resH["fat"] = resH["fat"] + h*b
  408. resS["fat"] = resS["fat"] + s*b
  409. b, _ = strconv.ParseFloat(restem[0]["kprice"].(string), 64)
  410. res["kprice"] = res["kprice"] + b
  411. resH["kprice"] = resH["kprice"] + h*b
  412. resS["kprice"] = resS["kprice"] + s*b
  413. b, _ = strconv.ParseFloat(restem[0]["nm"].(string), 64)
  414. res["nm"] = res["nm"] + b
  415. resH["nm"] = resH["nm"] + h*b
  416. resS["nm"] = resS["nm"] + s*b
  417. b, _ = strconv.ParseFloat(restem[0]["ndf"].(string), 64)
  418. res["ndf"] = res["ndf"] + b
  419. resH["ndf"] = resH["ndf"] + h*b
  420. resS["ndf"] = resS["ndf"] + s*b
  421. b, _ = strconv.ParseFloat(restem[0]["nm"].(string), 64)
  422. res["nm"] = res["nm"] + b
  423. resH["nm"] = resH["nm"] + h*b
  424. resS["nm"] = resS["nm"] + s*b
  425. b, _ = strconv.ParseFloat(restem[0]["nmd"].(string), 64)
  426. res["nmd"] = res["nmd"] + b
  427. resH["nmd"] = resH["nmd"] + h*b
  428. resS["nmd"] = resS["nmd"] + s*b
  429. b, _ = strconv.ParseFloat(restem[0]["nuint"].(string), 64)
  430. res["nuint"] = res["nuint"] + b
  431. resH["nuint"] = resH["nuint"] + h*b
  432. resS["nuint"] = resS["nuint"] + s*b
  433. b, _ = strconv.ParseFloat(restem[0]["p"].(string), 64)
  434. res["p"] = res["p"] + b
  435. resH["p"] = resH["p"] + h*b
  436. resS["p"] = resS["p"] + s*b
  437. b, _ = strconv.ParseFloat(restem[0]["pd"].(string), 64)
  438. res["pd"] = res["pd"] + b
  439. resH["pd"] = resH["pd"] + h*b
  440. resS["pd"] = resS["pd"] + s*b
  441. b, _ = strconv.ParseFloat(restem[0]["starch"].(string), 64)
  442. res["starch"] = res["starch"] + b
  443. resH["starch"] = resH["starch"] + h*b
  444. resS["starch"] = resS["starch"] + s*b
  445. b, _ = strconv.ParseFloat(restem[0]["trans4f"].(string), 64)
  446. res["trans4f"] = res["trans4f"] + b
  447. resH["trans4f"] = resH["trans4f"] + h*b
  448. resS["trans4f"] = resS["trans4f"] + s*b
  449. b, _ = strconv.ParseFloat(restem[0]["trans35f"].(string), 64)
  450. res["trans35f"] = res["trans35f"] + b
  451. resH["trans35f"] = resH["trans35f"] + h*b
  452. resS["trans35f"] = resS["trans35f"] + s*b
  453. b, _ = strconv.ParseFloat(restem[0]["uprice"].(string), 64)
  454. res["uprice"] = res["uprice"] + b
  455. resH["uprice"] = resH["uprice"] + h*b
  456. resS["uprice"] = resS["uprice"] + s*b
  457. b, _ = strconv.ParseFloat(restem[0]["upriced"].(string), 64)
  458. res["upriced"] = res["upriced"] + b
  459. resH["upriced"] = resH["upriced"] + h*b
  460. resS["upriced"] = resS["upriced"] + s*b
  461. }
  462. }
  463. for key, _ := range res {
  464. resf, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", res[key]/float64(i)), 64)
  465. resHf, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", resH[key]/float64(i)), 64)
  466. resSf, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", resS[key]/float64(i)), 64)
  467. res[key] = resf
  468. resH[key] = resHf
  469. resS[key] = resSf
  470. }
  471. resAll := make([]map[string]float64, 0)
  472. resAll = append(resAll, res, resH, resS)
  473. return resAll, nil
  474. }
  475. func Feedprovider(parammaps map[string]interface{}, tx *xorm.Session) ([]map[string]interface{}, error) {
  476. sql, p := restful.GetSqlByNameDBT(parammaps["name"].(string), tx)
  477. s_params := make([]interface{}, 0)
  478. paramslist := strings.Split(p, ",")
  479. if len(paramslist) > 0 && p != "" {
  480. for _, value := range paramslist {
  481. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  482. }
  483. }
  484. res, err := tx.SQL(sql, s_params...).Query().List()
  485. return res, err
  486. }
  487. func SHprovider(parammaps map[string]interface{}, tx *xorm.Session) ([]map[string]interface{}, error) {
  488. sql, p := restful.GetSqlByNameDBT(parammaps["name1"].(string), tx)
  489. //println(parammaps["name1"].(string),sql)
  490. s_params := make([]interface{}, 0)
  491. paramslist := strings.Split(p, ",")
  492. if len(paramslist) > 0 && p != "" {
  493. for _, value := range paramslist {
  494. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  495. }
  496. }
  497. res, err := tx.SQL(sql, s_params...).Query().List()
  498. return res, err
  499. }
  500. func CalNUR(formulas []map[string]interface{}, nur *Nur) (map[string]interface{}, error) {
  501. //formulaADF := make([]map[string]interface{},1)//ADF
  502. //formulaNDF := make([]map[string]interface{},1)//NDF
  503. //formulaNFC := make([]map[string]interface{},1)//NFC
  504. //formulacprotedm := make([]map[string]interface{},1)//小肠可消化粗蛋白
  505. //formuladry := make([]map[string]interface{},1) //干物质
  506. //formulastarch := make([]map[string]interface{},1)//淀粉
  507. //formulap := make([]map[string]interface{},1)//磷
  508. //formulacNDF := make([]map[string]interface{},1)//粗料中的NDF %DM
  509. //formulajcrate := make([]map[string]interface{},1)//粗料比例
  510. //formulacprote := make([]map[string]interface{},1)//粗蛋白
  511. //formulaenergy := make([]map[string]interface{},1)//能量
  512. //formulafat := make([]map[string]interface{},1)//脂肪
  513. //formulaga := make([]map[string]interface{},1)//钙
  514. //formulafeedrate := make([]map[string]interface{},1)//饲料转化效率,标准乳/干物质4.0
  515. //formulafeed35rate := make([]map[string]interface{},1)//饲料转化效率,标准乳/干物质3.5
  516. resmap := make(map[string]float64)
  517. resmapstr := make(map[string]string)
  518. resAll := make(map[string]interface{})
  519. for _, formula := range formulas {
  520. if formula["type"].(int64) == 0 {
  521. resmapstr[formula["note"].(string)] = formula["formula"].(string)
  522. continue
  523. }
  524. res, err := PraseFormula(formula["formula"].(string), nur)
  525. if err != nil {
  526. logging.Error("PraseFormula err", err.Error(), formula["nutrition"].(string))
  527. //println("PraseFormula err", err.Error(), formula["nutrition"].(string))
  528. }
  529. switch res.(type) {
  530. case string:
  531. resmapstr[formula["note"].(string)] = res.(string)
  532. case float64, int, float32, int64, int32, int16, int8:
  533. var rest float64
  534. switch res.(type) {
  535. case float64:
  536. rest = res.(float64)
  537. case int:
  538. rest = float64(res.(int))
  539. case int64:
  540. rest = float64(res.(int64))
  541. case int32:
  542. rest = float64(res.(int32))
  543. case int16:
  544. rest = float64(res.(int16))
  545. case int8:
  546. rest = float64(res.(int8))
  547. }
  548. switch formula["nutrition"].(string) {
  549. case "ADF %DM":
  550. resmap["adf"] = resmap["adf"] + rest
  551. case "NDF %DM":
  552. resmap["ndf"] = resmap["ndf"] + rest
  553. case "NFC %DM":
  554. resmap["nfc"] = resmap["nfc"] + rest
  555. //case "小肠可消化粗蛋白":
  556. // resmap["cprotedm"] = resmap["cprotedm"] + rest
  557. case "干物质":
  558. resmap["dry"] = resmap["dry"] + rest
  559. case "淀粉":
  560. resmap["starch"] = resmap["starch"] + rest
  561. case "磷":
  562. resmap["p"] = resmap["p"] + rest
  563. case "粗料中的NDF %DM":
  564. resmap["cndf"] = resmap["cndf"] + rest
  565. case "粗料比例":
  566. resmap["jcrate"] = resmap["jcrate"] + rest
  567. case "粗蛋白":
  568. resmap["cp"] = resmap["cp"] + rest
  569. case "能量":
  570. resmap["nm"] = resmap["nm"] + rest
  571. case "脂肪":
  572. resmap["fat"] = resmap["fat"] + rest
  573. case "钙":
  574. resmap["ca"] = resmap["ca"] + rest
  575. case "饲料转化效率,标准乳/干物质":
  576. resmap["trans4f"] = resmap["trans4f"] + rest
  577. case "饲料转化效率,标准乳/干物质3.5":
  578. resmap["trans35f"] = resmap["trans35f"] + rest
  579. }
  580. default:
  581. fmt.Printf("===============%T \n", res)
  582. }
  583. }
  584. if resmap["dry"] == 0 { //粗蛋白%dm = 粗蛋白/干物质
  585. resmap["cpd"] = 0
  586. resmap["cad"] = 0
  587. resmap["pd"] = 0
  588. resmap["nuint"] = 0
  589. resmap["nmd"] = 0
  590. } else {
  591. resmap["cpd"] = resmap["cp"] / resmap["dry"] / 10
  592. resmap["cad"] = resmap["ca"] / resmap["dry"] / 10
  593. resmap["pd"] = resmap["p"] / resmap["dry"] / 10
  594. resmap["nuint"] = resmap["nm"] / 3.138
  595. resmap["nmd"] = resmap["nm"] * 0.2389 / resmap["dry"]
  596. }
  597. for key, value := range resmap {
  598. va, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", value), 64)
  599. resAll[key] = va
  600. }
  601. for key, value := range resmapstr {
  602. //println(key, value)
  603. resAll[key] = value
  604. }
  605. resAll["uprice"] = ""
  606. resAll["upriced"] = ""
  607. resAll["kprice"] = ""
  608. return resAll, nil
  609. }
  610. func PraseFormula(formula string, nur *Nur) (interface{}, error) {
  611. str := formula
  612. str = strings.ReplaceAll(str, "日增重", strconv.FormatFloat(nur.Dayw, 'f', -1, 64))
  613. //str = strings.ReplaceAll(str, "体重", nur.Bw)
  614. str = strings.ReplaceAll(str, "怀孕天数", strconv.FormatFloat(nur.Dayspre, 'f', -1, 64))
  615. str = strings.ReplaceAll(str, "产量", strconv.FormatFloat(nur.Product, 'f', -1, 64))
  616. str = strings.ReplaceAll(str, "乳脂率", strconv.FormatFloat(nur.Fat, 'f', -1, 64))
  617. str = strings.ReplaceAll(str, "乳蛋白率", strconv.FormatFloat(nur.Pro, 'f', -1, 64))
  618. str = strings.ReplaceAll(str, "乳糖率", strconv.FormatFloat(nur.Lactose, 'f', -1, 64))
  619. str = strings.ReplaceAll(str, "标准乳", strconv.FormatFloat(0.35*nur.Product+15*nur.Fat/100*nur.Product, 'f', -1, 64)) // 3.5乳脂率
  620. //str = strings.ReplaceAll(str, "标准乳", strconv.FormatFloat(0.35*nur.Product+18.57*nur.Fat/100*nur.Product, 'f', -1, 64))
  621. //println("标准乳--",strconv.FormatFloat(0.4*nur.Product+15*nur.Fat/100*nur.Product, 'f', -1, 64))
  622. str = strings.ReplaceAll(str, "泌乳天数", strconv.FormatFloat(nur.Avgdim, 'f', -1, 64))
  623. str = strings.ReplaceAll(str, "月龄", strconv.FormatFloat(nur.Avgmonthage, 'f', -1, 64))
  624. str = strings.ReplaceAll(str, "温度", strconv.FormatFloat(nur.Tem, 'f', -1, 64))
  625. str = strings.ReplaceAll(str, "胎次", strconv.FormatFloat(nur.Fetal, 'f', -1, 64))
  626. str = strings.ReplaceAll(str, "体重", strconv.FormatFloat(nur.Bw, 'f', -1, 64))
  627. str = strings.ReplaceAll(str, "Math.pow", "Mathpow")
  628. str = strings.ReplaceAll(str, "Math.round", "Mathround")
  629. str = strings.ReplaceAll(str, "Math.log", "Mathlog")
  630. str = strings.ReplaceAll(str, "Math.exp", "Mathexp")
  631. //str := "(体重>200?4.2/0.55*Math.pow(体重,0.75):3.5/0.55*Math.pow(体重,0.75))"
  632. functions := map[string]govaluate.ExpressionFunction{
  633. "strlen": func(args ...interface{}) (interface{}, error) {
  634. length := len(args[0].(string))
  635. return (float64)(length), nil
  636. },
  637. "Mathpow": func(args ...interface{}) (interface{}, error) {
  638. c := math.Pow(args[0].(float64), args[1].(float64))
  639. return (float64)(c), nil
  640. },
  641. "Mathround": func(args ...interface{}) (interface{}, error) {
  642. c := math.Round(args[0].(float64))
  643. return (float64)(c), nil
  644. },
  645. "Mathlog": func(args ...interface{}) (interface{}, error) {
  646. c := math.Log(args[0].(float64))
  647. return (float64)(c), nil
  648. },
  649. "Mathexp": func(args ...interface{}) (interface{}, error) {
  650. c := math.Exp(args[0].(float64))
  651. return (float64)(c), nil
  652. },
  653. }
  654. expression, err := govaluate.NewEvaluableExpressionWithFunctions(str, functions)
  655. if err != nil {
  656. return 0, err
  657. }
  658. //fmt.Printf("== %+v\n", str)
  659. result, err := expression.Evaluate(nil)
  660. //fmt.Printf("== %+v\n", result)
  661. if err != nil || result == nil {
  662. return 0, err
  663. }
  664. return result, err
  665. }