pasture_service.go 34 KB

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