123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- 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 (
- CattleParentCategoryMap = 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: "其他",
- }
- ForageParentCategoryMap = map[operationPb.ForageCategoryParent_Kind]string{
- operationPb.ForageCategoryParent_ROUGHAGE: "粗料",
- operationPb.ForageCategoryParent_CONCENTRATE: "精料",
- operationPb.ForageCategoryParent_HALF_ROUGHAGE_HALF_CONCENTRATE: "粗料精料各半",
- operationPb.ForageCategoryParent_OTHER: "其他",
- }
- ForageSourceMap = map[operationPb.ForageSource_Kind]string{
- operationPb.ForageSource_SYSTEM_BUILT_IN: "系统内置",
- operationPb.ForageSource_USER_DEFINED: "用户自定义",
- }
- ForagePlanTypeMap = map[operationPb.ForagePlanType_Kind]string{
- operationPb.ForagePlanType_INVALID: "无",
- operationPb.ForagePlanType_FORKLIFT: "铲车",
- operationPb.ForagePlanType_CONCENTRATE: "精料",
- }
- JumpDelaTypeMap = map[operationPb.JumpDelaType_Kind]string{
- operationPb.JumpDelaType_INVALID: "禁用",
- operationPb.JumpDelaType_THREE: "3秒",
- operationPb.JumpDelaType_SIX: "6秒",
- operationPb.JumpDelaType_NINE: "9秒",
- }
- IsShowMap = map[operationPb.IsShow_Kind]string{
- operationPb.IsShow_OK: "是",
- operationPb.IsShow_NO: "否",
- }
- )
- type ForageEnumList struct {
- ForageSource map[operationPb.ForageSource_Kind]string `json:"forage_source"`
- ForagePlanType map[operationPb.ForagePlanType_Kind]string `json:"forage_plan_type"`
- JumpDelaType map[operationPb.JumpDelaType_Kind]string `json:"jump_dela_type"`
- CattleParentCategory map[operationPb.CattleCategoryParent_Kind]string `json:"cattle_parent_category"`
- ForageParentCategory map[operationPb.ForageCategoryParent_Kind]string `json:"forage_parent_category"`
- IsShow map[operationPb.IsShow_Kind]string `json:"is_show"`
- }
- // 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 CattleParentCategoryMap
- }
- // 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
- }
- // ParentForageCategoryList 饲料类别父类列表
- func (s *StoreEntry) ParentForageCategoryList(ctx context.Context) map[operationPb.ForageCategoryParent_Kind]string {
- return ForageParentCategoryMap
- }
- // 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)
- }
- return nil
- }
- // EditForageCategory 编辑饲料分类
- func (s *StoreEntry) EditForageCategory(ctx context.Context, req *operationPb.AddForageCategoryRequest) error {
- forageCategory := &model.ForageCategory{Id: 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)
- }
- return nil
- }
- // IsShowForageCategory 是否启用
- func (s *StoreEntry) IsShowForageCategory(ctx context.Context, req *operationPb.IsShowForageCategory) error {
- forageCategory := &model.ForageCategory{Id: 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)
- }
- 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)
- }
- return nil
- }
- // SearchForageCategoryList 饲料分类类别列表
- func (s *StoreEntry) SearchForageCategoryList(ctx context.Context, req *operationPb.SearchForageCategoryRequest) (*model.ForageCategoryResponse, 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.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(&forageCategory).Debug().Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &model.ForageCategoryResponse{
- Page: req.Pagination.Page,
- Total: int32(count),
- List: model.ForageCategorySlice(forageCategory).ToPB(),
- }, 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: 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: req.CategoryId,
- UniqueEncode: req.UniqueEncode,
- ForageSourceId: req.ForageSourceId,
- PlanTypeId: req.PlanTypeId,
- SmallMaterialScale: req.SmallMaterialScale,
- AllowError: req.AllowError,
- PackageWeight: req.PackageWeight,
- Price: req.Price,
- JumpWeight: req.JumpWeight,
- JumpDelay: req.JumpDelay,
- ConfirmStart: req.ConfirmStart,
- RelayLocations: 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 != "" {
- 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.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("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
- Find(&forage).Debug().Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &operationPb.SearchForageListResponse{
- Page: req.Pagination.Page,
- Total: int32(count),
- List: model.ForageSlice(forage).ToPB(),
- }, nil
- }
- func (s *StoreEntry) ForageEnumList(ctx context.Context) *ForageEnumList {
- return &ForageEnumList{
- JumpDelaType: JumpDelaTypeMap,
- ForageSource: ForageSourceMap,
- ForagePlanType: ForagePlanTypeMap,
- CattleParentCategory: CattleParentCategoryMap,
- ForageParentCategory: ForageParentCategoryMap,
- IsShow: IsShowMap,
- }
- }
- func (s *StoreEntry) DeleteForageList(ctx context.Context, ids []int64) error {
- if len(ids) == 0 {
- return xerr.Custom("参数错误")
- }
- if err := s.DB.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: 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
- }
|