pasture_service.go 35 KB

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