pasture_service.go 35 KB

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