123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- package crontab
- import (
- "errors"
- "fmt"
- "kpt-pasture/model"
- "time"
- "gorm.io/gorm"
- pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
- "gitee.com/xuyiping_admin/pkg/logger/zaplog"
- "go.uber.org/zap"
- )
- // FindPastureAllCow 查询所有牧场牛只总数
- func (e *Entry) FindPastureAllCow(pastureList []*model.AppPastureList) map[int64]string {
- res := make(map[int64]string)
- for _, pasture := range pastureList {
- var count int64
- if err := e.DB.Model(&model.Cow{}).
- Where("pasture_id = ?", pasture.Id).
- Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
- Count(&count).Error; err != nil {
- zaplog.Error("FindAllCow", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
- }
- res[pasture.Id] = fmt.Sprintf("%d", count)
- }
- return res
- }
- // FindCalvingInterval 产犊间隔
- func (e *Entry) FindCalvingInterval(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
- res := make(map[int64]string)
- for _, pasture := range pastureList {
- eventCalvingList := make([]*model.EventCalving, 0)
- if err := e.DB.Model(new(model.EventCalving)).
- Where("pasture_id = ?", pasture.Id).
- Where("status = ?", pasturePb.IsShow_Ok).
- Where("reality_day BETWEEN ? AND ?", startTime, endTime).
- Find(&eventCalvingList).Error; err != nil {
- zaplog.Error("FindCalvingInterval", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
- }
- cowInterval := make(map[int64]int64)
- allInterval := int64(0)
- for _, v := range eventCalvingList {
- preEventCalving := &model.EventCalving{}
- if err := e.DB.Model(new(model.EventCalving)).
- Where("pasture_id = ?", pasture.Id).
- Where("cow_id = ?", v.CowId).
- Where("status = ?", pasturePb.IsShow_Ok).
- Where("reality_day < ?", v.RealityDay).
- Order("reality_day DESC").
- First(&preEventCalving).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- continue
- } else {
- zaplog.Error("FindCalvingInterval", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
- }
- }
- if preEventCalving.RealityDay > 0 {
- interval := v.RealityDay - preEventCalving.RealityDay
- cowInterval[v.CowId] = interval
- allInterval += interval
- }
- }
- if len(cowInterval) > 0 {
- res[pasture.Id] = fmt.Sprintf("%d", int32(allInterval/int64(len(cowInterval))))
- }
- }
- return res
- }
- // FindOutputNumber 本月销售牛只
- func (e *Entry) FindOutputNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
- res := make(map[int64]string)
- for _, pasture := range pastureList {
- var count int64
- if err := e.DB.Model(new(model.EventSale)).
- Where("pasture_id = ?", pasture.Id).
- Where("sale_at BETWEEN ? AND ?", startTime, endTime).
- Count(&count).Error; err != nil {
- zaplog.Error("FindOutputNumber", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
- }
- res[pasture.Id] = fmt.Sprintf("%d", count)
- }
- return res
- }
- func (e *Entry) FindInputNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
- res := make(map[int64]string)
- for _, pasture := range pastureList {
- var count int64
- if err := e.DB.Model(new(model.EventEnter)).
- Where("pasture_id = ?", pasture.Id).
- Where("cow_source = ?", pasturePb.CowSource_Buy).
- Where("enter_at BETWEEN ? AND ?", startTime, endTime).
- Count(&count).Error; err != nil {
- zaplog.Error("FindInputNumber", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
- }
- res[pasture.Id] = fmt.Sprintf("%d", count)
- }
- return res
- }
- func (e *Entry) FindSalesVolume(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
- res := make(map[int64]string)
- for _, pasture := range pastureList {
- saleAllAmount := struct {
- SaleAllAmount int64 `json:"sale_all_amount"`
- }{
- SaleAllAmount: 0,
- }
- if err := e.DB.Model(new(model.EventSale)).
- Select("SUM(sale_all_amount) as sale_all_amount").
- Where("pasture_id = ?", pasture.Id).
- Where("sale_at BETWEEN ? AND ?", startTime, endTime).
- First(&saleAllAmount).Error; err != nil {
- zaplog.Error("FindSalesVolume", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
- }
- res[pasture.Id] = fmt.Sprintf("%d", saleAllAmount.SaleAllAmount)
- }
- return res
- }
- func (e *Entry) UpdatePastureIndicators(pastureId int64, indicatorsDetails *model.IndicatorsDetails, dateTime int64, value string) {
- date := time.Unix(dateTime, 0).Format(model.LayoutMonth)
- where := &model.IndicatorsData{
- PastureId: pastureId,
- Date: date,
- Kind: indicatorsDetails.Kind,
- }
- data := &model.IndicatorsData{
- PastureId: pastureId,
- CategoryType: indicatorsDetails.CategoryType,
- CategoryName: indicatorsDetails.CategoryName,
- Date: date,
- Kind: indicatorsDetails.Kind,
- Value: value,
- }
- var existData model.IndicatorsData
- if err := e.DB.Model(new(model.IndicatorsData)).
- Where(where).First(&existData).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- if err = e.DB.Model(new(model.IndicatorsData)).Create(data).Error; err != nil {
- zaplog.Error("UpdatePastureIndicators", zap.Any("Create", err))
- }
- } else {
- zaplog.Error("UpdatePastureIndicators", zap.Any("Find", err))
- return
- }
- }
- if err := e.DB.Model(new(model.IndicatorsData)).
- Where("id = ?", existData.Id).
- Update("value", value).Error; err != nil {
- zaplog.Error("UpdatePastureIndicators", zap.Any("Update", err))
- }
- }
|