123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package backend
- import (
- "context"
- "kpt-pasture/model"
- "kpt-pasture/util"
- "net/http"
- "strconv"
- "strings"
- "time"
- "gitee.com/xuyiping_admin/pkg/logger/zaplog"
- "go.uber.org/zap"
- "gitee.com/xuyiping_admin/pkg/xerr"
- pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
- )
- func (s *StoreEntry) Bar(ctx context.Context) (*pasturePb.BarCowStructResponse, error) {
- barCowStructList := make([]*model.BarCowStruct, 0)
- var count int32 = 0
- if err := s.DB.Model(new(model.Cow)).
- Select("COUNT(*) AS number ,cow_type").
- Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
- Group("cow_type").
- Find(&barCowStructList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- cowTypeMap := s.CowTypeMap()
- for _, v := range barCowStructList {
- count += v.Number
- }
- return &pasturePb.BarCowStructResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &pasturePb.BarCowStructData{
- List: model.BarCowStructSlice(barCowStructList).ToPB(cowTypeMap, count),
- Total: 38563,
- },
- }, nil
- }
- func (s *StoreEntry) NeckRingWarning(ctx context.Context) (*pasturePb.IndexNeckRingResponse, error) {
- userModel, err := s.GetUserModel(ctx)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- estrusWarningCowList := make([]*model.NeckRingEstrus, 0)
- estrusWarningLevelItems := map[int32]int32{
- int32(pasturePb.EstrusLevel_Low): 0,
- int32(pasturePb.EstrusLevel_Middle): 0,
- int32(pasturePb.EstrusLevel_High): 0,
- }
- if err = s.DB.Model(new(model.EventEstrus)).
- Where("pasture_id = ?", userModel.AppPasture.Id).
- Where("is_show = ?", pasturePb.IsShow_Ok).
- Group("cow_id").Find(&estrusWarningCowList).Error; err != nil {
- zaplog.Error("NeckRingWarning", zap.Any("estrusWarningNumber", err))
- }
- for _, v := range estrusWarningCowList {
- estrusWarningLevelItems[int32(v.Level)] += estrusWarningLevelItems[int32(v.Level)]
- }
- healthWarningNumber := int64(0)
- if err = s.DB.Model(new(model.NeckRingHealthWarning)).
- Where("pasture_id = ?", userModel.AppPasture.Id).
- Where("is_show = ?", pasturePb.IsShow_Ok).
- Group("cow_id").
- Count(&healthWarningNumber).Error; err != nil {
- zaplog.Error("NeckRingWarning", zap.Any("estrusWarningNumber", err))
- }
- return &pasturePb.IndexNeckRingResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &pasturePb.NeckRingData{
- EstrusWarningNumber: int32(len(estrusWarningCowList)),
- HealthWarningNumber: int32(healthWarningNumber),
- AbortionWarningNumber: 0,
- StressWarningNumber: 0,
- EstrusWarningLevelItems: estrusWarningLevelItems,
- },
- }, nil
- }
- func (s *StoreEntry) FocusIndicators(ctx context.Context, dimension string) (*pasturePb.IndexFocusIndicatorsResponse, error) {
- userModel, err := s.GetUserModel(ctx)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- userFocusIndicators := userModel.SystemUser.IndicatorsKinds
- if len(userFocusIndicators) <= 0 {
- userFocusIndicators = "all_cow,output_number,input_number,fatten_cattle_number,sales_volume"
- }
- userFocusIndicatorsList := strings.Split(userFocusIndicators, ",")
- indicatorsDataList := make([]*model.IndicatorsData, 0)
- pref := s.DB.Model(new(model.IndicatorsData)).
- Where("pasture_id = ?", userModel.AppPasture.Id).
- Where("kind in (?)", userFocusIndicatorsList)
- /*if dimension == "Year" {
- pref.Where("date = ?", time.Now().Format(model.LayoutMonth))
- }*/
- nowTime := time.Now()
- if dimension == "Month" {
- pref.Where("date = ?", nowTime.Format(model.LayoutMonth))
- }
- if err = pref.Find(&indicatorsDataList).Error; err != nil {
- zaplog.Error("FocusIndicators", zap.Any("err", err))
- }
- indicatorsDetailsMap, err := s.GetIndicatorsDetailsMap(ctx)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- data := make([]*pasturePb.FocusData, 0)
- for _, v := range indicatorsDataList {
- indicatorsDetails, ok := indicatorsDetailsMap[v.Kind]
- if !ok {
- continue
- }
- onYear, onMonth := "", ""
- if dimension == "Year" {
- return nil, xerr.Custom("暂不支持该维度")
- }
- if dimension == "Month" {
- lastMonth := nowTime.AddDate(0, -1, 0).Format(model.LayoutMonth)
- oldIndicators, _ := s.GetIndicatorsDataByDate(userModel.AppPasture.Id, v.Kind, lastMonth)
- if oldIndicators != nil {
- if oldIndicators.Value != "" && oldIndicators.Value != "0" {
- oldValue, _ := strconv.ParseFloat(oldIndicators.Value, 64)
- currValue, _ := strconv.ParseFloat(v.Value, 64)
- onMonthValue := (oldValue - currValue) / oldValue * 100
- omv := util.RoundToTwoDecimals(onMonthValue)
- onMonth = strconv.FormatFloat(omv, 'f', 2, 64) + "%"
- }
- }
- }
- data = append(data, &pasturePb.FocusData{
- Kind: indicatorsDetails.Kind,
- Name: indicatorsDetails.Name,
- Value: v.Value,
- Describe: indicatorsDetails.Zh,
- UnitName: indicatorsDetails.Unit,
- OnMonth: onMonth,
- OnYear: onYear,
- })
- }
- return &pasturePb.IndexFocusIndicatorsResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: data,
- }, err
- }
|