123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- package service
- import (
- "fmt"
- "kpt.xdmy/apiserver/model"
- "kpt.xdmy/apiserver/model/home"
- "kpt.xdmy/apiserver/model/http"
- "kpt.xdmy/pkg/util"
- "time"
- )
- func (s *Service) GetEquipmentIndex(pastureId, startTime, endTime string) (*http.EquipmentIndexResp, error) {
- resp := new(http.EquipmentIndexResp)
- startDate, _ := time.Parse("2006-01-02", startTime)
- endDate, _ := time.Parse("2006-01-02", endTime)
- monthBudgetList := make([]*home.MonthBudget, 0)
- err := s.d.DB.Debug().Raw(`SELECT
- sum( monthBudget ) monthBudget,
- feeType,pastureId
- FROM
- month_budget join pasture on pasture.id = month_budget.pastureId
- WHERE
- budgetMonth BETWEEN date_format(?,'%Y-%m') and date_format(?,'%Y-%m') and (pastureId = ? or ? ='') and vendor = 1 and feeType != '维修费'
- GROUP BY
- feeType,pastureId
- UNION ALL
- SELECT
- sum( monthBudget ) monthBudget,
- feeType,pastureId
- FROM
- month_budget join pasture on pasture.id = month_budget.pastureId
- WHERE
- budgetMonth BETWEEN date_format(?,'%Y-%m') and date_format(?,'%Y-%m') and (pastureId = ? or ? ='') and vendor = 1 and feeType = '维修费'
- 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 ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')
- GROUP BY
- feeType,pastureId `, startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), pastureId, pastureId,
- startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), pastureId, pastureId).Find(&monthBudgetList).Error
- if err != nil {
- return resp, err
- }
- //
- feedSumList := make([]*model.FeedSumAndPasture, 0)
- 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
- where feedDay between ? and ? and (fs.pastureId = ? or ? ='') group by fs.pastureId`, startDate.Format("2006-01-02"),
- endDate.Format("2006-01-02"), pastureId, pastureId).Find(&feedSumList).Error
- if err != nil {
- return resp, err
- }
- feetSumPriceList := make([]*home.ThisYearActual, 0)
- 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')
- between ? and ? and FYLX in ('水费','燃动费','电费','柴油费') and (pastureId = ? or ? = '') group by FYLX,pastureId
- UNION ALL
- SELECT
- '维修费' feeType ,IFNULL(SUM( pu.sumPrice - pu.quitNumber * pu.price),0) sumPrice,bpu.pastureId
- FROM
- partuse pu
- INNER JOIN bigpartuse bpu
- ON pu.bigId = bpu.id
- WHERE bpu.useType <> 3
- AND (
- pu.useTypeV = '维修'
- OR pu.useTypeV = '保养'
- ) AND (SELECT iscal FROM department d WHERE d.id=bpu.departmentId)= 0 and (SELECT name FROM department d WHERE d.id=bpu.departmentId)
- not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')
- AND bpu.receiveTime BETWEEN ? AND ?
- AND (bpu.pastureid = ? OR '' = ?) and pu.dflag != 1 group by pastureId`, startDate.Format("2006-01"),
- endDate.Format("2006-01"), pastureId, pastureId, startDate.Format("2006-01-02"),
- endDate.AddDate(0, 0, 1).Format("2006-01-02"), pastureId, pastureId).Find(&feetSumPriceList).Error
- if err != nil {
- return resp, err
- }
- var sumDay, realDay int64
- sumDay = int64(endDate.Sub(startDate).Hours() / 24)
- if endDate.Unix() < time.Now().Unix() {
- realDay = sumDay
- } else {
- realDay = int64(time.Now().Sub(startDate).Hours() / 24)
- }
- pastureList := make([]*model.Pasture, 0)
- err = s.d.DB.Debug().Where(" id != 18 ").Where(" id = ? or ? = '' ", pastureId, pastureId).Where("vendor = 1").Order("center_sort,id").Find(&pastureList).Error
- if err != nil {
- return resp, err
- }
- pastureLs := new(model.Pasture)
- equipmentList := make([]*http.EquipmentIndex, 0)
- chooseMonthWXList := make([]*http.EquipmentIndex, 0)
- chooseWaterPriceList := make([]*http.EquipmentIndex, 0)
- chooseElectricityPriceList := make([]*http.EquipmentIndex, 0)
- chooseMonthiolPriceList := make([]*http.EquipmentIndex, 0)
- lgnitionChargePriceList := make([]*http.EquipmentIndex, 0)
- dosageIndexList := make([]map[string]interface{}, 0)
- pastureCount := len(pastureList) - 1
- for i, pasture := range pastureList {
- if i == 0 {
- pastureLs = pasture
- }
- if pastureLs.Center != pasture.Center {
- equipmentList = append(equipmentList, &http.EquipmentIndex{PastureName: pastureList[i-1].Center, Center: pastureList[i-1].Center})
- chooseMonthWXList = append(chooseMonthWXList, &http.EquipmentIndex{PastureName: pastureList[i-1].Center, Center: pastureList[i-1].Center})
- chooseWaterPriceList = append(chooseWaterPriceList, &http.EquipmentIndex{PastureName: pastureList[i-1].Center, Center: pastureList[i-1].Center})
- chooseElectricityPriceList = append(chooseElectricityPriceList, &http.EquipmentIndex{PastureName: pastureList[i-1].Center, Center: pastureList[i-1].Center})
- chooseMonthiolPriceList = append(chooseMonthiolPriceList, &http.EquipmentIndex{PastureName: pastureList[i-1].Center, Center: pastureList[i-1].Center})
- lgnitionChargePriceList = append(lgnitionChargePriceList, &http.EquipmentIndex{PastureName: pastureList[i-1].Center, Center: pastureList[i-1].Center})
- //center := make(map[string]interface{})
- //center["pastureName"] = pastureList[i-1].Center
- //center["center"] = pastureList[i-1].Center
- //dosageIndexList = append(dosageIndexList, center)
- }
- data := make(map[string]interface{})
- data["pastureName"] = pasture.Name
- data["center"] = pasture.Center
- data["pastureId"] = pasture.ID
- dosageIndexList = append(dosageIndexList, data)
- equipmentList = append(equipmentList, &http.EquipmentIndex{PastureName: pasture.Name, PastureId: pasture.ID, Center: pasture.Center})
- chooseMonthWXList = append(chooseMonthWXList, &http.EquipmentIndex{PastureName: pasture.Name, PastureId: pasture.ID, Center: pasture.Center})
- chooseWaterPriceList = append(chooseWaterPriceList, &http.EquipmentIndex{PastureName: pasture.Name, PastureId: pasture.ID, Center: pasture.Center})
- chooseElectricityPriceList = append(chooseElectricityPriceList, &http.EquipmentIndex{PastureName: pasture.Name, PastureId: pasture.ID, Center: pasture.Center})
- chooseMonthiolPriceList = append(chooseMonthiolPriceList, &http.EquipmentIndex{PastureName: pasture.Name, PastureId: pasture.ID, Center: pasture.Center})
- lgnitionChargePriceList = append(lgnitionChargePriceList, &http.EquipmentIndex{PastureName: pasture.Name, PastureId: pasture.ID, Center: pasture.Center})
- if i == pastureCount {
- equipmentList = append(equipmentList, &http.EquipmentIndex{PastureName: pasture.Center, Center: pasture.Center})
- chooseWaterPriceList = append(chooseWaterPriceList, &http.EquipmentIndex{PastureName: pasture.Center, Center: pasture.Center})
- chooseWaterPriceList = append(chooseWaterPriceList, &http.EquipmentIndex{PastureName: pasture.Center, Center: pasture.Center})
- chooseElectricityPriceList = append(chooseElectricityPriceList, &http.EquipmentIndex{PastureName: pasture.Center, Center: pasture.Center})
- chooseMonthiolPriceList = append(chooseMonthiolPriceList, &http.EquipmentIndex{PastureName: pasture.Center, Center: pasture.Center})
- lgnitionChargePriceList = append(lgnitionChargePriceList, &http.EquipmentIndex{PastureName: pasture.Center, Center: pasture.Center})
- //center := make(map[string]interface{})
- //center["pastureName"] = pasture.Center
- //center["center"] = pasture.Center
- //dosageIndexList = append(dosageIndexList, center)
- }
- pastureLs = pasture
- }
- equipmentJT := new(http.EquipmentIndex)
- equipmentJT.PastureName = "集团"
- for _, eq := range equipmentList {
- for _, monthBudget := range monthBudgetList {
- if eq.PastureId == monthBudget.PastureId && (monthBudget.FeeType == "燃动费" || monthBudget.FeeType == "水费" || monthBudget.FeeType == "电费" || monthBudget.FeeType == "维修费") {
- eq.MonthBudget += monthBudget.MonthBudget
- eq.QmonthBudget += monthBudget.MonthBudget / float64(sumDay) * float64(realDay)
- }
- }
- for _, feedSum := range feedSumList {
- if eq.PastureId == int(feedSum.PastureId) {
- eq.CowSum += feedSum.CowNum
- break
- }
- }
- for _, feedSum := range feetSumPriceList {
- if eq.PastureId == feedSum.PastureId && (feedSum.FeeType == "燃动费" || feedSum.FeeType == "水费" || feedSum.FeeType == "电费" || feedSum.FeeType == "维修费") {
- eq.SumPrice += feedSum.SumPrice
- }
- }
- eq.Asave = eq.MonthBudget - eq.SumPrice
- if eq.MonthBudget > 0 && eq.SumPrice > 0 {
- eq.RealReason = fmt.Sprintf("%v", util.Round((1+((eq.MonthBudget/float64(sumDay)*float64(realDay))-(eq.SumPrice))/(eq.MonthBudget/float64(sumDay)*float64(realDay)))*100, 2)) + " %"
- } else {
- eq.RealReason = "0 %"
- }
- if eq.MonthBudget > 0 && eq.SumPrice > 0 && eq.CowSum > 0 {
- eq.OneCowSumPrice = util.Round(eq.SumPrice/float64(eq.CowSum/sumDay*realDay), 2)
- } else {
- eq.OneCowSumPrice = 0.0
- }
- equipmentJT.MonthBudget += eq.MonthBudget
- equipmentJT.SumPrice += eq.SumPrice
- equipmentJT.QmonthBudget += eq.QmonthBudget
- equipmentJT.Asave += eq.Asave
- equipmentJT.CowSum += eq.CowSum
- }
- for _, eq := range equipmentList {
- if eq.PastureId == 0 {
- for _, item := range equipmentList {
- if item.Center == eq.PastureName && eq.PastureName != item.PastureName {
- eq.MonthBudget += item.MonthBudget
- eq.SumPrice += item.SumPrice
- eq.QmonthBudget += item.QmonthBudget
- eq.Asave += item.Asave
- eq.CowSum += item.CowSum
- }
- }
- }
- }
- equipmentList = append(equipmentList, equipmentJT)
- for _, eq := range equipmentList {
- if eq.PastureId == 0 {
- if eq.MonthBudget > 0 && eq.SumPrice > 0 {
- eq.RealReason = fmt.Sprintf("%v", util.Round((1+((eq.MonthBudget/float64(sumDay)*float64(realDay))-(eq.SumPrice))/(eq.MonthBudget/float64(sumDay)*float64(realDay)))*100, 2)) + " %"
- } else {
- eq.RealReason = "0 %"
- }
- if eq.MonthBudget > 0 && eq.SumPrice > 0 && eq.CowSum > 0 {
- eq.OneCowSumPrice = util.Round(eq.SumPrice/float64(eq.CowSum/sumDay*realDay), 2)
- } else {
- eq.OneCowSumPrice = 0
- }
- }
- eq.MonthBudget = util.Round(eq.MonthBudget/10000, 2)
- eq.SumPrice = util.Round(eq.SumPrice/10000, 2)
- eq.QmonthBudget = util.Round(eq.QmonthBudget/10000, 2)
- eq.Asave = util.Round(eq.Asave/10000, 2)
- }
- resp.EquipmentIndex = equipmentList
- resp.ChooseMonthWX = chooseMonth("维修费", chooseMonthWXList, feedSumList, monthBudgetList, feetSumPriceList, sumDay, realDay)
- resp.ChooseWaterPrice = chooseMonth("水费", chooseWaterPriceList, feedSumList, monthBudgetList, feetSumPriceList, sumDay, realDay)
- resp.ChooseElectricityPrice = chooseMonth("电费", chooseElectricityPriceList, feedSumList, monthBudgetList, feetSumPriceList, sumDay, realDay)
- resp.ChooseMonthiolPrice = chooseMonth("柴油费", chooseMonthiolPriceList, feedSumList, monthBudgetList, feetSumPriceList, sumDay, realDay)
- resp.LgnitionChargePrice = chooseMonth("燃动费", lgnitionChargePriceList, feedSumList, monthBudgetList, feetSumPriceList, sumDay, realDay)
- waterIndexList := make([]*http.DosageIndex, 0)
- err = s.d.DB.Debug().Raw(`SELECT
- m.pastureId,
- ROUND(IFNULL(SUM(e.waterConsumption ),0),2) sumPrice,p.name pastureName
- FROM
- water e
- INNER JOIN measure m
- ON m.id = e.measureId
- join pasture p on m.pastureid = p.id
- WHERE m.meterType = '水表' AND m.useType='1级表' AND e.date between ? and ?
- and (p.id = ? or ? = 0)
- group by m.pastureId
- `, startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), pastureId, pastureId).Find(&waterIndexList).Error
- if err != nil {
- return resp, err
- }
- electricityIndexList := make([]*http.DosageIndex, 0)
- err = s.d.DB.Debug().Raw(`SELECT
- m.pastureId,
- ROUND(IFNULL(SUM(e.elecConsumption),0),2) sumPrice,
- p.name pastureName
- FROM
- electricity e
- INNER JOIN measure m
- ON m.id = e.measureId
- join pasture p on p.id = m.pastureid
- WHERE m.meterType = '电表' AND m.useType='1级表' AND e.date between ? and ?
- and (p.id = ? or ? = '')
- GROUP BY m.pastureId`, startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), pastureId, pastureId).Find(&electricityIndexList).Error
- if err != nil {
- return resp, err
- }
- dieselIndexList := make([]*http.DosageIndex, 0)
- err = s.d.DB.Debug().Raw(`SELECT
- d.pastureId,
- ROUND(IFNULL(SUM(d.oilAmount),0),2) sumPrice,
- p.name pastureName
- FROM
- diesel d
- join pasture p on p.id = d.pastureid
- WHERE d.selTime between ? and ?
- and (p.id = ? or ? = '')
- GROUP BY d.pastureId`, startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), pastureId, pastureId).Find(&dieselIndexList).Error
- if err != nil {
- return resp, err
- }
- for _, dosage := range dosageIndexList {
- dosageMap := dosage
- var cowNum int64
- for _, feedSum := range feedSumList {
- if dosageMap["pastureName"].(string) == feedSum.Name {
- cowNum = feedSum.CowNum
- break
- }
- }
- if _, ok := dosageMap["pastureId"]; !ok {
- continue
- }
- for _, water := range waterIndexList {
- if dosageMap["pastureName"].(string) == water.PastureName {
- dosageMap["waterOneCowSumPrice"] = 0
- dosageMap["waterSumPrice"] = 0
- if water.SumPrice > 0 {
- if cowNum > 0 {
- dosageMap["waterOneCowSumPrice"] = util.Round(water.SumPrice/float64(cowNum), 2)
- }
- dosageMap["waterSumPrice"] = util.Round(water.SumPrice, 2)
- }
- break
- }
- }
- for _, electricity := range electricityIndexList {
- if dosageMap["pastureName"].(string) == electricity.PastureName {
- dosageMap["electricityOneCowSumPrice"] = 0
- dosageMap["electricitySumPrice"] = 0
- if electricity.SumPrice > 0 {
- if cowNum > 0 {
- dosageMap["electricityOneCowSumPrice"] = util.Round(electricity.SumPrice/float64(cowNum), 2)
- }
- dosageMap["electricitySumPrice"] = util.Round(electricity.SumPrice, 2)
- }
- break
- }
- }
- for _, diesel := range dieselIndexList {
- if dosageMap["pastureName"].(string) == diesel.PastureName {
- dosageMap["monthiolOneCowSumPrice"] = 0
- dosageMap["monthiolSumPrice"] = 0
- if diesel.SumPrice > 0 {
- if cowNum > 0 {
- dosageMap["monthiolOneCowSumPrice"] = util.Round(diesel.SumPrice/float64(cowNum), 2)
- }
- dosageMap["monthiolSumPrice"] = util.Round(diesel.SumPrice, 2)
- }
- break
- }
- }
- dosageMap["cowNum"] = cowNum
- }
- for _, dosage := range dosageIndexList {
- dosageMap := dosage
- if _, ok := dosageMap["pastureId"]; !ok {
- var waterSumPrice, electricitySumPrice, monthiolSumPrice float64
- var cowNum int64
- for _, dosage1 := range dosageIndexList {
- dosage1Map := dosage1
- if _, ok := dosage1Map["pastureId"]; ok {
- if dosage1Map["center"].(string) == dosageMap["pastureName"].(string) {
- if _, ok := dosage1Map["waterSumPrice"]; ok {
- waterSumPrice += dosage1Map["waterSumPrice"].(float64)
- }
- if _, ok := dosage1Map["electricitySumPrice"]; ok {
- electricitySumPrice += dosage1Map["electricitySumPrice"].(float64)
- }
- if _, ok := dosage1Map["monthiolSumPrice"]; ok {
- monthiolSumPrice += dosage1Map["monthiolSumPrice"].(float64)
- }
- if _, ok := dosage1Map["cowNum"]; ok {
- cowNum += dosage1Map["cowNum"].(int64)
- }
- }
- }
- }
- if cowNum > 0 {
- dosageMap["waterOneCowSumPrice"] = util.Round(waterSumPrice/float64(cowNum), 2)
- dosageMap["electricityOneCowSumPrice"] = util.Round(waterSumPrice/float64(cowNum), 2)
- dosageMap["monthiolOneCowSumPrice"] = util.Round(waterSumPrice/float64(cowNum), 2)
- } else {
- dosageMap["waterOneCowSumPrice"] = 0
- dosageMap["electricityOneCowSumPrice"] = 0
- dosageMap["monthiolOneCowSumPrice"] = 0
- }
- dosageMap["waterSumPrice"] = util.Round(waterSumPrice, 2)
- dosageMap["electricitySumPrice"] = util.Round(waterSumPrice, 2)
- dosageMap["monthiolSumPrice"] = util.Round(waterSumPrice, 2)
- }
- }
- resp.DosageIndex = dosageIndexList
- return resp, nil
- }
- func chooseMonth(feeType string, equipmentList []*http.EquipmentIndex, feedSumList []*model.FeedSumAndPasture, monthBudgetList []*home.MonthBudget, feetSumPriceList []*home.ThisYearActual, sumDay, realDay int64) []*http.EquipmentIndex {
- chooseMonthJT := new(http.EquipmentIndex)
- chooseMonthJT.PastureName = "集团"
- for _, eq := range equipmentList {
- for _, monthBudget := range monthBudgetList {
- if eq.PastureId == monthBudget.PastureId && (monthBudget.FeeType == feeType) {
- eq.MonthBudget += monthBudget.MonthBudget
- eq.QmonthBudget += monthBudget.MonthBudget / float64(sumDay) * float64(realDay)
- }
- }
- for _, feedSum := range feedSumList {
- if eq.PastureId == int(feedSum.PastureId) {
- eq.CowSum += feedSum.CowNum
- break
- }
- }
- for _, feedSum := range feetSumPriceList {
- if eq.PastureId == feedSum.PastureId && feeType == feedSum.FeeType {
- eq.SumPrice += feedSum.SumPrice
- }
- }
- eq.Asave = eq.MonthBudget - eq.SumPrice
- if eq.MonthBudget > 0 && eq.SumPrice > 0 {
- eq.RealReason = fmt.Sprintf("%v", util.Round((1+((eq.MonthBudget/float64(sumDay)*float64(realDay))-(eq.SumPrice))/(eq.MonthBudget/float64(sumDay)*float64(realDay)))*100, 2)) + " %"
- } else {
- eq.RealReason = "0 %"
- }
- if eq.MonthBudget > 0 && eq.SumPrice > 0 && eq.CowSum > 0 {
- eq.OneCowSumPrice = util.Round(eq.SumPrice/float64(eq.CowSum/sumDay*realDay), 2)
- } else {
- eq.OneCowSumPrice = 0.0
- }
- chooseMonthJT.MonthBudget += eq.MonthBudget
- chooseMonthJT.SumPrice += eq.SumPrice
- chooseMonthJT.QmonthBudget += eq.QmonthBudget
- chooseMonthJT.Asave += eq.Asave
- chooseMonthJT.CowSum += eq.CowSum
- }
- for _, eq := range equipmentList {
- if eq.PastureId == 0 {
- for _, item := range equipmentList {
- if item.Center == eq.PastureName && eq.PastureName != item.PastureName {
- eq.MonthBudget += item.MonthBudget
- eq.SumPrice += item.SumPrice
- eq.QmonthBudget += item.QmonthBudget
- eq.Asave += item.Asave
- eq.CowSum += item.CowSum
- }
- }
- }
- }
- equipmentList = append(equipmentList, chooseMonthJT)
- for _, eq := range equipmentList {
- if eq.PastureId == 0 {
- if eq.MonthBudget > 0 && eq.SumPrice > 0 {
- eq.RealReason = fmt.Sprintf("%v", util.Round((1+((eq.MonthBudget/float64(sumDay)*float64(realDay))-(eq.SumPrice))/(eq.MonthBudget/float64(sumDay)*float64(realDay)))*100, 2)) + " %"
- } else {
- eq.RealReason = "0 %"
- }
- if eq.MonthBudget > 0 && eq.SumPrice > 0 && eq.CowSum > 0 {
- eq.OneCowSumPrice = util.Round(eq.SumPrice/float64(eq.CowSum/sumDay*realDay), 2)
- } else {
- eq.OneCowSumPrice = 0
- }
- }
- eq.MonthBudget = util.Round(eq.MonthBudget/10000, 2)
- eq.SumPrice = util.Round(eq.SumPrice/10000, 2)
- eq.QmonthBudget = util.Round(eq.QmonthBudget/10000, 2)
- eq.Asave = util.Round(eq.Asave/10000, 2)
- }
- return equipmentList
- }
- func (s *Service) GetChooseMonthWXByDepartment(pastureId, startTime, endTime string) ([]*http.EquipmentIndex, error) {
- startDate, _ := time.Parse("2006-01-02", startTime)
- endDate, _ := time.Parse("2006-01-02", endTime)
- departmentList := make([]*model.Department, 0)
- resp := make([]*http.EquipmentIndex, 0)
- err := s.d.DB.Where("pastureId = ? ", pastureId).Where("isPasture = 0 ").
- Where(" name not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')").Find(&departmentList).Error
- if err != nil {
- return resp, err
- }
- feetSumPriceList := make([]*home.ThisYearActual, 0)
- err = s.d.DB.Debug().Raw(`
- SELECT
- '维修费' feeType ,IFNULL(SUM( pu.sumPrice - pu.quitNumber * pu.price),0) sumPrice,bpu.pastureId,departmentId
- FROM
- partuse pu
- INNER JOIN bigpartuse bpu
- ON pu.bigId = bpu.id
- WHERE bpu.useType <> 3
- AND (
- pu.useTypeV = '维修'
- OR pu.useTypeV = '保养'
- ) AND (SELECT iscal FROM department d WHERE d.id=bpu.departmentId)= 0 and (SELECT name FROM department d WHERE d.id=bpu.departmentId)
- not in ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')
- AND bpu.receiveTime BETWEEN ? AND ?
- AND (bpu.pastureid = ? OR '' = ?) and pu.dflag != 1 group by departmentId,pastureId`, startDate.Format("2006-01-02"),
- endDate.AddDate(0, 0, 1).Format("2006-01-02"), pastureId, pastureId).Find(&feetSumPriceList).Error
- if err != nil {
- return resp, err
- }
- monthBudgetList := make([]*home.MonthBudget, 0)
- err = s.d.DB.Debug().Raw(`
- SELECT
- sum( monthBudget ) monthBudget,
- feeType,pastureId,month_budget.deptId
- FROM
- month_budget join pasture on pasture.id = month_budget.pastureId
- WHERE
- budgetMonth BETWEEN date_format(?,'%Y-%m') and date_format(?,'%Y-%m') and (pastureId = ? or ? ='') and vendor = 1 and feeType = '维修费'
- 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 ('饲养处','犊牛处','品控处','繁育处','兽医处','安环处','供应处')
- GROUP BY
- month_budget.deptId,feeType,pastureId `, startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), pastureId, pastureId).Find(&monthBudgetList).Error
- if err != nil {
- return resp, err
- }
- feedSum := new(model.FeedSumAndPasture)
- 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
- where feedDay between ? and ? and (fs.pastureId = ? or ? ='') group by fs.pastureId`, startDate.Format("2006-01-02"),
- endDate.Format("2006-01-02"), pastureId, pastureId).First(&feedSum).Error
- if err != nil {
- return resp, err
- }
- var sumDay, realDay int64
- sumDay = int64(endDate.Sub(startDate).Hours() / 24)
- if endDate.Unix() < time.Now().Unix() {
- realDay = sumDay
- } else {
- realDay = int64(time.Now().Sub(startDate).Hours() / 24)
- }
- for _, department := range departmentList {
- data := new(http.EquipmentIndex)
- data.DeptName = department.Name
- for _, monthBudget := range monthBudgetList {
- if department.ID == monthBudget.DeptId {
- data.MonthBudget = monthBudget.MonthBudget
- break
- }
- }
- for _, feetSum := range feetSumPriceList {
- if department.ID == feetSum.DepartmentId {
- data.SumPrice = feetSum.SumPrice
- break
- }
- }
- data.QmonthBudget = data.MonthBudget / float64(sumDay) * float64(realDay)
- data.Asave = data.MonthBudget - data.SumPrice
- if data.MonthBudget > 0 && data.SumPrice > 0 {
- data.RealReason = fmt.Sprintf("%v", util.Round((1+((data.MonthBudget/float64(sumDay)*float64(realDay))-(data.SumPrice))/(data.MonthBudget/float64(sumDay)*float64(realDay)))*100, 2)) + " %"
- } else {
- data.RealReason = "0 %"
- }
- data.CowSum = feedSum.CowNum
- if data.MonthBudget > 0 && data.SumPrice > 0 && data.CowSum > 0 {
- data.OneCowSumPrice = util.Round(data.SumPrice/float64(data.CowSum/sumDay*realDay), 2)
- } else {
- data.OneCowSumPrice = 0.0
- }
- resp = append(resp, data)
- }
- return resp, nil
- }
|