feed_service.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. package backend
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "kpt-tmr-group/model"
  10. "kpt-tmr-group/pkg/logger/zaplog"
  11. "kpt-tmr-group/pkg/xerr"
  12. operationPb "kpt-tmr-group/proto/go/backend/operation"
  13. "net/http"
  14. "strconv"
  15. "sync"
  16. "time"
  17. "go.uber.org/multierr"
  18. "github.com/xuri/excelize/v2"
  19. "go.uber.org/zap"
  20. "gorm.io/gorm"
  21. )
  22. const EncodeNumberPrefix = "encode_number"
  23. var PastureDataLogType = map[string]int32{
  24. "FeedFormula_Distribute": 1,
  25. "FeedFormula_IsModify": 2,
  26. "FeedFormula_Cancel_Distribute": 3,
  27. }
  28. // CreateFeedFormula 添加数据
  29. func (s *StoreEntry) CreateFeedFormula(ctx context.Context, req *operationPb.AddFeedFormulaRequest) error {
  30. forage := model.NewFeedFormula(req)
  31. if err := s.DB.Create(forage).Error; err != nil {
  32. return xerr.WithStack(err)
  33. }
  34. return nil
  35. }
  36. // EditFeedFormula 编辑数据
  37. func (s *StoreEntry) EditFeedFormula(ctx context.Context, req *operationPb.AddFeedFormulaRequest) error {
  38. forage := model.FeedFormula{Id: int64(req.Id)}
  39. if err := s.DB.Where("is_delete = ?", operationPb.IsShow_OK).First(&forage).Error; err != nil {
  40. if errors.Is(err, gorm.ErrRecordNotFound) {
  41. return xerr.Custom("该数据不存在")
  42. }
  43. return xerr.WithStack(err)
  44. }
  45. updateData := &model.FeedFormula{
  46. Name: req.Name,
  47. Colour: req.Colour,
  48. CattleCategoryId: req.CattleCategoryId,
  49. CattleCategoryName: req.CattleCategoryName,
  50. FormulaTypeId: req.FormulaTypeId,
  51. FormulaTypeName: req.FormulaTypeName,
  52. DataSourceId: req.DataSourceId,
  53. DataSourceName: req.DataSourceName,
  54. Remarks: req.Remarks,
  55. IsShow: req.IsShow,
  56. }
  57. if err := s.DB.Model(new(model.FeedFormula)).
  58. Omit("is_show", "is_delete", "encode_number", "formula_type_id", "formula_type_name", "data_source", "version", "is_modify").
  59. Where("id = ?", req.Id).
  60. Updates(updateData).Error; err != nil {
  61. return xerr.WithStack(err)
  62. }
  63. return nil
  64. }
  65. // SearchFeedFormulaList 查询数据列表
  66. func (s *StoreEntry) SearchFeedFormulaList(ctx context.Context, req *operationPb.SearchFeedFormulaRequest) (*operationPb.SearchFeedFormulaListResponse, error) {
  67. feedFormula := make([]*model.FeedFormula, 0)
  68. var count int64 = 0
  69. pref := s.DB.Model(new(model.FeedFormula)).Where("is_delete = ?", operationPb.IsShow_OK)
  70. if req.Name != "" {
  71. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  72. }
  73. if req.CattleCategoryId > 0 {
  74. pref.Where("cattle_category_id = ?", req.CattleCategoryId)
  75. }
  76. if req.FormulaTypeId > 0 {
  77. pref.Where("formula_type_id = ?", req.FormulaTypeId)
  78. }
  79. if req.IsShow > 0 {
  80. pref.Where("is_show = ?", req.IsShow)
  81. }
  82. if req.DataSource > 0 {
  83. pref.Where("data_source = ?", req.DataSource)
  84. }
  85. if req.Remarks != "" {
  86. pref.Where("remarks = ?", req.Remarks)
  87. }
  88. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  89. Find(&feedFormula).Error; err != nil {
  90. return nil, xerr.WithStack(err)
  91. }
  92. return &operationPb.SearchFeedFormulaListResponse{
  93. Code: http.StatusOK,
  94. Msg: "ok",
  95. Data: &operationPb.SearchFeedFormulaListData{
  96. Page: req.Pagination.Page,
  97. PageSize: req.Pagination.PageSize,
  98. Total: int32(count),
  99. List: model.FeedFormulaSlice(feedFormula).ToPB(),
  100. },
  101. }, nil
  102. }
  103. // SearchFeedFormulaById 查询指定数据
  104. func (s *StoreEntry) SearchFeedFormulaById(ctx context.Context, foodFormulaId int64) (*model.FeedFormula, error) {
  105. feedFormula := &model.FeedFormula{}
  106. if err := s.DB.Model(new(model.FeedFormula)).
  107. Where("is_delete = ?", operationPb.IsShow_OK).
  108. Where("id = ?", foodFormulaId).
  109. First(feedFormula).Error; err != nil {
  110. return nil, xerr.WithStack(err)
  111. }
  112. return feedFormula, nil
  113. }
  114. // IsShowFeedFormula 是否启用和是否可修改
  115. func (s *StoreEntry) IsShowFeedFormula(ctx context.Context, req *operationPb.IsShowModifyFeedFormula) error {
  116. feedFormula := &model.FeedFormula{Id: int64(req.FeedFormulaId)}
  117. if err := s.DB.First(feedFormula).Error; err != nil {
  118. if errors.Is(err, gorm.ErrRecordNotFound) {
  119. return xerr.Custom("该数据不存在")
  120. }
  121. return xerr.WithStack(err)
  122. }
  123. if req.EditType == 1 {
  124. if err := s.DB.Model(new(model.FeedFormula)).Where("id = ?", req.FeedFormulaId).Update("is_show", req.IsShow).Error; err != nil {
  125. return xerr.WithStack(err)
  126. }
  127. }
  128. if req.EditType == 2 {
  129. if err := s.DB.Model(new(model.FeedFormula)).Where("id = ?", req.FeedFormulaId).Update("is_modify", req.IsShow).Error; err != nil {
  130. return xerr.WithStack(err)
  131. } else {
  132. s.PastureFeedFormulaIsModify(ctx, req.FeedFormulaId, req.IsShow)
  133. }
  134. }
  135. return nil
  136. }
  137. // DeleteFeedFormula 是否删除
  138. func (s *StoreEntry) DeleteFeedFormula(ctx context.Context, feedFormulaId int64) error {
  139. feedFormula := &model.FeedFormula{Id: feedFormulaId}
  140. if err := s.DB.First(feedFormula).Error; err != nil {
  141. if errors.Is(err, gorm.ErrRecordNotFound) {
  142. return xerr.Custom("该数据不存在")
  143. }
  144. return xerr.WithStack(err)
  145. }
  146. if err := s.DB.Model(new(model.FeedFormula)).Where("id = ?", feedFormula.Id).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  147. return xerr.WithStack(err)
  148. }
  149. return nil
  150. }
  151. // ExcelImportFeedFormula 导入excel
  152. func (s *StoreEntry) ExcelImportFeedFormula(ctx context.Context, req io.Reader) error {
  153. xlsx, err := excelize.OpenReader(req)
  154. if err != nil {
  155. return xerr.WithStack(err)
  156. }
  157. defer xlsx.Close()
  158. rows, err := xlsx.GetRows(xlsx.GetSheetName(xlsx.GetActiveSheetIndex()))
  159. if err != nil {
  160. return xerr.WithStack(err)
  161. }
  162. if len(rows) > 10000 {
  163. rows = rows[:10000]
  164. }
  165. feedFormulaList := make([]*model.FeedFormula, 0)
  166. for i, row := range rows {
  167. if i == 0 {
  168. continue
  169. }
  170. var (
  171. name, encodeNumber, cattleCategoryName, formulaTypeName, dataSourceName, remarks, isShowStr string
  172. isShow operationPb.IsShow_Kind
  173. )
  174. for k, v := range row {
  175. if k == 0 {
  176. name = v
  177. }
  178. if k == 1 {
  179. encodeNumber = v
  180. }
  181. if k == 2 {
  182. cattleCategoryName = v
  183. }
  184. if k == 3 {
  185. formulaTypeName = v
  186. }
  187. if k == 4 {
  188. dataSourceName = v
  189. }
  190. if k == 5 {
  191. remarks = v
  192. }
  193. if k == 6 {
  194. isShowStr = v
  195. }
  196. }
  197. if isShowStr == "是" {
  198. isShow = operationPb.IsShow_OK
  199. } else {
  200. isShow = operationPb.IsShow_NO
  201. }
  202. feedFormulaItem := &model.FeedFormula{
  203. Name: name,
  204. EncodeNumber: encodeNumber,
  205. CattleCategoryName: cattleCategoryName,
  206. FormulaTypeName: formulaTypeName,
  207. Remarks: remarks,
  208. IsShow: isShow,
  209. IsDelete: operationPb.IsShow_OK,
  210. DataSourceId: operationPb.DataSource_EXCEL_IMPORT,
  211. DataSourceName: dataSourceName,
  212. }
  213. feedFormulaList = append(feedFormulaList, feedFormulaItem)
  214. }
  215. if len(feedFormulaList) > 0 {
  216. if err = s.DB.Create(feedFormulaList).Error; err != nil {
  217. return xerr.WithStack(err)
  218. }
  219. }
  220. return nil
  221. }
  222. // ExcelExportFeedFormula 流式导出excel
  223. func (s *StoreEntry) ExcelExportFeedFormula(ctx context.Context, req *operationPb.SearchFeedFormulaRequest) (*bytes.Buffer, error) {
  224. res, err := s.SearchFeedFormulaList(ctx, req)
  225. if err != nil {
  226. return nil, xerr.WithStack(err)
  227. }
  228. if len(res.Data.List) <= 0 {
  229. return nil, xerr.Custom("数据为空")
  230. }
  231. file := excelize.NewFile()
  232. defer file.Close()
  233. streamWriter, err := file.NewStreamWriter("Sheet1")
  234. if err != nil {
  235. return nil, xerr.WithStack(err)
  236. }
  237. titles := []interface{}{"配方名称", "配方编码", "畜牧类别", "配方类别", "来源", "备注", "是否启用",
  238. "饲料组", "饲料名称", "重量(kg)", "搅拌延迟(min)", "是否锁定牛头数比例", "顺序"}
  239. if err = streamWriter.SetRow("A1", titles); err != nil {
  240. return nil, xerr.WithStack(err)
  241. }
  242. for i, item := range res.Data.List {
  243. cell, err := excelize.CoordinatesToCellName(1, i+2)
  244. if err != nil {
  245. zaplog.Error("excelize.CoordinatesToCellName", zap.Any("Err", err))
  246. continue
  247. }
  248. row := make([]interface{}, 0)
  249. row = append(row, item.Name, item.EncodeNumber, item.CattleCategoryName, item.FormulaTypeName, item.DataSourceName,
  250. item.Remarks, item.IsShow)
  251. if err = streamWriter.SetRow(cell, row); err != nil {
  252. return nil, xerr.WithStack(err)
  253. }
  254. }
  255. if err = streamWriter.Flush(); err != nil {
  256. return nil, xerr.WithStack(err)
  257. }
  258. return file.WriteToBuffer()
  259. }
  260. // ExcelTemplateFeedFormula 导出模板
  261. func (s *StoreEntry) ExcelTemplateFeedFormula(ctx context.Context) (*bytes.Buffer, error) {
  262. file := excelize.NewFile()
  263. defer file.Close()
  264. streamWriter, err := file.NewStreamWriter("Sheet1")
  265. if err != nil {
  266. return nil, xerr.WithStack(err)
  267. }
  268. titles := []interface{}{"配方名称", "配方编码", "畜牧类别", "配方类别", "来源", "备注", "是否启用",
  269. "饲料组", "饲料名称", "重量(kg)", "搅拌延迟(min)", "是否锁定牛头数比例", "顺序"}
  270. if err = streamWriter.SetRow("A1", titles); err != nil {
  271. return nil, xerr.WithStack(err)
  272. }
  273. if err = streamWriter.Flush(); err != nil {
  274. return nil, xerr.WithStack(err)
  275. }
  276. return file.WriteToBuffer()
  277. }
  278. // EncodeNumber 配方编码
  279. func (s *StoreEntry) EncodeNumber(ctx context.Context) string {
  280. currTime := time.Now().Format(model.LayoutDate)
  281. prefix := fmt.Sprintf("%s_%s", EncodeNumberPrefix, currTime)
  282. data := &model.UniqueData{}
  283. if err := s.DB.Order("id desc").Where("prefix = ?", prefix).First(data).Error; err != nil {
  284. if !errors.Is(err, gorm.ErrRecordNotFound) {
  285. return ""
  286. }
  287. ud, _ := strconv.Atoi(currTime)
  288. result := ud*100 + 1
  289. newData := &model.UniqueData{
  290. Prefix: prefix,
  291. Data: int64(result),
  292. }
  293. if err = s.DB.Create(newData).Error; err != nil {
  294. zaplog.Error("EncodeNumber Create", zap.Any("data", newData), zap.Any("Err", err))
  295. return ""
  296. }
  297. return fmt.Sprintf("%d", newData.Data)
  298. }
  299. data.Data += 1
  300. if err := s.DB.Model(new(model.UniqueData)).Where("prefix = ?", prefix).Update("data", data.Data).Error; err != nil {
  301. return ""
  302. } else {
  303. return fmt.Sprintf("%d", data.Data)
  304. }
  305. }
  306. // DistributeFeedFormula 配方下发牧场
  307. func (s *StoreEntry) DistributeFeedFormula(ctx context.Context, req *operationPb.DistributeFeedFormulaRequest) error {
  308. distributeData, err := s.checkoutDistributeData(ctx, req)
  309. if err != nil {
  310. return xerr.WithStack(err)
  311. }
  312. wg := sync.WaitGroup{}
  313. wg.Add(len(distributeData.PastureList))
  314. var muError error
  315. for _, pasture := range distributeData.PastureList {
  316. go func(p *model.GroupPasture) {
  317. defer wg.Done()
  318. // 过滤已下发的
  319. body := make([]*model.FeedFormula, 0)
  320. for _, v := range distributeData.FeedFormulaList {
  321. if ok := s.checkoutDistributeLog(ctx, p.Id, v.Id); !ok {
  322. body = append(body, v)
  323. }
  324. }
  325. if len(body) <= 0 {
  326. return
  327. }
  328. request := &model.DistributeFeedFormulaRequest{
  329. PastureId: p.Id,
  330. Body: body,
  331. }
  332. response := &model.PastureResponse{}
  333. defer func() {
  334. if response.Code == http.StatusOK {
  335. s.DB.Create(model.NewFeedFormulaDistributeLogList(distributeData.FeedFormulaList, p.Id, p.Name, operationPb.IsShow_OK))
  336. } else {
  337. muError = multierr.Append(muError, xerr.Custom(response.Msg))
  338. }
  339. }()
  340. if _, err = s.PastureHttpClient(ctx, model.FeedFormulaDistributeUrl, p.Id, request, response); err != nil {
  341. muError = multierr.Append(muError, err)
  342. zaplog.Error("DistributeFeedFormula", zap.Any("pasture", p), zap.Any("body", distributeData.FeedFormulaList), zap.Any("err", err), zap.Any("response", response))
  343. b, _ := json.Marshal(request)
  344. res, _ := json.Marshal(response)
  345. pastureDataLog := model.NewPastureDataLog(p.Id, PastureDataLogType["FeedFormula_Distribute"], model.FeedFormulaDistributeUrl, string(b), string(res))
  346. s.DB.Create(pastureDataLog)
  347. }
  348. }(pasture)
  349. }
  350. wg.Wait()
  351. return muError
  352. }
  353. // CancelDistributeFeedFormula 取消配方下发牧场
  354. func (s *StoreEntry) CancelDistributeFeedFormula(ctx context.Context, req *operationPb.DistributeFeedFormulaRequest) error {
  355. distributeData, err := s.checkoutDistributeData(ctx, req)
  356. if err != nil {
  357. return xerr.WithStack(err)
  358. }
  359. wg := sync.WaitGroup{}
  360. wg.Add(len(distributeData.PastureList))
  361. var muError error
  362. for _, pasture := range distributeData.PastureList {
  363. go func(p *model.GroupPasture) {
  364. defer wg.Done()
  365. pastureDataId := make([]int64, 0)
  366. for _, v := range distributeData.FeedFormulaList {
  367. if v.PastureId == p.Id {
  368. pastureDataId = append(pastureDataId, v.PastureDataId)
  369. }
  370. }
  371. if len(pastureDataId) <= 0 {
  372. return
  373. }
  374. request := &model.CancelDistributeFeedFormulaRequest{
  375. PastureId: p.Id,
  376. PastureDataId: pastureDataId,
  377. }
  378. response := &model.PastureResponse{}
  379. if _, err = s.PastureHttpClient(ctx, model.FeedFormulaCancelDistributeUrl, p.Id, request, response); err != nil {
  380. zaplog.Error("DistributeFeedFormula", zap.Any("pasture", p), zap.Any("body", distributeData.FeedFormulaList), zap.Any("err", err), zap.Any("response", response))
  381. b, _ := json.Marshal(request)
  382. res, _ := json.Marshal(response)
  383. pastureDataLog := model.NewPastureDataLog(p.Id, PastureDataLogType["FeedFormula_Cancel_Distribute"], model.FeedFormulaCancelDistributeUrl, string(b), string(res))
  384. s.DB.Create(pastureDataLog)
  385. }
  386. }(pasture)
  387. }
  388. wg.Wait()
  389. return muError
  390. }
  391. // EditRecodeFeedFormula 配方修改记录
  392. func (s *StoreEntry) EditRecodeFeedFormula(ctx context.Context, req *operationPb.EditRecodeFeedFormulaRequest) (*operationPb.EditRecodeFeedFormulaResponse, error) {
  393. return nil, nil
  394. }
  395. func (s *StoreEntry) FeedFormulaDetailList(ctx context.Context, req *operationPb.FeedFormulaDetailRequest) (*operationPb.FeedFormulaDetailResponse, error) {
  396. feedFormula, err := s.SearchFeedFormulaById(ctx, int64(req.FeedFormulaId))
  397. if err != nil {
  398. return nil, xerr.WithStack(err)
  399. }
  400. feedFormulaId := feedFormula.Id
  401. if feedFormula.PastureDataId > 0 {
  402. feedFormulaId = feedFormula.PastureDataId
  403. }
  404. list, err := s.SearchFeedFormalDetailById(ctx, feedFormulaId, feedFormula.PastureId)
  405. if err != nil {
  406. return nil, xerr.WithStack(err)
  407. }
  408. return &operationPb.FeedFormulaDetailResponse{
  409. Code: http.StatusOK,
  410. Msg: "ok",
  411. Data: model.FeedFormulaDetailSlice(list).ToPB(),
  412. }, nil
  413. }
  414. // FeedFormulaUsage 配方使用概况
  415. func (s *StoreEntry) FeedFormulaUsage(ctx context.Context, req *operationPb.FeedFormulaUsageRequest) (*operationPb.FeedFormulaUsageResponse, error) {
  416. feedFormulaDistributeLogList := make([]*model.FeedFormulaDistributeLog, 0)
  417. if err := s.DB.Model(new(model.FeedFormulaDistributeLog)).
  418. Where("feed_formula_id = ?", req.FeedFormulaId).
  419. Where("is_show = ?", operationPb.IsShow_OK).Group("pasture_id").
  420. Find(&feedFormulaDistributeLogList).Error; err != nil {
  421. return nil, xerr.WithStack(err)
  422. }
  423. res := &operationPb.FeedFormulaUsageResponse{
  424. Code: http.StatusOK,
  425. Msg: "ok",
  426. Data: make([]*operationPb.FeedFormulaUsageList, 0),
  427. }
  428. wg := sync.WaitGroup{}
  429. wg.Add(len(feedFormulaDistributeLogList))
  430. for _, list := range feedFormulaDistributeLogList {
  431. go func(l *model.FeedFormulaDistributeLog) {
  432. defer wg.Done()
  433. groupDetail, err := s.PastureDetailById(ctx, l.PastureId)
  434. if err != nil {
  435. zaplog.Error("FeedFormulaUsage", zap.Any("PastureDetailById", err))
  436. return
  437. }
  438. req.PastureId = int32(groupDetail.PastureId)
  439. response := &operationPb.PastureFeedFormulaUsageResponse{}
  440. if _, err = s.PastureHttpClient(ctx, model.FeedUsageURl, groupDetail.Id, req, response); err != nil {
  441. zaplog.Error("FeedFormulaUsage", zap.Any("PastureDetailById", err))
  442. return
  443. }
  444. if response.Code == http.StatusOK {
  445. data := &operationPb.FeedFormulaUsageList{
  446. PastureId: int32(groupDetail.Id),
  447. PastureName: groupDetail.Name,
  448. MixedFodderAccurateRatio: response.Data.MixedFodderAccurateRatio,
  449. MixedFodderCorrectRatio: response.Data.MixedFodderCorrectRatio,
  450. SprinkleFodderAccurateRatio: response.Data.SprinkleFodderAccurateRatio,
  451. SprinkleFodderCorrectRatio: response.Data.SprinkleFodderCorrectRatio,
  452. AddFeedTime: response.Data.AddFeedTime,
  453. SprinkleTime: response.Data.SprinkleTime,
  454. StirTime: response.Data.StirTime,
  455. LastEditTime: response.Data.LastEditTime,
  456. }
  457. res.Data = append(res.Data, data)
  458. } else {
  459. zaplog.Error("FeedFormulaUsage-http", zap.Any("response", response))
  460. return
  461. }
  462. }(list)
  463. }
  464. wg.Wait()
  465. return res, nil
  466. }
  467. func (s *StoreEntry) PastureFeedFormulaIsModify(ctx context.Context, feedFormulaId int32, isModify operationPb.IsShow_Kind) {
  468. feedFormulaDistributeLogList := make([]*model.FeedFormulaDistributeLog, 0)
  469. if err := s.DB.Where("is_show = ?", operationPb.IsShow_OK).
  470. Where("feed_formula_id = ?", feedFormulaId).
  471. Group("pasture_id").Find(&feedFormulaDistributeLogList).Error; err != nil {
  472. zaplog.Error("PastureFeedFormulaIsModify", zap.Any("err", err), zap.Any("feed_formula_id", feedFormulaId))
  473. return
  474. }
  475. for _, v := range feedFormulaDistributeLogList {
  476. response := &model.PastureResponse{}
  477. request := &model.FeedFormulaIsModifyRequest{
  478. PastureId: v.PastureId,
  479. FeedFormulaId: v.FeedFormulaId,
  480. IsModify: int32(isModify),
  481. }
  482. if _, err := s.PastureHttpClient(ctx, model.FeedFormulaIsModifyUrl, v.Id, request, response); err != nil {
  483. zaplog.Error("PastureFeedFormulaIsModify", zap.Any("request", request), zap.Any("err", err), zap.Any("response", response))
  484. b, _ := json.Marshal(request)
  485. res, _ := json.Marshal(response)
  486. pastureDataLog := model.NewPastureDataLog(v.PastureId, PastureDataLogType["FeedFormula_IsModify"], model.FeedFormulaIsModifyUrl, string(b), string(res))
  487. s.DB.Create(pastureDataLog)
  488. }
  489. }
  490. }
  491. func (s *StoreEntry) checkoutDistributeData(ctx context.Context, req *operationPb.DistributeFeedFormulaRequest) (*model.DistributeData, error) {
  492. result := &model.DistributeData{
  493. PastureList: make([]*model.GroupPasture, 0),
  494. FeedFormulaList: make([]*model.FeedFormula, 0),
  495. }
  496. if err := s.DB.Where("id IN ?", req.PastureIds).Where("is_delete = ?", operationPb.IsShow_OK).Find(&result.PastureList).Error; err != nil {
  497. return result, xerr.WithStack(err)
  498. }
  499. if err := s.DB.Where("id IN ?", req.FeedFormulaIds).Find(&result.FeedFormulaList).Error; err != nil {
  500. return result, xerr.WithStack(err)
  501. }
  502. if len(result.PastureList) <= 0 || len(result.FeedFormulaList) <= 0 {
  503. return result, xerr.Customf("数据错误")
  504. }
  505. return result, nil
  506. }
  507. func (s *StoreEntry) checkoutDistributeLog(ctx context.Context, pastureId, feedFormulaId int64) bool {
  508. res := &model.FeedFormulaDistributeLog{}
  509. if err := s.DB.Model(new(model.FeedFormulaDistributeLog)).Where("feed_formula_id = ?", feedFormulaId).
  510. Where("pasture_id = ?", pastureId).Where("is_show = ?", operationPb.IsShow_OK).First(res).Error; err != nil {
  511. return false
  512. }
  513. if res.IsShow == operationPb.IsShow_OK {
  514. return true
  515. }
  516. return false
  517. }
  518. func (s *StoreEntry) SearchFeedFormalDetailById(ctx context.Context, feedFormulaId, pastureId int64) ([]*model.FeedFormulaDetail, error) {
  519. res := make([]*model.FeedFormulaDetail, 0)
  520. if err := s.DB.Model(new(model.FeedFormulaDetail)).Where("pasture_id = ?", pastureId).
  521. Where("feed_formula_id = ?", feedFormulaId).
  522. Where("is_show = ?", operationPb.IsShow_OK).
  523. Order("id desc").Find(&res).Error; err != nil {
  524. return nil, xerr.WithStack(err)
  525. }
  526. return res, nil
  527. }