pasture_service.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "kpt-tmr-group/model"
  7. "kpt-tmr-group/pkg/xerr"
  8. operationPb "kpt-tmr-group/proto/go/backend/operation"
  9. "gorm.io/gorm"
  10. )
  11. var (
  12. CattleParentCategoryMap = map[operationPb.CattleCategoryParent_Kind]string{
  13. operationPb.CattleCategoryParent_LACTATION_CAW: "泌乳牛",
  14. operationPb.CattleCategoryParent_FATTEN_CAW: "育肥牛",
  15. operationPb.CattleCategoryParent_RESERVE_CAW: "后备牛",
  16. operationPb.CattleCategoryParent_DRY_CAW: "干奶牛",
  17. operationPb.CattleCategoryParent_PERINATAL_CAW: "围产牛",
  18. operationPb.CattleCategoryParent_OTHER_CAW: "其他",
  19. }
  20. ForageParentCategoryMap = map[operationPb.ForageCategoryParent_Kind]string{
  21. operationPb.ForageCategoryParent_ROUGHAGE: "粗料",
  22. operationPb.ForageCategoryParent_CONCENTRATE: "精料",
  23. operationPb.ForageCategoryParent_HALF_ROUGHAGE_HALF_CONCENTRATE: "粗料精料各半",
  24. operationPb.ForageCategoryParent_OTHER: "其他",
  25. }
  26. ForageSourceMap = map[operationPb.ForageSource_Kind]string{
  27. operationPb.ForageSource_SYSTEM_BUILT_IN: "系统内置",
  28. operationPb.ForageSource_USER_DEFINED: "用户自定义",
  29. }
  30. ForagePlanTypeMap = map[operationPb.ForagePlanType_Kind]string{
  31. operationPb.ForagePlanType_INVALID: "无",
  32. operationPb.ForagePlanType_FORKLIFT: "铲车",
  33. operationPb.ForagePlanType_CONCENTRATE: "精料",
  34. }
  35. JumpDelaTypeMap = map[operationPb.JumpDelaType_Kind]string{
  36. operationPb.JumpDelaType_INVALID: "禁用",
  37. operationPb.JumpDelaType_THREE: "3秒",
  38. operationPb.JumpDelaType_SIX: "6秒",
  39. operationPb.JumpDelaType_NINE: "9秒",
  40. }
  41. IsShowMap = map[operationPb.IsShow_Kind]string{
  42. operationPb.IsShow_OK: "是",
  43. operationPb.IsShow_NO: "否",
  44. }
  45. )
  46. type ForageEnumList struct {
  47. ForageSource map[operationPb.ForageSource_Kind]string `json:"forage_source"`
  48. ForagePlanType map[operationPb.ForagePlanType_Kind]string `json:"forage_plan_type"`
  49. JumpDelaType map[operationPb.JumpDelaType_Kind]string `json:"jump_dela_type"`
  50. CattleParentCategory map[operationPb.CattleCategoryParent_Kind]string `json:"cattle_parent_category"`
  51. ForageParentCategory map[operationPb.ForageCategoryParent_Kind]string `json:"forage_parent_category"`
  52. IsShow map[operationPb.IsShow_Kind]string `json:"is_show"`
  53. }
  54. // CreateGroupPasture 创建集团牧场
  55. func (s *StoreEntry) CreateGroupPasture(ctx context.Context, req *operationPb.AddPastureRequest) error {
  56. pastureList := model.NewGroupPasture(req)
  57. if err := s.DB.Create(pastureList).Error; err != nil {
  58. return xerr.WithStack(err)
  59. }
  60. return nil
  61. }
  62. // EditGroupPasture 创建集团牧场
  63. func (s *StoreEntry) EditGroupPasture(ctx context.Context, req *operationPb.AddPastureRequest) error {
  64. groupPasture := &model.GroupPasture{Id: req.Id}
  65. if err := s.DB.First(groupPasture).Error; err != nil {
  66. if errors.Is(err, gorm.ErrRecordNotFound) {
  67. return xerr.Custom("该数据不存在!")
  68. }
  69. return xerr.WithStack(err)
  70. }
  71. updateData := &model.GroupPasture{
  72. Name: req.Name,
  73. Account: req.Account,
  74. ManagerUser: req.ManagerUser,
  75. ManagerPhone: req.ManagerPhone,
  76. Address: req.Address,
  77. }
  78. if err := s.DB.Model(new(model.GroupPasture)).Omit("is_show", "password").
  79. Where("id = ?", req.Id).
  80. Updates(updateData).Error; err != nil {
  81. return xerr.WithStack(err)
  82. }
  83. return nil
  84. }
  85. // SearchGroupPastureList 查询牧场列表
  86. func (s *StoreEntry) SearchGroupPastureList(ctx context.Context, req *operationPb.SearchPastureRequest) (*model.GroupPastureResponse, error) {
  87. groupPasture := make([]*model.GroupPasture, 0)
  88. var count int64 = 0
  89. pref := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK)
  90. if req.Name != "" {
  91. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  92. }
  93. if req.ManagerPhone != "" {
  94. pref.Where("manager_phone like ?", fmt.Sprintf("%s%s%s", "%", req.ManagerPhone, "%"))
  95. }
  96. if req.ManagerUser != "" {
  97. pref.Where("manager_user like ?", fmt.Sprintf("%s%s%s", "%", req.ManagerUser, "%"))
  98. }
  99. if req.StartTime > 0 && req.EndTime > 0 && req.EndTime >= req.StartTime {
  100. pref.Where("created_at BETWEEN ? AND ? ", req.StartTime, req.EndTime)
  101. }
  102. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  103. Find(&groupPasture).Debug().Error; err != nil {
  104. return nil, xerr.WithStack(err)
  105. }
  106. return &model.GroupPastureResponse{
  107. Page: req.Pagination.Page,
  108. Total: int32(count),
  109. List: model.GroupPastureSlice(groupPasture).ToPB(),
  110. }, nil
  111. }
  112. func (s *StoreEntry) DeleteGroupPasture(ctx context.Context, pastureId int64) error {
  113. groupPasture := &model.GroupPasture{
  114. Id: pastureId,
  115. }
  116. if err := s.DB.First(groupPasture).Error; err != nil {
  117. if errors.Is(err, gorm.ErrRecordNotFound) {
  118. return xerr.Custom("该数据不存在")
  119. }
  120. return xerr.WithStack(err)
  121. }
  122. if err := s.DB.Model(groupPasture).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  123. return xerr.WithStack(err)
  124. }
  125. return nil
  126. }
  127. func (s *StoreEntry) ResetPasswordGroupPasture(ctx context.Context, req *operationPb.RestPasswordGroupPasture) error {
  128. groupPasture := &model.GroupPasture{
  129. Id: req.PastureId,
  130. }
  131. if err := s.DB.First(groupPasture).Error; err != nil {
  132. if errors.Is(err, gorm.ErrRecordNotFound) {
  133. return xerr.Custom("该数据不存在")
  134. }
  135. return xerr.WithStack(err)
  136. }
  137. if err := s.DB.Model(groupPasture).Update("password", req.Password).Error; err != nil {
  138. return xerr.WithStack(err)
  139. }
  140. return nil
  141. }
  142. func (s *StoreEntry) IsShowGroupPasture(ctx context.Context, req *operationPb.IsShowGroupPasture) error {
  143. groupPasture := &model.GroupPasture{
  144. Id: req.PastureId,
  145. }
  146. if err := s.DB.First(groupPasture).Error; err != nil {
  147. if errors.Is(err, gorm.ErrRecordNotFound) {
  148. return xerr.Custom("该数据不存在")
  149. }
  150. return xerr.WithStack(err)
  151. }
  152. if err := s.DB.Model(groupPasture).Update("is_show", req.IsShow).Error; err != nil {
  153. return xerr.WithStack(err)
  154. }
  155. return nil
  156. }
  157. // ParentCattleCategoryList 畜牧类别父类列表
  158. func (s *StoreEntry) ParentCattleCategoryList(ctx context.Context) map[operationPb.CattleCategoryParent_Kind]string {
  159. return CattleParentCategoryMap
  160. }
  161. // AddCattleCategory 添加畜牧分类
  162. func (s *StoreEntry) AddCattleCategory(ctx context.Context, req *operationPb.AddCattleCategoryRequest) error {
  163. cattleCategory := model.NewCattleCategory(req)
  164. if err := s.DB.Create(cattleCategory).Error; err != nil {
  165. return xerr.WithStack(err)
  166. }
  167. return nil
  168. }
  169. // EditCattleCategory 编辑畜牧分类
  170. func (s *StoreEntry) EditCattleCategory(ctx context.Context, req *operationPb.AddCattleCategoryRequest) error {
  171. cattleCategory := &model.CattleCategory{Id: req.Id}
  172. if err := s.DB.First(cattleCategory).Error; err != nil {
  173. if errors.Is(err, gorm.ErrRecordNotFound) {
  174. return xerr.Custom("该数据不存在")
  175. }
  176. return xerr.WithStack(err)
  177. }
  178. updateData := &model.CattleCategory{
  179. ParentName: req.ParentName,
  180. Name: req.Name,
  181. Number: req.Number,
  182. ParentId: req.ParentId,
  183. }
  184. if err := s.DB.Model(new(model.CattleCategory)).Omit("is_show", "is_delete").
  185. Where("id = ?", req.Id).
  186. Updates(updateData).Error; err != nil {
  187. return xerr.WithStack(err)
  188. }
  189. return nil
  190. }
  191. // IsShowCattleCategory 是否启用
  192. func (s *StoreEntry) IsShowCattleCategory(ctx context.Context, req *operationPb.IsShowCattleCategory) error {
  193. cattleCategory := &model.CattleCategory{Id: req.CattleCategoryId}
  194. if err := s.DB.First(cattleCategory).Error; err != nil {
  195. if errors.Is(err, gorm.ErrRecordNotFound) {
  196. return xerr.Custom("该数据不存在")
  197. }
  198. return xerr.WithStack(err)
  199. }
  200. if err := s.DB.Model(new(model.CattleCategory)).Where("id = ?", req.CattleCategoryId).Update("is_show", req.IsShow).Error; err != nil {
  201. return xerr.WithStack(err)
  202. }
  203. return nil
  204. }
  205. // DeleteCattleCategory 是否删除
  206. func (s *StoreEntry) DeleteCattleCategory(ctx context.Context, cattleCategoryId int64) error {
  207. cattleCategory := &model.CattleCategory{Id: cattleCategoryId}
  208. if err := s.DB.First(cattleCategory).Error; err != nil {
  209. if errors.Is(err, gorm.ErrRecordNotFound) {
  210. return xerr.Custom("该数据不存在")
  211. }
  212. return xerr.WithStack(err)
  213. }
  214. if err := s.DB.Model(new(model.CattleCategory)).Where("id = ?", cattleCategoryId).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  215. return xerr.WithStack(err)
  216. }
  217. return nil
  218. }
  219. // SearchCattleCategoryList 牧畜分类类别列表
  220. func (s *StoreEntry) SearchCattleCategoryList(ctx context.Context, req *operationPb.SearchCattleCategoryRequest) (*model.CattleCategoryResponse, error) {
  221. cattleCategory := make([]*model.CattleCategory, 0)
  222. var count int64 = 0
  223. pref := s.DB.Model(new(model.CattleCategory)).Where("is_delete = ?", operationPb.IsShow_OK)
  224. if req.Name != "" {
  225. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  226. }
  227. if req.ParentName != "" {
  228. pref.Where("parent_name like ?", fmt.Sprintf("%s%s%s", "%", req.ParentName, "%"))
  229. }
  230. if req.IsShow > 0 {
  231. pref.Where("is_show = ?", req.IsShow)
  232. }
  233. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  234. Find(&cattleCategory).Debug().Error; err != nil {
  235. return nil, xerr.WithStack(err)
  236. }
  237. return &model.CattleCategoryResponse{
  238. Page: req.Pagination.Page,
  239. Total: int32(count),
  240. List: model.CattleCategorySlice(cattleCategory).ToPB(),
  241. }, nil
  242. }
  243. // ParentForageCategoryList 饲料类别父类列表
  244. func (s *StoreEntry) ParentForageCategoryList(ctx context.Context) map[operationPb.ForageCategoryParent_Kind]string {
  245. return ForageParentCategoryMap
  246. }
  247. // AddForageCategory 添加饲料分类
  248. func (s *StoreEntry) AddForageCategory(ctx context.Context, req *operationPb.AddForageCategoryRequest) error {
  249. forageCategory := model.NewForageCategory(req)
  250. if err := s.DB.Create(forageCategory).Error; err != nil {
  251. return xerr.WithStack(err)
  252. }
  253. return nil
  254. }
  255. // EditForageCategory 编辑饲料分类
  256. func (s *StoreEntry) EditForageCategory(ctx context.Context, req *operationPb.AddForageCategoryRequest) error {
  257. forageCategory := &model.ForageCategory{Id: req.Id}
  258. if err := s.DB.First(forageCategory).Error; err != nil {
  259. if errors.Is(err, gorm.ErrRecordNotFound) {
  260. return xerr.Custom("该数据不存在")
  261. }
  262. return xerr.WithStack(err)
  263. }
  264. updateData := &model.ForageCategory{
  265. ParentName: req.ParentName,
  266. Name: req.Name,
  267. Number: req.Number,
  268. ParentId: req.ParentId,
  269. }
  270. if err := s.DB.Model(new(model.ForageCategory)).Omit("is_show", "is_delete").
  271. Where("id = ?", req.Id).
  272. Updates(updateData).Error; err != nil {
  273. return xerr.WithStack(err)
  274. }
  275. return nil
  276. }
  277. // IsShowForageCategory 是否启用
  278. func (s *StoreEntry) IsShowForageCategory(ctx context.Context, req *operationPb.IsShowForageCategory) error {
  279. forageCategory := &model.ForageCategory{Id: req.ForageCategoryId}
  280. if err := s.DB.First(forageCategory).Error; err != nil {
  281. if errors.Is(err, gorm.ErrRecordNotFound) {
  282. return xerr.Custom("该数据不存在")
  283. }
  284. return xerr.WithStack(err)
  285. }
  286. if err := s.DB.Model(new(model.ForageCategory)).Where("id = ?", req.ForageCategoryId).Update("is_show", req.IsShow).Error; err != nil {
  287. return xerr.WithStack(err)
  288. }
  289. return nil
  290. }
  291. // DeleteForageCategory 是否删除
  292. func (s *StoreEntry) DeleteForageCategory(ctx context.Context, forageCategoryId int64) error {
  293. forageCategory := &model.ForageCategory{Id: forageCategoryId}
  294. if err := s.DB.First(forageCategory).Error; err != nil {
  295. if errors.Is(err, gorm.ErrRecordNotFound) {
  296. return xerr.Custom("该数据不存在")
  297. }
  298. return xerr.WithStack(err)
  299. }
  300. if err := s.DB.Model(new(model.ForageCategory)).Where("id = ?", forageCategoryId).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  301. return xerr.WithStack(err)
  302. }
  303. return nil
  304. }
  305. // SearchForageCategoryList 饲料分类类别列表
  306. func (s *StoreEntry) SearchForageCategoryList(ctx context.Context, req *operationPb.SearchForageCategoryRequest) (*model.ForageCategoryResponse, error) {
  307. forageCategory := make([]*model.ForageCategory, 0)
  308. var count int64 = 0
  309. pref := s.DB.Model(new(model.ForageCategory)).Where("is_delete = ?", operationPb.IsShow_OK)
  310. if req.Name != "" {
  311. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  312. }
  313. if req.ParentName != "" {
  314. pref.Where("parent_name like ?", fmt.Sprintf("%s%s%s", "%", req.ParentName, "%"))
  315. }
  316. if req.IsShow > 0 {
  317. pref.Where("is_show = ?", req.IsShow)
  318. }
  319. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  320. Find(&forageCategory).Debug().Error; err != nil {
  321. return nil, xerr.WithStack(err)
  322. }
  323. return &model.ForageCategoryResponse{
  324. Page: req.Pagination.Page,
  325. Total: int32(count),
  326. List: model.ForageCategorySlice(forageCategory).ToPB(),
  327. }, nil
  328. }
  329. // CreateForage 创建饲料
  330. func (s *StoreEntry) CreateForage(ctx context.Context, req *operationPb.AddForageRequest) error {
  331. forage := model.NewForage(req)
  332. if err := s.DB.Create(forage).Error; err != nil {
  333. return xerr.WithStack(err)
  334. }
  335. return nil
  336. }
  337. // EditForage 编辑饲料
  338. func (s *StoreEntry) EditForage(ctx context.Context, req *operationPb.AddForageRequest) error {
  339. forage := model.Forage{Id: req.Id}
  340. if err := s.DB.Where("is_delete = ?", operationPb.IsShow_OK).First(forage).Error; err != nil {
  341. if errors.Is(err, gorm.ErrRecordNotFound) {
  342. return xerr.Custom("该数据不存在")
  343. }
  344. return xerr.WithStack(err)
  345. }
  346. updateData := &model.Forage{
  347. Name: req.Name,
  348. CategoryId: req.CategoryId,
  349. UniqueEncode: req.UniqueEncode,
  350. ForageSourceId: req.ForageSourceId,
  351. PlanTypeId: req.PlanTypeId,
  352. SmallMaterialScale: req.SmallMaterialScale,
  353. AllowError: req.AllowError,
  354. PackageWeight: req.PackageWeight,
  355. Price: req.Price,
  356. JumpWeight: req.JumpWeight,
  357. JumpDelay: req.JumpDelay,
  358. ConfirmStart: req.ConfirmStart,
  359. RelayLocations: req.RelayLocations,
  360. Jmp: req.Jmp,
  361. Backup1: req.Backup1,
  362. Backup2: req.Backup2,
  363. Backup3: req.Backup3,
  364. }
  365. if err := s.DB.Model(new(model.Forage)).Omit("is_show", "is_delete").Where("id = ?", req.Id).
  366. Updates(updateData).Error; err != nil {
  367. return xerr.WithStack(err)
  368. }
  369. return nil
  370. }
  371. func (s *StoreEntry) SearchForageList(ctx context.Context, req *operationPb.SearchForageListRequest) (*operationPb.SearchForageListResponse, error) {
  372. forage := make([]*model.Forage, 0)
  373. var count int64 = 0
  374. pref := s.DB.Model(new(model.Forage)).Where("is_delete = ?", operationPb.IsShow_OK)
  375. if req.Name != "" {
  376. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  377. }
  378. if req.CategoryId != "" {
  379. pref.Where("category_id = ?", req.CategoryId)
  380. }
  381. if req.ForageSourceId > 0 {
  382. pref.Where("forage_source_id = ?", req.ForageSourceId)
  383. }
  384. if req.IsShow > 0 {
  385. pref.Where("is_show = ?", req.IsShow)
  386. }
  387. if req.AllowError > 0 {
  388. pref.Where("allow_error = ?", req.AllowError)
  389. }
  390. if req.AllowError > 0 {
  391. pref.Where("allow_error = ?", req.AllowError)
  392. }
  393. if req.JumpWeight > 0 {
  394. pref.Where("jump_weight = ?", req.JumpWeight)
  395. }
  396. if req.JumpDelay > 0 {
  397. pref.Where("jump_delay = ?", req.JumpDelay)
  398. }
  399. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  400. Find(&forage).Debug().Error; err != nil {
  401. return nil, xerr.WithStack(err)
  402. }
  403. return &operationPb.SearchForageListResponse{
  404. Page: req.Pagination.Page,
  405. Total: int32(count),
  406. List: model.ForageSlice(forage).ToPB(),
  407. }, nil
  408. }
  409. func (s *StoreEntry) ForageEnumList(ctx context.Context) *ForageEnumList {
  410. return &ForageEnumList{
  411. JumpDelaType: JumpDelaTypeMap,
  412. ForageSource: ForageSourceMap,
  413. ForagePlanType: ForagePlanTypeMap,
  414. CattleParentCategory: CattleParentCategoryMap,
  415. ForageParentCategory: ForageParentCategoryMap,
  416. IsShow: IsShowMap,
  417. }
  418. }
  419. func (s *StoreEntry) DeleteForageList(ctx context.Context, ids []int64) error {
  420. if len(ids) == 0 {
  421. return xerr.Custom("参数错误")
  422. }
  423. if err := s.DB.Where("id IN ?", ids).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  424. return xerr.WithStack(err)
  425. }
  426. return nil
  427. }
  428. func (s *StoreEntry) IsShowForage(ctx context.Context, req *operationPb.IsShowForage) error {
  429. forage := &model.Forage{Id: req.ForageId}
  430. if err := s.DB.First(forage).Error; err != nil {
  431. if errors.Is(err, gorm.ErrRecordNotFound) {
  432. return xerr.Custom("该数据不存在")
  433. }
  434. return xerr.WithStack(err)
  435. }
  436. if err := s.DB.Model(new(model.Forage)).Where("id = ?", req.ForageId).Update("is_show", req.IsShow).Error; err != nil {
  437. return xerr.WithStack(err)
  438. }
  439. return nil
  440. }