equipment.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. package service
  2. import (
  3. "fmt"
  4. "kpt.xdmy/apiserver/model"
  5. "kpt.xdmy/apiserver/model/home"
  6. "kpt.xdmy/apiserver/model/http"
  7. "kpt.xdmy/pkg/util"
  8. "time"
  9. )
  10. func (s *Service) GetEquipmentIndex(pastureId, startTime, endTime string) (*http.EquipmentIndexResp, error) {
  11. resp := new(http.EquipmentIndexResp)
  12. startDate, _ := time.Parse("2006-01-02", startTime)
  13. endDate, _ := time.Parse("2006-01-02", endTime)
  14. monthBudgetList := make([]*home.MonthBudget, 0)
  15. err := s.d.DB.Debug().Raw(`SELECT
  16. sum( monthBudget ) monthBudget,
  17. feeType,pastureId
  18. FROM
  19. month_budget join pasture on pasture.id = month_budget.pastureId
  20. WHERE
  21. budgetMonth BETWEEN date_format(?,'%Y-%m') and date_format(?,'%Y-%m') and (pastureId = ? or ? ='') and vendor = 1 and feeType != '维修费'
  22. GROUP BY
  23. feeType,pastureId
  24. UNION ALL
  25. SELECT
  26. sum( monthBudget ) monthBudget,
  27. feeType,pastureId
  28. FROM
  29. month_budget join pasture on pasture.id = month_budget.pastureId
  30. WHERE
  31. budgetMonth BETWEEN date_format(?,'%Y-%m') and date_format(?,'%Y-%m') and (pastureId = ? or ? ='') and vendor = 1 and feeType = '维修费'
  32. and (SELECT iscal FROM department d WHERE d.id=month_budget.deptId)= 0 and (SELECT name FROM department d WHERE d.id=month_budget.deptId) not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')
  33. GROUP BY
  34. feeType,pastureId `, startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), pastureId, pastureId,
  35. startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), pastureId, pastureId).Find(&monthBudgetList).Error
  36. if err != nil {
  37. return resp, err
  38. }
  39. //
  40. feedSumList := make([]*model.FeedSumAndPasture, 0)
  41. err = s.d.DB.Debug().Raw(`SELECT sum(fs.cowNum) cowNum ,fs.pastureId,p.name FROM feed_sum fs INNER JOIN pasture p ON p.id = fs.pastureId
  42. where feedDay between ? and ? and (fs.pastureId = ? or ? ='') group by fs.pastureId`, startDate.Format("2006-01-02"),
  43. endDate.Format("2006-01-02"), pastureId, pastureId).Find(&feedSumList).Error
  44. if err != nil {
  45. return resp, err
  46. }
  47. feetSumPriceList := make([]*home.ThisYearActual, 0)
  48. err = s.d.DB.Debug().Raw(`select FYLX feeType,sum(HSL) sumPrice,pastureId from feequery where DATE_FORMAT(STR_TO_DATE( Date, '%Y-%m' ),'%Y-%m')
  49. between ? and ? and FYLX in ('水费','燃动费','电费','柴油费') and (pastureId = ? or ? = '') group by FYLX,pastureId
  50. UNION ALL
  51. SELECT
  52. '维修费' feeType ,IFNULL(SUM( pu.sumPrice - pu.quitNumber * pu.price),0) sumPrice,bpu.pastureId
  53. FROM
  54. partuse pu
  55. INNER JOIN bigpartuse bpu
  56. ON pu.bigId = bpu.id
  57. WHERE bpu.useType <> 3
  58. AND (
  59. pu.useTypeV = '维修'
  60. OR pu.useTypeV = '保养'
  61. ) AND (SELECT iscal FROM department d WHERE d.id=bpu.departmentId)= 0 and (SELECT name FROM department d WHERE d.id=bpu.departmentId)
  62. not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')
  63. AND bpu.receiveTime BETWEEN ? AND ?
  64. AND (bpu.pastureid = ? OR '' = ?) and pu.dflag != 1 group by pastureId`, startDate.Format("2006-01"),
  65. endDate.Format("2006-01"), pastureId, pastureId, startDate.Format("2006-01-02"),
  66. endDate.AddDate(0, 0, 1).Format("2006-01-02"), pastureId, pastureId).Find(&feetSumPriceList).Error
  67. if err != nil {
  68. return resp, err
  69. }
  70. var sumDay, realDay int64
  71. sumDay = int64(endDate.Sub(startDate).Hours() / 24)
  72. if endDate.Unix() < time.Now().Unix() {
  73. realDay = sumDay
  74. } else {
  75. realDay = int64(time.Now().Sub(startDate).Hours() / 24)
  76. }
  77. pastureList := make([]*model.Pasture, 0)
  78. err = s.d.DB.Debug().Where(" id != 18 ").Where(" id = ? or ? = '' ", pastureId, pastureId).Where("vendor = 1").Order("center_sort,id").Find(&pastureList).Error
  79. if err != nil {
  80. return resp, err
  81. }
  82. pastureLs := new(model.Pasture)
  83. equipmentList := make([]*http.EquipmentIndex, 0)
  84. chooseMonthWXList := make([]*http.EquipmentIndex, 0)
  85. chooseWaterPriceList := make([]*http.EquipmentIndex, 0)
  86. chooseElectricityPriceList := make([]*http.EquipmentIndex, 0)
  87. chooseMonthiolPriceList := make([]*http.EquipmentIndex, 0)
  88. lgnitionChargePriceList := make([]*http.EquipmentIndex, 0)
  89. dosageIndexList := make([]map[string]interface{}, 0)
  90. pastureCount := len(pastureList) - 1
  91. for i, pasture := range pastureList {
  92. if i == 0 {
  93. pastureLs = pasture
  94. }
  95. if pastureLs.Center != pasture.Center {
  96. equipmentList = append(equipmentList, &http.EquipmentIndex{PastureName: pastureList[i-1].Center, Center: pastureList[i-1].Center})
  97. chooseMonthWXList = append(chooseMonthWXList, &http.EquipmentIndex{PastureName: pastureList[i-1].Center, Center: pastureList[i-1].Center})
  98. chooseWaterPriceList = append(chooseWaterPriceList, &http.EquipmentIndex{PastureName: pastureList[i-1].Center, Center: pastureList[i-1].Center})
  99. chooseElectricityPriceList = append(chooseElectricityPriceList, &http.EquipmentIndex{PastureName: pastureList[i-1].Center, Center: pastureList[i-1].Center})
  100. chooseMonthiolPriceList = append(chooseMonthiolPriceList, &http.EquipmentIndex{PastureName: pastureList[i-1].Center, Center: pastureList[i-1].Center})
  101. lgnitionChargePriceList = append(lgnitionChargePriceList, &http.EquipmentIndex{PastureName: pastureList[i-1].Center, Center: pastureList[i-1].Center})
  102. //center := make(map[string]interface{})
  103. //center["pastureName"] = pastureList[i-1].Center
  104. //center["center"] = pastureList[i-1].Center
  105. //dosageIndexList = append(dosageIndexList, center)
  106. }
  107. data := make(map[string]interface{})
  108. data["pastureName"] = pasture.Name
  109. data["center"] = pasture.Center
  110. data["pastureId"] = pasture.ID
  111. dosageIndexList = append(dosageIndexList, data)
  112. equipmentList = append(equipmentList, &http.EquipmentIndex{PastureName: pasture.Name, PastureId: pasture.ID, Center: pasture.Center})
  113. chooseMonthWXList = append(chooseMonthWXList, &http.EquipmentIndex{PastureName: pasture.Name, PastureId: pasture.ID, Center: pasture.Center})
  114. chooseWaterPriceList = append(chooseWaterPriceList, &http.EquipmentIndex{PastureName: pasture.Name, PastureId: pasture.ID, Center: pasture.Center})
  115. chooseElectricityPriceList = append(chooseElectricityPriceList, &http.EquipmentIndex{PastureName: pasture.Name, PastureId: pasture.ID, Center: pasture.Center})
  116. chooseMonthiolPriceList = append(chooseMonthiolPriceList, &http.EquipmentIndex{PastureName: pasture.Name, PastureId: pasture.ID, Center: pasture.Center})
  117. lgnitionChargePriceList = append(lgnitionChargePriceList, &http.EquipmentIndex{PastureName: pasture.Name, PastureId: pasture.ID, Center: pasture.Center})
  118. if i == pastureCount {
  119. equipmentList = append(equipmentList, &http.EquipmentIndex{PastureName: pasture.Center, Center: pasture.Center})
  120. chooseWaterPriceList = append(chooseWaterPriceList, &http.EquipmentIndex{PastureName: pasture.Center, Center: pasture.Center})
  121. chooseWaterPriceList = append(chooseWaterPriceList, &http.EquipmentIndex{PastureName: pasture.Center, Center: pasture.Center})
  122. chooseElectricityPriceList = append(chooseElectricityPriceList, &http.EquipmentIndex{PastureName: pasture.Center, Center: pasture.Center})
  123. chooseMonthiolPriceList = append(chooseMonthiolPriceList, &http.EquipmentIndex{PastureName: pasture.Center, Center: pasture.Center})
  124. lgnitionChargePriceList = append(lgnitionChargePriceList, &http.EquipmentIndex{PastureName: pasture.Center, Center: pasture.Center})
  125. //center := make(map[string]interface{})
  126. //center["pastureName"] = pasture.Center
  127. //center["center"] = pasture.Center
  128. //dosageIndexList = append(dosageIndexList, center)
  129. }
  130. pastureLs = pasture
  131. }
  132. equipmentJT := new(http.EquipmentIndex)
  133. equipmentJT.PastureName = "集团"
  134. for _, eq := range equipmentList {
  135. for _, monthBudget := range monthBudgetList {
  136. if eq.PastureId == monthBudget.PastureId && (monthBudget.FeeType == "燃动费" || monthBudget.FeeType == "水费" || monthBudget.FeeType == "电费" || monthBudget.FeeType == "维修费") {
  137. eq.MonthBudget += monthBudget.MonthBudget
  138. eq.QmonthBudget += monthBudget.MonthBudget / float64(sumDay) * float64(realDay)
  139. }
  140. }
  141. for _, feedSum := range feedSumList {
  142. if eq.PastureId == int(feedSum.PastureId) {
  143. eq.CowSum += feedSum.CowNum
  144. break
  145. }
  146. }
  147. for _, feedSum := range feetSumPriceList {
  148. if eq.PastureId == feedSum.PastureId && (feedSum.FeeType == "燃动费" || feedSum.FeeType == "水费" || feedSum.FeeType == "电费" || feedSum.FeeType == "维修费") {
  149. eq.SumPrice += feedSum.SumPrice
  150. }
  151. }
  152. eq.Asave = eq.MonthBudget - eq.SumPrice
  153. if eq.MonthBudget > 0 && eq.SumPrice > 0 {
  154. eq.RealReason = fmt.Sprintf("%v", util.Round((1+((eq.MonthBudget/float64(sumDay)*float64(realDay))-(eq.SumPrice))/(eq.MonthBudget/float64(sumDay)*float64(realDay)))*100, 2)) + " %"
  155. } else {
  156. eq.RealReason = "0 %"
  157. }
  158. if eq.MonthBudget > 0 && eq.SumPrice > 0 && eq.CowSum > 0 {
  159. eq.OneCowSumPrice = util.Round(eq.SumPrice/float64(eq.CowSum/sumDay*realDay), 2)
  160. } else {
  161. eq.OneCowSumPrice = 0.0
  162. }
  163. equipmentJT.MonthBudget += eq.MonthBudget
  164. equipmentJT.SumPrice += eq.SumPrice
  165. equipmentJT.QmonthBudget += eq.QmonthBudget
  166. equipmentJT.Asave += eq.Asave
  167. equipmentJT.CowSum += eq.CowSum
  168. }
  169. for _, eq := range equipmentList {
  170. if eq.PastureId == 0 {
  171. for _, item := range equipmentList {
  172. if item.Center == eq.PastureName && eq.PastureName != item.PastureName {
  173. eq.MonthBudget += item.MonthBudget
  174. eq.SumPrice += item.SumPrice
  175. eq.QmonthBudget += item.QmonthBudget
  176. eq.Asave += item.Asave
  177. eq.CowSum += item.CowSum
  178. }
  179. }
  180. }
  181. }
  182. equipmentList = append(equipmentList, equipmentJT)
  183. for _, eq := range equipmentList {
  184. if eq.PastureId == 0 {
  185. if eq.MonthBudget > 0 && eq.SumPrice > 0 {
  186. eq.RealReason = fmt.Sprintf("%v", util.Round((1+((eq.MonthBudget/float64(sumDay)*float64(realDay))-(eq.SumPrice))/(eq.MonthBudget/float64(sumDay)*float64(realDay)))*100, 2)) + " %"
  187. } else {
  188. eq.RealReason = "0 %"
  189. }
  190. if eq.MonthBudget > 0 && eq.SumPrice > 0 && eq.CowSum > 0 {
  191. eq.OneCowSumPrice = util.Round(eq.SumPrice/float64(eq.CowSum/sumDay*realDay), 2)
  192. } else {
  193. eq.OneCowSumPrice = 0
  194. }
  195. }
  196. eq.MonthBudget = util.Round(eq.MonthBudget/10000, 2)
  197. eq.SumPrice = util.Round(eq.SumPrice/10000, 2)
  198. eq.QmonthBudget = util.Round(eq.QmonthBudget/10000, 2)
  199. eq.Asave = util.Round(eq.Asave/10000, 2)
  200. }
  201. resp.EquipmentIndex = equipmentList
  202. resp.ChooseMonthWX = chooseMonth("维修费", chooseMonthWXList, feedSumList, monthBudgetList, feetSumPriceList, sumDay, realDay)
  203. resp.ChooseWaterPrice = chooseMonth("水费", chooseWaterPriceList, feedSumList, monthBudgetList, feetSumPriceList, sumDay, realDay)
  204. resp.ChooseElectricityPrice = chooseMonth("电费", chooseElectricityPriceList, feedSumList, monthBudgetList, feetSumPriceList, sumDay, realDay)
  205. resp.ChooseMonthiolPrice = chooseMonth("柴油费", chooseMonthiolPriceList, feedSumList, monthBudgetList, feetSumPriceList, sumDay, realDay)
  206. resp.LgnitionChargePrice = chooseMonth("燃动费", lgnitionChargePriceList, feedSumList, monthBudgetList, feetSumPriceList, sumDay, realDay)
  207. waterIndexList := make([]*http.DosageIndex, 0)
  208. err = s.d.DB.Debug().Raw(`SELECT
  209. m.pastureId,
  210. ROUND(IFNULL(SUM(e.waterConsumption ),0),2) sumPrice,p.name pastureName
  211. FROM
  212. water e
  213. INNER JOIN measure m
  214. ON m.id = e.measureId
  215. join pasture p on m.pastureid = p.id
  216. WHERE m.meterType = '水表' AND m.useType='1级表' AND e.date between ? and ?
  217. and (p.id = ? or ? = 0)
  218. group by m.pastureId
  219. `, startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), pastureId, pastureId).Find(&waterIndexList).Error
  220. if err != nil {
  221. return resp, err
  222. }
  223. electricityIndexList := make([]*http.DosageIndex, 0)
  224. err = s.d.DB.Debug().Raw(`SELECT
  225. m.pastureId,
  226. ROUND(IFNULL(SUM(e.elecConsumption),0),2) sumPrice,
  227. p.name pastureName
  228. FROM
  229. electricity e
  230. INNER JOIN measure m
  231. ON m.id = e.measureId
  232. join pasture p on p.id = m.pastureid
  233. WHERE m.meterType = '电表' AND m.useType='1级表' AND e.date between ? and ?
  234. and (p.id = ? or ? = '')
  235. GROUP BY m.pastureId`, startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), pastureId, pastureId).Find(&electricityIndexList).Error
  236. if err != nil {
  237. return resp, err
  238. }
  239. dieselIndexList := make([]*http.DosageIndex, 0)
  240. err = s.d.DB.Debug().Raw(`SELECT
  241. d.pastureId,
  242. ROUND(IFNULL(SUM(d.oilAmount),0),2) sumPrice,
  243. p.name pastureName
  244. FROM
  245. diesel d
  246. join pasture p on p.id = d.pastureid
  247. WHERE d.selTime between ? and ?
  248. and (p.id = ? or ? = '')
  249. GROUP BY d.pastureId`, startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), pastureId, pastureId).Find(&dieselIndexList).Error
  250. if err != nil {
  251. return resp, err
  252. }
  253. for _, dosage := range dosageIndexList {
  254. dosageMap := dosage
  255. var cowNum int64
  256. for _, feedSum := range feedSumList {
  257. if dosageMap["pastureName"].(string) == feedSum.Name {
  258. cowNum = feedSum.CowNum
  259. break
  260. }
  261. }
  262. if _, ok := dosageMap["pastureId"]; !ok {
  263. continue
  264. }
  265. for _, water := range waterIndexList {
  266. if dosageMap["pastureName"].(string) == water.PastureName {
  267. dosageMap["waterOneCowSumPrice"] = 0
  268. dosageMap["waterSumPrice"] = 0
  269. if water.SumPrice > 0 {
  270. if cowNum > 0 {
  271. dosageMap["waterOneCowSumPrice"] = util.Round(water.SumPrice/float64(cowNum), 2)
  272. }
  273. dosageMap["waterSumPrice"] = util.Round(water.SumPrice, 2)
  274. }
  275. break
  276. }
  277. }
  278. for _, electricity := range electricityIndexList {
  279. if dosageMap["pastureName"].(string) == electricity.PastureName {
  280. dosageMap["electricityOneCowSumPrice"] = 0
  281. dosageMap["electricitySumPrice"] = 0
  282. if electricity.SumPrice > 0 {
  283. if cowNum > 0 {
  284. dosageMap["electricityOneCowSumPrice"] = util.Round(electricity.SumPrice/float64(cowNum), 2)
  285. }
  286. dosageMap["electricitySumPrice"] = util.Round(electricity.SumPrice, 2)
  287. }
  288. break
  289. }
  290. }
  291. for _, diesel := range dieselIndexList {
  292. if dosageMap["pastureName"].(string) == diesel.PastureName {
  293. dosageMap["monthiolOneCowSumPrice"] = 0
  294. dosageMap["monthiolSumPrice"] = 0
  295. if diesel.SumPrice > 0 {
  296. if cowNum > 0 {
  297. dosageMap["monthiolOneCowSumPrice"] = util.Round(diesel.SumPrice/float64(cowNum), 2)
  298. }
  299. dosageMap["monthiolSumPrice"] = util.Round(diesel.SumPrice, 2)
  300. }
  301. break
  302. }
  303. }
  304. dosageMap["cowNum"] = cowNum
  305. }
  306. for _, dosage := range dosageIndexList {
  307. dosageMap := dosage
  308. if _, ok := dosageMap["pastureId"]; !ok {
  309. var waterSumPrice, electricitySumPrice, monthiolSumPrice float64
  310. var cowNum int64
  311. for _, dosage1 := range dosageIndexList {
  312. dosage1Map := dosage1
  313. if _, ok := dosage1Map["pastureId"]; ok {
  314. if dosage1Map["center"].(string) == dosageMap["pastureName"].(string) {
  315. if _, ok := dosage1Map["waterSumPrice"]; ok {
  316. waterSumPrice += dosage1Map["waterSumPrice"].(float64)
  317. }
  318. if _, ok := dosage1Map["electricitySumPrice"]; ok {
  319. electricitySumPrice += dosage1Map["electricitySumPrice"].(float64)
  320. }
  321. if _, ok := dosage1Map["monthiolSumPrice"]; ok {
  322. monthiolSumPrice += dosage1Map["monthiolSumPrice"].(float64)
  323. }
  324. if _, ok := dosage1Map["cowNum"]; ok {
  325. cowNum += dosage1Map["cowNum"].(int64)
  326. }
  327. }
  328. }
  329. }
  330. if cowNum > 0 {
  331. dosageMap["waterOneCowSumPrice"] = util.Round(waterSumPrice/float64(cowNum), 2)
  332. dosageMap["electricityOneCowSumPrice"] = util.Round(waterSumPrice/float64(cowNum), 2)
  333. dosageMap["monthiolOneCowSumPrice"] = util.Round(waterSumPrice/float64(cowNum), 2)
  334. } else {
  335. dosageMap["waterOneCowSumPrice"] = 0
  336. dosageMap["electricityOneCowSumPrice"] = 0
  337. dosageMap["monthiolOneCowSumPrice"] = 0
  338. }
  339. dosageMap["waterSumPrice"] = util.Round(waterSumPrice, 2)
  340. dosageMap["electricitySumPrice"] = util.Round(waterSumPrice, 2)
  341. dosageMap["monthiolSumPrice"] = util.Round(waterSumPrice, 2)
  342. }
  343. }
  344. resp.DosageIndex = dosageIndexList
  345. return resp, nil
  346. }
  347. func chooseMonth(feeType string, equipmentList []*http.EquipmentIndex, feedSumList []*model.FeedSumAndPasture, monthBudgetList []*home.MonthBudget, feetSumPriceList []*home.ThisYearActual, sumDay, realDay int64) []*http.EquipmentIndex {
  348. chooseMonthJT := new(http.EquipmentIndex)
  349. chooseMonthJT.PastureName = "集团"
  350. for _, eq := range equipmentList {
  351. for _, monthBudget := range monthBudgetList {
  352. if eq.PastureId == monthBudget.PastureId && (monthBudget.FeeType == feeType) {
  353. eq.MonthBudget += monthBudget.MonthBudget
  354. eq.QmonthBudget += monthBudget.MonthBudget / float64(sumDay) * float64(realDay)
  355. }
  356. }
  357. for _, feedSum := range feedSumList {
  358. if eq.PastureId == int(feedSum.PastureId) {
  359. eq.CowSum += feedSum.CowNum
  360. break
  361. }
  362. }
  363. for _, feedSum := range feetSumPriceList {
  364. if eq.PastureId == feedSum.PastureId && feeType == feedSum.FeeType {
  365. eq.SumPrice += feedSum.SumPrice
  366. }
  367. }
  368. eq.Asave = eq.MonthBudget - eq.SumPrice
  369. if eq.MonthBudget > 0 && eq.SumPrice > 0 {
  370. eq.RealReason = fmt.Sprintf("%v", util.Round((1+((eq.MonthBudget/float64(sumDay)*float64(realDay))-(eq.SumPrice))/(eq.MonthBudget/float64(sumDay)*float64(realDay)))*100, 2)) + " %"
  371. } else {
  372. eq.RealReason = "0 %"
  373. }
  374. if eq.MonthBudget > 0 && eq.SumPrice > 0 && eq.CowSum > 0 {
  375. eq.OneCowSumPrice = util.Round(eq.SumPrice/float64(eq.CowSum/sumDay*realDay), 2)
  376. } else {
  377. eq.OneCowSumPrice = 0.0
  378. }
  379. chooseMonthJT.MonthBudget += eq.MonthBudget
  380. chooseMonthJT.SumPrice += eq.SumPrice
  381. chooseMonthJT.QmonthBudget += eq.QmonthBudget
  382. chooseMonthJT.Asave += eq.Asave
  383. chooseMonthJT.CowSum += eq.CowSum
  384. }
  385. for _, eq := range equipmentList {
  386. if eq.PastureId == 0 {
  387. for _, item := range equipmentList {
  388. if item.Center == eq.PastureName && eq.PastureName != item.PastureName {
  389. eq.MonthBudget += item.MonthBudget
  390. eq.SumPrice += item.SumPrice
  391. eq.QmonthBudget += item.QmonthBudget
  392. eq.Asave += item.Asave
  393. eq.CowSum += item.CowSum
  394. }
  395. }
  396. }
  397. }
  398. equipmentList = append(equipmentList, chooseMonthJT)
  399. for _, eq := range equipmentList {
  400. if eq.PastureId == 0 {
  401. if eq.MonthBudget > 0 && eq.SumPrice > 0 {
  402. eq.RealReason = fmt.Sprintf("%v", util.Round((1+((eq.MonthBudget/float64(sumDay)*float64(realDay))-(eq.SumPrice))/(eq.MonthBudget/float64(sumDay)*float64(realDay)))*100, 2)) + " %"
  403. } else {
  404. eq.RealReason = "0 %"
  405. }
  406. if eq.MonthBudget > 0 && eq.SumPrice > 0 && eq.CowSum > 0 {
  407. eq.OneCowSumPrice = util.Round(eq.SumPrice/float64(eq.CowSum/sumDay*realDay), 2)
  408. } else {
  409. eq.OneCowSumPrice = 0
  410. }
  411. }
  412. eq.MonthBudget = util.Round(eq.MonthBudget/10000, 2)
  413. eq.SumPrice = util.Round(eq.SumPrice/10000, 2)
  414. eq.QmonthBudget = util.Round(eq.QmonthBudget/10000, 2)
  415. eq.Asave = util.Round(eq.Asave/10000, 2)
  416. }
  417. return equipmentList
  418. }
  419. func (s *Service) GetChooseMonthWXByDepartment(pastureId, startTime, endTime string) ([]*http.EquipmentIndex, error) {
  420. startDate, _ := time.Parse("2006-01-02", startTime)
  421. endDate, _ := time.Parse("2006-01-02", endTime)
  422. departmentList := make([]*model.Department, 0)
  423. resp := make([]*http.EquipmentIndex, 0)
  424. err := s.d.DB.Where("pastureId = ? ", pastureId).Where("isPasture = 0 ").
  425. Where(" name not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')").Find(&departmentList).Error
  426. if err != nil {
  427. return resp, err
  428. }
  429. feetSumPriceList := make([]*home.ThisYearActual, 0)
  430. err = s.d.DB.Debug().Raw(`
  431. SELECT
  432. '维修费' feeType ,IFNULL(SUM( pu.sumPrice - pu.quitNumber * pu.price),0) sumPrice,bpu.pastureId,departmentId
  433. FROM
  434. partuse pu
  435. INNER JOIN bigpartuse bpu
  436. ON pu.bigId = bpu.id
  437. WHERE bpu.useType <> 3
  438. AND (
  439. pu.useTypeV = '维修'
  440. OR pu.useTypeV = '保养'
  441. ) AND (SELECT iscal FROM department d WHERE d.id=bpu.departmentId)= 0 and (SELECT name FROM department d WHERE d.id=bpu.departmentId)
  442. not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')
  443. AND bpu.receiveTime BETWEEN ? AND ?
  444. AND (bpu.pastureid = ? OR '' = ?) and pu.dflag != 1 group by departmentId,pastureId`, startDate.Format("2006-01-02"),
  445. endDate.AddDate(0, 0, 1).Format("2006-01-02"), pastureId, pastureId).Find(&feetSumPriceList).Error
  446. if err != nil {
  447. return resp, err
  448. }
  449. monthBudgetList := make([]*home.MonthBudget, 0)
  450. err = s.d.DB.Debug().Raw(`
  451. SELECT
  452. sum( monthBudget ) monthBudget,
  453. feeType,pastureId,month_budget.deptId
  454. FROM
  455. month_budget join pasture on pasture.id = month_budget.pastureId
  456. WHERE
  457. budgetMonth BETWEEN date_format(?,'%Y-%m') and date_format(?,'%Y-%m') and (pastureId = ? or ? ='') and vendor = 1 and feeType = '维修费'
  458. and (SELECT iscal FROM department d WHERE d.id=month_budget.deptId)= 0 and (SELECT name FROM department d WHERE d.id=month_budget.deptId) not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')
  459. GROUP BY
  460. month_budget.deptId,feeType,pastureId `, startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), pastureId, pastureId).Find(&monthBudgetList).Error
  461. if err != nil {
  462. return resp, err
  463. }
  464. feedSum := new(model.FeedSumAndPasture)
  465. err = s.d.DB.Debug().Raw(`SELECT sum(fs.cowNum) cowNum ,fs.pastureId,p.name FROM feed_sum fs INNER JOIN pasture p ON p.id = fs.pastureId
  466. where feedDay between ? and ? and (fs.pastureId = ? or ? ='') group by fs.pastureId`, startDate.Format("2006-01-02"),
  467. endDate.Format("2006-01-02"), pastureId, pastureId).First(&feedSum).Error
  468. if err != nil {
  469. return resp, err
  470. }
  471. var sumDay, realDay int64
  472. sumDay = int64(endDate.Sub(startDate).Hours() / 24)
  473. if endDate.Unix() < time.Now().Unix() {
  474. realDay = sumDay
  475. } else {
  476. realDay = int64(time.Now().Sub(startDate).Hours() / 24)
  477. }
  478. for _, department := range departmentList {
  479. data := new(http.EquipmentIndex)
  480. data.DeptName = department.Name
  481. for _, monthBudget := range monthBudgetList {
  482. if department.ID == monthBudget.DeptId {
  483. data.MonthBudget = monthBudget.MonthBudget
  484. break
  485. }
  486. }
  487. for _, feetSum := range feetSumPriceList {
  488. if department.ID == feetSum.DepartmentId {
  489. data.SumPrice = feetSum.SumPrice
  490. break
  491. }
  492. }
  493. data.QmonthBudget = data.MonthBudget / float64(sumDay) * float64(realDay)
  494. data.Asave = data.MonthBudget - data.SumPrice
  495. if data.MonthBudget > 0 && data.SumPrice > 0 {
  496. data.RealReason = fmt.Sprintf("%v", util.Round((1+((data.MonthBudget/float64(sumDay)*float64(realDay))-(data.SumPrice))/(data.MonthBudget/float64(sumDay)*float64(realDay)))*100, 2)) + " %"
  497. } else {
  498. data.RealReason = "0 %"
  499. }
  500. data.CowSum = feedSum.CowNum
  501. if data.MonthBudget > 0 && data.SumPrice > 0 && data.CowSum > 0 {
  502. data.OneCowSumPrice = util.Round(data.SumPrice/float64(data.CowSum/sumDay*realDay), 2)
  503. } else {
  504. data.OneCowSumPrice = 0.0
  505. }
  506. resp = append(resp, data)
  507. }
  508. return resp, nil
  509. }