chart.go 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  1. package service
  2. import (
  3. "fmt"
  4. "github.com/astaxie/beego/logs"
  5. "kpt.xdmy/apiserver/model"
  6. "kpt.xdmy/apiserver/model/http"
  7. "kpt.xdmy/pkg/util"
  8. "strconv"
  9. "time"
  10. )
  11. type pastureStatistics struct {
  12. Psum float64 `gorm:"column:psum"`
  13. Name string `gorm:"column:name"`
  14. PastureId int `gorm:"column:pastureId"`
  15. }
  16. type monthBudget struct {
  17. MonthBudget float64 `gorm:"column:monthBudget"`
  18. Name string `gorm:"column:name"`
  19. }
  20. //各牧场年度用水量对比
  21. func (s *Service) GetWaterYieldByPasture(center, startTime, endTime string) (*http.StatementResp, error) {
  22. resp := new(http.StatementResp)
  23. layout := "2006-01-02"
  24. startdate, err := time.Parse(layout, startTime)
  25. if err != nil {
  26. logs.Error("时间转换失败:", err)
  27. }
  28. enddate, err := time.Parse(layout, endTime)
  29. if err != nil {
  30. logs.Error("时间转换失败:", err)
  31. }
  32. enddate = enddate.AddDate(0, 1, -1)
  33. nowYearList := make([]*pastureStatistics, 0)
  34. lastYearList := make([]*pastureStatistics, 0)
  35. sqlstr := `SELECT
  36. round( IFNULL(SUM(w.waterConsumption),0) ,2)psum,
  37. p.name as name
  38. FROM
  39. water w
  40. INNER JOIN measure ms
  41. ON w.measureId = ms.id
  42. INNER JOIN pasture p
  43. ON ms.pastureId = p.id
  44. WHERE
  45. (w.date between ? and ? ) AND ms.useType = '1级表' and p.name <> '现代牧业' and (p.center = ? or ? = '' ) and p.IsEnergy = 0
  46. GROUP BY ms.pastureId `
  47. err = s.d.DB.Debug().Raw(sqlstr, startdate.Format("2006-01-02"), enddate.Format("2006-01-02"), center, center).Find(&nowYearList).Error
  48. if err != nil {
  49. logs.Error(err)
  50. return nil, err
  51. }
  52. err = s.d.DB.Raw(sqlstr, startdate.AddDate(-1, 0, 0).Format("2006-01-02"),
  53. enddate.AddDate(-1, 0, 0).Format("2006-01-02"), center, center).Find(&lastYearList).Error
  54. if err != nil {
  55. logs.Error(err)
  56. return nil, err
  57. }
  58. pastureList := make([]*model.Pasture, 0)
  59. err = s.d.DB.Where(" vendor = 1 ").Where(" IsEnergy = 0").Order("id").Find(&pastureList).Error
  60. if err != nil {
  61. logs.Error(err)
  62. return resp, err
  63. }
  64. var pastureName []string
  65. var nowYearData, lastYearData []float64
  66. var all float64
  67. for _, nowYear := range nowYearList {
  68. pastureName = append(pastureName, nowYear.Name)
  69. nowYearData = append(nowYearData, util.Round(nowYear.Psum, 2))
  70. all += nowYear.Psum
  71. exist := false
  72. for _, lastYear := range lastYearList {
  73. if lastYear.Name == nowYear.Name {
  74. exist = true
  75. lastYearData = append(lastYearData, util.Round(lastYear.Psum, 2))
  76. break
  77. }
  78. }
  79. if !exist {
  80. lastYearData = append(lastYearData, 0)
  81. }
  82. }
  83. for _, pasture := range pastureList {
  84. exist := false
  85. for _, name := range pastureName {
  86. if pasture.Name == name {
  87. exist = true
  88. break
  89. }
  90. }
  91. if !exist {
  92. pastureName = append(pastureName, pasture.Name)
  93. nowYearData = append(nowYearData, 0)
  94. lastYearData = append(lastYearData, 0)
  95. }
  96. }
  97. resp.Pasture = pastureName
  98. resp.LastYear = lastYearData
  99. resp.NowYear = nowYearData
  100. resp.All = util.Round(all, 2)
  101. return resp, nil
  102. }
  103. func (s *Service) GetWaterSingleCowByPasture(center, startTime, endTime string) (*http.StatementResp, error) {
  104. resp := new(http.StatementResp)
  105. layout := "2006-01-02"
  106. startdate, err := time.Parse(layout, startTime)
  107. if err != nil {
  108. logs.Error("时间转换失败:", err)
  109. }
  110. enddate, err := time.Parse(layout, endTime)
  111. if err != nil {
  112. logs.Error("时间转换失败:", err)
  113. }
  114. enddate = enddate.AddDate(0, 1, -1)
  115. nowYearList := make([]*pastureStatistics, 0)
  116. lastYearList := make([]*pastureStatistics, 0)
  117. sqlstr := `SELECT
  118. round( IFNULL(SUM(w.waterConsumption),0) ,2)psum,
  119. p.name
  120. FROM
  121. water w
  122. INNER JOIN measure ms
  123. ON w.measureId = ms.id
  124. INNER JOIN pasture p
  125. ON ms.pastureId = p.id
  126. WHERE
  127. (w.date between ? and ? ) AND ms.useType = '1级表' and p.name <> '现代牧业' and (p.center = ? or ? = '' ) and p.IsEnergy = 0
  128. GROUP BY ms.pastureId desc `
  129. err = s.d.DB.Raw(sqlstr, startdate.Format("2006-01-02"), enddate.Format("2006-01-02"), center, center).Find(&nowYearList).Error
  130. if err != nil {
  131. logs.Error(err)
  132. return nil, err
  133. }
  134. err = s.d.DB.Raw(sqlstr, startdate.AddDate(-1, 0, 0).Format("2006-01-02"),
  135. enddate.AddDate(-1, 0, 0).Format("2006-01-02"), center, center).Find(&lastYearList).Error
  136. if err != nil {
  137. logs.Error(err)
  138. return nil, err
  139. }
  140. pastureList := make([]*model.Pasture, 0)
  141. err = s.d.DB.Where(" vendor = 1 ").Where(" IsEnergy = 0").Order("id").Find(&pastureList).Error
  142. if err != nil {
  143. logs.Error(err)
  144. return resp, err
  145. }
  146. cowNumList := make([]*model.FeedSumAndPasture, 0)
  147. lastCowNumList := make([]*model.FeedSumAndPasture, 0)
  148. err = s.d.DB.Raw(`select sum(cowNum)cowNum ,p.name from feed_sum fd join pasture p on p.id = fd.pastureid
  149. where fd.feedDay between ? and ? and (p.center = ? or ? = '' ) group by pastureid `, startdate.Format("2006-01-02"),
  150. enddate.Format("2006-01-02"), center, center).Find(&cowNumList).Error
  151. if err != nil {
  152. logs.Error(err)
  153. return nil, err
  154. }
  155. err = s.d.DB.Raw(`select sum(cowNum)cowNum ,p.name from feed_sum fd join pasture p on p.id = fd.pastureid
  156. where fd.feedDay between ? and ? and (p.center = ? or ? = '' ) group by pastureid `,
  157. startdate.AddDate(-1, 0, 0).Format("2006-01-02"),
  158. enddate.AddDate(-1, 0, 0).Format("2006-01-02"), center, center).Find(&lastCowNumList).Error
  159. if err != nil {
  160. logs.Error(err)
  161. return nil, err
  162. }
  163. cowNum := make(map[string]int64, 0)
  164. var cowNumAll, lastCowNumAll int64
  165. for _, item := range cowNumList {
  166. cowNumAll += item.FeedSum.CowNum
  167. cowNum[item.Pasture.Name] = item.FeedSum.CowNum
  168. }
  169. lastCowNum := make(map[string]int64, 0)
  170. for _, item := range lastCowNumList {
  171. lastCowNumAll += item.FeedSum.CowNum
  172. lastCowNum[item.Pasture.Name] = item.FeedSum.CowNum
  173. }
  174. var pastureName []string
  175. var nowYearData, lastYearData []float64
  176. var all, lastAll float64
  177. for _, nowYear := range nowYearList {
  178. pastureName = append(pastureName, nowYear.Name)
  179. if _, ok := cowNum[nowYear.Name]; ok {
  180. all += nowYear.Psum
  181. nowYearData = append(nowYearData, nowYear.Psum/float64(cowNum[nowYear.Name]))
  182. } else {
  183. nowYearData = append(nowYearData, 0)
  184. }
  185. exist := false
  186. for _, lastYear := range lastYearList {
  187. if lastYear.Name == nowYear.Name {
  188. exist = true
  189. if _, ok := lastCowNum[lastYear.Name]; ok {
  190. lastAll += nowYear.Psum
  191. lastYearData = append(lastYearData, lastYear.Psum/float64(lastCowNum[nowYear.Name]))
  192. } else {
  193. lastYearData = append(lastYearData, 0)
  194. }
  195. break
  196. }
  197. }
  198. if !exist {
  199. lastYearData = append(lastYearData, 0)
  200. }
  201. }
  202. for _, pasture := range pastureList {
  203. exist := false
  204. for _, name := range pastureName {
  205. if pasture.Name == name {
  206. exist = true
  207. break
  208. }
  209. }
  210. if !exist {
  211. pastureName = append(pastureName, pasture.Name)
  212. nowYearData = append(nowYearData, 0)
  213. lastYearData = append(lastYearData, 0)
  214. }
  215. }
  216. //resp.Pasture = append(resp.Pasture, "集团")
  217. resp.Pasture = append(resp.Pasture, pastureName...)
  218. //resp.LastYear = append(resp.LastYear, util.Round(lastAll/float64(lastCowNumAll), 3))
  219. //resp.LastYear = append(resp.LastYear, lastYearData...)
  220. for _, item := range lastYearData {
  221. resp.LastYear = append(resp.LastYear, util.Round(item, 3))
  222. }
  223. //resp.NowYear = append(resp.NowYear, util.Round(all/float64(cowNumAll), 3))
  224. for _, item := range nowYearData {
  225. resp.NowYear = append(resp.NowYear, util.Round(item, 3))
  226. }
  227. resp.All = all
  228. resp.Line = util.Round(all/float64(cowNumAll), 3)
  229. return resp, nil
  230. }
  231. //单头牛水费
  232. func (s *Service) GetWaterRateByPasture(center, startTime, endTime string) (*http.GetWaterRateByPastureResp, error) {
  233. resp := new(http.GetWaterRateByPastureResp)
  234. layout := "2006-01-02"
  235. startdate, err := time.Parse(layout, startTime)
  236. if err != nil {
  237. logs.Error("时间转换失败:", err)
  238. }
  239. enddate, err := time.Parse(layout, endTime)
  240. if err != nil {
  241. logs.Error("时间转换失败:", err)
  242. }
  243. enddate = enddate.AddDate(0, 1, -1)
  244. cowNumList := make([]*model.FeedSumAndPasture, 0)
  245. err = s.d.DB.Raw(`select sum(cowNum)cowNum ,p.name from feed_sum fd join pasture p on p.id = fd.pastureid
  246. where fd.feedDay between ? and ? and (p.center = ? or ? = '' ) group by pastureid `, startdate.Format("2006-01-02"),
  247. enddate.Format("2006-01-02"), center, center).Find(&cowNumList).Error
  248. if err != nil {
  249. logs.Error(err)
  250. return nil, err
  251. }
  252. cowNum := make(map[string]int64, 0)
  253. var cowNumAll int64
  254. for _, item := range cowNumList {
  255. //cowNumAll += cowNum[feequery.Pasture.Name]
  256. cowNum[item.Pasture.Name] = item.FeedSum.CowNum
  257. }
  258. pastureList := make([]*model.Pasture, 0)
  259. err = s.d.DB.Where(" vendor = 1 ").Where(" IsEnergy = 0").Order("id").Find(&pastureList).Error
  260. if err != nil {
  261. logs.Error(err)
  262. return resp, err
  263. }
  264. feequeryList := make([]*model.FeeQueryAndPasture, 0)
  265. err = s.d.DB.Debug().Raw(`select sum(HSL) HSL ,p.name from feequery fd join pasture p on p.id = fd.pastureid
  266. where fd.Date between ? and ? and fd.FYLX ='水费' and (p.center = ? or ? = '' ) group by fd.pastureId order by p.id`,
  267. startdate.Format("2006-01"), enddate.Format("2006-01"), center, center).Find(&feequeryList).Error
  268. if err != nil {
  269. logs.Error(err)
  270. return resp, err
  271. }
  272. var pastureName []string
  273. var hslList []float64
  274. var all float64
  275. for _, feequery := range feequeryList {
  276. pastureName = append(pastureName, feequery.Pasture.Name)
  277. var hsl float64
  278. hsl, err = strconv.ParseFloat(feequery.HSL, 64)
  279. if err != nil {
  280. logs.Error("转换失败:", err)
  281. hslList = append(hslList, 0)
  282. }
  283. if _, ok := cowNum[feequery.Name]; ok {
  284. all += hsl
  285. cowNumAll += cowNum[feequery.Pasture.Name]
  286. hslList = append(hslList, hsl/float64(cowNum[feequery.Pasture.Name]))
  287. } else {
  288. hslList = append(hslList, 0)
  289. }
  290. }
  291. for _, item := range pastureList {
  292. exist := false
  293. for _, f := range feequeryList {
  294. if item.Name == f.Name {
  295. exist = true
  296. break
  297. }
  298. }
  299. if !exist {
  300. pastureName = append(pastureName, item.Name)
  301. hslList = append(hslList, 0)
  302. }
  303. }
  304. resp.Pasture = append(resp.Pasture, "集团")
  305. resp.Pasture = append(resp.Pasture, pastureName...)
  306. resp.Actual = append(resp.Actual, util.Round(all/float64(cowNumAll), 3))
  307. //select 1748193.91/8271863
  308. for _, item := range hslList {
  309. resp.Actual = append(resp.Actual, util.Round(item, 3))
  310. }
  311. resp.Line = util.Round(all/float64(cowNumAll), 3)
  312. //resp.Actual = append(resp.Actual, hslList...)
  313. return resp, err
  314. }
  315. func (s *Service) GetWaterRateCompleteByPasture(center, startTime, endTime string) (*http.GetWaterRateCompleteByPastureResp, error) {
  316. resp := new(http.GetWaterRateCompleteByPastureResp)
  317. layout := "2006-01-02"
  318. startdate, err := time.Parse(layout, startTime)
  319. if err != nil {
  320. logs.Error("时间转换失败:", err)
  321. }
  322. enddate, err := time.Parse(layout, endTime)
  323. if err != nil {
  324. logs.Error("时间转换失败:", err)
  325. }
  326. feequeryList := make([]*model.FeeQueryAndPasture, 0)
  327. err = s.d.DB.Raw(`select ifnull(sum(HSL),0) HSL ,p.name from feequery fd join pasture p on p.id = fd.pastureid
  328. where fd.Date between ? and ? and fd.FYLX ='水费' and (p.center = ? or ? = '' ) group by fd.pastureId`,
  329. startdate.Format("2006-01"), enddate.Format("2006-01"), center, center).Find(&feequeryList).Error
  330. if err != nil {
  331. logs.Error(err)
  332. return resp, err
  333. }
  334. monthBudgetList := make([]*monthBudget, 0)
  335. err = s.d.DB.Raw(`select ifnull(sum(monthBudget),0) monthBudget,p.name from month_budget mb join pasture p on p.id = mb.pastureId
  336. where mb.feeType = '水费' and mb.budgetMonth between ? and ? and (p.center = ? or ? = '' ) group by mb.pastureId`,
  337. startdate.Format("2006-01"), enddate.Format("2006-01"), center, center).Find(&monthBudgetList).Error
  338. if err != nil {
  339. logs.Error(err)
  340. return resp, err
  341. }
  342. pastureList := make([]*model.Pasture, 0)
  343. err = s.d.DB.Where(" vendor = 1 ").Where(" IsEnergy = 0").Order("id").Find(&pastureList).Error
  344. if err != nil {
  345. logs.Error(err)
  346. return resp, err
  347. }
  348. var pastureName []string
  349. var budgetList, actualList, complete []float64
  350. var budgetAll, actualAll float64
  351. for _, monthBudget := range monthBudgetList {
  352. pastureName = append(pastureName, monthBudget.Name)
  353. budgetList = append(budgetList, util.Round(monthBudget.MonthBudget, 2))
  354. budgetAll += monthBudget.MonthBudget
  355. exist := false
  356. var hsl float64
  357. for _, feequery := range feequeryList {
  358. if feequery.Name == monthBudget.Name {
  359. exist = true
  360. hsl, err = strconv.ParseFloat(feequery.HSL, 64)
  361. if err != nil {
  362. actualList = append(actualList, 0)
  363. logs.Error("转换失败:", err)
  364. } else {
  365. actualAll += hsl
  366. actualList = append(actualList, util.Round(hsl, 2))
  367. continue
  368. }
  369. break
  370. }
  371. }
  372. if !exist {
  373. actualList = append(actualList, 0)
  374. }
  375. //预算减实际除预算加1
  376. if monthBudget.MonthBudget > 0 && hsl > 0 {
  377. complete = append(complete, util.Round(((monthBudget.MonthBudget-hsl)/monthBudget.MonthBudget+1)*100, 2))
  378. } else {
  379. complete = append(complete, 0)
  380. }
  381. }
  382. for _, pasture := range pastureList {
  383. exist := false
  384. for _, name := range pastureName {
  385. if pasture.Name == name {
  386. exist = true
  387. break
  388. }
  389. }
  390. if !exist {
  391. pastureName = append(pastureName, pasture.Name)
  392. actualList = append(actualList, 0)
  393. budgetList = append(budgetList, 0)
  394. complete = append(complete, 0)
  395. }
  396. }
  397. resp.Pasture = append(resp.Pasture, "集团")
  398. resp.Pasture = append(resp.Pasture, pastureName...)
  399. resp.Actual = append(resp.Actual, util.Round(actualAll, 2))
  400. resp.Actual = append(resp.Actual, actualList...)
  401. resp.Budget = append(resp.Budget, util.Round(budgetAll, 2))
  402. resp.Budget = append(resp.Budget, budgetList...)
  403. if budgetAll > 0 && actualAll > 0 {
  404. resp.Complete = append(resp.Complete, util.Round(((budgetAll-actualAll)/budgetAll+1)*100, 2))
  405. } else {
  406. resp.Complete = append(resp.Complete, 0)
  407. }
  408. resp.Complete = append(resp.Complete, complete...)
  409. return resp, nil
  410. }
  411. type dosagePerCow struct {
  412. Psum float64 `gorm:"column:psum"`
  413. Date string `gorm:"column:date"`
  414. Name string `gorm:"column:name"`
  415. CowNum int64 `gorm:"column:cowNum"`
  416. }
  417. //每月单头牛用水量对比
  418. func (s *Service) GetWaterPerHeadByPasture(pastureId, startTime, endTime string) (map[string]interface{}, error) {
  419. layout := "2006-01-02"
  420. startdate, err := time.Parse(layout, startTime)
  421. if err != nil {
  422. logs.Error("时间转换失败:", err)
  423. }
  424. enddate, err := time.Parse(layout, endTime)
  425. if err != nil {
  426. logs.Error("时间转换失败:", err)
  427. }
  428. enddate = enddate.AddDate(0, 1, -1)
  429. dosagePerCowList := make([]*dosagePerCow, 0)
  430. err = s.d.DB.Debug().Raw(`SELECT
  431. round( IFNULL(SUM(w.waterConsumption),0) ,2)psum,
  432. p.name,DATE_FORMAT( w.date, '%Y-%m') date,
  433. ifnull((select sum(cowNum) from feed_sum where DATE_FORMAT( feedDay, '%Y-%m') = DATE_FORMAT( w.date, '%Y-%m') and pastureId = p.id ),0)cowNum
  434. FROM
  435. water w
  436. INNER JOIN measure ms
  437. ON w.measureId = ms.id
  438. INNER JOIN pasture p
  439. ON ms.pastureId = p.id
  440. WHERE
  441. (w.date between ? and ? ) AND ms.useType = '1级表' and p.name <> '现代牧业' and FIND_IN_SET(p.id,?)
  442. GROUP BY DATE_FORMAT( w.date, '%Y-%m'),ms.pastureId order by DATE_FORMAT( w.date, '%Y-%m'),p.name asc `, startdate.Format("2006-01-02"),
  443. enddate.Format("2006-01-02"), pastureId).Find(&dosagePerCowList).Error
  444. if err != nil {
  445. logs.Error(err)
  446. return nil, err
  447. }
  448. data := dosagePerCowFormat(dosagePerCowList)
  449. return data, nil
  450. }
  451. func dosagePerCowFormat(dosagePerCowList []*dosagePerCow) map[string]interface{} {
  452. var dateList, pastureNameList []string
  453. var dataList []interface{}
  454. data := make(map[string]interface{}, 0)
  455. data["data1"] = []string{}
  456. data["data2"] = []string{}
  457. data["data3"] = []interface{}{}
  458. for _, item := range dosagePerCowList {
  459. if len(dateList) == 0 {
  460. dateList = append(dateList, item.Date)
  461. }
  462. dateExist := false
  463. for _, date := range dateList {
  464. if date == item.Date {
  465. dateExist = true
  466. break
  467. }
  468. }
  469. if !dateExist {
  470. dateList = append(dateList, item.Date)
  471. }
  472. if len(pastureNameList) == 0 {
  473. pastureNameList = append(pastureNameList, item.Name)
  474. }
  475. pastureNameExist := false
  476. for _, pastureName := range pastureNameList {
  477. if pastureName == item.Name {
  478. pastureNameExist = true
  479. break
  480. }
  481. }
  482. if !pastureNameExist {
  483. pastureNameList = append(pastureNameList, item.Name)
  484. }
  485. }
  486. for _, pastureName := range pastureNameList {
  487. var number []float64
  488. for _, date := range dateList {
  489. exist := false
  490. for _, item := range dosagePerCowList {
  491. if item.Date == date && item.Name == pastureName {
  492. exist = true
  493. number = append(number, util.Round(item.Psum/float64(item.CowNum), 2))
  494. break
  495. }
  496. }
  497. if !exist {
  498. number = append(number, 0)
  499. }
  500. }
  501. dataList = append(dataList, number)
  502. }
  503. data["data1"] = pastureNameList
  504. data["data2"] = dateList
  505. data["data3"] = dataList
  506. return data
  507. }
  508. //-----------------------
  509. //各牧场年度用电量对比
  510. func (s *Service) GetElectricityYieldByPasture(center, startTime, endTime string) (*http.StatementResp, error) {
  511. resp := new(http.StatementResp)
  512. layout := "2006-01-02"
  513. startdate, err := time.Parse(layout, startTime)
  514. if err != nil {
  515. logs.Error("时间转换失败:", err)
  516. }
  517. enddate, err := time.Parse(layout, endTime)
  518. if err != nil {
  519. logs.Error("时间转换失败:", err)
  520. }
  521. enddate = enddate.AddDate(0, 1, -1)
  522. nowYearList := make([]*pastureStatistics, 0)
  523. lastYearList := make([]*pastureStatistics, 0)
  524. sqlstr := `SELECT
  525. round( IFNULL(SUM(w.elecConsumption),0) ,2)psum,
  526. p.name as name
  527. FROM
  528. electricity w
  529. INNER JOIN measure ms
  530. ON w.measureId = ms.id
  531. INNER JOIN pasture p
  532. ON ms.pastureId = p.id
  533. WHERE
  534. (w.date between ? and ? ) AND ms.useType = '1级表' and p.name <> '现代牧业' and (p.center = ? or ? = '' )
  535. GROUP BY ms.pastureId `
  536. err = s.d.DB.Debug().Raw(sqlstr, startdate.Format("2006-01-02"), enddate.Format("2006-01-02"), center, center).Find(&nowYearList).Error
  537. if err != nil {
  538. logs.Error(err)
  539. return nil, err
  540. }
  541. err = s.d.DB.Raw(sqlstr, startdate.AddDate(-1, 0, 0).Format("2006-01-02"),
  542. enddate.AddDate(-1, 0, 0).Format("2006-01-02"), center, center).Find(&lastYearList).Error
  543. if err != nil {
  544. logs.Error(err)
  545. return nil, err
  546. }
  547. pastureList := make([]*model.Pasture, 0)
  548. err = s.d.DB.Where(" vendor = 1 ").Where(" IsEnergy = 0").Order("id").Find(&pastureList).Error
  549. if err != nil {
  550. logs.Error(err)
  551. return resp, err
  552. }
  553. var pastureName []string
  554. var nowYearData, lastYearData []float64
  555. var all float64
  556. for _, nowYear := range nowYearList {
  557. pastureName = append(pastureName, nowYear.Name)
  558. nowYearData = append(nowYearData, util.Round(nowYear.Psum, 2))
  559. all += nowYear.Psum
  560. exist := false
  561. for _, lastYear := range lastYearList {
  562. if lastYear.Name == nowYear.Name {
  563. exist = true
  564. lastYearData = append(lastYearData, util.Round(lastYear.Psum, 2))
  565. break
  566. }
  567. }
  568. if !exist {
  569. lastYearData = append(lastYearData, 0)
  570. }
  571. }
  572. for _, pasture := range pastureList {
  573. exist := false
  574. for _, name := range pastureName {
  575. if pasture.Name == name {
  576. exist = true
  577. break
  578. }
  579. }
  580. if !exist {
  581. pastureName = append(pastureName, pasture.Name)
  582. nowYearData = append(nowYearData, 0)
  583. lastYearData = append(lastYearData, 0)
  584. }
  585. }
  586. resp.Pasture = pastureName
  587. resp.LastYear = lastYearData
  588. resp.NowYear = nowYearData
  589. resp.All = util.Round(all, 2)
  590. return resp, nil
  591. }
  592. func (s *Service) GetElectricitySingleCowByPasture(center, startTime, endTime string) (*http.StatementResp, error) {
  593. resp := new(http.StatementResp)
  594. layout := "2006-01-02"
  595. startdate, err := time.Parse(layout, startTime)
  596. if err != nil {
  597. logs.Error("时间转换失败:", err)
  598. }
  599. enddate, err := time.Parse(layout, endTime)
  600. if err != nil {
  601. logs.Error("时间转换失败:", err)
  602. }
  603. enddate = enddate.AddDate(0, 1, -1)
  604. nowYearList := make([]*pastureStatistics, 0)
  605. lastYearList := make([]*pastureStatistics, 0)
  606. sqlstr := `SELECT
  607. round( IFNULL(SUM(w.elecConsumption),0) ,2)psum,
  608. p.name,ms.pastureId
  609. FROM
  610. electricity w
  611. INNER JOIN measure ms
  612. ON w.measureId = ms.id
  613. INNER JOIN pasture p
  614. ON ms.pastureId = p.id
  615. WHERE
  616. (w.date between ? and ? ) AND ms.useType = '1级表' and ms.meterType = '电表' and p.name <> '现代牧业' and (p.center = ? or ? = '' ) and p.vendor = 1 and IsEnergy = 0
  617. GROUP BY ms.pastureId `
  618. err = s.d.DB.Raw(sqlstr, startdate.Format("2006-01-02"), enddate.Format("2006-01-02"), center, center).Find(&nowYearList).Error
  619. if err != nil {
  620. logs.Error(err)
  621. return nil, err
  622. }
  623. err = s.d.DB.Raw(sqlstr, startdate.AddDate(-1, 0, 0).Format("2006-01-02"),
  624. enddate.AddDate(-1, 0, 0).Format("2006-01-02"), center, center).Find(&lastYearList).Error
  625. if err != nil {
  626. logs.Error(err)
  627. return nil, err
  628. }
  629. pastureList := make([]*model.Pasture, 0)
  630. err = s.d.DB.Where(" vendor = 1 ").Where(" IsEnergy = 0").Order("id").Find(&pastureList).Error
  631. if err != nil {
  632. logs.Error(err)
  633. return resp, err
  634. }
  635. cowNumList := make([]*model.FeedSumAndPasture, 0)
  636. lastCowNumList := make([]*model.FeedSumAndPasture, 0)
  637. err = s.d.DB.Raw(`select sum(cowNum)cowNum ,p.name,fd.pastureId from feed_sum fd join pasture p on p.id = fd.pastureid
  638. where fd.feedDay between ? and ? and (p.center = ? or ? = '' ) and p.vendor = 1 and IsEnergy = 0 group by p.id `,
  639. startdate.Format("2006-01-02"), enddate.Format("2006-01-02"), center, center).Find(&cowNumList).Error
  640. if err != nil {
  641. logs.Error(err)
  642. return nil, err
  643. }
  644. err = s.d.DB.Raw(`select sum(cowNum)cowNum ,p.name,fd.pastureId from feed_sum fd join pasture p on p.id = fd.pastureid
  645. where fd.feedDay between ? and ? and (p.center = ? or ? = '' ) and p.vendor = 1 and IsEnergy = 0 group by p.id `,
  646. startdate.AddDate(-1, 0, 0).Format("2006-01-02"),
  647. enddate.AddDate(-1, 0, 0).Format("2006-01-02"), center, center).Find(&lastCowNumList).Error
  648. if err != nil {
  649. logs.Error(err)
  650. return nil, err
  651. }
  652. cowNum := make(map[string]int64, 0)
  653. var cowNumAll, lastCowNumAll int64
  654. for _, item := range cowNumList {
  655. //cowNumAll += item.FeedSum.CowNum
  656. cowNum[item.Pasture.Name] = item.FeedSum.CowNum
  657. }
  658. lastCowNum := make(map[string]int64, 0)
  659. for _, item := range lastCowNumList {
  660. //lastCowNumAll += item.FeedSum.CowNum
  661. lastCowNum[item.Pasture.Name] = item.FeedSum.CowNum
  662. }
  663. var pastureName []string
  664. var nowYearData, lastYearData []float64
  665. var all, lastAll float64
  666. for _, nowYear := range nowYearList {
  667. pastureName = append(pastureName, nowYear.Name)
  668. if _, ok := cowNum[nowYear.Name]; ok {
  669. all += nowYear.Psum
  670. cowNumAll += cowNum[nowYear.Name]
  671. nowYearData = append(nowYearData, util.Round(nowYear.Psum/float64(cowNum[nowYear.Name]), 3))
  672. } else {
  673. nowYearData = append(nowYearData, 0)
  674. }
  675. exist := false
  676. for _, lastYear := range lastYearList {
  677. if lastYear.Name == nowYear.Name {
  678. exist = true
  679. if _, ok := lastCowNum[lastYear.Name]; ok {
  680. lastAll += nowYear.Psum
  681. lastCowNumAll += lastCowNum[nowYear.Name]
  682. lastYearData = append(lastYearData, util.Round(lastYear.Psum/float64(lastCowNum[nowYear.Name]), 3))
  683. } else {
  684. lastYearData = append(lastYearData, 0)
  685. }
  686. break
  687. }
  688. }
  689. if !exist {
  690. lastYearData = append(lastYearData, 0)
  691. }
  692. }
  693. for _, pasture := range pastureList {
  694. exist := false
  695. for _, name := range pastureName {
  696. if pasture.Name == name {
  697. exist = true
  698. break
  699. }
  700. }
  701. if !exist {
  702. pastureName = append(pastureName, pasture.Name)
  703. nowYearData = append(nowYearData, 0)
  704. lastYearData = append(lastYearData, 0)
  705. }
  706. }
  707. resp.Pasture = append(resp.Pasture, "集团")
  708. resp.Pasture = append(resp.Pasture, pastureName...)
  709. resp.LastYear = append(resp.LastYear, util.Round(lastAll/float64(lastCowNumAll), 3))
  710. //resp.LastYear = append(resp.LastYear, lastYearData...)
  711. for _, item := range lastYearData {
  712. resp.LastYear = append(resp.LastYear, util.Round(item, 3))
  713. }
  714. resp.NowYear = append(resp.NowYear, util.Round(all/float64(cowNumAll), 3))
  715. num := 0.0
  716. for _, item := range nowYearData {
  717. num += item
  718. resp.NowYear = append(resp.NowYear, util.Round(item, 3))
  719. }
  720. fmt.Println(num)
  721. resp.All = util.Round(all, 3)
  722. cowNumAll1 := float64(cowNumAll)
  723. resp.Line = util.Round(all/cowNumAll1, 3)
  724. return resp, nil
  725. }
  726. //单头牛电费
  727. func (s *Service) GetElectricityRateByPasture(center, startTime, endTime string) (*http.GetWaterRateByPastureResp, error) {
  728. resp := new(http.GetWaterRateByPastureResp)
  729. layout := "2006-01-02"
  730. startdate, err := time.Parse(layout, startTime)
  731. if err != nil {
  732. logs.Error("时间转换失败:", err)
  733. }
  734. enddate, err := time.Parse(layout, endTime)
  735. if err != nil {
  736. logs.Error("时间转换失败:", err)
  737. }
  738. enddate = enddate.AddDate(0, 1, -1)
  739. cowNumList := make([]*model.FeedSumAndPasture, 0)
  740. err = s.d.DB.Raw(`select sum(cowNum)cowNum ,p.name from feed_sum fd join pasture p on p.id = fd.pastureid
  741. where fd.feedDay between ? and ? and (p.center = ? or ? = '' ) group by pastureid `,
  742. startdate.Format("2006-01-02"), enddate.Format("2006-01-02"), center, center).Find(&cowNumList).Error
  743. if err != nil {
  744. logs.Error(err)
  745. return nil, err
  746. }
  747. cowNum := make(map[string]int64, 0)
  748. var cowNumAll int64
  749. for _, item := range cowNumList {
  750. cowNum[item.Pasture.Name] = item.FeedSum.CowNum
  751. }
  752. pastureList := make([]*model.Pasture, 0)
  753. err = s.d.DB.Where(" vendor = 1 ").Where(" IsEnergy = 0").Order("id").Find(&pastureList).Error
  754. if err != nil {
  755. logs.Error(err)
  756. return resp, err
  757. }
  758. feequeryList := make([]*model.FeeQueryAndPasture, 0)
  759. err = s.d.DB.Debug().Raw(`select sum(HSL) HSL ,p.name from feequery fd join pasture p on p.id = fd.pastureid
  760. where fd.Date between ? and ? and fd.FYLX ='电费' and (p.center = ? or ? = '' ) group by fd.pastureId order by p.id`,
  761. startdate.Format("2006-01"), enddate.Format("2006-01"), center, center).Find(&feequeryList).Error
  762. if err != nil {
  763. logs.Error(err)
  764. return resp, err
  765. }
  766. var pastureName []string
  767. var hslList []float64
  768. var all float64
  769. for _, feequery := range feequeryList {
  770. pastureName = append(pastureName, feequery.Pasture.Name)
  771. var hsl float64
  772. hsl, err = strconv.ParseFloat(feequery.HSL, 64)
  773. if err != nil {
  774. logs.Error("转换失败:", err)
  775. hslList = append(hslList, 0)
  776. }
  777. if _, ok := cowNum[feequery.Name]; ok {
  778. all += hsl
  779. cowNumAll += cowNum[feequery.Pasture.Name]
  780. hslList = append(hslList, hsl/float64(cowNum[feequery.Pasture.Name]))
  781. } else {
  782. hslList = append(hslList, 0)
  783. }
  784. }
  785. for _, item := range pastureList {
  786. exist := false
  787. for _, f := range feequeryList {
  788. if item.Name == f.Name {
  789. exist = true
  790. break
  791. }
  792. }
  793. if !exist {
  794. pastureName = append(pastureName, item.Name)
  795. hslList = append(hslList, 0)
  796. }
  797. }
  798. resp.Pasture = append(resp.Pasture, "集团")
  799. resp.Pasture = append(resp.Pasture, pastureName...)
  800. resp.Actual = append(resp.Actual, util.Round(all/float64(cowNumAll), 3))
  801. for _, item := range hslList {
  802. resp.Actual = append(resp.Actual, util.Round(item, 3))
  803. }
  804. resp.Line = util.Round(all/float64(cowNumAll), 3)
  805. //resp.Actual = append(resp.Actual, hslList...)
  806. return resp, err
  807. }
  808. func (s *Service) GetElectricityRateCompleteByPasture(center, startTime, endTime string) (*http.GetWaterRateCompleteByPastureResp, error) {
  809. resp := new(http.GetWaterRateCompleteByPastureResp)
  810. layout := "2006-01-02"
  811. startdate, err := time.Parse(layout, startTime)
  812. if err != nil {
  813. logs.Error("时间转换失败:", err)
  814. }
  815. enddate, err := time.Parse(layout, endTime)
  816. if err != nil {
  817. logs.Error("时间转换失败:", err)
  818. }
  819. enddate = enddate.AddDate(0, 1, -1)
  820. feequeryList := make([]*model.FeeQueryAndPasture, 0)
  821. err = s.d.DB.Raw(`select ifnull(sum(HSL),0) HSL ,p.name from feequery fd join pasture p on p.id = fd.pastureid
  822. where fd.Date between ? and ? and fd.FYLX ='电费' and (p.center = ? or ? = '' ) group by fd.pastureId`,
  823. startdate.Format("2006-01"), enddate.Format("2006-01"), center, center).Find(&feequeryList).Error
  824. if err != nil {
  825. logs.Error(err)
  826. return resp, err
  827. }
  828. monthBudgetList := make([]*monthBudget, 0)
  829. err = s.d.DB.Raw(`select ifnull(sum(monthBudget),0) monthBudget,p.name from month_budget mb join pasture p on p.id = mb.pastureId
  830. where mb.feeType = '电费' and mb.budgetMonth between ? and ? and (p.center = ? or ? = '' ) group by mb.pastureId`, startdate.Format("2006-01"), enddate.Format("2006-01"), center, center).Find(&monthBudgetList).Error
  831. if err != nil {
  832. logs.Error(err)
  833. return resp, err
  834. }
  835. pastureList := make([]*model.Pasture, 0)
  836. err = s.d.DB.Where(" vendor = 1 ").Where(" IsEnergy = 0").Order("id").Find(&pastureList).Error
  837. if err != nil {
  838. logs.Error(err)
  839. return resp, err
  840. }
  841. var pastureName []string
  842. var budgetList, actualList, complete []float64
  843. var budgetAll, actualAll float64
  844. for _, monthBudget := range monthBudgetList {
  845. pastureName = append(pastureName, monthBudget.Name)
  846. budgetList = append(budgetList, util.Round(monthBudget.MonthBudget/10000, 2))
  847. budgetAll += monthBudget.MonthBudget
  848. exist := false
  849. var hsl float64
  850. for _, feequery := range feequeryList {
  851. if feequery.Name == monthBudget.Name {
  852. exist = true
  853. hsl, err = strconv.ParseFloat(feequery.HSL, 64)
  854. if err != nil {
  855. actualList = append(actualList, 0)
  856. logs.Error("转换失败:", err)
  857. } else {
  858. actualAll += hsl
  859. actualList = append(actualList, util.Round(hsl/10000, 2))
  860. continue
  861. }
  862. break
  863. }
  864. }
  865. if !exist {
  866. actualList = append(actualList, 0)
  867. }
  868. //预算减实际除预算加1
  869. if monthBudget.MonthBudget > 0 && hsl > 0 {
  870. complete = append(complete, util.Round(((monthBudget.MonthBudget-hsl)/monthBudget.MonthBudget+1)*100, 2))
  871. } else {
  872. complete = append(complete, 0)
  873. }
  874. }
  875. for _, pasture := range pastureList {
  876. exist := false
  877. for _, name := range pastureName {
  878. if pasture.Name == name {
  879. exist = true
  880. break
  881. }
  882. }
  883. if !exist {
  884. pastureName = append(pastureName, pasture.Name)
  885. actualList = append(actualList, 0)
  886. budgetList = append(budgetList, 0)
  887. complete = append(complete, 0)
  888. }
  889. }
  890. //resp.Pasture = append(resp.Pasture, "集团")
  891. resp.Pasture = append(resp.Pasture, pastureName...)
  892. //resp.Actual = append(resp.Actual, util.Round(actualAll/10000, 2))
  893. resp.Actual = append(resp.Actual, actualList...)
  894. //resp.Budget = append(resp.Budget, util.Round(budgetAll/10000, 2))
  895. resp.Budget = append(resp.Budget, budgetList...)
  896. //if budgetAll > 0 && actualAll > 0 {
  897. // resp.Complete = append(resp.Complete, util.Round(((budgetAll-actualAll)/budgetAll+1)*100, 2))
  898. //} else {
  899. // resp.Complete = append(resp.Complete, 0)
  900. //}
  901. resp.Complete = append(resp.Complete, complete...)
  902. return resp, nil
  903. }
  904. //每月单头牛用电量对比
  905. func (s *Service) GetElectricityPerHeadByPasture(pastureId, startTime, endTime string) (map[string]interface{}, error) {
  906. layout := "2006-01-02"
  907. startdate, err := time.Parse(layout, startTime)
  908. if err != nil {
  909. logs.Error("时间转换失败:", err)
  910. }
  911. enddate, err := time.Parse(layout, endTime)
  912. if err != nil {
  913. logs.Error("时间转换失败:", err)
  914. }
  915. enddate = enddate.AddDate(0, 1, -1)
  916. dosagePerCowList := make([]*dosagePerCow, 0)
  917. err = s.d.DB.Debug().Raw(`SELECT
  918. round( IFNULL(SUM(w.elecConsumption),0) ,2)psum,
  919. p.name,DATE_FORMAT( w.date, '%Y-%m') date,
  920. ifnull((select sum(cowNum) from feed_sum where DATE_FORMAT( feedDay, '%Y-%m') = DATE_FORMAT( w.date, '%Y-%m') and pastureId = p.id ),0)cowNum
  921. FROM
  922. electricity w
  923. INNER JOIN measure ms
  924. ON w.measureId = ms.id
  925. INNER JOIN pasture p
  926. ON ms.pastureId = p.id
  927. WHERE
  928. (w.date between ? and ? ) AND ms.useType = '1级表' and p.name <> '现代牧业' and FIND_IN_SET(p.id,?)
  929. GROUP BY DATE_FORMAT( w.date, '%Y-%m'),ms.pastureId order by DATE_FORMAT( w.date, '%Y-%m'),p.id `, startdate.Format("2006-01-02"),
  930. enddate.Format("2006-01-02"), pastureId).Find(&dosagePerCowList).Error
  931. if err != nil {
  932. logs.Error(err)
  933. return nil, err
  934. }
  935. data := dosagePerCowFormat(dosagePerCowList)
  936. return data, nil
  937. }
  938. //--------------------------------------- 维修
  939. //各牧场年度维修对比
  940. func (s *Service) GetWxYieldByPasture(deptname, center, startTime, endTime string) (*http.StatementResp, error) {
  941. resp := new(http.StatementResp)
  942. layout := "2006-01-02"
  943. startdate, err := time.Parse(layout, startTime)
  944. if err != nil {
  945. logs.Error("时间转换失败:", err)
  946. }
  947. enddate, err := time.Parse(layout, endTime)
  948. if err != nil {
  949. logs.Error("时间转换失败:", err)
  950. }
  951. enddate = enddate.AddDate(0, 1, -1)
  952. nowYearList := make([]*pastureStatistics, 0)
  953. //lastYearList := make([]*pastureStatistics, 0)
  954. sqlstr := `SELECT
  955. round(ifnull(SUM( pu.sumPrice - pu.quitNumber * pu.price),0),4) psum,
  956. p.name
  957. FROM
  958. partuse pu
  959. INNER JOIN bigpartuse bpu
  960. ON pu.bigId = bpu.id
  961. INNER JOIN pasture p
  962. ON bpu.pastureId = p.id
  963. #INNER JOIN department dpt
  964. #on dpt.id = bpu.departmentId
  965. WHERE bpu.useType <> 3 and p.IsEnergy = 0
  966. AND (SELECT name FROM department d WHERE d.id=bpu.departmentId) not in ('挤奶处','牧场办公室','财务部','餐厅','信息处')
  967. AND (
  968. pu.useTypeV = '维修'
  969. OR pu.useTypeV = '保养'
  970. )
  971. and ( bpu.receiveTime between ? and ?)
  972. AND (bpu.departmentId in (select id from department where pastureId = bpu.pastureId and name = ? )
  973. or if(? = 1 ,bpu.departmentId in(select id from department where pastureId = bpu.pastureId and name != '挤奶处' and name != '牧场办公室' ),0=1) or ?='' ) and p.name <> '现代牧业' and (p.center = ? or ? = '' ) and pu.dflag != 1
  974. GROUP BY bpu.pastureId `
  975. err = s.d.DB.Debug().Raw(sqlstr, startdate.Format("2006-01-02"), enddate.Format("2006-01-02"), deptname, deptname, deptname, center, center).Find(&nowYearList).Error
  976. if err != nil {
  977. logs.Error(err)
  978. return nil, err
  979. }
  980. monthBudgetList := make([]*monthBudget, 0)
  981. err = s.d.DB.Debug().Raw(`select ifnull(sum(monthBudget),0) monthBudget,p.name from month_budget mb join pasture p on p.id = mb.pastureId
  982. where mb.feeType = '维修费' and mb.budgetMonth between ? and ? AND (mb.deptId in (select id from department where pastureId = mb.pastureId and name = ? )
  983. or if(? = 1 ,mb.deptId in(select id from department where pastureId = mb.pastureId and name != '挤奶处' and name != '牧场办公室' ),0=1) or ?='' ) and (p.center = ? or ? = '' ) group by mb.pastureId`,
  984. startdate.Format("2006-01-02"), enddate.Format("2006-01-02"), deptname, deptname, deptname, center, center).Find(&monthBudgetList).Error
  985. if err != nil {
  986. logs.Error(err)
  987. return resp, err
  988. }
  989. //err = s.d.DB.Raw(sqlstr, startdate.AddDate(-1, 0, 0).Format("2006-01-02"),
  990. // enddate.AddDate(-1, 0, 0).Format("2006-01-02"), deptname, deptname, deptname, deptname, deptname, center, center).Find(&lastYearList).Error
  991. //if err != nil {
  992. // logs.Error(err)
  993. // return nil, err
  994. //}
  995. pastureList := make([]*model.Pasture, 0)
  996. err = s.d.DB.Where(" vendor = 1 ").Where("IsEnergy = 0").Order("id").Find(&pastureList).Error
  997. if err != nil {
  998. logs.Error(err)
  999. return resp, err
  1000. }
  1001. var pastureName []string
  1002. var nowYearData, lastYearData []float64
  1003. var all float64
  1004. for _, nowYear := range nowYearList {
  1005. pastureName = append(pastureName, nowYear.Name)
  1006. nowYearData = append(nowYearData, util.Round(nowYear.Psum, 2))
  1007. all += nowYear.Psum
  1008. //exist := false
  1009. //for _, lastYear := range lastYearList {
  1010. // if lastYear.Name == nowYear.Name {
  1011. // exist = true
  1012. //
  1013. // break
  1014. // }
  1015. //}
  1016. //if !exist {
  1017. // lastYearData = append(lastYearData, 0)
  1018. //}
  1019. }
  1020. for _, pasture := range pastureList {
  1021. exist := false
  1022. for _, name := range pastureName {
  1023. if pasture.Name == name {
  1024. exist = true
  1025. break
  1026. }
  1027. }
  1028. if !exist {
  1029. pastureName = append(pastureName, pasture.Name)
  1030. nowYearData = append(nowYearData, 0)
  1031. //lastYearData = append(lastYearData, 0)
  1032. }
  1033. }
  1034. for _, name := range pastureName {
  1035. exist := false
  1036. for _, monthBudget := range monthBudgetList {
  1037. if monthBudget.Name == name {
  1038. exist = true
  1039. lastYearData = append(lastYearData, util.Round(monthBudget.MonthBudget, 2))
  1040. break
  1041. }
  1042. }
  1043. if !exist {
  1044. lastYearData = append(lastYearData, 0)
  1045. }
  1046. }
  1047. resp.Pasture = pastureName
  1048. resp.LastYear = lastYearData
  1049. resp.NowYear = nowYearData
  1050. resp.All = util.Round(all, 2)
  1051. return resp, nil
  1052. }
  1053. func (s *Service) GetWxSingleCowByPasture(center, startTime, endTime string) (*http.StatementResp, error) {
  1054. resp := new(http.StatementResp)
  1055. layout := "2006-01-02"
  1056. startdate, err := time.Parse(layout, startTime)
  1057. if err != nil {
  1058. logs.Error("时间转换失败:", err)
  1059. }
  1060. enddate, err := time.Parse(layout, endTime)
  1061. if err != nil {
  1062. logs.Error("时间转换失败:", err)
  1063. }
  1064. enddate = enddate.AddDate(0, 1, -1)
  1065. nowYearList := make([]*pastureStatistics, 0)
  1066. //lastYearList := make([]*pastureStatistics, 0)
  1067. sqlstr := `SELECT
  1068. round(ifnull(SUM( pu.sumPrice - pu.quitNumber * pu.price),0),4) psum,
  1069. p.name
  1070. FROM
  1071. partuse pu
  1072. INNER JOIN bigpartuse bpu
  1073. ON pu.bigId = bpu.id
  1074. INNER JOIN pasture p
  1075. ON bpu.pastureId = p.id
  1076. #INNER JOIN department dpt
  1077. #on dpt.id = bpu.departmentId
  1078. WHERE bpu.useType <> 3
  1079. AND (
  1080. pu.useTypeV = '维修'
  1081. OR pu.useTypeV = '保养'
  1082. )
  1083. and ( bpu.receiveTime between ? and ?)
  1084. and p.name <> '现代牧业' and (p.center = ? or ? = '' ) and pu.dflag != 1
  1085. AND (SELECT name FROM department d WHERE d.id=bpu.departmentId) not in ('挤奶处','牧场办公室','财务部','餐厅','信息处')
  1086. GROUP BY bpu.pastureId `
  1087. err = s.d.DB.Raw(sqlstr, startdate.Format("2006-01-02"), enddate.Format("2006-01-02"), center, center).Find(&nowYearList).Error
  1088. if err != nil {
  1089. logs.Error(err)
  1090. return nil, err
  1091. }
  1092. pastureList := make([]*model.Pasture, 0)
  1093. err = s.d.DB.Where(" vendor = 1 ").Where(" IsEnergy = 0").Order("id").Find(&pastureList).Error
  1094. if err != nil {
  1095. logs.Error(err)
  1096. return resp, err
  1097. }
  1098. cowNumList := make([]*model.FeedSumAndPasture, 0)
  1099. //lastCowNumList := make([]*model.FeedSumAndPasture, 0)
  1100. err = s.d.DB.Raw(`select sum(cowNum)cowNum ,p.name from feed_sum fd join pasture p on p.id = fd.pastureid
  1101. where fd.feedDay between ? and ?
  1102. and (p.center = ? or ? = '' )
  1103. group by pastureid `, startdate.Format("2006-01-02"), enddate.Format("2006-01-02"), center, center).Find(&cowNumList).Error
  1104. if err != nil {
  1105. logs.Error(err)
  1106. return nil, err
  1107. }
  1108. monthBudgetList := make([]*monthBudget, 0)
  1109. err = s.d.DB.Raw(`select ifnull(sum(monthBudget),0) monthBudget,p.name from month_budget mb join pasture p on p.id = mb.pastureId
  1110. where mb.feeType = '维修费' and mb.budgetMonth between ? and ? and (p.center = ? or ? = '' ) group by mb.pastureId`,
  1111. startdate.Format("2006-01-02"), enddate.Format("2006-01-02"), center, center).Find(&monthBudgetList).Error
  1112. if err != nil {
  1113. logs.Error(err)
  1114. return resp, err
  1115. }
  1116. cowNum := make(map[string]int64, 0)
  1117. var cowNumAll int64
  1118. for _, item := range cowNumList {
  1119. cowNum[item.Pasture.Name] = item.FeedSum.CowNum
  1120. }
  1121. //lastCowNum := make(map[string]int64, 0)
  1122. //for _, item := range lastCowNumList {
  1123. // lastCowNumAll += item.FeedSum.CowNum
  1124. // lastCowNum[item.Pasture.Name] = item.FeedSum.CowNum
  1125. //}
  1126. var pastureName []string
  1127. var nowYearData, lastYearData []float64
  1128. var all, lastAll float64
  1129. for _, nowYear := range nowYearList {
  1130. pastureName = append(pastureName, nowYear.Name)
  1131. if _, ok := cowNum[nowYear.Name]; ok {
  1132. all += nowYear.Psum
  1133. cowNumAll += cowNum[nowYear.Name]
  1134. nowYearData = append(nowYearData, nowYear.Psum/float64(cowNum[nowYear.Name]))
  1135. } else {
  1136. nowYearData = append(nowYearData, 0)
  1137. }
  1138. }
  1139. for _, pasture := range pastureList {
  1140. exist := false
  1141. for _, name := range pastureName {
  1142. if pasture.Name == name {
  1143. exist = true
  1144. break
  1145. }
  1146. }
  1147. if !exist {
  1148. pastureName = append(pastureName, pasture.Name)
  1149. nowYearData = append(nowYearData, 0)
  1150. lastYearData = append(lastYearData, 0)
  1151. }
  1152. }
  1153. for _, name := range pastureName {
  1154. exist := false
  1155. for _, monthBudget := range monthBudgetList {
  1156. if monthBudget.Name == name {
  1157. exist = true
  1158. if _, ok := cowNum[monthBudget.Name]; ok {
  1159. lastAll += monthBudget.MonthBudget
  1160. lastYearData = append(lastYearData, monthBudget.MonthBudget/float64(cowNum[monthBudget.Name]))
  1161. } else {
  1162. lastYearData = append(lastYearData, 0)
  1163. }
  1164. break
  1165. }
  1166. }
  1167. if !exist {
  1168. lastYearData = append(lastYearData, 0)
  1169. }
  1170. }
  1171. resp.Pasture = append(resp.Pasture, "集团")
  1172. resp.Pasture = append(resp.Pasture, pastureName...)
  1173. resp.LastYear = append(resp.LastYear, util.Round(lastAll/float64(cowNumAll), 3))
  1174. //resp.LastYear = append(resp.LastYear, lastYearData...)
  1175. for _, item := range lastYearData {
  1176. resp.LastYear = append(resp.LastYear, util.Round(item, 3))
  1177. }
  1178. resp.NowYear = append(resp.NowYear, util.Round(all/float64(cowNumAll), 3))
  1179. for _, item := range nowYearData {
  1180. resp.NowYear = append(resp.NowYear, util.Round(item, 3))
  1181. }
  1182. resp.All = all
  1183. resp.Line = util.Round(all/float64(cowNumAll), 2)
  1184. return resp, nil
  1185. }
  1186. //每月单头牛维修对比
  1187. func (s *Service) GetWxPerHeadByPasture(pastureId, startTime, endTime string) (map[string]interface{}, error) {
  1188. layout := "2006-01-02"
  1189. startdate, err := time.Parse(layout, startTime)
  1190. if err != nil {
  1191. logs.Error("时间转换失败:", err)
  1192. }
  1193. enddate, err := time.Parse(layout, endTime)
  1194. if err != nil {
  1195. logs.Error("时间转换失败:", err)
  1196. }
  1197. enddate = enddate.AddDate(0, 1, -1)
  1198. dosagePerCowList := make([]*dosagePerCow, 0)
  1199. err = s.d.DB.Debug().Raw(`SELECT
  1200. round(ifnull(SUM( pu.sumPrice - pu.quitNumber * pu.price),0),4) psum,
  1201. p.name,DATE_FORMAT( bpu.receiveTime, '%Y-%m') date,
  1202. ifnull((select sum(cowNum) from feed_sum where DATE_FORMAT( feedDay, '%Y-%m') = DATE_FORMAT( bpu.receiveTime, '%Y-%m') and pastureId = p.id ),0)cowNum
  1203. FROM
  1204. partuse pu
  1205. INNER JOIN bigpartuse bpu
  1206. ON pu.bigId = bpu.id
  1207. INNER JOIN pasture p
  1208. ON bpu.pastureId = p.id
  1209. #INNER JOIN department dpt
  1210. #on dpt.id = bpu.departmentId
  1211. WHERE bpu.useType <> 3
  1212. AND (
  1213. pu.useTypeV = '维修'
  1214. OR pu.useTypeV = '保养'
  1215. )
  1216. and ( bpu.receiveTime between ? and ?)
  1217. AND (SELECT name FROM department d WHERE d.id=bpu.departmentId) not in ('挤奶处','牧场办公室','财务部','餐厅','信息处')
  1218. and p.name <> '现代牧业' and FIND_IN_SET(p.id,?) and pu.dflag != 1
  1219. GROUP BY DATE_FORMAT( bpu.receiveTime, '%Y-%m'),p.id order by DATE_FORMAT( bpu.receiveTime, '%Y-%m'),p.id `, startdate.Format("2006-01-02"),
  1220. enddate.Format("2006-01-02"), pastureId).Find(&dosagePerCowList).Error
  1221. if err != nil {
  1222. logs.Error(err)
  1223. return nil, err
  1224. }
  1225. data := dosagePerCowFormat(dosagePerCowList)
  1226. return data, nil
  1227. }
  1228. //各牧场年度燃动费对比
  1229. func (s *Service) GetIgnitionYieldByPasture(center, startTime, endTime string) (*http.StatementResp, error) {
  1230. resp := new(http.StatementResp)
  1231. layout := "2006-01-02"
  1232. startdate, err := time.Parse(layout, startTime)
  1233. if err != nil {
  1234. logs.Error("时间转换失败:", err)
  1235. }
  1236. enddate, err := time.Parse(layout, endTime)
  1237. if err != nil {
  1238. logs.Error("时间转换失败:", err)
  1239. }
  1240. feequeryList := make([]*model.FeeQueryAndPasture, 0)
  1241. err = s.d.DB.Debug().Raw(`select sum(HSL) HSL ,p.name from feequery fd join pasture p on p.id = fd.pastureid
  1242. where fd.Date between ? and ? and fd.FYLX ='燃动费' and (p.center = ? or ? = '' ) group by fd.pastureId order by p.id`,
  1243. startdate.Format("2006-01"), enddate.Format("2006-01"), center, center).Find(&feequeryList).Error
  1244. if err != nil {
  1245. logs.Error(err)
  1246. return resp, err
  1247. }
  1248. monthBudgetList := make([]*monthBudget, 0)
  1249. err = s.d.DB.Raw(`select ifnull(sum(monthBudget),0) monthBudget,p.name from month_budget mb join pasture p on p.id = mb.pastureId
  1250. where mb.feeType = '燃动费' and mb.budgetMonth between ? and ? and (p.center = ? or ? = '' ) group by mb.pastureId`,
  1251. startTime, endTime, center, center).Find(&monthBudgetList).Error
  1252. if err != nil {
  1253. logs.Error(err)
  1254. return resp, err
  1255. }
  1256. pastureList := make([]*model.Pasture, 0)
  1257. err = s.d.DB.Where(" vendor = 1 ").Where(" IsEnergy = 0").Order("id").Find(&pastureList).Error
  1258. if err != nil {
  1259. logs.Error(err)
  1260. return resp, err
  1261. }
  1262. var pastureName []string
  1263. var hslList, monthBudgets []float64
  1264. var all float64
  1265. for _, feequery := range feequeryList {
  1266. pastureName = append(pastureName, feequery.Pasture.Name)
  1267. var hsl float64
  1268. hsl, err = strconv.ParseFloat(feequery.HSL, 64)
  1269. if err != nil {
  1270. logs.Error("转换失败:", err)
  1271. hslList = append(hslList, 0)
  1272. continue
  1273. }
  1274. all += util.Round(hsl, 2)
  1275. hslList = append(hslList, hsl)
  1276. }
  1277. for _, item := range pastureList {
  1278. exist := false
  1279. for _, f := range feequeryList {
  1280. if item.Name == f.Name {
  1281. exist = true
  1282. break
  1283. }
  1284. }
  1285. if !exist {
  1286. pastureName = append(pastureName, item.Name)
  1287. hslList = append(hslList, 0)
  1288. }
  1289. }
  1290. for _, name := range pastureName {
  1291. exist := false
  1292. for _, monthBudget := range monthBudgetList {
  1293. if name == monthBudget.Name {
  1294. exist = true
  1295. monthBudgets = append(monthBudgets, util.Round(monthBudget.MonthBudget, 2))
  1296. break
  1297. }
  1298. }
  1299. if !exist {
  1300. monthBudgets = append(monthBudgets, 0)
  1301. }
  1302. }
  1303. resp.Pasture = pastureName
  1304. resp.LastYear = monthBudgets
  1305. resp.NowYear = hslList
  1306. resp.All = util.Round(all, 2)
  1307. return resp, nil
  1308. }
  1309. //各牧场年度柴油对比
  1310. func (s *Service) GetDieselYieldByPasture(center, startTime, endTime string) (*http.StatementResp, error) {
  1311. resp := new(http.StatementResp)
  1312. layout := "2006-01-02"
  1313. startdate, err := time.Parse(layout, startTime)
  1314. if err != nil {
  1315. logs.Error("时间转换失败:", err)
  1316. }
  1317. enddate, err := time.Parse(layout, endTime)
  1318. if err != nil {
  1319. logs.Error("时间转换失败:", err)
  1320. }
  1321. enddate = enddate.AddDate(0, 1, -1)
  1322. nowYearList := make([]*pastureStatistics, 0)
  1323. lastYearList := make([]*pastureStatistics, 0)
  1324. sqlstr := `SELECT
  1325. ROUND( IFNULL(SUM(w.oilAmount),0) ,2)psum,
  1326. p.name
  1327. FROM
  1328. diesel w
  1329. INNER JOIN oilcard ms
  1330. ON w.oilcardId = ms.id
  1331. INNER JOIN pasture p
  1332. ON ms.pastureId = p.id
  1333. WHERE
  1334. w.selTime between ? and ? and p.name <> '现代牧业' and (p.center = ? or ? = '' )
  1335. GROUP BY ms.pastureId `
  1336. err = s.d.DB.Debug().Raw(sqlstr, startdate.Format("2006-01-02"), enddate.Format("2006-01-02"), center, center).Find(&nowYearList).Error
  1337. if err != nil {
  1338. logs.Error(err)
  1339. return nil, err
  1340. }
  1341. err = s.d.DB.Raw(sqlstr, startdate.AddDate(-1, 0, 0).Format("2006-01-02"),
  1342. enddate.AddDate(-1, 0, 0).Format("2006-01-02"), center, center).Find(&lastYearList).Error
  1343. if err != nil {
  1344. logs.Error(err)
  1345. return nil, err
  1346. }
  1347. pastureList := make([]*model.Pasture, 0)
  1348. err = s.d.DB.Where(" vendor = 1 ").Where(" IsEnergy = 0").Order("id").Find(&pastureList).Error
  1349. if err != nil {
  1350. logs.Error(err)
  1351. return resp, err
  1352. }
  1353. var pastureName []string
  1354. var nowYearData, lastYearData []float64
  1355. var all float64
  1356. for _, nowYear := range nowYearList {
  1357. pastureName = append(pastureName, nowYear.Name)
  1358. nowYearData = append(nowYearData, util.Round(nowYear.Psum, 2))
  1359. all += nowYear.Psum
  1360. exist := false
  1361. for _, lastYear := range lastYearList {
  1362. if lastYear.Name == nowYear.Name {
  1363. exist = true
  1364. lastYearData = append(lastYearData, util.Round(lastYear.Psum, 2))
  1365. break
  1366. }
  1367. }
  1368. if !exist {
  1369. lastYearData = append(lastYearData, 0)
  1370. }
  1371. }
  1372. for _, pasture := range pastureList {
  1373. exist := false
  1374. for _, name := range pastureName {
  1375. if pasture.Name == name {
  1376. exist = true
  1377. break
  1378. }
  1379. }
  1380. if !exist {
  1381. pastureName = append(pastureName, pasture.Name)
  1382. nowYearData = append(nowYearData, 0)
  1383. lastYearData = append(lastYearData, 0)
  1384. }
  1385. }
  1386. resp.Pasture = pastureName
  1387. resp.LastYear = lastYearData
  1388. resp.NowYear = nowYearData
  1389. resp.All = util.Round(all, 2)
  1390. return resp, nil
  1391. }
  1392. //单头牛柴油
  1393. func (s *Service) GetDieselRateByPasture(center, startTime, endTime string) (*http.GetWaterRateByPastureResp, error) {
  1394. resp := new(http.GetWaterRateByPastureResp)
  1395. layout := "2006-01-02"
  1396. startdate, err := time.Parse(layout, startTime)
  1397. if err != nil {
  1398. logs.Error("时间转换失败:", err)
  1399. }
  1400. enddate, err := time.Parse(layout, endTime)
  1401. if err != nil {
  1402. logs.Error("时间转换失败:", err)
  1403. }
  1404. enddate = enddate.AddDate(0, 1, -1)
  1405. cowNumList := make([]*model.FeedSumAndPasture, 0)
  1406. err = s.d.DB.Raw(`select sum(cowNum)cowNum ,p.name from feed_sum fd join pasture p on p.id = fd.pastureid
  1407. where fd.feedDay between ? and ? and (p.center = ? or ? = '' ) group by pastureid `, startdate.Format("2006-01-02"),
  1408. enddate.Format("2006-01-02"), center, center).Find(&cowNumList).Error
  1409. if err != nil {
  1410. logs.Error(err)
  1411. return nil, err
  1412. }
  1413. cowNum := make(map[string]int64, 0)
  1414. var cowNumAll int64
  1415. for _, item := range cowNumList {
  1416. cowNum[item.Pasture.Name] = item.FeedSum.CowNum
  1417. }
  1418. pastureList := make([]*model.Pasture, 0)
  1419. err = s.d.DB.Where(" vendor = 1 ").Where("IsEnergy = 0").Order("id").Find(&pastureList).Error
  1420. if err != nil {
  1421. logs.Error(err)
  1422. return resp, err
  1423. }
  1424. dieselList := make([]*pastureStatistics, 0)
  1425. sqlstr := `SELECT
  1426. ROUND( IFNULL(SUM(w.oilAmount),0) ,2)psum,
  1427. p.name
  1428. FROM
  1429. diesel w
  1430. INNER JOIN oilcard ms
  1431. ON w.oilcardId = ms.id
  1432. INNER JOIN pasture p
  1433. ON ms.pastureId = p.id
  1434. WHERE
  1435. w.selTime between ? and ? and p.name <> '现代牧业' and (p.center = ? or ? = '' )
  1436. GROUP BY ms.pastureId `
  1437. err = s.d.DB.Debug().Raw(sqlstr, startdate.Format("2006-01-02"), enddate.Format("2006-01-02"), center, center).Find(&dieselList).Error
  1438. if err != nil {
  1439. logs.Error(err)
  1440. return nil, err
  1441. }
  1442. var pastureName []string
  1443. var hslList []float64
  1444. var all float64
  1445. for _, diesel := range dieselList {
  1446. pastureName = append(pastureName, diesel.Name)
  1447. if _, ok := cowNum[diesel.Name]; ok {
  1448. all += diesel.Psum
  1449. cowNumAll += cowNum[diesel.Name]
  1450. hslList = append(hslList, diesel.Psum/float64(cowNum[diesel.Name]))
  1451. } else {
  1452. hslList = append(hslList, 0)
  1453. }
  1454. }
  1455. for _, item := range pastureList {
  1456. exist := false
  1457. for _, f := range dieselList {
  1458. if item.Name == f.Name {
  1459. exist = true
  1460. break
  1461. }
  1462. }
  1463. if !exist {
  1464. pastureName = append(pastureName, item.Name)
  1465. hslList = append(hslList, 0)
  1466. }
  1467. }
  1468. resp.Pasture = append(resp.Pasture, "集团")
  1469. resp.Pasture = append(resp.Pasture, pastureName...)
  1470. resp.Actual = append(resp.Actual, util.Round(all/float64(cowNumAll), 3))
  1471. for _, item := range hslList {
  1472. resp.Actual = append(resp.Actual, util.Round(item, 3))
  1473. }
  1474. resp.Line = util.Round(all/float64(cowNumAll), 3)
  1475. return resp, err
  1476. }
  1477. //单头牛燃动费
  1478. func (s *Service) GetIgnitionRateByPasture(center, startTime, endTime string) (*http.GetWaterRateCompleteByPastureResp, error) {
  1479. resp := new(http.GetWaterRateCompleteByPastureResp)
  1480. layout := "2006-01-02"
  1481. startdate, err := time.Parse(layout, startTime)
  1482. if err != nil {
  1483. logs.Error("时间转换失败:", err)
  1484. }
  1485. enddate, err := time.Parse(layout, endTime)
  1486. if err != nil {
  1487. logs.Error("时间转换失败:", err)
  1488. }
  1489. enddate = enddate.AddDate(0, 1, -1)
  1490. cowNumList := make([]*model.FeedSumAndPasture, 0)
  1491. err = s.d.DB.Raw(`select sum(cowNum)cowNum ,p.name from feed_sum fd join pasture p on p.id = fd.pastureid
  1492. where fd.feedDay between ? and ? and (p.center = ? or ? = '' ) group by pastureid `, startdate.Format("2006-01-02"),
  1493. enddate.Format("2006-01-02"), center, center).Find(&cowNumList).Error
  1494. if err != nil {
  1495. logs.Error(err)
  1496. return nil, err
  1497. }
  1498. cowNum := make(map[string]int64, 0)
  1499. var cowNumAll int64
  1500. for _, item := range cowNumList {
  1501. cowNum[item.Pasture.Name] = item.FeedSum.CowNum
  1502. }
  1503. pastureList := make([]*model.Pasture, 0)
  1504. err = s.d.DB.Where(" vendor = 1 ").Where(" IsEnergy = 0").Order("id").Find(&pastureList).Error
  1505. if err != nil {
  1506. logs.Error(err)
  1507. return resp, err
  1508. }
  1509. feequeryList := make([]*model.FeeQueryAndPasture, 0)
  1510. err = s.d.DB.Debug().Raw(`select sum(HSL) HSL ,p.name from feequery fd join pasture p on p.id = fd.pastureid
  1511. where fd.Date between ? and ? and fd.FYLX ='燃动费' and (p.center = ? or ? = '' ) group by fd.pastureId order by p.id`,
  1512. startdate.Format("2006-01"), enddate.Format("2006-01"), center, center).Find(&feequeryList).Error
  1513. if err != nil {
  1514. logs.Error(err)
  1515. return resp, err
  1516. }
  1517. monthBudgetList := make([]*monthBudget, 0)
  1518. err = s.d.DB.Raw(`select ifnull(sum(monthBudget),0) monthBudget,p.name from month_budget mb join pasture p on p.id = mb.pastureId
  1519. where mb.feeType = '燃动费' and mb.budgetMonth between ? and ? and (p.center = ? or ? = '' ) group by mb.pastureId`,
  1520. startdate.Format("2006-01"), enddate.Format("2006-01"), center, center).Find(&monthBudgetList).Error
  1521. if err != nil {
  1522. logs.Error(err)
  1523. return resp, err
  1524. }
  1525. var pastureName []string
  1526. var hslList []float64
  1527. var monthBudgets []float64
  1528. var all, monthBudgetAll float64
  1529. for _, feequery := range feequeryList {
  1530. pastureName = append(pastureName, feequery.Name)
  1531. var hsl float64
  1532. hsl, err = strconv.ParseFloat(feequery.HSL, 64)
  1533. if err != nil {
  1534. logs.Error("转换失败:", err)
  1535. hslList = append(hslList, 0)
  1536. }
  1537. if _, ok := cowNum[feequery.Name]; ok {
  1538. all += hsl
  1539. cowNumAll += cowNum[feequery.Name]
  1540. hslList = append(hslList, hsl/float64(cowNum[feequery.Name]))
  1541. } else {
  1542. hslList = append(hslList, 0)
  1543. }
  1544. }
  1545. for _, item := range pastureList {
  1546. exist := false
  1547. for _, f := range feequeryList {
  1548. if item.Name == f.Name {
  1549. exist = true
  1550. break
  1551. }
  1552. }
  1553. if !exist {
  1554. pastureName = append(pastureName, item.Name)
  1555. hslList = append(hslList, 0)
  1556. }
  1557. }
  1558. for _, name := range pastureName {
  1559. exist := false
  1560. for _, monthBudget := range monthBudgetList {
  1561. if name == monthBudget.Name {
  1562. exist = true
  1563. if _, ok := cowNum[name]; ok {
  1564. monthBudgetAll += monthBudget.MonthBudget
  1565. monthBudgets = append(monthBudgets, monthBudget.MonthBudget/float64(cowNum[name]))
  1566. } else {
  1567. monthBudgets = append(monthBudgets, 0)
  1568. }
  1569. break
  1570. }
  1571. }
  1572. if !exist {
  1573. monthBudgets = append(monthBudgets, 0)
  1574. }
  1575. }
  1576. resp.Pasture = append(resp.Pasture, "集团")
  1577. resp.Pasture = append(resp.Pasture, pastureName...)
  1578. resp.Actual = append(resp.Actual, util.Round(all/float64(cowNumAll), 3))
  1579. for _, item := range hslList {
  1580. resp.Actual = append(resp.Actual, util.Round(item, 3))
  1581. }
  1582. resp.Line = util.Round(all/float64(cowNumAll), 3)
  1583. resp.Budget = append(resp.Budget, util.Round(monthBudgetAll/float64(cowNumAll), 3))
  1584. for _, item := range monthBudgets {
  1585. resp.Budget = append(resp.Budget, util.Round(item, 3))
  1586. }
  1587. return resp, err
  1588. }
  1589. //每月单头牛柴油对比
  1590. func (s *Service) GetDieselPerHeadByPasture(pastureId, startTime, endTime string) (map[string]interface{}, error) {
  1591. layout := "2006-01-02"
  1592. startdate, err := time.Parse(layout, startTime)
  1593. if err != nil {
  1594. logs.Error("时间转换失败:", err)
  1595. }
  1596. enddate, err := time.Parse(layout, endTime)
  1597. if err != nil {
  1598. logs.Error("时间转换失败:", err)
  1599. }
  1600. enddate = enddate.AddDate(0, 1, -1)
  1601. dosagePerCowList := make([]*dosagePerCow, 0)
  1602. err = s.d.DB.Debug().Raw(`SELECT
  1603. round( IFNULL(SUM(w.oilAmount),0) ,2)psum,
  1604. p.name,DATE_FORMAT( w.selTime, '%Y-%m') date,
  1605. ifnull((select sum(cowNum) from feed_sum where DATE_FORMAT( feedDay, '%Y-%m') = DATE_FORMAT( w.selTime, '%Y-%m') and pastureId = p.id ),0)cowNum
  1606. FROM
  1607. diesel w
  1608. INNER JOIN oilcard ms
  1609. ON w.oilcardId = ms.id
  1610. INNER JOIN pasture p
  1611. ON ms.pastureId = p.id
  1612. WHERE
  1613. (w.selTime between ? and ? ) and p.name <> '现代牧业' and FIND_IN_SET(p.id,?)
  1614. GROUP BY DATE_FORMAT( w.selTime, '%Y-%m'),ms.pastureId order by DATE_FORMAT( w.selTime, '%Y-%m'),p.id `, startdate.Format("2006-01-02"),
  1615. enddate.Format("2006-01-02"), pastureId).Find(&dosagePerCowList).Error
  1616. if err != nil {
  1617. logs.Error(err)
  1618. return nil, err
  1619. }
  1620. data := dosagePerCowFormat(dosagePerCowList)
  1621. return data, nil
  1622. }