home.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. package service
  2. import (
  3. "fmt"
  4. "kpt.xdmy/apiserver/model"
  5. "kpt.xdmy/apiserver/model/home"
  6. "kpt.xdmy/pkg/util"
  7. "time"
  8. )
  9. func (s *Service) OverallIndex() (*home.OverallIndexResp, error) {
  10. resp := new(home.OverallIndexResp)
  11. monthBudgetList := make([]*home.MonthBudget, 0)
  12. err := s.d.DB.Raw(`SELECT
  13. (
  14. sum( monthBudget )*(
  15. DATEDIFF(
  16. NOW(),
  17. CONCAT( YEAR ( NOW()), '-01-01' )) / DATEDIFF(
  18. CONCAT( YEAR ( NOW()), '-12-31' ),
  19. CONCAT( YEAR ( NOW()), '-01-01' ))
  20. )) monthBudget,
  21. feeType
  22. FROM
  23. month_budget join pasture on pasture.id = month_budget.pastureId
  24. WHERE
  25. budgetMonth LIKE concat( '%', date_format( now(), '%Y' ), '%' ) and vendor = 1 and feeType != '维修费'
  26. GROUP BY
  27. feeType
  28. UNION ALL
  29. SELECT
  30. (
  31. sum( monthBudget )*(
  32. DATEDIFF(
  33. NOW(),
  34. CONCAT( YEAR ( NOW()), '-01-01' )) / DATEDIFF(
  35. CONCAT( YEAR ( NOW()), '-12-31' ),
  36. CONCAT( YEAR ( NOW()), '-01-01' ))
  37. )) monthBudget,
  38. feeType
  39. FROM
  40. month_budget join pasture on pasture.id = month_budget.pastureId
  41. WHERE
  42. budgetMonth LIKE concat( '%', date_format( now(), '%Y' ), '%' ) and vendor = 1 and feeType = '维修费'
  43. 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 ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')
  44. GROUP BY
  45. feeType`).Find(&monthBudgetList).Error
  46. if err != nil {
  47. return resp, err
  48. }
  49. //
  50. thisYearActualList := make([]*home.ThisYearActual, 0)
  51. err = s.d.DB.Raw(`select FYLX feeType,sum(HSL) sumPrice from feequery where Date like concat( '%',YEAR(NOW()),'%') and FYLX in ('水费','燃动费','电费') group by FYLX
  52. UNION ALL
  53. SELECT
  54. '维修费' feeType ,IFNULL(SUM( pu.sumPrice - pu.quitNumber * pu.price),0) sumPrice
  55. FROM
  56. partuse pu
  57. INNER JOIN bigpartuse bpu
  58. ON pu.bigId = bpu.id
  59. WHERE bpu.useType <> 3
  60. AND (
  61. pu.useTypeV = '维修'
  62. OR pu.useTypeV = '保养'
  63. ) AND (SELECT iscal FROM department d WHERE d.id=bpu.departmentId)= 0 and (SELECT name FROM department d WHERE d.id=bpu.departmentId)
  64. not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')AND bpu.receiveTime BETWEEN CONCAT(YEAR(NOW()),'-01-01') AND CONCAT(YEAR(NOW()),'-12-31')
  65. AND (bpu.pastureid = (SELECT id FROM pasture WHERE name = '现代牧业') OR '现代牧业' = '现代牧业') and pu.dflag != 1`).Find(&thisYearActualList).Error
  66. if err != nil {
  67. return resp, err
  68. }
  69. lastYearActualList := make([]*home.ThisYearActual, 0)
  70. err = s.d.DB.Raw(`select FYLX feeType,sum(HSL) sumPrice from feequery where DATE_FORMAT(STR_TO_DATE( Date, '%Y-%m' ),'%Y-%m') between CONCAT(YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)),'-01') and DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 YEAR),'%Y-%m')
  71. and FYLX in ('水费','燃动费','电费') group by FYLX
  72. UNION ALL
  73. SELECT
  74. '维修费' feeType ,IFNULL(SUM( pu.sumPrice - pu.quitNumber * pu.price),0) sumPrice
  75. FROM
  76. partuse pu
  77. INNER JOIN bigpartuse bpu
  78. ON pu.bigId = bpu.id
  79. WHERE bpu.useType <> 3
  80. AND (
  81. pu.useTypeV = '维修'
  82. OR pu.useTypeV = '保养'
  83. ) AND (SELECT iscal FROM department d WHERE d.id=bpu.departmentId)= 0 and (SELECT name FROM department d WHERE d.id=bpu.departmentId)
  84. not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处') AND bpu.receiveTime BETWEEN CONCAT(YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)),'-01-01') AND DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
  85. AND (bpu.pastureid = (SELECT id FROM pasture WHERE name = '现代牧业') OR '现代牧业' = '现代牧业') and pu.dflag != 1`).Find(&lastYearActualList).Error
  86. if err != nil {
  87. return resp, err
  88. }
  89. feedSum := new(model.FeedSum)
  90. err = s.d.DB.Raw(` select sum(cowNum) cowNum from feed_sum where feedDay like concat('%',?,'%')`, time.Now().Format("2006")).First(feedSum).Error
  91. if err != nil {
  92. return resp, err
  93. }
  94. var repair = new(home.Overall)
  95. var water = new(home.Overall)
  96. var electricity = new(home.Overall)
  97. var ignition = new(home.Overall)
  98. for _, monthBudget := range monthBudgetList {
  99. if monthBudget.FeeType == "维修费" {
  100. repair.Budget = util.Round(monthBudget.MonthBudget/10000, 2)
  101. } else if monthBudget.FeeType == "电费" {
  102. electricity.Budget = util.Round(monthBudget.MonthBudget/10000, 2)
  103. } else if monthBudget.FeeType == "水费" {
  104. water.Budget = util.Round(monthBudget.MonthBudget/10000, 2)
  105. } else if monthBudget.FeeType == "燃动费" {
  106. ignition.Budget = util.Round(monthBudget.MonthBudget/10000, 2)
  107. }
  108. }
  109. cowNum := float64(feedSum.CowNum)
  110. for _, item := range thisYearActualList {
  111. if item.FeeType == "维修费" {
  112. repair.Actual = util.Round(item.SumPrice/10000, 2)
  113. repair.SingleCow = util.Round(item.SumPrice/cowNum, 2)
  114. } else if item.FeeType == "电费" {
  115. electricity.Actual = util.Round(item.SumPrice/10000, 2)
  116. electricity.SingleCow = util.Round(item.SumPrice/cowNum, 2)
  117. } else if item.FeeType == "水费" {
  118. water.Actual = util.Round(item.SumPrice/10000, 2)
  119. water.SingleCow = util.Round(item.SumPrice/cowNum, 2)
  120. } else if item.FeeType == "燃动费" {
  121. ignition.Actual = util.Round(item.SumPrice/10000, 2)
  122. ignition.SingleCow = util.Round(item.SumPrice/cowNum, 2)
  123. }
  124. }
  125. for _, item := range lastYearActualList {
  126. if item.FeeType == "维修费" {
  127. repair.LastActual = util.Round(item.SumPrice/10000, 2)
  128. } else if item.FeeType == "电费" {
  129. electricity.LastActual = util.Round(item.SumPrice/10000, 2)
  130. } else if item.FeeType == "水费" {
  131. water.LastActual = util.Round(item.SumPrice/10000, 2)
  132. } else if item.FeeType == "燃动费" {
  133. ignition.LastActual = util.Round(item.SumPrice/10000, 2)
  134. }
  135. }
  136. resp.Repair = repair
  137. resp.Electricity = electricity
  138. resp.Water = water
  139. resp.Ignition = ignition
  140. return resp, nil
  141. }
  142. func (s *Service) CentralIndex() (*home.CentralIndexResp, error) {
  143. resp := new(home.CentralIndexResp)
  144. pastureList := make([]*model.Pasture, 0)
  145. err := s.d.DB.Select("center").Where(" center is not null or center != '' ").Group("center").Order("center_id").Find(&pastureList).Error
  146. if err != nil {
  147. return resp, err
  148. }
  149. centralIndexList := make([]*home.CentralIndex, 0)
  150. for _, pasture := range pastureList {
  151. centralIndexList = append(centralIndexList, &home.CentralIndex{
  152. Central: pasture.Center,
  153. })
  154. }
  155. monthBudgetList := make([]*home.MonthBudget, 0)
  156. err = s.d.DB.Raw(`SELECT
  157. (
  158. sum( monthBudget )*(
  159. DATEDIFF(
  160. NOW(),
  161. CONCAT( YEAR ( NOW()), '-01-01' )) / DATEDIFF(
  162. CONCAT( YEAR ( NOW()), '-12-31' ),
  163. CONCAT( YEAR ( NOW()), '-01-01' ))
  164. )) monthBudget,
  165. center
  166. FROM
  167. month_budget mb
  168. JOIN pasture p ON p.id = mb.pastureId
  169. WHERE
  170. budgetMonth LIKE concat( '%', date_format( now(), '%Y' ), '%' )
  171. AND feeType IN ( '电费', '水费', '燃动费' )
  172. GROUP BY
  173. center ,feeType
  174. UNION ALL
  175. SELECT
  176. (
  177. sum( monthBudget )*(
  178. DATEDIFF(
  179. NOW(),
  180. CONCAT( YEAR ( NOW()), '-01-01' )) / DATEDIFF(
  181. CONCAT( YEAR ( NOW()), '-12-31' ),
  182. CONCAT( YEAR ( NOW()), '-01-01' ))
  183. )) monthBudget,
  184. center
  185. FROM
  186. month_budget join pasture on pasture.id = month_budget.pastureId
  187. WHERE
  188. budgetMonth LIKE concat( '%', date_format( now(), '%Y' ), '%' ) and vendor = 1 and feeType = '维修费'
  189. 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 ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')
  190. GROUP BY
  191. center`).Find(&monthBudgetList).Error
  192. if err != nil {
  193. return resp, err
  194. }
  195. thisYearActualList := make([]*home.ThisYearActual, 0)
  196. err = s.d.DB.Raw(`select sum(t.sumPrice) sumPrice,p.center from (
  197. select pastureid,FYLX feeType,sum(HSL) sumPrice from feequery where Date like concat( '%',YEAR(NOW()),'%')
  198. and FYLX in ('水费','燃动费','电费') group by FYLX,pastureid
  199. UNION ALL
  200. SELECT
  201. bpu.pastureid,'维修费' feeType ,IFNULL(SUM( pu.sumPrice - pu.quitNumber * pu.price),0) sumPrice
  202. FROM
  203. partuse pu
  204. INNER JOIN bigpartuse bpu
  205. ON pu.bigId = bpu.id
  206. WHERE bpu.useType <> 3
  207. AND (
  208. pu.useTypeV = '维修'
  209. OR pu.useTypeV = '保养'
  210. ) AND (SELECT iscal FROM department d WHERE d.id=bpu.departmentId)= 0 and (SELECT name FROM department d WHERE d.id=bpu.departmentId)
  211. not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处') AND bpu.receiveTime BETWEEN CONCAT(YEAR(NOW()),'-01-01') AND CONCAT(YEAR(NOW()),'-12-31')
  212. AND (bpu.pastureid = (SELECT id FROM pasture WHERE name = '现代牧业') OR '现代牧业' = '现代牧业') and pu.dflag != 1 group by bpu.pastureId) t
  213. join pasture p on p.id = t.pastureId and p.vendor = 1 and p.IsEnergy = 0 GROUP BY p.center`).Find(&thisYearActualList).Error
  214. if err != nil {
  215. return resp, err
  216. }
  217. nowStr := time.Now().Format("2006-01-02")
  218. now, _ := time.Parse("2006-01-02", nowStr)
  219. // 获取当年的一月一号
  220. year := now.Year()
  221. start := time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC)
  222. fmt.Println(start)
  223. end := time.Date(year, 12, 31, 0, 0, 0, 0, time.UTC)
  224. fmt.Println(end)
  225. real := now.Sub(start)
  226. sum := end.Sub(start)
  227. sumDay := sum.Hours() / 24
  228. realDay := real.Hours() / 24
  229. for _, central := range centralIndexList {
  230. for _, monthBudget := range monthBudgetList {
  231. if monthBudget.Center == central.Central {
  232. central.Budget += util.Round(monthBudget.MonthBudget, 2)
  233. //break
  234. }
  235. }
  236. for _, item := range thisYearActualList {
  237. if item.Center == central.Central {
  238. central.Actual += util.Round(item.SumPrice, 2)
  239. //break
  240. }
  241. }
  242. if central.Budget > 0 && central.Actual > 0 {
  243. central.Complete = util.Round((1+(central.Budget/sumDay*realDay-(central.Actual))/central.Budget)*100, 2)
  244. central.Budget = util.Round(central.Budget/10000, 2)
  245. central.Actual = util.Round(central.Actual/10000, 2)
  246. }
  247. }
  248. resp.Central = append(resp.Central, centralIndexList...)
  249. return resp, nil
  250. }
  251. func (s *Service) GetExpense(pastureName string) (*home.GetExpenseResp, error) {
  252. resp := new(home.GetExpenseResp)
  253. pasture := new(model.Pasture)
  254. if pastureName != "现代牧业" && pastureName != "" {
  255. err := s.d.DB.Where(" name = ? ", pastureName).First(&pasture).Error
  256. if err != nil {
  257. return resp, err
  258. }
  259. }
  260. monthBudgetList := make([]*home.MonthBudget, 0)
  261. err := s.d.DB.Raw(`SELECT
  262. (
  263. sum( monthBudget )*(
  264. DATEDIFF(
  265. NOW(),
  266. CONCAT( YEAR ( NOW()), '-01-01' )) / DATEDIFF(
  267. CONCAT( YEAR ( NOW()), '-12-31' ),
  268. CONCAT( YEAR ( NOW()), '-01-01' ))
  269. )) monthBudget,
  270. feeType ,pasture.center
  271. FROM
  272. month_budget join pasture on pasture.id = month_budget.pastureId
  273. WHERE
  274. budgetMonth LIKE concat( '%', date_format( now(), '%Y' ), '%' ) and vendor = 1 and feeType != '维修费'
  275. GROUP BY
  276. feeType
  277. UNION ALL
  278. SELECT
  279. (
  280. sum( monthBudget )*(
  281. DATEDIFF(
  282. NOW(),
  283. CONCAT( YEAR ( NOW()), '-01-01' )) / DATEDIFF(
  284. CONCAT( YEAR ( NOW()), '-12-31' ),
  285. CONCAT( YEAR ( NOW()), '-01-01' ))
  286. )) monthBudget,
  287. feeType ,pasture.center
  288. FROM
  289. month_budget join pasture on pasture.id = month_budget.pastureId
  290. WHERE
  291. budgetMonth LIKE concat( '%', date_format( now(), '%Y' ), '%' ) and vendor = 1 and feeType = '维修费'
  292. 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 ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')
  293. GROUP BY
  294. feeType`).Find(&monthBudgetList).Error
  295. if err != nil {
  296. return resp, err
  297. }
  298. thisYearActualList := make([]*home.ThisYearActual, 0)
  299. err = s.d.DB.Debug().Raw(`select FYLX feeType,sum(HSL) sumPrice,p.center from feequery fq join pasture p on p.id = fq.pastureId
  300. where Date like concat( '%',YEAR(NOW()),'%') and ( ? = 0 or pastureId = ?)
  301. and FYLX in ('水费','燃动费','电费') group by FYLX,p.center
  302. UNION ALL
  303. SELECT
  304. '维修费' feeType ,IFNULL(SUM( pu.sumPrice - pu.quitNumber * pu.price),0) sumPrice,p.center
  305. FROM
  306. partuse pu
  307. INNER JOIN bigpartuse bpu
  308. ON pu.bigId = bpu.id
  309. join pasture p on p.id = bpu.pastureId
  310. WHERE bpu.useType <> 3
  311. AND (
  312. pu.useTypeV = '维修'
  313. OR pu.useTypeV = '保养'
  314. ) AND (SELECT iscal FROM department d WHERE d.id=bpu.departmentId)= 0 AND bpu.receiveTime BETWEEN CONCAT(YEAR(NOW()),'-01-01')
  315. AND CONCAT(YEAR(NOW()),'-12-31') and (SELECT name FROM department d WHERE d.id=bpu.departmentId)
  316. not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')
  317. AND ( ? = 0 or pastureId = ?) and pu.dflag != 1 and p.vendor = 1 group by p.center`, pasture.ID, pasture.ID, pasture.ID, pasture.ID).Find(&thisYearActualList).Error
  318. if err != nil {
  319. return resp, err
  320. }
  321. lastYearActualList := make([]*home.ThisYearActual, 0)
  322. err = s.d.DB.Debug().Raw(`select FYLX feeType,sum(HSL) sumPrice,p.center from feequery fq join pasture p on p.id = fq.pastureId where DATE_FORMAT(STR_TO_DATE( Date, '%Y-%m' ),'%Y-%m') between CONCAT(YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)),'-01') and DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 YEAR),'%Y-%m')
  323. and FYLX in ('水费','燃动费','电费') and ( ? = 0 or pastureId = ?) group by FYLX,p.center
  324. UNION ALL
  325. SELECT
  326. '维修费' feeType ,IFNULL(SUM( pu.sumPrice - pu.quitNumber * pu.price),0) sumPrice,p.center
  327. FROM
  328. partuse pu
  329. INNER JOIN bigpartuse bpu
  330. ON pu.bigId = bpu.id
  331. join pasture p on p.id = bpu.pastureId
  332. WHERE bpu.useType <> 3
  333. AND (
  334. pu.useTypeV = '维修'
  335. OR pu.useTypeV = '保养'
  336. ) AND (SELECT iscal FROM department d WHERE d.id=bpu.departmentId)= 0 AND bpu.receiveTime BETWEEN CONCAT(YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)),'-01-01') AND DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
  337. AND ( ? = 0 or pastureId = ?) and (SELECT name FROM department d WHERE d.id=bpu.departmentId)
  338. not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处') and pu.dflag != 1 group by p.center`, pasture.ID, pasture.ID, pasture.ID, pasture.ID).Find(&lastYearActualList).Error
  339. if err != nil {
  340. return resp, err
  341. }
  342. pastureList := make([]*model.Pasture, 0)
  343. err = s.d.DB.Select("center").Where(" center is not null or center != '' ").Group("center").Order("center_id").Find(&pastureList).Error
  344. if err != nil {
  345. return resp, err
  346. }
  347. var repairList = make([]*home.Overall, 0)
  348. var waterList = make([]*home.Overall, 0)
  349. var electricityList = make([]*home.Overall, 0)
  350. var ignitionList = make([]*home.Overall, 0)
  351. for _, item := range pastureList {
  352. repairList = append(repairList, &home.Overall{Center: item.Center})
  353. waterList = append(waterList, &home.Overall{Center: item.Center})
  354. electricityList = append(electricityList, &home.Overall{Center: item.Center})
  355. ignitionList = append(ignitionList, &home.Overall{Center: item.Center})
  356. }
  357. for _, monthBudget := range monthBudgetList {
  358. if monthBudget.FeeType == "维修费" {
  359. for _, repair := range repairList {
  360. if repair.Center == monthBudget.Center {
  361. repair.Budget = util.Round(monthBudget.MonthBudget/10000, 2)
  362. break
  363. }
  364. }
  365. } else if monthBudget.FeeType == "电费" {
  366. for _, electricity := range electricityList {
  367. if electricity.Center == monthBudget.Center {
  368. electricity.Budget = util.Round(monthBudget.MonthBudget/10000, 2)
  369. break
  370. }
  371. }
  372. } else if monthBudget.FeeType == "水费" {
  373. for _, water := range waterList {
  374. if water.Center == monthBudget.Center {
  375. water.Budget = util.Round(monthBudget.MonthBudget/10000, 2)
  376. break
  377. }
  378. }
  379. } else if monthBudget.FeeType == "燃动费" {
  380. for _, ignition := range ignitionList {
  381. if ignition.Center == monthBudget.Center {
  382. ignition.Budget = util.Round(monthBudget.MonthBudget/10000, 2)
  383. break
  384. }
  385. }
  386. }
  387. }
  388. feedSumList := make([]*model.FeedSumAndPasture, 0)
  389. feedSum := new(model.FeedSumAndPasture)
  390. if pasture.ID == 0 {
  391. err = s.d.DB.Raw(` select sum(cowNum) cowNum,center from feed_sum join pasture on pasture.id = feed_sum.pastureId
  392. where feedDay like concat('%',?,'%') group by center`, time.Now().Format("2006")).Find(&feedSumList).Error
  393. if err != nil {
  394. return resp, err
  395. }
  396. } else {
  397. err = s.d.DB.Raw(` select sum(cowNum) cowNum from feed_sum join pasture on pasture.id = feed_sum.pastureId
  398. where feedDay like concat('%',?,'%') and pastureId = ? `, time.Now().Format("2006"), pasture.ID).First(&feedSum).Error
  399. if err != nil {
  400. return resp, err
  401. }
  402. }
  403. //cowNum := float64(feedSum.CowNum)
  404. for _, item := range thisYearActualList {
  405. var cowNum float64
  406. if pasture.ID == 0 {
  407. for _, center := range feedSumList {
  408. if center.Center == item.Center {
  409. cowNum = float64(center.CowNum)
  410. break
  411. }
  412. }
  413. } else {
  414. cowNum = float64(feedSum.CowNum)
  415. }
  416. if item.FeeType == "维修费" {
  417. for _, repair := range repairList {
  418. if repair.Center == item.Center {
  419. repair.Actual = util.Round(item.SumPrice/10000, 2)
  420. repair.SingleCow = util.Round(item.SumPrice/cowNum, 2)
  421. break
  422. }
  423. }
  424. } else if item.FeeType == "电费" {
  425. //electricity.Actual = util.Round(item.SumPrice, 2)
  426. //electricity.Actual = util.Round(electricity.Actual/cowNum, 2)
  427. for _, electricity := range electricityList {
  428. if electricity.Center == item.Center {
  429. electricity.Actual = util.Round(item.SumPrice/10000, 2)
  430. electricity.SingleCow = util.Round(item.SumPrice/cowNum, 2)
  431. break
  432. }
  433. }
  434. } else if item.FeeType == "水费" {
  435. //water.Actual = util.Round(item.SumPrice, 2)
  436. //water.Actual = util.Round(water.Actual/cowNum, 2)
  437. for _, water := range waterList {
  438. if water.Center == item.Center {
  439. water.Actual = util.Round(item.SumPrice/10000, 2)
  440. water.SingleCow = util.Round(item.SumPrice/cowNum, 2)
  441. break
  442. }
  443. }
  444. } else if item.FeeType == "燃动费" {
  445. //ignition.Actual = util.Round(item.SumPrice, 2)
  446. //ignition.Actual = util.Round(ignition.Actual/cowNum, 2)
  447. for _, ignition := range ignitionList {
  448. if ignition.Center == item.Center {
  449. //ignition.Actual = util.Round(item.SumPrice, 2)
  450. ignition.SingleCow = util.Round(item.SumPrice/cowNum, 2)
  451. ignition.Actual = util.Round(item.SumPrice/10000, 2)
  452. break
  453. }
  454. }
  455. }
  456. }
  457. for _, item := range lastYearActualList {
  458. if item.FeeType == "维修费" {
  459. for _, repair := range repairList {
  460. if repair.Center == item.Center {
  461. repair.LastActual = util.Round(item.SumPrice/10000, 2)
  462. break
  463. }
  464. }
  465. //repair.LastActual = util.Round(item.SumPrice, 2)
  466. } else if item.FeeType == "电费" {
  467. //electricity.LastActual = util.Round(item.SumPrice, 2)
  468. for _, electricity := range electricityList {
  469. if electricity.Center == item.Center {
  470. electricity.LastActual = util.Round(item.SumPrice/10000, 2)
  471. break
  472. }
  473. }
  474. } else if item.FeeType == "水费" {
  475. //water.LastActual = util.Round(item.SumPrice, 2)
  476. for _, water := range waterList {
  477. if water.Center == item.Center {
  478. water.LastActual = util.Round(item.SumPrice/10000, 2)
  479. break
  480. }
  481. }
  482. } else if item.FeeType == "燃动费" {
  483. //ignition.LastActual = util.Round(item.SumPrice, 2)
  484. for _, ignition := range ignitionList {
  485. if ignition.Center == item.Center {
  486. ignition.LastActual = util.Round(item.SumPrice/10000, 2)
  487. break
  488. }
  489. }
  490. }
  491. }
  492. resp.Repair = append(resp.Repair, repairList...)
  493. resp.Electricity = append(resp.Electricity, electricityList...)
  494. resp.Water = append(resp.Water, waterList...)
  495. resp.Ignition = append(resp.Ignition, ignitionList...)
  496. return resp, nil
  497. }
  498. func (s *Service) GetPastureIndex(pastureName string) (*home.GetPastureIndexResp, error) {
  499. resp := new(home.GetPastureIndexResp)
  500. monthBudget := new(home.MonthBudget)
  501. err := s.d.DB.Raw(`SELECT
  502. sum( monthBudget ) monthBudget
  503. FROM
  504. month_budget mb
  505. JOIN pasture p ON p.id = mb.pastureId
  506. WHERE
  507. budgetMonth LIKE concat( '%', date_format( now(), '%Y' ), '%' )
  508. AND feeType IN ( '维修费', '电费', '水费', '燃动费' ) and p.name = ? `, pastureName).Find(&monthBudget).Error
  509. if err != nil {
  510. return resp, err
  511. }
  512. thisYearActual := new(home.ThisYearActual)
  513. err = s.d.DB.Raw(`select sum(t.sumPrice) sumPrice from (
  514. select pastureid,FYLX feeType,sum(HSL) sumPrice from feequery fq join pasture p on p.id = fq.pastureId where Date like concat( '%',YEAR(NOW()),'%')
  515. and FYLX in ('水费','燃动费','电费') and p.name = ?
  516. UNION ALL
  517. SELECT
  518. bpu.pastureid,'维修费' feeType ,IFNULL(SUM( pu.sumPrice - pu.quitNumber * pu.price),0) sumPrice
  519. FROM
  520. partuse pu
  521. INNER JOIN bigpartuse bpu
  522. ON pu.bigId = bpu.id
  523. join pasture p on p.id = bpu.pastureId
  524. WHERE bpu.useType <> 3
  525. AND (
  526. pu.useTypeV = '维修'
  527. OR pu.useTypeV = '保养'
  528. ) AND (SELECT iscal FROM department d WHERE d.id=bpu.departmentId)= 0 AND bpu.receiveTime BETWEEN CONCAT(YEAR(NOW()),'-01-01') AND CONCAT(YEAR(NOW()),'-12-31')
  529. AND p.name = ? and (SELECT name FROM department d WHERE d.id=bpu.departmentId) not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处') and pu.dflag != 1 group by bpu.pastureId) t
  530. join pasture p on p.id = t.pastureId and p.vendor = 1 GROUP BY p.center`, pastureName, pastureName).Find(&thisYearActual).Error
  531. if err != nil {
  532. return resp, err
  533. }
  534. nowStr := time.Now().Format("2006-01-02")
  535. now, _ := time.Parse("2006-01-02", nowStr)
  536. // 获取当年的一月一号
  537. year := now.Year()
  538. start := time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC)
  539. fmt.Println(start)
  540. end := time.Date(year, 12, 31, 0, 0, 0, 0, time.UTC)
  541. fmt.Println(end)
  542. real := now.Sub(start)
  543. sum := end.Sub(start)
  544. sumDay := sum.Hours() / 24
  545. realDay := real.Hours() / 24
  546. fmt.Println(realDay, sumDay)
  547. resp.Budget = monthBudget.MonthBudget
  548. resp.Actual = thisYearActual.SumPrice
  549. if resp.Budget != 0 && resp.Actual != 0 {
  550. resp.Complete = util.Round((1+(resp.Budget/sumDay*realDay-(resp.Actual))/resp.Budget)*100, 2)
  551. }
  552. //ROUND(1+((qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000))/(qq.monthLimit/qq.sumDay*qq.realDay)), 2)
  553. resp.Budget = util.Round(resp.Budget/10000, 2)
  554. resp.Actual = util.Round(resp.Actual/10000, 2)
  555. //resp.Complete = util.Round(1+(resp.Budget/sumDay*realDay-(resp.Actual/10000))/(resp.Budget/sumDay*realDay), 2)
  556. return resp, nil
  557. }