1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120 |
- package backend
- import (
- "bytes"
- "context"
- "errors"
- "fmt"
- "io"
- "kpt-tmr-group/model"
- "kpt-tmr-group/pkg/logger/zaplog"
- "kpt-tmr-group/pkg/tool"
- "kpt-tmr-group/pkg/xerr"
- operationPb "kpt-tmr-group/proto/go/backend/operation"
- "net/http"
- "strconv"
- "sync"
- "go.uber.org/zap"
- "github.com/xuri/excelize/v2"
- "gorm.io/gorm"
- )
- // CreateGroupPasture 创建集团牧场
- func (s *StoreEntry) CreateGroupPasture(ctx context.Context, req *operationPb.AddPastureRequest) error {
- // 账号同步牧场端
- groupPasture := model.NewGroupPasture(req)
- if err := s.DB.Create(groupPasture).Error; err != nil {
- return xerr.WithStack(err)
- }
- if err := s.PastureAccountDistribution(ctx, groupPasture); err != nil {
- s.DB.Model(new(model.GroupPasture)).Where("id = ?", groupPasture.Id).Update("is_delete", operationPb.IsShow_NO)
- return xerr.WithStack(err)
- } else {
- s.DB.Model(new(model.GroupPasture)).Where("id = ?", groupPasture.Id).Update("is_distribution", operationPb.IsShow_OK)
- }
- return nil
- }
- // PastureAccountDistribution 账号同步牧场端
- func (s *StoreEntry) PastureAccountDistribution(ctx context.Context, groupPasture *model.GroupPasture) error {
- if groupPasture.Domain == "" {
- return nil
- }
- body := &model.AccountDistribution{
- Account: groupPasture.Account,
- UserName: groupPasture.ManagerUser,
- Password: groupPasture.Password,
- Phone: groupPasture.ManagerPhone,
- PastureId: int32(groupPasture.Id),
- PastureName: groupPasture.Name,
- Address: groupPasture.Address,
- }
- res := &model.PastureCommonResponse{}
- if err := s.PastureHttpClient(ctx, model.PastureAccountDistributionURl, groupPasture.Id, body, res); err != nil {
- zaplog.Error("AccountDistribution", zap.Any("url", model.PastureAccountDistributionURl), zap.Any("err", err), zap.Any("body", body), zap.Any("res", res))
- return xerr.WithStack(err)
- }
- if res.Code != http.StatusOK {
- if err := s.DB.Model(new(model.GroupPasture)).Where("id = ?", groupPasture.Id).Update("is_distribution", operationPb.IsShow_OK).Error; err != nil {
- zaplog.Error("AccountDistribution-Update", zap.Any("url", model.PastureAccountDistributionURl), zap.Any("err", err), zap.Any("body", body), zap.Any("res", res))
- return xerr.Customf("%s", res.Msg)
- }
- }
- return nil
- }
- func (s *StoreEntry) FindGroupPastureListByIds(ctx context.Context, pastureIds []int32) ([]*model.GroupPasture, error) {
- groupPastureList := make([]*model.GroupPasture, 0)
- if err := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK).
- Where("id IN ?", pastureIds).
- Find(&groupPastureList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return groupPastureList, nil
- }
- func (s *StoreEntry) GetGroupPastureListById(ctx context.Context, pastureId int64) (*model.GroupPasture, error) {
- groupPasture := &model.GroupPasture{
- Id: int64(pastureId),
- }
- if err := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK).
- First(&groupPasture).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return groupPasture, nil
- }
- // EditGroupPasture 创建集团牧场
- func (s *StoreEntry) EditGroupPasture(ctx context.Context, req *operationPb.AddPastureRequest) error {
- groupPasture := &model.GroupPasture{Id: int64(req.Id)}
- if err := s.DB.First(groupPasture).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.Custom("该数据不存在!")
- }
- return xerr.WithStack(err)
- }
- updateData := &model.GroupPasture{
- Name: req.Name,
- Account: req.Account,
- ManagerUser: req.ManagerUser,
- ManagerPhone: req.ManagerPhone,
- Address: req.Address,
- Domain: req.Domain,
- }
- if err := s.DB.Model(new(model.GroupPasture)).Omit("is_show", "password").
- Where("id = ?", req.Id).
- Updates(updateData).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- // SearchGroupPastureList 查询牧场列表
- func (s *StoreEntry) SearchGroupPastureList(ctx context.Context, req *operationPb.SearchPastureRequest) (*operationPb.SearchPastureResponse, error) {
- groupPasture := make([]*model.GroupPasture, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK)
- if req.Name != "" {
- pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
- }
- if req.ManagerPhone != "" {
- pref.Where("manager_phone like ?", fmt.Sprintf("%s%s%s", "%", req.ManagerPhone, "%"))
- }
- if req.ManagerUser != "" {
- pref.Where("manager_user like ?", fmt.Sprintf("%s%s%s", "%", req.ManagerUser, "%"))
- }
- if req.StartTime > 0 && req.EndTime > 0 && req.EndTime >= req.StartTime {
- pref.Where("created_at BETWEEN ? AND ? ", req.StartTime, req.EndTime)
- }
- if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
- Find(&groupPasture).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &operationPb.SearchPastureResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &operationPb.SearchPastureData{
- Page: req.Pagination.Page,
- Total: int32(count),
- PageSize: req.Pagination.PageSize,
- List: model.GroupPastureSlice(groupPasture).ToPB(),
- },
- }, nil
- }
- func (s *StoreEntry) GroupPastureList(ctx context.Context) ([]*model.GroupPasture, error) {
- groupPastureList := make([]*model.GroupPasture, 0)
- if err := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK).Find(&groupPastureList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return groupPastureList, nil
- }
- func (s *StoreEntry) DeleteGroupPasture(ctx context.Context, pastureId int64) error {
- groupPasture := &model.GroupPasture{
- Id: pastureId,
- }
- if err := s.DB.First(groupPasture).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.Custom("该数据不存在")
- }
- return xerr.WithStack(err)
- }
- if err := s.DB.Model(groupPasture).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- func (s *StoreEntry) ResetPasswordGroupPasture(ctx context.Context, pastureId int64) error {
- groupPasture := &model.GroupPasture{
- Id: pastureId,
- }
- if err := s.DB.Where("is_delete = ?", operationPb.IsShow_OK).First(groupPasture).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.Custom("该数据不存在")
- }
- return xerr.WithStack(err)
- }
- if err := s.DB.Model(groupPasture).Update("password", tool.Md5String(model.InitManagerPassword)).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- func (s *StoreEntry) IsShowGroupPasture(ctx context.Context, req *operationPb.IsShowGroupPasture) error {
- groupPasture := &model.GroupPasture{
- Id: int64(req.PastureId),
- }
- if err := s.DB.First(groupPasture).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.Custom("该数据不存在")
- }
- return xerr.WithStack(err)
- }
- if err := s.DB.Model(groupPasture).Where("is_delete = ?", operationPb.IsShow_OK).Update("is_show", req.IsShow).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- // AddCattleCategory 添加畜牧分类
- func (s *StoreEntry) AddCattleCategory(ctx context.Context, req *operationPb.AddCattleCategoryRequest) error {
- cattleCategory := model.NewCattleCategory(req)
- if err := s.DB.Create(cattleCategory).Error; err != nil {
- return xerr.WithStack(err)
- } else {
- body := &model.CategoryRequest{
- ParentId: int32(req.ParentId),
- ParentName: req.ParentName,
- Name: req.Name,
- Id: int32(req.Id),
- Number: req.Number,
- IsShow: int32(req.IsShow),
- GroupId: int32(cattleCategory.Id),
- }
- if err = s.CategoryDistribution(ctx, model.CattleCategoryDistributionURl, body); err != nil {
- zaplog.Error("AddCattleCategory", zap.Any("CategoryDistribution", err))
- }
- }
- return nil
- }
- // EditCattleCategory 编辑畜牧分类
- func (s *StoreEntry) EditCattleCategory(ctx context.Context, req *operationPb.AddCattleCategoryRequest) error {
- cattleCategory := &model.CattleCategory{Id: int64(req.Id)}
- if err := s.DB.First(cattleCategory).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.Custom("该数据不存在")
- }
- return xerr.WithStack(err)
- }
- updateData := &model.CattleCategory{
- ParentName: req.ParentName,
- Name: req.Name,
- Number: req.Number,
- ParentId: req.ParentId,
- }
- if err := s.DB.Model(new(model.CattleCategory)).Omit("is_show", "is_delete").
- Where("id = ?", req.Id).
- Updates(updateData).Error; err != nil {
- return xerr.WithStack(err)
- } else {
- body := &model.CategoryRequest{
- ParentId: int32(req.ParentId),
- ParentName: req.ParentName,
- Name: req.Name,
- Id: int32(req.Id),
- Number: req.Number,
- IsShow: int32(req.IsShow),
- GroupId: int32(req.Id),
- }
- if err = s.CategoryDistribution(ctx, model.CattleCategoryDistributionURl, body); err != nil {
- zaplog.Error("EditCattleCategory", zap.Any("CategoryDistribution", err))
- }
- }
- return nil
- }
- // IsShowCattleCategory 是否启用
- func (s *StoreEntry) IsShowCattleCategory(ctx context.Context, req *operationPb.IsShowCattleCategory) error {
- cattleCategory := &model.CattleCategory{Id: int64(req.CattleCategoryId)}
- if err := s.DB.First(cattleCategory).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.Custom("该数据不存在")
- }
- return xerr.WithStack(err)
- }
- if err := s.DB.Model(new(model.CattleCategory)).Where("id = ?", req.CattleCategoryId).Update("is_show", req.IsShow).Error; err != nil {
- return xerr.WithStack(err)
- } else {
- body := &model.CategoryRequest{
- ParentId: int32(cattleCategory.ParentId),
- ParentName: cattleCategory.ParentName,
- Name: cattleCategory.Name,
- Id: req.CattleCategoryId,
- Number: cattleCategory.Number,
- IsShow: int32(req.IsShow),
- GroupId: int32(cattleCategory.Id),
- }
- if err = s.CategoryDistribution(ctx, model.CattleCategoryDistributionURl, body); err != nil {
- zaplog.Error("IsShowCattleCategory", zap.Any("CategoryDistribution", err), zap.Any("body", body))
- }
- }
- return nil
- }
- // DeleteCattleCategory 是否删除
- func (s *StoreEntry) DeleteCattleCategory(ctx context.Context, cattleCategoryId int64) error {
- cattleCategory := &model.CattleCategory{Id: cattleCategoryId}
- if err := s.DB.First(cattleCategory).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.Custom("该数据不存在")
- }
- return xerr.WithStack(err)
- }
- if err := s.DB.Model(new(model.CattleCategory)).Where("id = ?", cattleCategoryId).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
- return xerr.WithStack(err)
- } else {
- body := &model.CategoryDeleteRequest{
- GroupId: int32(cattleCategory.Id),
- IsDelete: int32(operationPb.IsShow_OK),
- }
- if err = s.CategoryDelete(ctx, model.CattleCategoryDeleteURl, body); err != nil {
- zaplog.Error("DeleteCattleCategory", zap.Any("CategoryDelete", err), zap.Any("body", body))
- }
- }
- return nil
- }
- // SearchCattleCategoryList 牧畜分类类别列表
- func (s *StoreEntry) SearchCattleCategoryList(ctx context.Context, req *operationPb.SearchCattleCategoryRequest) (*operationPb.SearchCattleCategoryResponse, error) {
- cattleCategory := make([]*model.CattleCategory, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.CattleCategory)).Where("is_delete = ?", operationPb.IsShow_OK)
- if req.Name != "" {
- pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
- }
- if req.ParentId > 0 {
- pref.Where("parent_id = ?", req.ParentId)
- }
- if req.IsShow > 0 {
- pref.Where("is_show = ?", req.IsShow)
- }
- if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
- Find(&cattleCategory).Debug().Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &operationPb.SearchCattleCategoryResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &operationPb.SearchCattleCategoryData{
- Page: req.Pagination.Page,
- PageSize: req.Pagination.PageSize,
- Total: int32(count),
- List: model.CattleCategorySlice(cattleCategory).ToPB(),
- },
- }, nil
- }
- // AddForageCategory 添加饲料分类
- func (s *StoreEntry) AddForageCategory(ctx context.Context, req *operationPb.AddForageCategoryRequest) error {
- forageCategory := model.NewForageCategory(req)
- if err := s.DB.Create(forageCategory).Error; err != nil {
- return xerr.WithStack(err)
- } else {
- body := &model.CategoryRequest{
- ParentId: int32(req.ParentId),
- ParentName: req.ParentName,
- Name: req.Name,
- Id: int32(req.Id),
- Number: req.Number,
- IsShow: int32(req.IsShow),
- GroupId: int32(forageCategory.Id),
- }
- if err = s.CategoryDistribution(ctx, model.ForageCategoryDistributionURl, body); err != nil {
- zaplog.Error("AddForageCategory", zap.Any("CategoryDistribution", err), zap.Any("body", body))
- }
- }
- return nil
- }
- // EditForageCategory 编辑饲料分类
- func (s *StoreEntry) EditForageCategory(ctx context.Context, req *operationPb.AddForageCategoryRequest) error {
- forageCategory := &model.ForageCategory{Id: int64(req.Id)}
- if err := s.DB.First(forageCategory).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.Custom("该数据不存在")
- }
- return xerr.WithStack(err)
- }
- updateData := &model.ForageCategory{
- ParentName: req.ParentName,
- Name: req.Name,
- Number: req.Number,
- ParentId: req.ParentId,
- }
- if err := s.DB.Model(new(model.ForageCategory)).Omit("is_show", "is_delete").
- Where("id = ?", req.Id).
- Updates(updateData).Error; err != nil {
- return xerr.WithStack(err)
- } else {
- body := &model.CategoryRequest{
- ParentId: int32(req.ParentId),
- ParentName: req.ParentName,
- Name: req.Name,
- Id: int32(req.Id),
- Number: req.Number,
- IsShow: int32(req.IsShow),
- GroupId: int32(req.Id),
- }
- if err = s.CategoryDistribution(ctx, model.ForageCategoryDistributionURl, body); err != nil {
- zaplog.Error("EditForageCategory", zap.Any("CategoryDistribution", err), zap.Any("body", body))
- }
- }
- return nil
- }
- // IsShowForageCategory 是否启用
- func (s *StoreEntry) IsShowForageCategory(ctx context.Context, req *operationPb.IsShowForageCategory) error {
- forageCategory := &model.ForageCategory{Id: int64(req.ForageCategoryId)}
- if err := s.DB.First(forageCategory).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.Custom("该数据不存在")
- }
- return xerr.WithStack(err)
- }
- if err := s.DB.Model(new(model.ForageCategory)).Where("id = ?", req.ForageCategoryId).Update("is_show", req.IsShow).Error; err != nil {
- return xerr.WithStack(err)
- } else {
- body := &model.CategoryRequest{
- ParentId: int32(forageCategory.ParentId),
- ParentName: forageCategory.ParentName,
- Name: forageCategory.Name,
- Id: req.ForageCategoryId,
- Number: forageCategory.Number,
- IsShow: int32(req.IsShow),
- GroupId: int32(forageCategory.Id),
- }
- if err = s.CategoryDistribution(ctx, model.ForageCategoryDistributionURl, body); err != nil {
- zaplog.Error("IsShowForageCategory", zap.Any("CategoryDistribution", err), zap.Any("body", body))
- }
- }
- return nil
- }
- // DeleteForageCategory 是否删除
- func (s *StoreEntry) DeleteForageCategory(ctx context.Context, forageCategoryId int64) error {
- forageCategory := &model.ForageCategory{Id: forageCategoryId}
- if err := s.DB.First(forageCategory).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.Custom("该数据不存在")
- }
- return xerr.WithStack(err)
- }
- if err := s.DB.Model(new(model.ForageCategory)).Where("id = ?", forageCategoryId).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
- return xerr.WithStack(err)
- } else {
- body := &model.CategoryDeleteRequest{
- GroupId: int32(forageCategory.Id),
- IsDelete: int32(operationPb.IsShow_OK),
- }
- if err = s.CategoryDelete(ctx, model.CattleCategoryDeleteURl, body); err != nil {
- zaplog.Error("DeleteForageCategory", zap.Any("CategoryDelete", err), zap.Any("body", body))
- }
- }
- return nil
- }
- // SearchForageCategoryList 饲料分类类别列表
- func (s *StoreEntry) SearchForageCategoryList(ctx context.Context, req *operationPb.SearchForageCategoryRequest) (*operationPb.SearchForageCategoryResponse, error) {
- forageCategory := make([]*model.ForageCategory, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.ForageCategory)).Where("is_delete = ?", operationPb.IsShow_OK)
- if req.Name != "" {
- pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
- }
- if req.ParentId > 0 {
- pref.Where("parent_id = ?", req.ParentId)
- }
- if req.Number != "" {
- pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
- }
- if req.IsShow > 0 {
- pref.Where("is_show = ?", req.IsShow)
- }
- if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
- Find(&forageCategory).Debug().Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &operationPb.SearchForageCategoryResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &operationPb.SearchForageCategoryData{
- Page: req.Pagination.Page,
- PageSize: req.Pagination.PageSize,
- Total: int32(count),
- List: model.ForageCategorySlice(forageCategory).ToPB(),
- },
- }, nil
- }
- // CategoryDistribution 饲料分类和畜牧分类下发
- func (s *StoreEntry) CategoryDistribution(ctx context.Context, url string, req *model.CategoryRequest) error {
- groupList, err := s.GroupPastureList(ctx)
- if err != nil {
- return xerr.WithStack(err)
- }
- wg := sync.WaitGroup{}
- wg.Add(len(groupList))
- for _, v := range groupList {
- go func(data *model.GroupPasture) {
- res := &model.PastureCommonResponse{}
- req.PastureId = int32(data.Id)
- if err = s.PastureHttpClient(ctx, url, int64(data.Id), req, res); err != nil {
- zaplog.Error("CategoryDistribution",
- zap.Any("url", url),
- zap.Any("err", err),
- zap.Any("body", req),
- zap.Any("res", res),
- )
- }
- if res.Code != http.StatusOK {
- zaplog.Error("CategoryDistribution-http",
- zap.Any("url", url),
- zap.Any("body", req),
- zap.Any("res", res),
- )
- }
- wg.Done()
- }(v)
- }
- wg.Wait()
- return nil
- }
- // CategoryDelete 饲料分类和畜牧分类删除
- func (s *StoreEntry) CategoryDelete(ctx context.Context, url string, req *model.CategoryDeleteRequest) error {
- groupList, err := s.GroupPastureList(ctx)
- if err != nil {
- return xerr.WithStack(err)
- }
- wg := sync.WaitGroup{}
- wg.Add(len(groupList))
- for _, v := range groupList {
- go func(data *model.GroupPasture) {
- res := &model.PastureCommonResponse{}
- req.PastureId = int32(data.Id)
- if err = s.PastureHttpClient(ctx, url, int64(data.Id), req, res); err != nil {
- zaplog.Error("CategoryDistribution",
- zap.Any("url", url),
- zap.Any("err", err),
- zap.Any("body", req),
- zap.Any("res", res),
- )
- }
- if res.Code != http.StatusOK {
- zaplog.Error("CategoryDistribution-http",
- zap.Any("url", url),
- zap.Any("body", req),
- zap.Any("res", res),
- )
- }
- wg.Done()
- }(v)
- }
- wg.Wait()
- return nil
- }
- // CreateForage 创建饲料
- func (s *StoreEntry) CreateForage(ctx context.Context, req *operationPb.AddForageRequest) error {
- forage := model.NewForage(req)
- if err := s.DB.Create(forage).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- // EditForage 编辑饲料
- func (s *StoreEntry) EditForage(ctx context.Context, req *operationPb.AddForageRequest) error {
- forage := model.Forage{Id: int64(req.Id)}
- if err := s.DB.Where("is_delete = ?", operationPb.IsShow_OK).First(&forage).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.Custom("该数据不存在")
- }
- return xerr.WithStack(err)
- }
- updateData := &model.Forage{
- Name: req.Name,
- CategoryId: int64(req.CategoryId),
- CategoryName: req.CategoryName,
- MaterialType: req.MaterialType,
- UniqueEncode: req.UniqueEncode,
- ForageSourceId: req.ForageSourceId,
- PlanTypeId: req.PlanTypeId,
- SmallMaterialScale: req.SmallMaterialScale,
- AllowError: int64(req.AllowError),
- PackageWeight: int64(req.PackageWeight),
- Price: int64(req.Price),
- JumpWeight: int64(req.JumpWeight),
- JumpDelay: req.JumpDelay,
- ConfirmStart: req.ConfirmStart,
- RelayLocations: int64(req.RelayLocations),
- Jmp: req.Jmp,
- Backup1: req.Backup1,
- Backup2: req.Backup2,
- Backup3: req.Backup3,
- }
- if err := s.DB.Model(new(model.Forage)).Omit("is_show", "is_delete").Where("id = ?", req.Id).
- Updates(updateData).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- func (s *StoreEntry) SearchForageList(ctx context.Context, req *operationPb.SearchForageListRequest) (*operationPb.SearchForageListResponse, error) {
- forage := make([]*model.Forage, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.Forage)).Where("is_delete = ?", operationPb.IsShow_OK)
- if req.Name != "" {
- pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
- }
- if req.CategoryId > 0 {
- pref.Where("category_id = ?", req.CategoryId)
- }
- if req.ForageSourceId > 0 {
- pref.Where("forage_source_id = ?", req.ForageSourceId)
- }
- if req.IsShow > 0 {
- pref.Where("is_show = ?", req.IsShow)
- }
- if req.AllowError > 0 {
- pref.Where("allow_error = ?", req.AllowError)
- }
- if req.JumpWeight > 0 {
- pref.Where("jump_weight = ?", req.JumpWeight)
- }
- if req.JumpDelay > 0 {
- pref.Where("jump_delay = ?", req.JumpDelay)
- }
- if err := pref.Order("sort").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
- Find(&forage).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &operationPb.SearchForageListResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &operationPb.SearchForageList{
- Page: req.Pagination.Page,
- PageSize: req.Pagination.PageSize,
- Total: int32(count),
- List: model.ForageSlice(forage).ToPB(),
- },
- }, nil
- }
- func (s *StoreEntry) ForageListSort(ctx context.Context, req *operationPb.ForageListSortRequest) error {
- for _, v := range req.List {
- if err := s.DB.Model(new(model.Forage)).Select("sort").Where("id = ?", v.Id).Updates(map[string]interface{}{
- "sort": v.Sort,
- }).Error; err != nil {
- zaplog.Error("ForageListSort", zap.Any("err", err), zap.Any("id", v.Id), zap.Any("sort", v.Sort))
- }
- }
- return nil
- }
- func (s *StoreEntry) ForageEnumList(ctx context.Context) *operationPb.ForageEnumListResponse {
- res := &operationPb.ForageEnumListResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &operationPb.ForageEnumList{
- JumpDelaType: make([]*operationPb.JumpDelaTypeEnum, 0),
- ForageSource: make([]*operationPb.ForageSourceEnum, 0),
- ForagePlanType: make([]*operationPb.ForagePlanTypeEnum, 0),
- CattleParentCategory: make([]*operationPb.CattleParentCategoryEnum, 0),
- ForageParentCategory: make([]*operationPb.ForageParentCategoryEnum, 0),
- IsShow: make([]*operationPb.IsShowEnum, 0),
- FormulaType: make([]*operationPb.FormulaTypeEnum, 0),
- FormulaList: make([]*operationPb.FormulaOptionEnum, 0),
- ConfirmStart: make([]*operationPb.IsShowEnum, 0),
- FormulationEvaluation: make([]*operationPb.FormulaOptionEnum, 0),
- UseMaterialsType: make([]*operationPb.FormulaOptionEnum, 0),
- UseMaterialsList: make([]*operationPb.FormulaOptionEnum, 0),
- PriceMaterialsType: make([]*operationPb.FormulaOptionEnum, 0),
- },
- }
- // 跳转延迟
- res.Data.JumpDelaType = append(res.Data.JumpDelaType, &operationPb.JumpDelaTypeEnum{
- Value: operationPb.JumpDelaType_INVALID,
- Label: "禁用",
- }, &operationPb.JumpDelaTypeEnum{
- Value: operationPb.JumpDelaType_THREE,
- Label: "3秒",
- }, &operationPb.JumpDelaTypeEnum{
- Value: operationPb.JumpDelaType_SIX,
- Label: "6秒",
- })
- // 饲料来源
- res.Data.ForageSource = append(res.Data.ForageSource, &operationPb.ForageSourceEnum{
- Value: operationPb.ForageSource_SYSTEM_BUILT_IN,
- Label: "系统内置",
- }, &operationPb.ForageSourceEnum{
- Value: operationPb.ForageSource_USER_DEFINED,
- Label: "用户自定义",
- })
- // 计划类型
- res.Data.ForagePlanType = append(res.Data.ForagePlanType,
- &operationPb.ForagePlanTypeEnum{
- Value: operationPb.ForagePlanType_INVALID,
- Label: "无",
- },
- &operationPb.ForagePlanTypeEnum{
- Value: operationPb.ForagePlanType_FORKLIFT,
- Label: "铲车",
- }, &operationPb.ForagePlanTypeEnum{
- Value: operationPb.ForagePlanType_CONCENTRATE,
- Label: "精料",
- })
- // 畜牧类别
- res.Data.CattleParentCategory = append(res.Data.CattleParentCategory, &operationPb.CattleParentCategoryEnum{
- Value: operationPb.CattleCategoryParent_LACTATION_CAW,
- Label: "泌乳牛",
- }, &operationPb.CattleParentCategoryEnum{
- Value: operationPb.CattleCategoryParent_FATTEN_CAW,
- Label: "育肥牛",
- }, &operationPb.CattleParentCategoryEnum{
- Value: operationPb.CattleCategoryParent_RESERVE_CAW,
- Label: "后备牛",
- }, &operationPb.CattleParentCategoryEnum{
- Value: operationPb.CattleCategoryParent_DRY_CAW,
- Label: "干奶牛",
- }, &operationPb.CattleParentCategoryEnum{
- Value: operationPb.CattleCategoryParent_PERINATAL_CAW,
- Label: "围产牛",
- }, &operationPb.CattleParentCategoryEnum{
- Value: operationPb.CattleCategoryParent_OTHER_CAW,
- Label: "其他",
- })
- // 饲料类别
- res.Data.ForageParentCategory = append(res.Data.ForageParentCategory, &operationPb.ForageParentCategoryEnum{
- Value: operationPb.ForageCategoryParent_ROUGHAGE,
- Label: "粗料",
- }, &operationPb.ForageParentCategoryEnum{
- Value: operationPb.ForageCategoryParent_CONCENTRATE,
- Label: "精料",
- }, &operationPb.ForageParentCategoryEnum{
- Value: operationPb.ForageCategoryParent_HALF_ROUGHAGE_HALF_CONCENTRATE,
- Label: "粗料精料参半",
- }, &operationPb.ForageParentCategoryEnum{
- Value: operationPb.ForageCategoryParent_OTHER,
- Label: "其他",
- })
- res.Data.IsShow = append(res.Data.IsShow, &operationPb.IsShowEnum{
- Value: operationPb.IsShow_OK,
- Label: "是",
- }, &operationPb.IsShowEnum{
- Value: operationPb.IsShow_NO,
- Label: "否",
- })
- res.Data.FormulationEvaluation = append(res.Data.FormulationEvaluation, &operationPb.FormulaOptionEnum{
- Value: 0,
- Label: "按配方查询",
- }, &operationPb.FormulaOptionEnum{
- Value: 1,
- Label: "按栏舍查询",
- })
- res.Data.FormulaType = append(res.Data.FormulaType, &operationPb.FormulaTypeEnum{
- Value: operationPb.FormulaType_FEED_FORMULA,
- Label: "饲喂配方",
- }, &operationPb.FormulaTypeEnum{
- Value: operationPb.FormulaType_PREMIXED_FORMULA,
- Label: "预混配方",
- }, &operationPb.FormulaTypeEnum{
- Value: operationPb.FormulaType_SUPPLEMENTARY_FORMULA,
- Label: "补料配方",
- })
- res.Data.ConfirmStart = append(res.Data.ConfirmStart, &operationPb.IsShowEnum{
- Value: operationPb.IsShow_OK,
- Label: "启用",
- }, &operationPb.IsShowEnum{
- Value: operationPb.IsShow_NO,
- Label: "禁用",
- })
- res.Data.UseMaterialsList = append(res.Data.UseMaterialsList, &operationPb.FormulaOptionEnum{
- Value: 1,
- Label: "理论",
- }, &operationPb.FormulaOptionEnum{
- Value: 2,
- Label: "实际",
- })
- res.Data.PriceMaterialsType = append(res.Data.PriceMaterialsType, &operationPb.FormulaOptionEnum{
- Value: 1,
- Label: "畜牧类别",
- }, &operationPb.FormulaOptionEnum{
- Value: 2,
- Label: "栏舍名称",
- }, &operationPb.FormulaOptionEnum{
- Value: 3,
- Label: "日期",
- }, &operationPb.FormulaOptionEnum{
- Value: 4,
- Label: "TMR设备号",
- })
- res.Data.UseMaterialsType = append(res.Data.PriceMaterialsType, &operationPb.FormulaOptionEnum{
- Value: 5,
- Label: "TMR班次",
- }, &operationPb.FormulaOptionEnum{
- Value: 6,
- Label: "车次",
- })
- res.Data.JumpType = append(res.Data.JumpType, &operationPb.FormulaOptionEnum{
- Value: 0,
- Label: "手动跳转",
- }, &operationPb.FormulaOptionEnum{
- Value: 1,
- Label: "自动跳转",
- })
- res.Data.StatisticsType = append(res.Data.StatisticsType, &operationPb.FormulaOptionEnum{
- Value: 7,
- Label: "无分类",
- }, &operationPb.FormulaOptionEnum{
- Value: 0,
- Label: "驾驶员",
- }, &operationPb.FormulaOptionEnum{
- Value: 1,
- Label: "配方名称",
- }, &operationPb.FormulaOptionEnum{
- Value: 2,
- Label: "栏舍名称",
- }, &operationPb.FormulaOptionEnum{
- Value: 3,
- Label: "牧畜类别",
- }, &operationPb.FormulaOptionEnum{
- Value: 4,
- Label: "车次",
- }, &operationPb.FormulaOptionEnum{
- Value: 5,
- Label: "TMR名称",
- }, &operationPb.FormulaOptionEnum{
- Value: 6,
- Label: "饲料",
- })
- res.Data.FormulaList = append(res.Data.FormulaList, &operationPb.FormulaOptionEnum{
- Value: 0,
- Label: "所有",
- })
- formulaList := make([]*model.FeedFormula, 0)
- if err := s.DB.Where("is_delete = ?", operationPb.IsShow_OK).Find(&formulaList).Error; err != nil {
- zaplog.Error("OptionFormula", zap.Any("Err", err))
- return res
- }
- for _, v := range formulaList {
- res.Data.FormulaList = append(res.Data.FormulaList, &operationPb.FormulaOptionEnum{
- Value: int32(v.Id),
- Label: v.Name,
- })
- }
- return res
- }
- func (s *StoreEntry) DeleteForageList(ctx context.Context, ids []int64) error {
- if len(ids) == 0 {
- return xerr.Custom("参数错误")
- }
- if err := s.DB.Model(new(model.Forage)).Where("id IN ?", ids).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- func (s *StoreEntry) IsShowForage(ctx context.Context, req *operationPb.IsShowForage) error {
- forage := &model.Forage{Id: int64(req.ForageId)}
- if err := s.DB.First(forage).Error; err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.Custom("该数据不存在")
- }
- return xerr.WithStack(err)
- }
- if err := s.DB.Model(new(model.Forage)).Where("id = ?", req.ForageId).Update("is_show", req.IsShow).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- // ExcelImportForage 导入excel
- func (s *StoreEntry) ExcelImportForage(ctx context.Context, req io.Reader) error {
- xlsx, err := excelize.OpenReader(req)
- if err != nil {
- return xerr.WithStack(err)
- }
- defer xlsx.Close()
- rows, err := xlsx.GetRows(xlsx.GetSheetName(xlsx.GetActiveSheetIndex()))
- if err != nil {
- return xerr.WithStack(err)
- }
- if len(rows) > 10000 {
- rows = rows[:10000]
- }
- forageList := make([]*model.Forage, 0)
- for i, row := range rows {
- if i == 0 {
- continue
- }
- var (
- forageSourceId int32
- categoryId, price, jumpWeight, relayLocations int
- allowError, packageWeight int
- name, uniqueEncode, backup1, backup2, backup3 string
- )
- for k, v := range row {
- if k == 0 {
- name = v
- }
- if k == 2 {
- uniqueEncode = v
- }
- if k == 3 {
- forageSourceId = operationPb.ForageSource_Kind_value[v]
- }
- if k == 5 {
- allowError, _ = strconv.Atoi(v)
- }
- if k == 6 {
- packageWeight, _ = strconv.Atoi(v)
- }
- if k == 7 {
- price, _ = strconv.Atoi(v)
- }
- if k == 8 {
- jumpWeight, _ = strconv.Atoi(v)
- }
- if k == 11 {
- relayLocations, _ = strconv.Atoi(v)
- }
- if k == 13 {
- backup1 = v
- }
- if k == 14 {
- backup2 = v
- }
- if k == 15 {
- backup3 = v
- }
- }
- forageItem := &model.Forage{
- Name: name,
- CategoryId: int64(categoryId),
- MaterialType: 0,
- UniqueEncode: uniqueEncode,
- ForageSourceId: operationPb.ForageSource_Kind(forageSourceId),
- PlanTypeId: 0,
- SmallMaterialScale: "",
- AllowError: int64(allowError),
- PackageWeight: int64(packageWeight),
- Price: int64(price),
- JumpWeight: int64(jumpWeight),
- JumpDelay: 0,
- ConfirmStart: 0,
- RelayLocations: int64(relayLocations),
- Jmp: 0,
- Backup1: backup1,
- Backup2: backup2,
- Backup3: backup3,
- IsShow: operationPb.IsShow_OK,
- IsDelete: operationPb.IsShow_OK,
- DataSource: operationPb.DataSource_EXCEL_IMPORT,
- }
- forageList = append(forageList, forageItem)
- }
- if len(forageList) > 0 {
- if err = s.DB.Create(forageList).Error; err != nil {
- return xerr.WithStack(err)
- }
- }
- return nil
- }
- // ExcelExportForage 流式导出excel
- func (s *StoreEntry) ExcelExportForage(ctx context.Context, req *operationPb.SearchForageListRequest) (*bytes.Buffer, error) {
- res, err := s.SearchForageList(ctx, req)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- if len(res.Data.List) <= 0 {
- return nil, xerr.Custom("数据为空")
- }
- file := excelize.NewFile()
- defer file.Close()
- streamWriter, err := file.NewStreamWriter(model.DefaultSheetName)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- titles := []interface{}{"饲料名称", "饲料分类", "唯一编码", "饲料来源", "计划类型", "允许误差(kg)", "包装单位重量(kg)",
- "单价", "跳转重量域(kg)", "跳转延迟", "确认开始", "继电器位置", "无上域", "备用字段01", "备用字段02", "备用字段03"}
- if err = streamWriter.SetRow("A1", titles); err != nil {
- return nil, xerr.WithStack(err)
- }
- for i, item := range res.Data.List {
- cell, err := excelize.CoordinatesToCellName(1, i+2)
- if err != nil {
- zaplog.Error("excelize.CoordinatesToCellName", zap.Any("Err", err))
- continue
- }
- row := make([]interface{}, 0)
- row = append(row, item.Name, item.CategoryName, item.UniqueEncode, item.ForageSourceId, item.PlanTypeId,
- item.AllowError, item.PackageWeight, float64(item.Price/100.00), item.JumpWeight, item.JumpDelay,
- item.ConfirmStart, item.RelayLocations, item.Jmp, item.Backup1, item.Backup2, item.Backup3)
- if err = streamWriter.SetRow(cell, row); err != nil {
- return nil, xerr.WithStack(err)
- }
- }
- if err = streamWriter.Flush(); err != nil {
- return nil, xerr.WithStack(err)
- }
- return file.WriteToBuffer()
- }
- // ExcelTemplateForage 导出模板
- func (s *StoreEntry) ExcelTemplateForage(ctx context.Context) (*bytes.Buffer, error) {
- file := excelize.NewFile()
- defer file.Close()
- streamWriter, err := file.NewStreamWriter("Sheet1")
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- titles := []interface{}{"饲料名称", "饲料分类", "唯一编码", "饲料来源", "计划类型", "允许误差(kg)", "包装单位重量(kg)",
- "单价", "跳转重量域(kg)", "跳转延迟", "确认开始", "继电器位置", "无上域", "备用字段01", "备用字段02", "备用字段03"}
- if err = streamWriter.SetRow("A1", titles); err != nil {
- return nil, xerr.WithStack(err)
- }
- if err = streamWriter.Flush(); err != nil {
- return nil, xerr.WithStack(err)
- }
- return file.WriteToBuffer()
- }
- func (s *StoreEntry) SmallMaterial(ctx context.Context, req *operationPb.SmallMaterialRequest) (*model.PastureCommonResponse, error) {
- body := &model.PastureCommonRequest{
- Name: req.ApiName,
- ReturnType: "Map",
- ParamMaps: &model.FormulaEstimateParams{
- PastureId: fmt.Sprintf("%d", req.PastureId),
- ImforName: req.InfoName,
- },
- }
- response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
- if err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
- return nil, xerr.WithStack(err)
- }
- return response, nil
- }
|