feed_service.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. var EditRecodeMap = map[string]string{
  29. "forage_name": "饲料名称",
  30. "weight": "重量",
  31. "stir_delay": "搅拌延迟",
  32. "allow_error": "允许误差",
  33. "sort": "排序",
  34. }
  35. // CreateFeedFormula 添加数据
  36. func (s *StoreEntry) CreateFeedFormula(ctx context.Context, req *operationPb.AddFeedFormulaRequest) error {
  37. forage := model.NewFeedFormula(req)
  38. if err := s.DB.Create(forage).Error; err != nil {
  39. return xerr.WithStack(err)
  40. }
  41. return nil
  42. }
  43. // EditFeedFormula 编辑数据
  44. func (s *StoreEntry) EditFeedFormula(ctx context.Context, req *operationPb.AddFeedFormulaRequest) error {
  45. feedFormula := &model.FeedFormula{Id: int64(req.Id)}
  46. if err := s.DB.Where("is_delete = ?", operationPb.IsShow_OK).First(feedFormula).Error; err != nil {
  47. if errors.Is(err, gorm.ErrRecordNotFound) {
  48. return xerr.Custom("该数据不存在")
  49. }
  50. return xerr.WithStack(err)
  51. }
  52. // 更新版本号
  53. defer s.UpdateFeedFormalVersion(ctx, feedFormula)
  54. updateData := &model.FeedFormula{
  55. Name: req.Name,
  56. Colour: req.Colour,
  57. CattleCategoryId: req.CattleCategoryId,
  58. CattleCategoryName: req.CattleCategoryName,
  59. FormulaTypeId: req.FormulaTypeId,
  60. FormulaTypeName: req.FormulaTypeName,
  61. DataSourceId: req.DataSourceId,
  62. DataSourceName: req.DataSourceName,
  63. Remarks: req.Remarks,
  64. IsShow: req.IsShow,
  65. }
  66. if err := s.DB.Model(new(model.FeedFormula)).
  67. Omit("is_show", "is_delete", "encode_number", "formula_type_id", "formula_type_name", "data_source", "is_modify").
  68. Where("id = ?", req.Id).
  69. Updates(updateData).Error; err != nil {
  70. return xerr.WithStack(err)
  71. }
  72. return nil
  73. }
  74. // AddFeedByFeedFormula 配方添加饲料
  75. func (s *StoreEntry) AddFeedByFeedFormula(ctx context.Context, req *operationPb.GroupAddFeedFormulaDetail) error {
  76. feedFormulaData := &model.FeedFormula{Id: int64(req.FeedFormulaId)}
  77. if err := s.DB.Model(new(model.FeedFormula)).First(feedFormulaData).Error; err != nil {
  78. return xerr.WithStack(err)
  79. }
  80. // 更新修改记录
  81. defer s.addFeedFormulaDetailAddRecode(ctx, req)
  82. // 更新版本号
  83. defer s.UpdateFeedFormalVersion(ctx, feedFormulaData)
  84. insertData := make([]*model.FeedFormulaDetail, 0)
  85. for _, v := range req.List {
  86. feedData := &model.Forage{Id: int64(v.ForageId)}
  87. if err := s.DB.Model(new(model.Forage)).First(feedData).Error; err != nil {
  88. return xerr.WithStack(err)
  89. }
  90. if v.AllowError > v.StirDelay {
  91. return xerr.Customf("允许误差不能大于搅拌延迟")
  92. }
  93. insertData = append(insertData, &model.FeedFormulaDetail{
  94. PastureName: "集团",
  95. FeedFormulaId: int64(req.FeedFormulaId),
  96. ForageId: int64(v.ForageId),
  97. ForageName: v.ForageName,
  98. ForageGroupName: v.ForageGroupName,
  99. Weight: int32(v.Weight * 100),
  100. StirDelay: v.StirDelay,
  101. AllowError: v.AllowError,
  102. IsShow: operationPb.IsShow_OK,
  103. IsModify: v.IsModify,
  104. Sort: v.Sort,
  105. })
  106. }
  107. if err := s.DB.Model(new(model.FeedFormulaDetail)).Save(insertData).Error; err != nil {
  108. return xerr.WithStack(err)
  109. }
  110. return nil
  111. }
  112. // addFeedFormulaDetailAddRecode 添加配方记录
  113. func (s *StoreEntry) addFeedFormulaDetailAddRecode(ctx context.Context, req *operationPb.GroupAddFeedFormulaDetail) {
  114. editRecordList := make([]*model.FeedFormulaEditRecord, 0)
  115. for _, v := range req.List {
  116. editRecordList = append(editRecordList, &model.FeedFormulaEditRecord{
  117. FeedFormulaId: int64(req.FeedFormulaId),
  118. PastureName: "集团",
  119. ForageName: v.ForageName,
  120. Status: operationPb.FeedFormulaEditRecordType_INSERT,
  121. })
  122. }
  123. if err := s.CreateFeedFormulaEditRecord(ctx, editRecordList); err != nil {
  124. zaplog.Error("deleteFeedFormulaDetailAddRecode", zap.Any("CreateFeedFormulaEditRecord", err))
  125. }
  126. }
  127. // EditFeedByFeedFormula 配方饲料编辑
  128. func (s *StoreEntry) EditFeedByFeedFormula(ctx context.Context, req *operationPb.AddFeedFormulaDetail) error {
  129. feedFormulaData := &model.FeedFormula{Id: int64(req.FeedFormulaId)}
  130. if err := s.DB.Model(new(model.FeedFormula)).First(feedFormulaData).Error; err != nil {
  131. return xerr.WithStack(err)
  132. }
  133. feedFormulaDetail := &model.FeedFormulaDetail{Id: int64(req.Id)}
  134. if err := s.DB.Model(new(model.FeedFormulaDetail)).
  135. Where("is_show = ?", operationPb.IsShow_OK).
  136. First(feedFormulaDetail).Error; err != nil {
  137. return xerr.WithStack(err)
  138. }
  139. // 添加修改记录
  140. defer s.editFeedFormulaDetailAddRecode(ctx, req, feedFormulaDetail)
  141. // 更新版本号
  142. defer s.UpdateFeedFormalVersion(ctx, feedFormulaData)
  143. // 更新数据
  144. updateData := &model.FeedFormulaDetail{
  145. ForageId: int64(req.ForageId),
  146. ForageName: req.ForageName,
  147. ForageGroupName: req.ForageGroupName,
  148. Weight: int32(req.Weight * 100),
  149. StirDelay: req.StirDelay,
  150. AllowError: req.AllowError,
  151. Sort: req.Sort,
  152. }
  153. if err := s.DB.Model(new(model.FeedFormulaDetail)).Where("id = ?", req.Id).Updates(updateData).Error; err != nil {
  154. return xerr.WithStack(err)
  155. }
  156. return nil
  157. }
  158. // EditFeedFormulaDetailAddRecode 更新饲料配方修改记录
  159. func (s *StoreEntry) editFeedFormulaDetailAddRecode(ctx context.Context, req *operationPb.AddFeedFormulaDetail, feedFormulaDetail *model.FeedFormulaDetail) {
  160. editRecordList := make([]*model.FeedFormulaEditRecord, 0)
  161. editRecordData := &model.FeedFormulaEditRecord{
  162. FeedFormulaId: int64(req.FeedFormulaId),
  163. PastureName: "集团",
  164. ForageName: req.ForageName,
  165. Status: operationPb.FeedFormulaEditRecordType_UPDATE,
  166. }
  167. if operationName, err := s.GetCurrentUserName(ctx); err != nil {
  168. zaplog.Error("EditFeedByFeedFormula", zap.Any("GetCurrentUserName", err))
  169. } else {
  170. editRecordData.OperationName = operationName
  171. }
  172. lastGroupIdData := &model.FeedFormulaEditRecord{}
  173. if err := s.DB.Model(new(model.FeedFormulaEditRecord)).
  174. Where("is_show = ?", operationPb.IsShow_OK).
  175. Order("group_id desc").
  176. First(&lastGroupIdData).Error; err != nil {
  177. zaplog.Error("EditFeedByFeedFormula", zap.Any("lastGroupIdData", err))
  178. } else {
  179. editRecordData.GroupId = lastGroupIdData.GroupId + 1
  180. editRecordData.BeforeValue = lastGroupIdData.BeforeValue
  181. }
  182. if feedFormulaDetail.ForageName != req.ForageName {
  183. editRecordData.FieldName = EditRecodeMap["forage_name"]
  184. editRecordData.BeforeValue = lastGroupIdData.ForageName
  185. editRecordData.AfterValue = req.ForageName
  186. editRecordList = append(editRecordList, editRecordData)
  187. }
  188. if feedFormulaDetail.Weight != int32(req.Weight*100) {
  189. editRecordData.FieldName = EditRecodeMap["weight"]
  190. editRecordData.AfterValue = fmt.Sprintf("%d", int32(req.Weight*100))
  191. editRecordList = append(editRecordList, editRecordData)
  192. }
  193. if feedFormulaDetail.AllowError != req.AllowError {
  194. editRecordData.FieldName = EditRecodeMap["allow_error"]
  195. editRecordData.AfterValue = fmt.Sprintf("%d", req.AllowError)
  196. editRecordList = append(editRecordList, editRecordData)
  197. }
  198. if feedFormulaDetail.StirDelay != req.StirDelay {
  199. editRecordData.FieldName = EditRecodeMap["stir_delay"]
  200. editRecordData.AfterValue = fmt.Sprintf("%d", req.StirDelay)
  201. editRecordList = append(editRecordList, editRecordData)
  202. }
  203. if feedFormulaDetail.Sort != req.Sort {
  204. editRecordData.FieldName = EditRecodeMap["sort"]
  205. editRecordData.AfterValue = fmt.Sprintf("%d", req.Sort)
  206. editRecordList = append(editRecordList, editRecordData)
  207. }
  208. if err := s.CreateFeedFormulaEditRecord(ctx, editRecordList); err != nil {
  209. zaplog.Error("EditFeedByFeedFormula", zap.Any("CreateFeedFormulaEditRecord", err))
  210. }
  211. }
  212. // CreateFeedFormulaEditRecord 创建配方修改记录
  213. func (s *StoreEntry) CreateFeedFormulaEditRecord(ctx context.Context, req []*model.FeedFormulaEditRecord) error {
  214. if req == nil {
  215. return nil
  216. }
  217. if err := s.DB.Model(new(model.FeedFormulaEditRecord)).Save(req).Error; err != nil {
  218. return xerr.WithStack(err)
  219. }
  220. return nil
  221. }
  222. // FeedFormulaDetailBySort 配方饲料排序
  223. func (s *StoreEntry) FeedFormulaDetailBySort(ctx context.Context, req *operationPb.GroupAddFeedFormulaDetail) error {
  224. feedFormulaData := &model.FeedFormula{Id: int64(req.FeedFormulaId)}
  225. if err := s.DB.Model(new(model.FeedFormula)).First(feedFormulaData).Error; err != nil {
  226. return xerr.WithStack(err)
  227. }
  228. // 更新版本号
  229. defer s.UpdateFeedFormalVersion(ctx, feedFormulaData)
  230. tx := s.DB.Transaction(func(tx *gorm.DB) error {
  231. for _, v := range req.List {
  232. if err := tx.Model(new(model.FeedFormulaDetail)).
  233. Where("id = ?", v.Id).
  234. Updates(map[string]interface{}{
  235. "sort": v.Sort,
  236. }).Error; err != nil {
  237. return xerr.WithStack(err)
  238. }
  239. }
  240. return nil
  241. })
  242. return tx
  243. }
  244. // FeedFormulaDetailIsModify 配方饲料是否可修改
  245. func (s *StoreEntry) FeedFormulaDetailIsModify(ctx context.Context, req *operationPb.AddFeedFormulaDetail) error {
  246. feedFormulaData := &model.FeedFormula{Id: int64(req.FeedFormulaId)}
  247. if err := s.DB.Model(new(model.FeedFormula)).First(feedFormulaData).Error; err != nil {
  248. return xerr.WithStack(err)
  249. }
  250. // 更新版本号
  251. defer s.UpdateFeedFormalVersion(ctx, feedFormulaData)
  252. return s.DB.Model(new(model.FeedFormulaDetail)).
  253. Where("id = ?", req.Id).
  254. Where("feed_formula_id = ?", req.FeedFormulaId).
  255. Updates(map[string]interface{}{
  256. "is_modify": req.IsModify,
  257. }).Error
  258. }
  259. // DeleteFeedFormulaDetail 配方删除饲料
  260. func (s *StoreEntry) DeleteFeedFormulaDetail(ctx context.Context, req *operationPb.GroupAddFeedFormulaDetail) error {
  261. feedFormulaData := &model.FeedFormula{Id: int64(req.FeedFormulaId)}
  262. if err := s.DB.Model(new(model.FeedFormula)).First(feedFormulaData).Error; err != nil {
  263. return xerr.WithStack(err)
  264. }
  265. // 增加配方记录
  266. defer s.deleteFeedFormulaDetailAddRecode(ctx, req)
  267. tr := s.DB.Transaction(func(tx *gorm.DB) error {
  268. for _, v := range req.List {
  269. if err := tx.Model(new(model.FeedFormulaDetail)).
  270. Where("id = ?", v.Id).
  271. Updates(map[string]interface{}{
  272. "is_show": operationPb.IsShow_NO,
  273. }).Error; err != nil {
  274. return xerr.WithStack(err)
  275. }
  276. }
  277. return nil
  278. })
  279. return tr
  280. }
  281. // deleteFeedFormulaDetailAddRecode 删除配方增加修改记录
  282. func (s *StoreEntry) deleteFeedFormulaDetailAddRecode(ctx context.Context, req *operationPb.GroupAddFeedFormulaDetail) {
  283. editRecordList := make([]*model.FeedFormulaEditRecord, 0)
  284. for _, v := range req.List {
  285. editRecordList = append(editRecordList, &model.FeedFormulaEditRecord{
  286. FeedFormulaId: int64(req.FeedFormulaId),
  287. PastureName: "集团",
  288. ForageName: v.ForageName,
  289. Status: operationPb.FeedFormulaEditRecordType_DELETE,
  290. })
  291. }
  292. if err := s.CreateFeedFormulaEditRecord(ctx, editRecordList); err != nil {
  293. zaplog.Error("deleteFeedFormulaDetailAddRecode", zap.Any("CreateFeedFormulaEditRecord", err))
  294. }
  295. }
  296. // SearchFeedFormulaDetail 查询配方饲料详情
  297. func (s *StoreEntry) SearchFeedFormulaDetail(ctx context.Context, req *operationPb.AddFeedFormulaDetail) (*operationPb.FeedFormulaDetailResponse, error) {
  298. feedFormulaData := &model.FeedFormula{Id: int64(req.FeedFormulaId)}
  299. if err := s.DB.Model(new(model.FeedFormula)).First(feedFormulaData).Debug().Error; err != nil {
  300. return nil, xerr.WithStack(err)
  301. }
  302. var count int64
  303. feedFormulaDetailList := make([]*model.FeedFormulaDetail, 0)
  304. pref := s.DB.Model(new(model.FeedFormulaDetail)).
  305. Where("is_show = ?", operationPb.IsShow_OK).
  306. Where("feed_formula_id = ?", req.FeedFormulaId)
  307. if req.ForageName != "" {
  308. pref.Where("forage_name = ?", req.ForageName)
  309. }
  310. if req.ForageGroupName != "" {
  311. pref.Where("forage_group_name = ?", req.ForageGroupName)
  312. }
  313. if req.Weight > 0 {
  314. pref.Where("weight = ?", int64(req.Weight*100))
  315. }
  316. if req.IsLockCowCountRatio > 0 {
  317. pref.Where("is_lock_cow_count_ratio = ?", req.IsLockCowCountRatio)
  318. }
  319. if req.StirDelay > 0 {
  320. pref.Where("stir_delay = ?", req.StirDelay)
  321. }
  322. if req.Sort > 0 {
  323. pref.Where("sort = ?", req.Sort)
  324. }
  325. if err := pref.Order("sort").Count(&count).Limit(int(req.Pagination.PageSize)).
  326. Offset(int(req.Pagination.PageOffset)).Find(&feedFormulaDetailList).Debug().Error; err != nil {
  327. return nil, xerr.WithStack(err)
  328. }
  329. return &operationPb.FeedFormulaDetailResponse{
  330. Code: http.StatusOK,
  331. Msg: "ok",
  332. Data: model.FeedFormulaDetailSlice(feedFormulaDetailList).ToPB(),
  333. }, nil
  334. }
  335. // MixedFeedFormula 合成预混料
  336. func (s *StoreEntry) MixedFeedFormula(ctx context.Context, req *operationPb.MixedFeedFormulaRequest) error {
  337. feedFormulaData := &model.FeedFormula{Id: int64(req.FeedFormulaId)}
  338. if err := s.DB.Model(new(model.FeedFormula)).First(feedFormulaData).Error; err != nil {
  339. return xerr.WithStack(err)
  340. }
  341. if feedFormulaData.FormulaTypeId == operationPb.FormulaType_PREMIXED_FORMULA {
  342. return xerr.Customf("预混料配方不能合成预混料")
  343. }
  344. tr := s.DB.Transaction(func(tx *gorm.DB) error {
  345. newFeedFormulaData := &model.FeedFormula{
  346. Name: req.Name,
  347. Colour: req.Colour,
  348. EncodeNumber: s.EncodeNumber(ctx),
  349. CattleCategoryId: operationPb.CattleCategoryParent_Kind(req.CattleCategoryId),
  350. CattleCategoryName: req.CattleCategoryName,
  351. FormulaTypeId: req.FormulaTypeId,
  352. FormulaTypeName: req.FormulaTypeName,
  353. DataSourceId: operationPb.DataSource_Kind(req.DataSourceId),
  354. DataSourceName: operationPb.DataSource_Kind_name[req.DataSourceId],
  355. Remarks: req.Remarks,
  356. PastureName: "集团",
  357. IsShow: operationPb.IsShow_OK,
  358. IsModify: operationPb.IsShow_OK,
  359. IsDelete: operationPb.IsShow_OK,
  360. }
  361. if err := s.DB.Model(new(model.FeedFormula)).Create(newFeedFormulaData).Error; err != nil {
  362. return xerr.WithStack(err)
  363. }
  364. feedFormulaDetailList := make([]*model.FeedFormulaDetail, 0)
  365. for _, v := range req.FeedList {
  366. feedFormulaDetailList = append(feedFormulaDetailList, &model.FeedFormulaDetail{
  367. PastureName: "集团",
  368. FeedFormulaId: newFeedFormulaData.Id,
  369. ForageId: int64(v.ForageId),
  370. ForageName: v.ForageName,
  371. ForageGroupName: v.ForageGroupName,
  372. Weight: int32(v.Weight * 100),
  373. StirDelay: v.StirDelay,
  374. AllowError: v.AllowError,
  375. IsShow: operationPb.IsShow_OK,
  376. Sort: v.Sort,
  377. })
  378. }
  379. if err := s.DB.Model(new(model.FeedFormulaDetail)).Save(feedFormulaDetailList).Error; err != nil {
  380. return xerr.WithStack(err)
  381. }
  382. return nil
  383. })
  384. return tr
  385. }
  386. // SearchFeedFormulaList 查询数据列表
  387. func (s *StoreEntry) SearchFeedFormulaList(ctx context.Context, req *operationPb.SearchFeedFormulaRequest) (*operationPb.SearchFeedFormulaListResponse, error) {
  388. feedFormula := make([]*model.FeedFormula, 0)
  389. var count int64 = 0
  390. pref := s.DB.Model(new(model.FeedFormula)).Where("is_delete = ?", operationPb.IsShow_OK)
  391. if req.Name != "" {
  392. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  393. }
  394. if req.CattleCategoryId > 0 {
  395. pref.Where("cattle_category_id = ?", req.CattleCategoryId)
  396. }
  397. if req.FormulaTypeId > 0 {
  398. pref.Where("formula_type_id = ?", req.FormulaTypeId)
  399. }
  400. if req.IsShow > 0 {
  401. pref.Where("is_show = ?", req.IsShow)
  402. }
  403. if req.DataSource > 0 {
  404. pref.Where("data_source = ?", req.DataSource)
  405. }
  406. if req.Remarks != "" {
  407. pref.Where("remarks = ?", req.Remarks)
  408. }
  409. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  410. Find(&feedFormula).Error; err != nil {
  411. return nil, xerr.WithStack(err)
  412. }
  413. return &operationPb.SearchFeedFormulaListResponse{
  414. Code: http.StatusOK,
  415. Msg: "ok",
  416. Data: &operationPb.SearchFeedFormulaListData{
  417. Page: req.Pagination.Page,
  418. PageSize: req.Pagination.PageSize,
  419. Total: int32(count),
  420. List: model.FeedFormulaSlice(feedFormula).ToPB(),
  421. },
  422. }, nil
  423. }
  424. // SearchFeedFormulaById 查询指定数据
  425. func (s *StoreEntry) SearchFeedFormulaById(ctx context.Context, foodFormulaId int64) (*model.FeedFormula, error) {
  426. feedFormula := &model.FeedFormula{}
  427. if err := s.DB.Model(new(model.FeedFormula)).
  428. Where("is_delete = ?", operationPb.IsShow_OK).
  429. Where("id = ?", foodFormulaId).
  430. First(feedFormula).Error; err != nil {
  431. return nil, xerr.WithStack(err)
  432. }
  433. return feedFormula, nil
  434. }
  435. // IsShowFeedFormula 是否启用和是否可修改
  436. func (s *StoreEntry) IsShowFeedFormula(ctx context.Context, req *operationPb.IsShowModifyFeedFormula) error {
  437. feedFormula := &model.FeedFormula{Id: int64(req.FeedFormulaId)}
  438. if err := s.DB.First(feedFormula).Error; err != nil {
  439. if errors.Is(err, gorm.ErrRecordNotFound) {
  440. return xerr.Custom("该数据不存在")
  441. }
  442. return xerr.WithStack(err)
  443. }
  444. if req.EditType == 1 {
  445. if err := s.DB.Model(new(model.FeedFormula)).Where("id = ?", req.FeedFormulaId).Update("is_show", req.IsShow).Error; err != nil {
  446. return xerr.WithStack(err)
  447. }
  448. }
  449. if req.EditType == 2 {
  450. if err := s.DB.Model(new(model.FeedFormula)).Where("id = ?", req.FeedFormulaId).Update("is_modify", req.IsShow).Error; err != nil {
  451. return xerr.WithStack(err)
  452. } else {
  453. s.PastureFeedFormulaIsModify(ctx, req.FeedFormulaId, req.IsShow)
  454. }
  455. }
  456. return nil
  457. }
  458. // DeleteFeedFormula 是否删除
  459. func (s *StoreEntry) DeleteFeedFormula(ctx context.Context, feedFormulaId int64) error {
  460. feedFormula := &model.FeedFormula{Id: feedFormulaId}
  461. if err := s.DB.First(feedFormula).Error; err != nil {
  462. if errors.Is(err, gorm.ErrRecordNotFound) {
  463. return xerr.Custom("该数据不存在")
  464. }
  465. return xerr.WithStack(err)
  466. }
  467. if err := s.DB.Model(new(model.FeedFormula)).Where("id = ?", feedFormula.Id).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  468. return xerr.WithStack(err)
  469. }
  470. return nil
  471. }
  472. // ExcelImportFeedFormula 导入excel
  473. func (s *StoreEntry) ExcelImportFeedFormula(ctx context.Context, req io.Reader) error {
  474. xlsx, err := excelize.OpenReader(req)
  475. if err != nil {
  476. return xerr.WithStack(err)
  477. }
  478. defer xlsx.Close()
  479. rows, err := xlsx.GetRows(xlsx.GetSheetName(xlsx.GetActiveSheetIndex()))
  480. if err != nil {
  481. return xerr.WithStack(err)
  482. }
  483. if len(rows) > 10000 {
  484. rows = rows[:10000]
  485. }
  486. feedFormulaList := make([]*model.FeedFormula, 0)
  487. for i, row := range rows {
  488. if i == 0 {
  489. continue
  490. }
  491. var (
  492. name, encodeNumber, cattleCategoryName, formulaTypeName, dataSourceName, remarks, isShowStr string
  493. isShow operationPb.IsShow_Kind
  494. )
  495. for k, v := range row {
  496. if k == 0 {
  497. name = v
  498. }
  499. if k == 1 {
  500. encodeNumber = v
  501. }
  502. if k == 2 {
  503. cattleCategoryName = v
  504. }
  505. if k == 3 {
  506. formulaTypeName = v
  507. }
  508. if k == 4 {
  509. dataSourceName = v
  510. }
  511. if k == 5 {
  512. remarks = v
  513. }
  514. if k == 6 {
  515. isShowStr = v
  516. }
  517. }
  518. if isShowStr == "是" {
  519. isShow = operationPb.IsShow_OK
  520. } else {
  521. isShow = operationPb.IsShow_NO
  522. }
  523. feedFormulaItem := &model.FeedFormula{
  524. Name: name,
  525. EncodeNumber: encodeNumber,
  526. CattleCategoryName: cattleCategoryName,
  527. FormulaTypeName: formulaTypeName,
  528. Remarks: remarks,
  529. IsShow: isShow,
  530. IsDelete: operationPb.IsShow_OK,
  531. DataSourceId: operationPb.DataSource_EXCEL_IMPORT,
  532. DataSourceName: dataSourceName,
  533. }
  534. feedFormulaList = append(feedFormulaList, feedFormulaItem)
  535. }
  536. if len(feedFormulaList) > 0 {
  537. if err = s.DB.Create(feedFormulaList).Error; err != nil {
  538. return xerr.WithStack(err)
  539. }
  540. }
  541. return nil
  542. }
  543. // ExcelExportFeedFormula 流式导出excel
  544. func (s *StoreEntry) ExcelExportFeedFormula(ctx context.Context, req *operationPb.SearchFeedFormulaRequest) (*bytes.Buffer, error) {
  545. res, err := s.SearchFeedFormulaList(ctx, req)
  546. if err != nil {
  547. return nil, xerr.WithStack(err)
  548. }
  549. if len(res.Data.List) <= 0 {
  550. return nil, xerr.Custom("数据为空")
  551. }
  552. file := excelize.NewFile()
  553. defer file.Close()
  554. streamWriter, err := file.NewStreamWriter("Sheet1")
  555. if err != nil {
  556. return nil, xerr.WithStack(err)
  557. }
  558. titles := []interface{}{"配方名称", "配方编码", "畜牧类别", "配方类别", "来源", "备注", "是否启用",
  559. "饲料组", "饲料名称", "重量(kg)", "搅拌延迟(min)", "是否锁定牛头数比例", "顺序"}
  560. if err = streamWriter.SetRow("A1", titles); err != nil {
  561. return nil, xerr.WithStack(err)
  562. }
  563. for i, item := range res.Data.List {
  564. cell, err := excelize.CoordinatesToCellName(1, i+2)
  565. if err != nil {
  566. zaplog.Error("exclude CoordinatesToCellName", zap.Any("Err", err))
  567. continue
  568. }
  569. row := make([]interface{}, 0)
  570. row = append(row, item.Name, item.EncodeNumber, item.CattleCategoryName, item.FormulaTypeName, item.DataSourceName,
  571. item.Remarks, item.IsShow)
  572. if err = streamWriter.SetRow(cell, row); err != nil {
  573. return nil, xerr.WithStack(err)
  574. }
  575. }
  576. if err = streamWriter.Flush(); err != nil {
  577. return nil, xerr.WithStack(err)
  578. }
  579. return file.WriteToBuffer()
  580. }
  581. // ExcelTemplateFeedFormula 导出模板
  582. func (s *StoreEntry) ExcelTemplateFeedFormula(ctx context.Context) (*bytes.Buffer, error) {
  583. file := excelize.NewFile()
  584. defer file.Close()
  585. streamWriter, err := file.NewStreamWriter("Sheet1")
  586. if err != nil {
  587. return nil, xerr.WithStack(err)
  588. }
  589. titles := []interface{}{"配方名称", "配方编码", "畜牧类别", "配方类别", "来源", "备注", "是否启用",
  590. "饲料组", "饲料名称", "重量(kg)", "搅拌延迟(min)", "是否锁定牛头数比例", "顺序"}
  591. if err = streamWriter.SetRow("A1", titles); err != nil {
  592. return nil, xerr.WithStack(err)
  593. }
  594. if err = streamWriter.Flush(); err != nil {
  595. return nil, xerr.WithStack(err)
  596. }
  597. return file.WriteToBuffer()
  598. }
  599. // EncodeNumber 配方编码
  600. func (s *StoreEntry) EncodeNumber(ctx context.Context) string {
  601. currTime := time.Now().Format(model.LayoutDate)
  602. prefix := fmt.Sprintf("%s_%s", EncodeNumberPrefix, currTime)
  603. data := &model.UniqueData{}
  604. if err := s.DB.Order("id desc").Where("prefix = ?", prefix).First(data).Error; err != nil {
  605. if !errors.Is(err, gorm.ErrRecordNotFound) {
  606. return ""
  607. }
  608. ud, _ := strconv.Atoi(currTime)
  609. result := ud*100 + 1
  610. newData := &model.UniqueData{
  611. Prefix: prefix,
  612. Data: int64(result),
  613. }
  614. if err = s.DB.Create(newData).Error; err != nil {
  615. zaplog.Error("EncodeNumber Create", zap.Any("data", newData), zap.Any("Err", err))
  616. return ""
  617. }
  618. return fmt.Sprintf("%d", newData.Data)
  619. }
  620. data.Data += 1
  621. if err := s.DB.Model(new(model.UniqueData)).Where("prefix = ?", prefix).Update("data", data.Data).Error; err != nil {
  622. return ""
  623. } else {
  624. return fmt.Sprintf("%d", data.Data)
  625. }
  626. }
  627. // DistributeFeedFormula 配方下发牧场
  628. func (s *StoreEntry) DistributeFeedFormula(ctx context.Context, req *operationPb.DistributeFeedFormulaRequest) error {
  629. distributeData, err := s.checkoutDistributeData(ctx, req)
  630. if err != nil {
  631. return xerr.WithStack(err)
  632. }
  633. wg := sync.WaitGroup{}
  634. wg.Add(len(distributeData.PastureList))
  635. var muError error
  636. for _, pasture := range distributeData.PastureList {
  637. go func(p *model.GroupPasture) {
  638. defer wg.Done()
  639. request := &model.DistributeFeedFormulaRequest{
  640. PastureId: p.Id,
  641. FeedFormula: distributeData.FeedFormulaList,
  642. FeedFormulaDetail: distributeData.FeedFormulaDetail,
  643. }
  644. response := &model.PastureResponse{}
  645. defer func() {
  646. if response.Code == http.StatusOK {
  647. s.DB.Create(model.NewFeedFormulaDistributeLogList(distributeData.FeedFormulaList, p.Id, p.Name, operationPb.IsShow_OK))
  648. } else {
  649. muError = multierr.Append(muError, xerr.Custom(response.Msg))
  650. }
  651. }()
  652. if _, err = s.PastureHttpClient(ctx, model.FeedFormulaDistributeUrl, p.Id, request, response); err != nil {
  653. muError = multierr.Append(muError, err)
  654. zaplog.Error("DistributeFeedFormula", zap.Any("pasture", p), zap.Any("body", distributeData.FeedFormulaList), zap.Any("err", err), zap.Any("response", response))
  655. b, _ := json.Marshal(request)
  656. res, _ := json.Marshal(response)
  657. pastureDataLog := model.NewPastureDataLog(p.Id, PastureDataLogType["FeedFormula_Distribute"], model.FeedFormulaDistributeUrl, string(b), string(res))
  658. s.DB.Create(pastureDataLog)
  659. }
  660. }(pasture)
  661. }
  662. wg.Wait()
  663. return muError
  664. }
  665. // CancelDistributeFeedFormula 取消配方下发牧场
  666. func (s *StoreEntry) CancelDistributeFeedFormula(ctx context.Context, req *operationPb.DistributeFeedFormulaRequest) error {
  667. distributeData, err := s.checkoutDistributeData(ctx, req)
  668. if err != nil {
  669. return xerr.WithStack(err)
  670. }
  671. wg := sync.WaitGroup{}
  672. wg.Add(len(distributeData.PastureList))
  673. var muError error
  674. for _, pasture := range distributeData.PastureList {
  675. go func(p *model.GroupPasture) {
  676. defer wg.Done()
  677. pastureDataId := make([]int64, 0)
  678. for _, v := range distributeData.FeedFormulaList {
  679. if v.PastureId == p.Id {
  680. pastureDataId = append(pastureDataId, v.PastureDataId)
  681. }
  682. }
  683. if len(pastureDataId) <= 0 {
  684. return
  685. }
  686. request := &model.CancelDistributeFeedFormulaRequest{
  687. PastureId: p.Id,
  688. PastureDataId: pastureDataId,
  689. }
  690. response := &model.PastureResponse{}
  691. if _, err = s.PastureHttpClient(ctx, model.FeedFormulaCancelDistributeUrl, p.Id, request, response); err != nil {
  692. zaplog.Error("DistributeFeedFormula", zap.Any("pasture", p), zap.Any("body", distributeData.FeedFormulaList), zap.Any("err", err), zap.Any("response", response))
  693. b, _ := json.Marshal(request)
  694. res, _ := json.Marshal(response)
  695. pastureDataLog := model.NewPastureDataLog(p.Id, PastureDataLogType["FeedFormula_Cancel_Distribute"], model.FeedFormulaCancelDistributeUrl, string(b), string(res))
  696. s.DB.Create(pastureDataLog)
  697. }
  698. }(pasture)
  699. }
  700. wg.Wait()
  701. return muError
  702. }
  703. // EditRecodeFeedFormula 配方修改记录
  704. func (s *StoreEntry) EditRecodeFeedFormula(ctx context.Context, req *operationPb.EditRecodeFeedFormulaRequest) (*operationPb.EditRecodeFeedFormulaResponse, error) {
  705. feedFormulaData := &model.FeedFormula{Id: int64(req.FeedFormulaId)}
  706. if err := s.DB.Model(new(model.FeedFormula)).First(feedFormulaData).Error; err != nil {
  707. return nil, xerr.WithStack(err)
  708. }
  709. res := &operationPb.EditRecodeFeedFormulaResponse{
  710. Code: http.StatusOK,
  711. Msg: "ok",
  712. Data: make([]*operationPb.EditRecodeFeedFormulaData, 0),
  713. }
  714. feedFormulaEditRecordList := make([]*model.FeedFormulaEditRecord, 0)
  715. pref := s.DB.Model(new(model.FeedFormulaEditRecord)).Where("status > 0").Where("feed_formula_id = ?", req.FeedFormulaId)
  716. if req.PastureId > 0 {
  717. pref.Where("pasture_id = ?", req.PastureId)
  718. }
  719. if req.StartTime > 0 && req.EndTime > 0 && req.EndTime >= req.StartTime {
  720. pref.Where("created_at >= ?", req.StartTime).Where("created_at <= ?", req.EndTime)
  721. }
  722. if err := pref.Order("group_id").Find(&feedFormulaEditRecordList).Error; err != nil {
  723. return res, xerr.WithStack(err)
  724. }
  725. groupByFeedFormulaEditRecordList := make(map[int64][]*model.FeedFormulaEditRecord)
  726. for _, v := range feedFormulaEditRecordList {
  727. if groupByFeedFormulaEditRecordList[v.GroupId] == nil {
  728. groupByFeedFormulaEditRecordList[v.GroupId] = make([]*model.FeedFormulaEditRecord, 0)
  729. }
  730. groupByFeedFormulaEditRecordList[v.GroupId] = append(groupByFeedFormulaEditRecordList[v.GroupId], v)
  731. }
  732. editRecodeFeedFormulaDataList := make([]*operationPb.EditRecodeFeedFormulaData, 0)
  733. for _, data := range groupByFeedFormulaEditRecordList {
  734. var modifyDetail = ""
  735. var pastureId int32 = 0
  736. var pastureName = ""
  737. var createTime int64 = 0
  738. for i, v := range data {
  739. if i == 0 {
  740. modifyDetail += fmt.Sprintf("%s\n ", v.PastureName)
  741. pastureId = int32(v.PastureId)
  742. pastureName = v.PastureName
  743. createTime = v.CreatedAt
  744. }
  745. switch v.Status {
  746. case operationPb.FeedFormulaEditRecordType_INSERT:
  747. modifyDetail += fmt.Sprintf(`%s新增了饲料%s\n `, v.OperationName, v.ForageName)
  748. case operationPb.FeedFormulaEditRecordType_UPDATE:
  749. modifyDetail += fmt.Sprintf(`%s将%s的%s"%s"更新为"%s"\n `, v.OperationName, v.ForageName, v.FieldName, v.BeforeValue, v.AfterValue)
  750. case operationPb.FeedFormulaEditRecordType_DELETE:
  751. modifyDetail += fmt.Sprintf(`%s删除了%s\n `, v.OperationName, v.ForageName)
  752. }
  753. }
  754. editRecodeFeedFormulaDataList = append(editRecodeFeedFormulaDataList, &operationPb.EditRecodeFeedFormulaData{
  755. PastureId: pastureId,
  756. PastureName: pastureName,
  757. ModifyTime: time.Unix(createTime, 0).Format(model.LayoutTime),
  758. ModifyDetail: modifyDetail,
  759. })
  760. }
  761. res.Data = editRecodeFeedFormulaDataList
  762. return res, nil
  763. }
  764. func (s *StoreEntry) FeedFormulaDetailList(ctx context.Context, req *operationPb.FeedFormulaDetailRequest) (*operationPb.FeedFormulaDetailResponse, error) {
  765. res := &operationPb.FeedFormulaDetailResponse{
  766. Code: http.StatusOK,
  767. Msg: "ok",
  768. Data: make([]*operationPb.AddFeedFormulaDetail, 0),
  769. }
  770. feedFormula, err := s.SearchFeedFormulaById(ctx, int64(req.FeedFormulaId))
  771. if err != nil {
  772. return nil, xerr.WithStack(err)
  773. }
  774. feedFormulaId := feedFormula.Id
  775. if feedFormula.PastureDataId > 0 {
  776. feedFormulaId = feedFormula.PastureDataId
  777. }
  778. list, err := s.SearchFeedFormalDetailById(ctx, feedFormulaId, feedFormula.PastureId)
  779. if err != nil {
  780. return nil, xerr.WithStack(err)
  781. }
  782. if len(list) <= 0 {
  783. list, err = s.SearchFeedFormalDetailById(ctx, feedFormula.Id, 0)
  784. if err != nil {
  785. return nil, xerr.WithStack(err)
  786. }
  787. }
  788. res.Data = model.FeedFormulaDetailSlice(list).ToPB()
  789. return res, nil
  790. }
  791. // FeedFormulaUsage 配方使用概况
  792. func (s *StoreEntry) FeedFormulaUsage(ctx context.Context, req *operationPb.FeedFormulaUsageRequest) (*operationPb.FeedFormulaUsageResponse, error) {
  793. feedFormulaDistributeLogList := make([]*model.FeedFormulaDistributeLog, 0)
  794. if err := s.DB.Model(new(model.FeedFormulaDistributeLog)).
  795. Where("feed_formula_id = ?", req.FeedFormulaId).
  796. Where("is_show = ?", operationPb.IsShow_OK).Group("pasture_id").
  797. Find(&feedFormulaDistributeLogList).Error; err != nil {
  798. return nil, xerr.WithStack(err)
  799. }
  800. res := &operationPb.FeedFormulaUsageResponse{
  801. Code: http.StatusOK,
  802. Msg: "ok",
  803. Data: make([]*operationPb.FeedFormulaUsageList, 0),
  804. }
  805. wg := sync.WaitGroup{}
  806. wg.Add(len(feedFormulaDistributeLogList))
  807. for _, list := range feedFormulaDistributeLogList {
  808. go func(l *model.FeedFormulaDistributeLog) {
  809. defer wg.Done()
  810. groupDetail, err := s.PastureDetailById(ctx, l.PastureId)
  811. if err != nil {
  812. zaplog.Error("FeedFormulaUsage", zap.Any("PastureDetailById", err))
  813. return
  814. }
  815. req.PastureId = int32(groupDetail.PastureId)
  816. response := &operationPb.PastureFeedFormulaUsageResponse{}
  817. if _, err = s.PastureHttpClient(ctx, model.FeedUsageURl, groupDetail.Id, req, response); err != nil {
  818. zaplog.Error("FeedFormulaUsage", zap.Any("PastureDetailById", err))
  819. return
  820. }
  821. if response.Code == http.StatusOK {
  822. data := &operationPb.FeedFormulaUsageList{
  823. PastureId: int32(groupDetail.Id),
  824. PastureName: groupDetail.Name,
  825. MixedFodderAccurateRatio: response.Data.MixedFodderAccurateRatio,
  826. MixedFodderCorrectRatio: response.Data.MixedFodderCorrectRatio,
  827. SprinkleFodderAccurateRatio: response.Data.SprinkleFodderAccurateRatio,
  828. SprinkleFodderCorrectRatio: response.Data.SprinkleFodderCorrectRatio,
  829. AddFeedTime: response.Data.AddFeedTime,
  830. SprinkleTime: response.Data.SprinkleTime,
  831. StirTime: response.Data.StirTime,
  832. LastEditTime: response.Data.LastEditTime,
  833. }
  834. res.Data = append(res.Data, data)
  835. } else {
  836. zaplog.Error("FeedFormulaUsage-http", zap.Any("response", response))
  837. return
  838. }
  839. }(list)
  840. }
  841. wg.Wait()
  842. return res, nil
  843. }
  844. func (s *StoreEntry) PastureFeedFormulaIsModify(ctx context.Context, feedFormulaId int32, isModify operationPb.IsShow_Kind) {
  845. feedFormulaDistributeLogList := make([]*model.FeedFormulaDistributeLog, 0)
  846. if err := s.DB.Where("is_show = ?", operationPb.IsShow_OK).
  847. Where("feed_formula_id = ?", feedFormulaId).
  848. Group("pasture_id").Find(&feedFormulaDistributeLogList).Error; err != nil {
  849. zaplog.Error("PastureFeedFormulaIsModify", zap.Any("err", err), zap.Any("feed_formula_id", feedFormulaId))
  850. return
  851. }
  852. for _, v := range feedFormulaDistributeLogList {
  853. response := &model.PastureResponse{}
  854. request := &model.FeedFormulaIsModifyRequest{
  855. PastureId: v.PastureId,
  856. FeedFormulaId: v.FeedFormulaId,
  857. IsModify: int32(isModify),
  858. }
  859. if _, err := s.PastureHttpClient(ctx, model.FeedFormulaIsModifyUrl, v.Id, request, response); err != nil {
  860. zaplog.Error("PastureFeedFormulaIsModify", zap.Any("request", request), zap.Any("err", err), zap.Any("response", response))
  861. b, _ := json.Marshal(request)
  862. res, _ := json.Marshal(response)
  863. pastureDataLog := model.NewPastureDataLog(v.PastureId, PastureDataLogType["FeedFormula_IsModify"], model.FeedFormulaIsModifyUrl, string(b), string(res))
  864. s.DB.Create(pastureDataLog)
  865. }
  866. }
  867. }
  868. func (s *StoreEntry) checkoutDistributeData(ctx context.Context, req *operationPb.DistributeFeedFormulaRequest) (*model.DistributeData, error) {
  869. result := &model.DistributeData{
  870. PastureList: make([]*model.GroupPasture, 0),
  871. FeedFormulaList: make([]*model.FeedFormula, 0),
  872. FeedFormulaDetail: make([]*model.FeedFormulaDetail, 0),
  873. }
  874. if err := s.DB.Where("id IN ?", req.PastureIds).Where("is_delete = ?", operationPb.IsShow_OK).Find(&result.PastureList).Error; err != nil {
  875. return result, xerr.WithStack(err)
  876. }
  877. if err := s.DB.Where("id IN ?", req.FeedFormulaIds).Find(&result.FeedFormulaList).Error; err != nil {
  878. return result, xerr.WithStack(err)
  879. }
  880. feedFormulaDetailList := make([]*model.FeedFormulaDetail, 0)
  881. for _, v := range req.FeedFormulaIds {
  882. feedFormulaDetail := make([]*model.FeedFormulaDetail, 0)
  883. if err := s.DB.Model(new(model.FeedFormulaDetail)).Where("feed_formula_id = ?", v).Find(&feedFormulaDetail).Error; err != nil {
  884. if errors.Is(err, gorm.ErrRecordNotFound) {
  885. return result, xerr.Customf("该配方没有添加相关饲料:%d", v)
  886. }
  887. return result, xerr.WithStack(err)
  888. } else {
  889. feedFormulaDetailList = append(feedFormulaDetailList, feedFormulaDetail...)
  890. }
  891. }
  892. result.FeedFormulaDetail = feedFormulaDetailList
  893. if len(result.PastureList) <= 0 || len(result.FeedFormulaList) <= 0 {
  894. return result, xerr.Customf("数据错误")
  895. }
  896. return result, nil
  897. }
  898. func (s *StoreEntry) checkoutDistributeLog(ctx context.Context, pastureId, feedFormulaId int64) bool {
  899. res := &model.FeedFormulaDistributeLog{}
  900. if err := s.DB.Model(new(model.FeedFormulaDistributeLog)).Where("feed_formula_id = ?", feedFormulaId).
  901. Where("pasture_id = ?", pastureId).Where("is_show = ?", operationPb.IsShow_OK).First(res).Error; err != nil {
  902. return false
  903. }
  904. if res.IsShow == operationPb.IsShow_OK {
  905. return true
  906. }
  907. return false
  908. }
  909. func (s *StoreEntry) SearchFeedFormalDetailById(ctx context.Context, feedFormulaId, pastureId int64) ([]*model.FeedFormulaDetail, error) {
  910. res := make([]*model.FeedFormulaDetail, 0)
  911. if err := s.DB.Model(new(model.FeedFormulaDetail)).Where("pasture_id = ?", pastureId).
  912. Where("feed_formula_id = ?", feedFormulaId).
  913. Where("is_show = ?", operationPb.IsShow_OK).
  914. Order("id desc").Find(&res).Error; err != nil {
  915. return nil, xerr.WithStack(err)
  916. }
  917. return res, nil
  918. }
  919. func (s *StoreEntry) UpdateFeedFormalVersion(ctx context.Context, req *model.FeedFormula) {
  920. if err := s.DB.Model(req).UpdateColumn("version", gorm.Expr("version + ?", 1)).Error; err != nil {
  921. zaplog.Error("UpdateFeedFormalVersion", zap.Any("err", err))
  922. }
  923. }