123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- package backend
- import (
- "context"
- "errors"
- "fmt"
- "kpt-tmr-group/model"
- "kpt-tmr-group/pkg/xerr"
- operationPb "kpt-tmr-group/proto/go/backend/operation"
- "gorm.io/gorm"
- )
- var CattleCategoryMap = map[operationPb.CattleCategoryParent_Kind]string{
- operationPb.CattleCategoryParent_LACTATION_CAW: "泌乳牛",
- operationPb.CattleCategoryParent_FATTEN_CAW: "育肥牛",
- operationPb.CattleCategoryParent_RESERVE_CAW: "后备牛",
- operationPb.CattleCategoryParent_DRY_CAW: "干奶牛",
- operationPb.CattleCategoryParent_PERINATAL_CAW: "围产牛",
- operationPb.CattleCategoryParent_OTHER_CAW: "其他",
- }
- // CreateGroupPasture 创建集团牧场
- func (s *StoreEntry) CreateGroupPasture(ctx context.Context, req *operationPb.AddPastureRequest) error {
- pastureList := model.NewGroupPasture(req)
- if err := s.DB.Create(pastureList).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- // EditGroupPasture 创建集团牧场
- func (s *StoreEntry) EditGroupPasture(ctx context.Context, req *operationPb.AddPastureRequest) error {
- groupPasture := &model.GroupPasture{Id: 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,
- }
- 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) (*model.GroupPastureResponse, 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).Debug().Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &model.GroupPastureResponse{
- Page: req.Pagination.Page,
- Total: int32(count),
- List: model.GroupPastureSlice(groupPasture).ToPB(),
- }, 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, req *operationPb.RestPasswordGroupPasture) error {
- groupPasture := &model.GroupPasture{
- Id: 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).Update("password", req.Password).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- func (s *StoreEntry) IsShowGroupPasture(ctx context.Context, req *operationPb.IsShowGroupPasture) error {
- groupPasture := &model.GroupPasture{
- Id: 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).Update("is_show", req.IsShow).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- // ParentCattleCategoryList 畜牧类别父类列表
- func (s *StoreEntry) ParentCattleCategoryList(ctx context.Context) map[operationPb.CattleCategoryParent_Kind]string {
- return CattleCategoryMap
- }
- // 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)
- }
- return nil
- }
- // EditCattleCategory 编辑畜牧分类
- func (s *StoreEntry) EditCattleCategory(ctx context.Context, req *operationPb.AddCattleCategoryRequest) error {
- cattleCategory := &model.CattleCategory{Id: 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)
- }
- return nil
- }
- // IsShowCattleCategory 是否启用
- func (s *StoreEntry) IsShowCattleCategory(ctx context.Context, req *operationPb.IsShowCattleCategory) error {
- cattleCategory := &model.CattleCategory{Id: 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)
- }
- 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)
- }
- return nil
- }
- // SearchCattleCategoryList 牧畜分类类别列表
- func (s *StoreEntry) SearchCattleCategoryList(ctx context.Context, req *operationPb.SearchCattleCategoryRequest) (*model.CattleCategoryResponse, 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.ParentName != "" {
- pref.Where("parent_name like ?", fmt.Sprintf("%s%s%s", "%", req.ParentName, "%"))
- }
- 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 &model.CattleCategoryResponse{
- Page: req.Pagination.Page,
- Total: int32(count),
- List: model.CattleCategorySlice(cattleCategory).ToPB(),
- }, nil
- }
|