ftjudge.go 21 KB

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