5112501a8fdf78fcbec043d556dbf7f8bde09863.svn-base 21 KB

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