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