pasture_service.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. package backend
  2. import (
  3. "bytes"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "kpt-tmr-group/model"
  9. "kpt-tmr-group/pkg/logger/zaplog"
  10. "kpt-tmr-group/pkg/tool"
  11. "kpt-tmr-group/pkg/xerr"
  12. operationPb "kpt-tmr-group/proto/go/backend/operation"
  13. "net/http"
  14. "strconv"
  15. "sync"
  16. "go.uber.org/zap"
  17. "github.com/xuri/excelize/v2"
  18. "gorm.io/gorm"
  19. )
  20. // CreateGroupPasture 创建集团牧场
  21. func (s *StoreEntry) CreateGroupPasture(ctx context.Context, req *operationPb.AddPastureRequest) error {
  22. // 账号同步牧场端
  23. groupPasture := model.NewGroupPasture(req)
  24. if err := s.DB.Create(groupPasture).Error; err != nil {
  25. return xerr.WithStack(err)
  26. }
  27. if err := s.PastureAccountDistribution(ctx, groupPasture); err != nil {
  28. s.DB.Model(new(model.GroupPasture)).Where("id = ?", groupPasture.Id).Update("is_delete", operationPb.IsShow_NO)
  29. return xerr.WithStack(err)
  30. } else {
  31. s.DB.Model(new(model.GroupPasture)).Where("id = ?", groupPasture.Id).Update("is_distribution", operationPb.IsShow_OK)
  32. }
  33. return nil
  34. }
  35. // PastureAccountDistribution 账号同步牧场端
  36. func (s *StoreEntry) PastureAccountDistribution(ctx context.Context, groupPasture *model.GroupPasture) error {
  37. if groupPasture.Domain == "" {
  38. return nil
  39. }
  40. body := &model.AccountDistribution{
  41. Account: groupPasture.Account,
  42. UserName: groupPasture.ManagerUser,
  43. Password: groupPasture.Password,
  44. Phone: groupPasture.ManagerPhone,
  45. PastureId: int32(groupPasture.Id),
  46. PastureName: groupPasture.Name,
  47. Address: groupPasture.Address,
  48. }
  49. res := &model.PastureCommonResponse{}
  50. if _, err := s.PastureHttpClient(ctx, model.PastureAccountDistributionURl, groupPasture.GetPastureId(), body, res); err != nil {
  51. zaplog.Error("AccountDistribution", zap.Any("url", model.PastureAccountDistributionURl), zap.Any("err", err), zap.Any("body", body), zap.Any("res", res))
  52. return xerr.WithStack(err)
  53. }
  54. if res.Code != http.StatusOK {
  55. if err := s.DB.Model(new(model.GroupPasture)).Where("id = ?", groupPasture.Id).Update("is_distribution", operationPb.IsShow_OK).Error; err != nil {
  56. zaplog.Error("AccountDistribution-Update", zap.Any("url", model.PastureAccountDistributionURl), zap.Any("err", err), zap.Any("body", body), zap.Any("res", res))
  57. return xerr.Customf("%s", res.Msg)
  58. }
  59. }
  60. return nil
  61. }
  62. func (s *StoreEntry) FindGroupPastureListByIds(ctx context.Context, pastureIds []int32) ([]*model.GroupPasture, error) {
  63. groupPastureList := make([]*model.GroupPasture, 0)
  64. if err := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK).
  65. Where("id IN ?", pastureIds).
  66. Find(&groupPastureList).Error; err != nil {
  67. return nil, xerr.WithStack(err)
  68. }
  69. return groupPastureList, nil
  70. }
  71. func (s *StoreEntry) GetGroupPastureListById(ctx context.Context, pastureId int64) (*model.GroupPasture, error) {
  72. groupPasture := &model.GroupPasture{
  73. Id: pastureId,
  74. }
  75. if err := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK).
  76. First(&groupPasture).Error; err != nil {
  77. return nil, xerr.WithStack(err)
  78. }
  79. return groupPasture, nil
  80. }
  81. // EditGroupPasture 创建集团牧场
  82. func (s *StoreEntry) EditGroupPasture(ctx context.Context, req *operationPb.AddPastureRequest) error {
  83. groupPasture := &model.GroupPasture{Id: int64(req.Id)}
  84. if err := s.DB.First(groupPasture).Error; err != nil {
  85. if errors.Is(err, gorm.ErrRecordNotFound) {
  86. return xerr.Custom("该数据不存在!")
  87. }
  88. return xerr.WithStack(err)
  89. }
  90. updateData := &model.GroupPasture{
  91. Name: req.Name,
  92. Account: req.Account,
  93. ManagerUser: req.ManagerUser,
  94. ManagerPhone: req.ManagerPhone,
  95. Address: req.Address,
  96. Domain: req.Domain,
  97. }
  98. if err := s.DB.Model(new(model.GroupPasture)).Omit("is_show", "password").
  99. Where("id = ?", req.Id).
  100. Updates(updateData).Error; err != nil {
  101. return xerr.WithStack(err)
  102. }
  103. return nil
  104. }
  105. // SearchGroupPastureList 查询牧场列表
  106. func (s *StoreEntry) SearchGroupPastureList(ctx context.Context, req *operationPb.SearchPastureRequest) (*operationPb.SearchPastureResponse, error) {
  107. groupPasture := make([]*model.GroupPasture, 0)
  108. var count int64 = 0
  109. pref := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK)
  110. if req.Name != "" {
  111. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  112. }
  113. if req.ManagerPhone != "" {
  114. pref.Where("manager_phone like ?", fmt.Sprintf("%s%s%s", "%", req.ManagerPhone, "%"))
  115. }
  116. if req.ManagerUser != "" {
  117. pref.Where("manager_user like ?", fmt.Sprintf("%s%s%s", "%", req.ManagerUser, "%"))
  118. }
  119. if req.StartTime > 0 && req.EndTime > 0 && req.EndTime >= req.StartTime {
  120. pref.Where("created_at BETWEEN ? AND ? ", req.StartTime, req.EndTime)
  121. }
  122. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  123. Find(&groupPasture).Error; err != nil {
  124. return nil, xerr.WithStack(err)
  125. }
  126. return &operationPb.SearchPastureResponse{
  127. Code: http.StatusOK,
  128. Msg: "ok",
  129. Data: &operationPb.SearchPastureData{
  130. Page: req.Pagination.Page,
  131. Total: int32(count),
  132. PageSize: req.Pagination.PageSize,
  133. List: model.GroupPastureSlice(groupPasture).ToPB(),
  134. },
  135. }, nil
  136. }
  137. // DataSyncGroupPastureList 用户数据下发的牧场
  138. func (s *StoreEntry) DataSyncGroupPastureList(ctx context.Context) ([]*model.GroupPasture, error) {
  139. groupPastureList := make([]*model.GroupPasture, 0)
  140. if err := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK).
  141. Where("is_show = ?", operationPb.IsShow_OK).Where("domain != ''").
  142. Where("is_distribution = ?", operationPb.IsShow_OK).
  143. Find(&groupPastureList).Error; err != nil {
  144. return nil, xerr.WithStack(err)
  145. }
  146. zaplog.Info("GroupPastureList", zap.Any("data", groupPastureList))
  147. return groupPastureList, nil
  148. }
  149. func (s *StoreEntry) DeleteGroupPasture(ctx context.Context, pastureId int64) error {
  150. groupPasture := &model.GroupPasture{
  151. Id: pastureId,
  152. }
  153. if err := s.DB.First(groupPasture).Error; err != nil {
  154. if errors.Is(err, gorm.ErrRecordNotFound) {
  155. return xerr.Custom("该数据不存在")
  156. }
  157. return xerr.WithStack(err)
  158. }
  159. if err := s.DB.Model(groupPasture).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  160. return xerr.WithStack(err)
  161. }
  162. return nil
  163. }
  164. func (s *StoreEntry) ResetPasswordGroupPasture(ctx context.Context, pastureId int64) error {
  165. groupPasture := &model.GroupPasture{
  166. Id: pastureId,
  167. }
  168. if err := s.DB.Where("is_delete = ?", operationPb.IsShow_OK).First(groupPasture).Error; err != nil {
  169. if errors.Is(err, gorm.ErrRecordNotFound) {
  170. return xerr.Custom("该数据不存在")
  171. }
  172. return xerr.WithStack(err)
  173. }
  174. if err := s.DB.Model(groupPasture).Update("password", tool.Md5String(model.InitManagerPassword)).Error; err != nil {
  175. return xerr.WithStack(err)
  176. }
  177. return nil
  178. }
  179. func (s *StoreEntry) IsShowGroupPasture(ctx context.Context, req *operationPb.IsShowGroupPasture) error {
  180. groupPasture := &model.GroupPasture{
  181. Id: int64(req.PastureId),
  182. }
  183. if err := s.DB.First(groupPasture).Error; err != nil {
  184. if errors.Is(err, gorm.ErrRecordNotFound) {
  185. return xerr.Custom("该数据不存在")
  186. }
  187. return xerr.WithStack(err)
  188. }
  189. if err := s.DB.Model(groupPasture).Where("is_delete = ?", operationPb.IsShow_OK).Update("is_show", req.IsShow).Error; err != nil {
  190. return xerr.WithStack(err)
  191. }
  192. return nil
  193. }
  194. // AddCattleCategory 添加畜牧分类
  195. func (s *StoreEntry) AddCattleCategory(ctx context.Context, req *operationPb.AddCattleCategoryRequest) error {
  196. cattleCategory := model.NewCattleCategory(req)
  197. if err := s.DB.Create(cattleCategory).Error; err != nil {
  198. return xerr.WithStack(err)
  199. } else {
  200. body := &model.CategoryRequest{
  201. ParentId: int32(req.ParentId),
  202. ParentName: req.ParentName,
  203. Name: req.Name,
  204. Number: req.Number,
  205. IsShow: int32(req.IsShow),
  206. GroupId: int32(cattleCategory.Id),
  207. }
  208. if err = s.CategoryDistribution(ctx, model.CattleCategoryDistributionURl, body); err != nil {
  209. zaplog.Error("AddCattleCategory", zap.Any("CategoryDistribution", err))
  210. }
  211. }
  212. return nil
  213. }
  214. // EditCattleCategory 编辑畜牧分类
  215. func (s *StoreEntry) EditCattleCategory(ctx context.Context, req *operationPb.AddCattleCategoryRequest) error {
  216. cattleCategory := &model.CattleCategory{Id: int64(req.Id)}
  217. if err := s.DB.First(cattleCategory).Error; err != nil {
  218. if errors.Is(err, gorm.ErrRecordNotFound) {
  219. return xerr.Custom("该数据不存在")
  220. }
  221. return xerr.WithStack(err)
  222. }
  223. updateData := &model.CattleCategory{
  224. ParentName: req.ParentName,
  225. Name: req.Name,
  226. Number: req.Number,
  227. ParentId: req.ParentId,
  228. }
  229. if err := s.DB.Model(new(model.CattleCategory)).Omit("is_show", "is_delete").
  230. Where("id = ?", req.Id).
  231. Updates(updateData).Error; err != nil {
  232. return xerr.WithStack(err)
  233. } else {
  234. body := &model.CategoryRequest{
  235. ParentId: int32(req.ParentId),
  236. ParentName: req.ParentName,
  237. Name: req.Name,
  238. Number: req.Number,
  239. IsShow: int32(req.IsShow),
  240. GroupId: int32(req.Id),
  241. }
  242. if err = s.CategoryDistribution(ctx, model.CattleCategoryDistributionURl, body); err != nil {
  243. zaplog.Error("EditCattleCategory", zap.Any("CategoryDistribution", err))
  244. }
  245. }
  246. return nil
  247. }
  248. // IsShowCattleCategory 是否启用
  249. func (s *StoreEntry) IsShowCattleCategory(ctx context.Context, req *operationPb.IsShowCattleCategory) error {
  250. cattleCategory := &model.CattleCategory{Id: int64(req.CattleCategoryId)}
  251. if err := s.DB.First(cattleCategory).Error; err != nil {
  252. if errors.Is(err, gorm.ErrRecordNotFound) {
  253. return xerr.Custom("该数据不存在")
  254. }
  255. return xerr.WithStack(err)
  256. }
  257. if err := s.DB.Model(new(model.CattleCategory)).Where("id = ?", req.CattleCategoryId).Update("is_show", req.IsShow).Error; err != nil {
  258. return xerr.WithStack(err)
  259. } else {
  260. body := &model.CategoryRequest{
  261. ParentId: int32(cattleCategory.ParentId),
  262. ParentName: cattleCategory.ParentName,
  263. Name: cattleCategory.Name,
  264. Number: cattleCategory.Number,
  265. IsShow: int32(req.IsShow),
  266. GroupId: int32(cattleCategory.Id),
  267. }
  268. if err = s.CategoryDistribution(ctx, model.CattleCategoryDistributionURl, body); err != nil {
  269. zaplog.Error("IsShowCattleCategory", zap.Any("CategoryDistribution", err), zap.Any("body", body))
  270. }
  271. }
  272. return nil
  273. }
  274. // DeleteCattleCategory 是否删除
  275. func (s *StoreEntry) DeleteCattleCategory(ctx context.Context, cattleCategoryId int64) error {
  276. cattleCategory := &model.CattleCategory{Id: cattleCategoryId}
  277. if err := s.DB.First(cattleCategory).Error; err != nil {
  278. if errors.Is(err, gorm.ErrRecordNotFound) {
  279. return xerr.Custom("该数据不存在")
  280. }
  281. return xerr.WithStack(err)
  282. }
  283. if err := s.DB.Model(new(model.CattleCategory)).Where("id = ?", cattleCategoryId).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  284. return xerr.WithStack(err)
  285. } else {
  286. body := &model.CategoryDeleteRequest{
  287. GroupId: int32(cattleCategory.Id),
  288. IsDelete: int32(operationPb.IsShow_OK),
  289. }
  290. if err = s.CategoryDelete(ctx, model.CattleCategoryDeleteURl, body); err != nil {
  291. zaplog.Error("DeleteCattleCategory", zap.Any("CategoryDelete", err), zap.Any("body", body))
  292. }
  293. }
  294. return nil
  295. }
  296. // SearchCattleCategoryList 牧畜分类类别列表
  297. func (s *StoreEntry) SearchCattleCategoryList(ctx context.Context, req *operationPb.SearchCattleCategoryRequest) (*operationPb.SearchCattleCategoryResponse, error) {
  298. cattleCategory := make([]*model.CattleCategory, 0)
  299. var count int64 = 0
  300. pref := s.DB.Model(new(model.CattleCategory)).Where("is_delete = ?", operationPb.IsShow_OK)
  301. if req.Name != "" {
  302. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  303. }
  304. if req.ParentId > 0 {
  305. pref.Where("parent_id = ?", req.ParentId)
  306. }
  307. if req.IsShow > 0 {
  308. pref.Where("is_show = ?", req.IsShow)
  309. }
  310. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  311. Find(&cattleCategory).Debug().Error; err != nil {
  312. return nil, xerr.WithStack(err)
  313. }
  314. return &operationPb.SearchCattleCategoryResponse{
  315. Code: http.StatusOK,
  316. Msg: "ok",
  317. Data: &operationPb.SearchCattleCategoryData{
  318. Page: req.Pagination.Page,
  319. PageSize: req.Pagination.PageSize,
  320. Total: int32(count),
  321. List: model.CattleCategorySlice(cattleCategory).ToPB(),
  322. },
  323. }, nil
  324. }
  325. // AddForageCategory 添加饲料分类
  326. func (s *StoreEntry) AddForageCategory(ctx context.Context, req *operationPb.AddForageCategoryRequest) error {
  327. forageCategory := model.NewForageCategory(req)
  328. if err := s.DB.Create(forageCategory).Error; err != nil {
  329. return xerr.WithStack(err)
  330. } else {
  331. body := &model.CategoryRequest{
  332. ParentId: int32(req.ParentId),
  333. ParentName: req.ParentName,
  334. Name: req.Name,
  335. Number: req.Number,
  336. IsShow: int32(req.IsShow),
  337. GroupId: int32(forageCategory.Id),
  338. }
  339. if err = s.CategoryDistribution(ctx, model.ForageCategoryDistributionURl, body); err != nil {
  340. zaplog.Error("AddForageCategory", zap.Any("CategoryDistribution", err), zap.Any("body", body))
  341. }
  342. }
  343. return nil
  344. }
  345. // EditForageCategory 编辑饲料分类
  346. func (s *StoreEntry) EditForageCategory(ctx context.Context, req *operationPb.AddForageCategoryRequest) error {
  347. forageCategory := &model.ForageCategory{Id: int64(req.Id)}
  348. if err := s.DB.First(forageCategory).Error; err != nil {
  349. if errors.Is(err, gorm.ErrRecordNotFound) {
  350. return xerr.Custom("该数据不存在")
  351. }
  352. return xerr.WithStack(err)
  353. }
  354. updateData := &model.ForageCategory{
  355. ParentName: req.ParentName,
  356. Name: req.Name,
  357. Number: req.Number,
  358. ParentId: req.ParentId,
  359. }
  360. if err := s.DB.Model(new(model.ForageCategory)).Omit("is_show", "is_delete").
  361. Where("id = ?", req.Id).
  362. Updates(updateData).Error; err != nil {
  363. return xerr.WithStack(err)
  364. } else {
  365. body := &model.CategoryRequest{
  366. ParentId: int32(req.ParentId),
  367. ParentName: req.ParentName,
  368. Name: req.Name,
  369. Number: req.Number,
  370. IsShow: int32(req.IsShow),
  371. GroupId: int32(req.Id),
  372. }
  373. if err = s.CategoryDistribution(ctx, model.ForageCategoryDistributionURl, body); err != nil {
  374. zaplog.Error("EditForageCategory", zap.Any("CategoryDistribution", err), zap.Any("body", body))
  375. }
  376. }
  377. return nil
  378. }
  379. // IsShowForageCategory 是否启用
  380. func (s *StoreEntry) IsShowForageCategory(ctx context.Context, req *operationPb.IsShowForageCategory) error {
  381. forageCategory := &model.ForageCategory{Id: int64(req.ForageCategoryId)}
  382. if err := s.DB.First(forageCategory).Error; err != nil {
  383. if errors.Is(err, gorm.ErrRecordNotFound) {
  384. return xerr.Custom("该数据不存在")
  385. }
  386. return xerr.WithStack(err)
  387. }
  388. if err := s.DB.Model(new(model.ForageCategory)).Where("id = ?", req.ForageCategoryId).Update("is_show", req.IsShow).Error; err != nil {
  389. return xerr.WithStack(err)
  390. } else {
  391. body := &model.CategoryRequest{
  392. ParentId: int32(forageCategory.ParentId),
  393. ParentName: forageCategory.ParentName,
  394. Name: forageCategory.Name,
  395. Number: forageCategory.Number,
  396. IsShow: int32(req.IsShow),
  397. GroupId: int32(forageCategory.Id),
  398. }
  399. if err = s.CategoryDistribution(ctx, model.ForageCategoryDistributionURl, body); err != nil {
  400. zaplog.Error("IsShowForageCategory", zap.Any("CategoryDistribution", err), zap.Any("body", body))
  401. }
  402. }
  403. return nil
  404. }
  405. // DeleteForageCategory 是否删除
  406. func (s *StoreEntry) DeleteForageCategory(ctx context.Context, forageCategoryId int64) error {
  407. forageCategory := &model.ForageCategory{Id: forageCategoryId}
  408. if err := s.DB.First(forageCategory).Error; err != nil {
  409. if errors.Is(err, gorm.ErrRecordNotFound) {
  410. return xerr.Custom("该数据不存在")
  411. }
  412. return xerr.WithStack(err)
  413. }
  414. if err := s.DB.Model(new(model.ForageCategory)).Where("id = ?", forageCategoryId).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  415. return xerr.WithStack(err)
  416. } else {
  417. body := &model.CategoryDeleteRequest{
  418. GroupId: int32(forageCategory.Id),
  419. IsDelete: int32(operationPb.IsShow_OK),
  420. }
  421. if err = s.CategoryDelete(ctx, model.ForageCategoryDeleteURl, body); err != nil {
  422. zaplog.Error("DeleteForageCategory", zap.Any("CategoryDelete", err), zap.Any("body", body))
  423. }
  424. }
  425. return nil
  426. }
  427. // SearchForageCategoryList 饲料分类类别列表
  428. func (s *StoreEntry) SearchForageCategoryList(ctx context.Context, req *operationPb.SearchForageCategoryRequest) (*operationPb.SearchForageCategoryResponse, error) {
  429. forageCategory := make([]*model.ForageCategory, 0)
  430. var count int64 = 0
  431. pref := s.DB.Model(new(model.ForageCategory)).Where("is_delete = ?", operationPb.IsShow_OK)
  432. if req.Name != "" {
  433. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  434. }
  435. if req.ParentId > 0 {
  436. pref.Where("parent_id = ?", req.ParentId)
  437. }
  438. if req.Number != "" {
  439. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  440. }
  441. if req.IsShow > 0 {
  442. pref.Where("is_show = ?", req.IsShow)
  443. }
  444. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  445. Find(&forageCategory).Debug().Error; err != nil {
  446. return nil, xerr.WithStack(err)
  447. }
  448. return &operationPb.SearchForageCategoryResponse{
  449. Code: http.StatusOK,
  450. Msg: "ok",
  451. Data: &operationPb.SearchForageCategoryData{
  452. Page: req.Pagination.Page,
  453. PageSize: req.Pagination.PageSize,
  454. Total: int32(count),
  455. List: model.ForageCategorySlice(forageCategory).ToPB(),
  456. },
  457. }, nil
  458. }
  459. // CategoryDistribution 饲料分类和畜牧分类下发
  460. func (s *StoreEntry) CategoryDistribution(ctx context.Context, url string, req *model.CategoryRequest) error {
  461. groupList, err := s.DataSyncGroupPastureList(ctx)
  462. if err != nil {
  463. return xerr.WithStack(err)
  464. }
  465. wg := sync.WaitGroup{}
  466. wg.Add(len(groupList))
  467. for _, v := range groupList {
  468. go func(data *model.GroupPasture) {
  469. defer wg.Done()
  470. res := &model.PastureCommonResponse{}
  471. req.PastureId = int32(data.Id)
  472. if _, err = s.PastureHttpClient(ctx, url, int64(data.Id), req, res); err != nil {
  473. zaplog.Error("CategoryDistribution",
  474. zap.Any("url", url),
  475. zap.Any("err", err),
  476. zap.Any("body", req),
  477. zap.Any("res", res),
  478. )
  479. }
  480. if res.Code != http.StatusOK {
  481. zaplog.Error("CategoryDistribution-http",
  482. zap.Any("url", url),
  483. zap.Any("body", req),
  484. zap.Any("res", res),
  485. )
  486. }
  487. }(v)
  488. }
  489. wg.Wait()
  490. return nil
  491. }
  492. // CategoryDelete 饲料分类和畜牧分类删除
  493. func (s *StoreEntry) CategoryDelete(ctx context.Context, url string, req *model.CategoryDeleteRequest) error {
  494. groupList, err := s.DataSyncGroupPastureList(ctx)
  495. if err != nil {
  496. return xerr.WithStack(err)
  497. }
  498. wg := sync.WaitGroup{}
  499. wg.Add(len(groupList))
  500. for _, v := range groupList {
  501. go func(data *model.GroupPasture) {
  502. defer wg.Done()
  503. res := &model.PastureCommonResponse{}
  504. req.PastureId = int32(data.Id)
  505. if _, err = s.PastureHttpClient(ctx, url, int64(data.Id), req, res); err != nil {
  506. zaplog.Error("CategoryDelete",
  507. zap.Any("url", url),
  508. zap.Any("err", err),
  509. zap.Any("body", req),
  510. zap.Any("res", res),
  511. )
  512. }
  513. if res.Code != http.StatusOK {
  514. zaplog.Error("CategoryDelete-http",
  515. zap.Any("url", url),
  516. zap.Any("body", req),
  517. zap.Any("res", res),
  518. )
  519. }
  520. }(v)
  521. }
  522. wg.Wait()
  523. return nil
  524. }
  525. // CreateForage 创建饲料
  526. func (s *StoreEntry) CreateForage(ctx context.Context, req *operationPb.AddForageRequest) error {
  527. forage := model.NewForage(req)
  528. if err := s.DB.Create(forage).Error; err != nil {
  529. return xerr.WithStack(err)
  530. }
  531. return nil
  532. }
  533. // EditForage 编辑饲料
  534. func (s *StoreEntry) EditForage(ctx context.Context, req *operationPb.AddForageRequest) error {
  535. forage := model.Forage{Id: int64(req.Id)}
  536. if err := s.DB.Where("is_delete = ?", operationPb.IsShow_OK).First(&forage).Error; err != nil {
  537. if errors.Is(err, gorm.ErrRecordNotFound) {
  538. return xerr.Custom("该数据不存在")
  539. }
  540. return xerr.WithStack(err)
  541. }
  542. updateData := &model.Forage{
  543. Name: req.Name,
  544. CategoryId: int64(req.CategoryId),
  545. CategoryName: req.CategoryName,
  546. MaterialType: req.MaterialType,
  547. UniqueEncode: req.UniqueEncode,
  548. ForageSourceId: req.ForageSourceId,
  549. PlanTypeId: req.PlanTypeId,
  550. SmallMaterialScale: req.SmallMaterialScale,
  551. AllowError: int64(req.AllowError),
  552. PackageWeight: int64(req.PackageWeight),
  553. Price: int64(req.Price),
  554. JumpWeight: int64(req.JumpWeight),
  555. JumpDelay: req.JumpDelay,
  556. ConfirmStart: req.ConfirmStart,
  557. RelayLocations: int64(req.RelayLocations),
  558. Jmp: req.Jmp,
  559. Backup1: req.Backup1,
  560. Backup2: req.Backup2,
  561. Backup3: req.Backup3,
  562. }
  563. if err := s.DB.Model(new(model.Forage)).Omit("is_show", "is_delete").Where("id = ?", req.Id).
  564. Updates(updateData).Error; err != nil {
  565. return xerr.WithStack(err)
  566. }
  567. return nil
  568. }
  569. func (s *StoreEntry) SearchForageList(ctx context.Context, req *operationPb.SearchForageListRequest) (*operationPb.SearchForageListResponse, error) {
  570. forage := make([]*model.Forage, 0)
  571. var count int64 = 0
  572. pref := s.DB.Model(new(model.Forage)).Where("is_delete = ?", operationPb.IsShow_OK)
  573. if req.Name != "" {
  574. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  575. }
  576. if req.CategoryId > 0 {
  577. pref.Where("category_id = ?", req.CategoryId)
  578. }
  579. if req.ForageSourceId > 0 {
  580. pref.Where("forage_source_id = ?", req.ForageSourceId)
  581. }
  582. if req.IsShow > 0 {
  583. pref.Where("is_show = ?", req.IsShow)
  584. }
  585. if req.AllowError > 0 {
  586. pref.Where("allow_error = ?", req.AllowError)
  587. }
  588. if req.JumpWeight > 0 {
  589. pref.Where("jump_weight = ?", req.JumpWeight)
  590. }
  591. if req.JumpDelay > 0 {
  592. pref.Where("jump_delay = ?", req.JumpDelay)
  593. }
  594. if err := pref.Order("sort").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  595. Find(&forage).Error; err != nil {
  596. return nil, xerr.WithStack(err)
  597. }
  598. return &operationPb.SearchForageListResponse{
  599. Code: http.StatusOK,
  600. Msg: "ok",
  601. Data: &operationPb.SearchForageList{
  602. Page: req.Pagination.Page,
  603. PageSize: req.Pagination.PageSize,
  604. Total: int32(count),
  605. List: model.ForageSlice(forage).ToPB(),
  606. },
  607. }, nil
  608. }
  609. func (s *StoreEntry) ForageListSort(ctx context.Context, req *operationPb.ForageListSortRequest) error {
  610. for _, v := range req.List {
  611. if err := s.DB.Model(new(model.Forage)).Select("sort").Where("id = ?", v.Id).Updates(map[string]interface{}{
  612. "sort": v.Sort,
  613. }).Error; err != nil {
  614. zaplog.Error("ForageListSort", zap.Any("err", err), zap.Any("id", v.Id), zap.Any("sort", v.Sort))
  615. }
  616. }
  617. return nil
  618. }
  619. func (s *StoreEntry) ForageEnumList(ctx context.Context) *operationPb.ForageEnumListResponse {
  620. res := &operationPb.ForageEnumListResponse{
  621. Code: http.StatusOK,
  622. Msg: "ok",
  623. Data: &operationPb.ForageEnumList{
  624. JumpDelaType: make([]*operationPb.JumpDelaTypeEnum, 0),
  625. ForageSource: make([]*operationPb.ForageSourceEnum, 0),
  626. ForagePlanType: make([]*operationPb.ForagePlanTypeEnum, 0),
  627. CattleParentCategory: make([]*operationPb.CattleParentCategoryEnum, 0),
  628. ForageParentCategory: make([]*operationPb.ForageParentCategoryEnum, 0),
  629. IsShow: make([]*operationPb.IsShowEnum, 0),
  630. FormulaType: make([]*operationPb.FormulaTypeEnum, 0),
  631. FormulaList: make([]*operationPb.FormulaOptionEnum, 0),
  632. ConfirmStart: make([]*operationPb.IsShowEnum, 0),
  633. FormulationEvaluation: make([]*operationPb.FormulaOptionEnum, 0),
  634. UseMaterialsType: make([]*operationPb.FormulaOptionEnum, 0),
  635. UseMaterialsList: make([]*operationPb.FormulaOptionEnum, 0),
  636. PriceMaterialsType: make([]*operationPb.FormulaOptionEnum, 0),
  637. },
  638. }
  639. // 跳转延迟
  640. res.Data.JumpDelaType = append(res.Data.JumpDelaType, &operationPb.JumpDelaTypeEnum{
  641. Value: operationPb.JumpDelaType_INVALID,
  642. Label: "禁用",
  643. }, &operationPb.JumpDelaTypeEnum{
  644. Value: operationPb.JumpDelaType_THREE,
  645. Label: "3秒",
  646. }, &operationPb.JumpDelaTypeEnum{
  647. Value: operationPb.JumpDelaType_SIX,
  648. Label: "6秒",
  649. })
  650. // 饲料来源
  651. res.Data.ForageSource = append(res.Data.ForageSource, &operationPb.ForageSourceEnum{
  652. Value: operationPb.ForageSource_SYSTEM_BUILT_IN,
  653. Label: "系统内置",
  654. }, &operationPb.ForageSourceEnum{
  655. Value: operationPb.ForageSource_USER_DEFINED,
  656. Label: "用户自定义",
  657. })
  658. // 计划类型
  659. res.Data.ForagePlanType = append(res.Data.ForagePlanType,
  660. &operationPb.ForagePlanTypeEnum{
  661. Value: operationPb.ForagePlanType_INVALID,
  662. Label: "无",
  663. },
  664. &operationPb.ForagePlanTypeEnum{
  665. Value: operationPb.ForagePlanType_FORKLIFT,
  666. Label: "铲车",
  667. }, &operationPb.ForagePlanTypeEnum{
  668. Value: operationPb.ForagePlanType_CONCENTRATE,
  669. Label: "精料",
  670. })
  671. // 畜牧类别
  672. res.Data.CattleParentCategory = append(res.Data.CattleParentCategory,
  673. &operationPb.CattleParentCategoryEnum{
  674. Value: operationPb.CattleCategoryParent_INVALID,
  675. Label: "所有",
  676. },
  677. &operationPb.CattleParentCategoryEnum{
  678. Value: operationPb.CattleCategoryParent_LACTATION_CAW,
  679. Label: "泌乳牛",
  680. }, &operationPb.CattleParentCategoryEnum{
  681. Value: operationPb.CattleCategoryParent_FATTEN_CAW,
  682. Label: "育肥牛",
  683. }, &operationPb.CattleParentCategoryEnum{
  684. Value: operationPb.CattleCategoryParent_RESERVE_CAW,
  685. Label: "后备牛",
  686. }, &operationPb.CattleParentCategoryEnum{
  687. Value: operationPb.CattleCategoryParent_DRY_CAW,
  688. Label: "干奶牛",
  689. }, &operationPb.CattleParentCategoryEnum{
  690. Value: operationPb.CattleCategoryParent_PERINATAL_CAW,
  691. Label: "围产牛",
  692. }, &operationPb.CattleParentCategoryEnum{
  693. Value: operationPb.CattleCategoryParent_OTHER_CAW,
  694. Label: "其他",
  695. })
  696. // 饲料类别
  697. res.Data.ForageParentCategory = append(res.Data.ForageParentCategory, &operationPb.ForageParentCategoryEnum{
  698. Value: operationPb.ForageCategoryParent_ROUGHAGE,
  699. Label: "粗料",
  700. }, &operationPb.ForageParentCategoryEnum{
  701. Value: operationPb.ForageCategoryParent_CONCENTRATE,
  702. Label: "精料",
  703. }, &operationPb.ForageParentCategoryEnum{
  704. Value: operationPb.ForageCategoryParent_HALF_ROUGHAGE_HALF_CONCENTRATE,
  705. Label: "粗料精料参半",
  706. }, &operationPb.ForageParentCategoryEnum{
  707. Value: operationPb.ForageCategoryParent_OTHER,
  708. Label: "其他",
  709. })
  710. res.Data.IsShow = append(res.Data.IsShow, &operationPb.IsShowEnum{
  711. Value: operationPb.IsShow_OK,
  712. Label: "是",
  713. }, &operationPb.IsShowEnum{
  714. Value: operationPb.IsShow_NO,
  715. Label: "否",
  716. })
  717. res.Data.FormulationEvaluation = append(res.Data.FormulationEvaluation, &operationPb.FormulaOptionEnum{
  718. Value: 0,
  719. Label: "按配方查询",
  720. }, &operationPb.FormulaOptionEnum{
  721. Value: 1,
  722. Label: "按栏舍查询",
  723. })
  724. res.Data.FormulaType = append(res.Data.FormulaType, &operationPb.FormulaTypeEnum{
  725. Value: operationPb.FormulaType_FEED_FORMULA,
  726. Label: "饲喂配方",
  727. }, &operationPb.FormulaTypeEnum{
  728. Value: operationPb.FormulaType_PREMIXED_FORMULA,
  729. Label: "预混配方",
  730. }, &operationPb.FormulaTypeEnum{
  731. Value: operationPb.FormulaType_SUPPLEMENTARY_FORMULA,
  732. Label: "补料配方",
  733. })
  734. res.Data.ConfirmStart = append(res.Data.ConfirmStart, &operationPb.IsShowEnum{
  735. Value: operationPb.IsShow_OK,
  736. Label: "启用",
  737. }, &operationPb.IsShowEnum{
  738. Value: operationPb.IsShow_NO,
  739. Label: "禁用",
  740. })
  741. res.Data.UseMaterialsList = append(res.Data.UseMaterialsList, &operationPb.FormulaOptionEnum{
  742. Value: 1,
  743. Label: "理论",
  744. }, &operationPb.FormulaOptionEnum{
  745. Value: 2,
  746. Label: "实际",
  747. })
  748. res.Data.PriceMaterialsType = append(res.Data.PriceMaterialsType, &operationPb.FormulaOptionEnum{
  749. Value: 1,
  750. Label: "畜牧类别",
  751. }, &operationPb.FormulaOptionEnum{
  752. Value: 2,
  753. Label: "栏舍名称",
  754. }, &operationPb.FormulaOptionEnum{
  755. Value: 3,
  756. Label: "日期",
  757. }, &operationPb.FormulaOptionEnum{
  758. Value: 4,
  759. Label: "TMR设备号",
  760. })
  761. res.Data.UseMaterialsType = append(res.Data.PriceMaterialsType, &operationPb.FormulaOptionEnum{
  762. Value: 5,
  763. Label: "TMR班次",
  764. }, &operationPb.FormulaOptionEnum{
  765. Value: 6,
  766. Label: "车次",
  767. })
  768. res.Data.JumpType = append(res.Data.JumpType, &operationPb.FormulaOptionEnum{
  769. Value: 0,
  770. Label: "手动跳转",
  771. }, &operationPb.FormulaOptionEnum{
  772. Value: 1,
  773. Label: "自动跳转",
  774. })
  775. res.Data.StatisticsType = append(res.Data.StatisticsType, &operationPb.FormulaOptionEnum{
  776. Value: 7,
  777. Label: "无分类",
  778. }, &operationPb.FormulaOptionEnum{
  779. Value: 0,
  780. Label: "驾驶员",
  781. }, &operationPb.FormulaOptionEnum{
  782. Value: 1,
  783. Label: "配方名称",
  784. }, &operationPb.FormulaOptionEnum{
  785. Value: 2,
  786. Label: "栏舍名称",
  787. }, &operationPb.FormulaOptionEnum{
  788. Value: 3,
  789. Label: "牧畜类别",
  790. }, &operationPb.FormulaOptionEnum{
  791. Value: 4,
  792. Label: "车次",
  793. }, &operationPb.FormulaOptionEnum{
  794. Value: 5,
  795. Label: "TMR名称",
  796. }, &operationPb.FormulaOptionEnum{
  797. Value: 6,
  798. Label: "饲料",
  799. })
  800. res.Data.FormulaList = append(res.Data.FormulaList, &operationPb.FormulaOptionEnum{
  801. Value: 0,
  802. Label: "所有",
  803. })
  804. formulaList := make([]*model.FeedFormula, 0)
  805. if err := s.DB.Where("is_delete = ?", operationPb.IsShow_OK).Find(&formulaList).Error; err != nil {
  806. zaplog.Error("OptionFormula", zap.Any("Err", err))
  807. return res
  808. }
  809. for _, v := range formulaList {
  810. res.Data.FormulaList = append(res.Data.FormulaList, &operationPb.FormulaOptionEnum{
  811. Value: int32(v.Id),
  812. Label: v.Name,
  813. })
  814. }
  815. return res
  816. }
  817. func (s *StoreEntry) DeleteForageList(ctx context.Context, ids []int64) error {
  818. if len(ids) == 0 {
  819. return xerr.Custom("参数错误")
  820. }
  821. if err := s.DB.Model(new(model.Forage)).Where("id IN ?", ids).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  822. return xerr.WithStack(err)
  823. }
  824. return nil
  825. }
  826. func (s *StoreEntry) IsShowForage(ctx context.Context, req *operationPb.IsShowForage) error {
  827. forage := &model.Forage{Id: int64(req.ForageId)}
  828. if err := s.DB.First(forage).Error; err != nil {
  829. if errors.Is(err, gorm.ErrRecordNotFound) {
  830. return xerr.Custom("该数据不存在")
  831. }
  832. return xerr.WithStack(err)
  833. }
  834. if err := s.DB.Model(new(model.Forage)).Where("id = ?", req.ForageId).Update("is_show", req.IsShow).Error; err != nil {
  835. return xerr.WithStack(err)
  836. }
  837. return nil
  838. }
  839. // ExcelImportForage 导入excel
  840. func (s *StoreEntry) ExcelImportForage(ctx context.Context, req io.Reader) error {
  841. xlsx, err := excelize.OpenReader(req)
  842. if err != nil {
  843. return xerr.WithStack(err)
  844. }
  845. defer xlsx.Close()
  846. rows, err := xlsx.GetRows(xlsx.GetSheetName(xlsx.GetActiveSheetIndex()))
  847. if err != nil {
  848. return xerr.WithStack(err)
  849. }
  850. if len(rows) > 10000 {
  851. rows = rows[:10000]
  852. }
  853. forageList := make([]*model.Forage, 0)
  854. for i, row := range rows {
  855. if i == 0 {
  856. continue
  857. }
  858. var (
  859. forageSourceId int32
  860. categoryId, price, jumpWeight, relayLocations int
  861. allowError, packageWeight int
  862. name, uniqueEncode, backup1, backup2, backup3 string
  863. )
  864. for k, v := range row {
  865. if k == 0 {
  866. name = v
  867. }
  868. if k == 2 {
  869. uniqueEncode = v
  870. }
  871. if k == 3 {
  872. forageSourceId = operationPb.ForageSource_Kind_value[v]
  873. }
  874. if k == 5 {
  875. allowError, _ = strconv.Atoi(v)
  876. }
  877. if k == 6 {
  878. packageWeight, _ = strconv.Atoi(v)
  879. }
  880. if k == 7 {
  881. price, _ = strconv.Atoi(v)
  882. }
  883. if k == 8 {
  884. jumpWeight, _ = strconv.Atoi(v)
  885. }
  886. if k == 11 {
  887. relayLocations, _ = strconv.Atoi(v)
  888. }
  889. if k == 13 {
  890. backup1 = v
  891. }
  892. if k == 14 {
  893. backup2 = v
  894. }
  895. if k == 15 {
  896. backup3 = v
  897. }
  898. }
  899. forageItem := &model.Forage{
  900. Name: name,
  901. CategoryId: int64(categoryId),
  902. MaterialType: 0,
  903. UniqueEncode: uniqueEncode,
  904. ForageSourceId: operationPb.ForageSource_Kind(forageSourceId),
  905. PlanTypeId: 0,
  906. SmallMaterialScale: "",
  907. AllowError: int64(allowError),
  908. PackageWeight: int64(packageWeight),
  909. Price: int64(price),
  910. JumpWeight: int64(jumpWeight),
  911. JumpDelay: 0,
  912. ConfirmStart: 0,
  913. RelayLocations: int64(relayLocations),
  914. Jmp: 0,
  915. Backup1: backup1,
  916. Backup2: backup2,
  917. Backup3: backup3,
  918. IsShow: operationPb.IsShow_OK,
  919. IsDelete: operationPb.IsShow_OK,
  920. DataSource: operationPb.DataSource_EXCEL_IMPORT,
  921. }
  922. forageList = append(forageList, forageItem)
  923. }
  924. if len(forageList) > 0 {
  925. if err = s.DB.Create(forageList).Error; err != nil {
  926. return xerr.WithStack(err)
  927. }
  928. }
  929. return nil
  930. }
  931. // ExcelExportForage 流式导出excel
  932. func (s *StoreEntry) ExcelExportForage(ctx context.Context, req *operationPb.SearchForageListRequest) (*bytes.Buffer, error) {
  933. res, err := s.SearchForageList(ctx, req)
  934. if err != nil {
  935. return nil, xerr.WithStack(err)
  936. }
  937. if len(res.Data.List) <= 0 {
  938. return nil, xerr.Custom("数据为空")
  939. }
  940. file := excelize.NewFile()
  941. defer file.Close()
  942. streamWriter, err := file.NewStreamWriter(model.DefaultSheetName)
  943. if err != nil {
  944. return nil, xerr.WithStack(err)
  945. }
  946. titles := []interface{}{"饲料名称", "饲料分类", "唯一编码", "饲料来源", "计划类型", "允许误差(kg)", "包装单位重量(kg)",
  947. "单价", "跳转重量域(kg)", "跳转延迟", "确认开始", "继电器位置", "无上域", "备用字段01", "备用字段02", "备用字段03"}
  948. if err = streamWriter.SetRow("A1", titles); err != nil {
  949. return nil, xerr.WithStack(err)
  950. }
  951. for i, item := range res.Data.List {
  952. cell, err := excelize.CoordinatesToCellName(1, i+2)
  953. if err != nil {
  954. zaplog.Error("excelize.CoordinatesToCellName", zap.Any("Err", err))
  955. continue
  956. }
  957. row := make([]interface{}, 0)
  958. row = append(row, item.Name, item.CategoryName, item.UniqueEncode, item.ForageSourceId, item.PlanTypeId,
  959. item.AllowError, item.PackageWeight, float64(item.Price/100.00), item.JumpWeight, item.JumpDelay,
  960. item.ConfirmStart, item.RelayLocations, item.Jmp, item.Backup1, item.Backup2, item.Backup3)
  961. if err = streamWriter.SetRow(cell, row); err != nil {
  962. return nil, xerr.WithStack(err)
  963. }
  964. }
  965. if err = streamWriter.Flush(); err != nil {
  966. return nil, xerr.WithStack(err)
  967. }
  968. return file.WriteToBuffer()
  969. }
  970. // ExcelTemplateForage 导出模板
  971. func (s *StoreEntry) ExcelTemplateForage(ctx context.Context) (*bytes.Buffer, error) {
  972. file := excelize.NewFile()
  973. defer file.Close()
  974. streamWriter, err := file.NewStreamWriter("Sheet1")
  975. if err != nil {
  976. return nil, xerr.WithStack(err)
  977. }
  978. titles := []interface{}{"饲料名称", "饲料分类", "唯一编码", "饲料来源", "计划类型", "允许误差(kg)", "包装单位重量(kg)",
  979. "单价", "跳转重量域(kg)", "跳转延迟", "确认开始", "继电器位置", "无上域", "备用字段01", "备用字段02", "备用字段03"}
  980. if err = streamWriter.SetRow("A1", titles); err != nil {
  981. return nil, xerr.WithStack(err)
  982. }
  983. if err = streamWriter.Flush(); err != nil {
  984. return nil, xerr.WithStack(err)
  985. }
  986. return file.WriteToBuffer()
  987. }
  988. func (s *StoreEntry) SmallMaterial(ctx context.Context, req *operationPb.SmallMaterialRequest) (*model.PastureCommonResponse, error) {
  989. body := &model.PastureCommonRequest{
  990. Name: req.ApiName,
  991. ReturnType: "Map",
  992. ParamMaps: &model.FormulaEstimateParams{
  993. PastureId: fmt.Sprintf("%d", req.PastureId),
  994. ImforName: req.InfoName,
  995. },
  996. }
  997. response := &model.PastureCommonResponse{Data: &model.PastureCommonData{}}
  998. if _, err := s.PastureHttpClient(ctx, model.UrlDataByName, int64(req.PastureId), body, response); err != nil {
  999. return nil, xerr.WithStack(err)
  1000. }
  1001. return response, nil
  1002. }