goods.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "kpt-pasture/util"
  7. "net/http"
  8. "time"
  9. "gorm.io/gorm"
  10. "gitee.com/xuyiping_admin/pkg/xerr"
  11. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  12. )
  13. func (s *StoreEntry) DrugsList(ctx context.Context, req *pasturePb.SearchDrugsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchDrugsResponse, error) {
  14. drugsList := make([]*model.Drugs, 0)
  15. var count int64 = 0
  16. pref := s.DB.Model(new(model.Drugs))
  17. if req.Name != "" {
  18. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  19. }
  20. if req.Usage > 0 {
  21. pref.Where("usage_method = ?", req.Usage)
  22. }
  23. if req.CategoryId > 0 {
  24. pref.Where("category_id = ?", req.CategoryId)
  25. }
  26. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  27. Find(&drugsList).Error; err != nil {
  28. return nil, xerr.WithStack(err)
  29. }
  30. return &pasturePb.SearchDrugsResponse{
  31. Code: http.StatusOK,
  32. Message: "ok",
  33. Data: &pasturePb.SearchDrugsData{
  34. List: model.DrugsSlice(drugsList).ToPB(),
  35. Total: int32(count),
  36. PageSize: pagination.PageSize,
  37. Page: pagination.Page,
  38. },
  39. }, nil
  40. }
  41. func (s *StoreEntry) DrugsCreateOrUpdate(ctx context.Context, req *pasturePb.SearchDrugsList) error {
  42. currentUser, err := s.GetCurrentSystemUser(ctx)
  43. if err != nil {
  44. return xerr.Custom("登录人信息失效")
  45. }
  46. req.CategoryName = s.DrugCategoryMaps()[req.CategoryId]
  47. req.UnitName = s.UnitMap()[req.Unit]
  48. req.UnitName = s.DrugUsageMaps()[req.Usage]
  49. newDrugs := model.NewDrugs(req, currentUser)
  50. if req.Id <= 0 {
  51. if err = s.DB.Create(newDrugs).Error; err != nil {
  52. return xerr.WithStack(err)
  53. }
  54. } else {
  55. if err = s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
  56. return xerr.WithStack(err)
  57. }
  58. }
  59. return nil
  60. }
  61. func (s *StoreEntry) MedicalEquipmentList(ctx context.Context, req *pasturePb.SearchMedicalEquipmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchMedicalEquipmentResponse, error) {
  62. medicalEquipmentList := make([]*model.MedicalEquipment, 0)
  63. var count int64 = 0
  64. pref := s.DB.Model(new(model.MedicalEquipment))
  65. if req.Name != "" {
  66. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  67. }
  68. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  69. Find(&medicalEquipmentList).Error; err != nil {
  70. return nil, xerr.WithStack(err)
  71. }
  72. unitMap := s.UnitMap()
  73. return &pasturePb.SearchMedicalEquipmentResponse{
  74. Code: http.StatusOK,
  75. Message: "ok",
  76. Data: &pasturePb.SearchMedicalEquipmentData{
  77. List: model.MedicalEquipmentSlice(medicalEquipmentList).ToPB(unitMap),
  78. Total: int32(count),
  79. PageSize: pagination.PageSize,
  80. Page: pagination.Page,
  81. },
  82. }, nil
  83. }
  84. func (s *StoreEntry) MedicalEquipmentCreateOrUpdate(ctx context.Context, req *pasturePb.SearchMedicalEquipmentList) error {
  85. currentUser, err := s.GetCurrentSystemUser(ctx)
  86. if err != nil {
  87. return xerr.Custom("登录人信息失效")
  88. }
  89. newDrugs := model.NewMedicalEquipment(req, currentUser)
  90. if req.Id <= 0 {
  91. if err = s.DB.Create(newDrugs).Error; err != nil {
  92. return xerr.WithStack(err)
  93. }
  94. } else {
  95. if err = s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
  96. return xerr.WithStack(err)
  97. }
  98. }
  99. return nil
  100. }
  101. func (s *StoreEntry) NeckRingCreateOrUpdate(ctx context.Context, req *pasturePb.NeckRingCreateRequest) error {
  102. currentUser, err := s.GetCurrentSystemUser(ctx)
  103. if err != nil {
  104. return xerr.Custom("登录人信息失效")
  105. }
  106. if req.Items == nil || len(req.Items) == 0 {
  107. return xerr.Custom("请选择要脖环数据")
  108. }
  109. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  110. for _, v := range req.Items {
  111. switch req.Status {
  112. // 绑定
  113. case pasturePb.NeckRingOperationStatus_Bind:
  114. _, err = s.GetCowInfoByCowId(ctx, int64(v.CowId))
  115. if err != nil {
  116. return xerr.Customf("该牛不存在")
  117. }
  118. neckRing, ok := s.NeckRingIsExist(ctx, v.Number)
  119. if !ok {
  120. newNeckRing := model.NewNeckRing(v.Number, int64(v.CowId), currentUser)
  121. if err = tx.Create(newNeckRing).Error; err != nil {
  122. return xerr.WithStack(err)
  123. }
  124. }
  125. if ok {
  126. neckRing.Update(int64(v.CowId))
  127. if err = tx.Model(new(model.NeckRing)).
  128. Select("cow_id,wear_at").
  129. Where("id = ?", neckRing.Id).
  130. Updates(neckRing).Error; err != nil {
  131. return xerr.WithStack(err)
  132. }
  133. }
  134. newNeckRingLog := model.NewNeckRingBindLog(v.Number, int64(v.CowId), currentUser)
  135. if err = tx.Create(newNeckRingLog).Error; err != nil {
  136. return xerr.WithStack(err)
  137. }
  138. if err = tx.Model(new(model.Cow)).
  139. Where("id = ?", v.CowId).
  140. Update("neck_ring_number", v.Number).
  141. Error; err != nil {
  142. return xerr.WithStack(err)
  143. }
  144. // 解绑
  145. case pasturePb.NeckRingOperationStatus_UnBind:
  146. if err = tx.Model(new(model.NeckRing)).
  147. Where("number = ?", v.Number).
  148. Updates(map[string]interface{}{
  149. "wear_at": 0,
  150. "cow_id": 0,
  151. "status": pasturePb.NeckRingStatus_Unbind,
  152. }).Error; err != nil {
  153. return xerr.WithStack(err)
  154. }
  155. if err = tx.Model(new(model.Cow)).
  156. Where("neck_ring_number = ?", v.Number).
  157. Updates(map[string]interface{}{
  158. "neck_ring_number": "",
  159. }).Error; err != nil {
  160. return xerr.WithStack(err)
  161. }
  162. if err = tx.Model(new(model.NeckRingBindLog)).
  163. Where("number = ?", v.Number).
  164. Update("un_bind_at", time.Now().Unix()).Error; err != nil {
  165. return xerr.WithStack(err)
  166. }
  167. // 编辑
  168. case pasturePb.NeckRingOperationStatus_Edit:
  169. if err = tx.Model(new(model.NeckRing)).
  170. Where("cow_id = ?", v.CowId).
  171. Update("number", v.Number).Error; err != nil {
  172. return xerr.WithStack(err)
  173. }
  174. if err = tx.Model(new(model.Cow)).
  175. Where("id = ?", v.CowId).
  176. Update("neck_ring_number", v.Number).Error; err != nil {
  177. return xerr.WithStack(err)
  178. }
  179. }
  180. }
  181. return nil
  182. }); err != nil {
  183. return xerr.WithStack(err)
  184. }
  185. return nil
  186. }
  187. func (s *StoreEntry) NeckRingList(ctx context.Context, req *pasturePb.SearchNeckRingRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchNeckRingResponse, error) {
  188. neckRingLogList := make([]*model.NeckRing, 0)
  189. var count int64 = 0
  190. pref := s.DB.Model(new(model.NeckRing)).
  191. Where("status >= ?", pasturePb.NeckRingStatus_Unbind).
  192. Where("number != ''")
  193. if req.Status > 0 {
  194. pref.Where("status = ?", req.Status)
  195. }
  196. if req.CowId > 0 {
  197. pref.Where("cow_id = ?", req.CowId)
  198. }
  199. if req.Number != "" {
  200. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  201. }
  202. if err := pref.Order("id desc").
  203. Count(&count).
  204. Limit(int(pagination.PageSize)).
  205. Offset(int(pagination.PageOffset)).
  206. Find(&neckRingLogList).Error; err != nil {
  207. return nil, xerr.WithStack(err)
  208. }
  209. neckRingStatusMap := s.NeckRingStatusMap()
  210. return &pasturePb.SearchNeckRingResponse{
  211. Code: http.StatusOK,
  212. Message: "ok",
  213. Data: &pasturePb.SearchNeckRingData{
  214. List: model.NeckRingSlice(neckRingLogList).ToPB(neckRingStatusMap, s.DB, GetCowPenInfoByCowId),
  215. Total: int32(count),
  216. PageSize: pagination.PageSize,
  217. Page: pagination.Page,
  218. },
  219. }, nil
  220. }
  221. func (s *StoreEntry) OutboundApply(ctx context.Context, req *pasturePb.OutboundApplyItem) error {
  222. currentUser, err := s.GetCurrentSystemUser(ctx)
  223. if err != nil {
  224. return xerr.Custom("登录人信息失效")
  225. }
  226. if len(req.Goods) <= 0 {
  227. return xerr.Custom("请选择要出库商品")
  228. }
  229. var outbound *model.Outbound
  230. if req.Id > 0 {
  231. outbound, err = s.GetOutboundById(ctx, int64(req.Id))
  232. if err != nil || outbound == nil || outbound.Id <= 0 {
  233. return xerr.Customf("该出库单不存在")
  234. }
  235. if currentUser.Id != int64(outbound.ApplicantId) {
  236. return xerr.Custom("非申请人,无权修改该出库单")
  237. }
  238. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  239. return xerr.Custom("该出库单不能修改")
  240. }
  241. } else {
  242. // 创建出库申请
  243. outbound = model.NewOutbound(req, currentUser)
  244. }
  245. goodsItems := make([]*pasturePb.OutboundApplyGoodsItem, 0)
  246. switch req.OutType {
  247. case pasturePb.OutType_Drugs:
  248. for _, v := range req.Goods {
  249. if v.Quantity <= 0 {
  250. return xerr.Custom("请填写商品数量")
  251. }
  252. if v.GoodsId <= 0 {
  253. return xerr.Custom("请选择要出库商品")
  254. }
  255. newDrugs := &model.Drugs{}
  256. if err = s.DB.Model(new(model.Drugs)).
  257. Where("id = ?", v.GoodsId).
  258. Where("inventory >= ?", v.Quantity).
  259. First(newDrugs).Error; err != nil {
  260. return xerr.WithStack(err)
  261. }
  262. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  263. GoodsId: v.GoodsId,
  264. Quantity: v.Quantity,
  265. Unit: v.Unit,
  266. GoodsName: newDrugs.Name,
  267. Specs: newDrugs.Specs,
  268. Producer: newDrugs.Producer,
  269. BatchNumber: newDrugs.BatchNumber,
  270. Price: float32(newDrugs.Price) / 100,
  271. })
  272. }
  273. case pasturePb.OutType_Medical_Equipment:
  274. for _, v := range req.Goods {
  275. if v.Quantity <= 0 {
  276. return xerr.Custom("请填写商品数量")
  277. }
  278. if v.GoodsId <= 0 {
  279. return xerr.Custom("请选择要出库商品")
  280. }
  281. newMedicalEquipment := &model.MedicalEquipment{}
  282. if err = s.DB.Model(new(model.Drugs)).
  283. Where("id = ?", v.GoodsId).
  284. Where("inventory >= ?", v.Quantity).
  285. First(newMedicalEquipment).Error; err != nil {
  286. return xerr.WithStack(err)
  287. }
  288. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  289. GoodsId: v.GoodsId,
  290. Quantity: v.Quantity,
  291. Unit: v.Unit,
  292. GoodsName: newMedicalEquipment.Name,
  293. Specs: newMedicalEquipment.Specs,
  294. Producer: newMedicalEquipment.Producer,
  295. BatchNumber: newMedicalEquipment.BatchNumber,
  296. Price: float32(newMedicalEquipment.Price) / 100,
  297. })
  298. }
  299. default:
  300. return xerr.Custom("未知的出库类型")
  301. }
  302. unitMap := s.UnitMap()
  303. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  304. if req.Id > 0 {
  305. if err = tx.Model(new(model.Outbound)).
  306. Where("id = ?", req.Id).
  307. Update("applicant_remarks", req.ApplicantRemarks).Error; err != nil {
  308. return xerr.WithStack(err)
  309. }
  310. if err = tx.Model(new(model.OutboundDetail)).
  311. Where("outbound_id = ?", req.Id).
  312. Update("is_delete = ?", pasturePb.IsShow_No).Error; err != nil {
  313. return xerr.WithStack(err)
  314. }
  315. } else {
  316. if err = tx.Create(outbound).Error; err != nil {
  317. return xerr.WithStack(err)
  318. }
  319. }
  320. outboundLog := model.NewOutboundDetailList(outbound.Id, goodsItems, unitMap)
  321. if err = tx.Create(outboundLog).Error; err != nil {
  322. return xerr.WithStack(err)
  323. }
  324. return nil
  325. }); err != nil {
  326. return xerr.WithStack(err)
  327. }
  328. return nil
  329. }
  330. func (s *StoreEntry) OutboundList(ctx context.Context, req *pasturePb.SearchOutboundApplyRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchOutboundApplyResponse, error) {
  331. startUnix := util.TimeParseLocalUnix(req.StartDayTime)
  332. endUnix := util.TimeParseLocalEndUnix(req.EndDayTime)
  333. var count int64 = 0
  334. outboundList := make([]*model.Outbound, 0)
  335. pref := s.DB.Model(new(model.Outbound))
  336. if req.OutType > 0 {
  337. pref.Where("out_type = ?", req.OutType)
  338. }
  339. if startUnix > 0 && endUnix > 0 && startUnix <= endUnix {
  340. pref.Where("applicant_at BETWEEN ? AND ?", startUnix, endUnix)
  341. }
  342. if req.Number != "" {
  343. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  344. }
  345. if req.AuditStatus > 0 {
  346. pref.Where("audit_status = ?", req.AuditStatus)
  347. }
  348. if req.ApplicantId > 0 {
  349. pref.Where("applicant_id = ?", req.ApplicantId)
  350. }
  351. if req.ExamineId > 0 {
  352. pref.Where("examine_id = ?", req.ExamineId)
  353. }
  354. if err := pref.Order("id desc").
  355. Count(&count).
  356. Limit(int(pagination.PageSize)).
  357. Offset(int(pagination.PageOffset)).
  358. Find(&outboundList).Error; err != nil {
  359. return nil, xerr.WithStack(err)
  360. }
  361. outTypeMap := s.OutTypeMap()
  362. auditStatusMap := s.AuditStatusMap()
  363. return &pasturePb.SearchOutboundApplyResponse{
  364. Code: http.StatusOK,
  365. Message: "ok",
  366. Data: &pasturePb.SearchOutboundApplyData{
  367. List: model.OutboundSlice(outboundList).ToPB(outTypeMap, auditStatusMap),
  368. Total: int32(count),
  369. PageSize: pagination.PageSize,
  370. Page: pagination.Page,
  371. },
  372. }, nil
  373. }
  374. func (s *StoreEntry) OutboundAudit(ctx context.Context, req *pasturePb.OutboundApplyAuditRequest) error {
  375. outbound, err := s.GetOutboundById(ctx, int64(req.Id))
  376. if err != nil {
  377. return xerr.WithStack(err)
  378. }
  379. if outbound == nil {
  380. return xerr.Custom("出库单不存在")
  381. }
  382. if req.AuditStatus != pasturePb.AuditStatus_Pass && req.AuditStatus != pasturePb.AuditStatus_Reject && req.AuditStatus != pasturePb.AuditStatus_Cancel {
  383. return xerr.Custom("审核状态异常")
  384. }
  385. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  386. return xerr.Custom("异常出库单")
  387. }
  388. currentUser, err := s.GetCurrentSystemUser(ctx)
  389. if err != nil {
  390. return xerr.Custom("登录人信息失效")
  391. }
  392. outboundDetails, err := s.GetOutboundDetailByOutboundId(ctx, outbound.Id)
  393. if err != nil {
  394. return xerr.WithStack(err)
  395. }
  396. if len(outboundDetails) <= 0 {
  397. return xerr.Custom("出库单商品不存在")
  398. }
  399. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  400. if err = tx.Model(outbound).
  401. Where("id = ?", outbound.Id).
  402. Updates(map[string]interface{}{
  403. "audit_status": req.AuditStatus,
  404. "examine_id": currentUser.Id,
  405. "examine_name": currentUser.Name,
  406. "examine_remarks": req.ExamineRemarks,
  407. "examine_at": time.Now().Unix(),
  408. }).Error; err != nil {
  409. return xerr.WithStack(err)
  410. }
  411. if req.AuditStatus != pasturePb.AuditStatus_Pass {
  412. return nil
  413. }
  414. tableName := ""
  415. switch outbound.OutType {
  416. case pasturePb.OutType_Drugs:
  417. tableName = new(model.Drugs).TableName()
  418. case pasturePb.OutType_Medical_Equipment:
  419. tableName = new(model.MedicalEquipment).TableName()
  420. default:
  421. return nil
  422. }
  423. for _, v := range outboundDetails {
  424. if err = tx.Table(tableName).
  425. Where("id = ?", v.GoodsId).
  426. Updates(map[string]interface{}{
  427. "inventory": gorm.Expr("inventory - ?", v.Quantity),
  428. }).Error; err != nil {
  429. return xerr.WithStack(err)
  430. }
  431. }
  432. return nil
  433. }); err != nil {
  434. return xerr.WithStack(err)
  435. }
  436. return nil
  437. }
  438. func (s *StoreEntry) OutboundDetail(ctx context.Context, id int64) (*pasturePb.OutboundDetailResponse, error) {
  439. outbound, err := s.GetOutboundById(ctx, id)
  440. if err != nil {
  441. return nil, xerr.WithStack(err)
  442. }
  443. outboundLogs, err := s.GetOutboundDetailByOutboundId(ctx, id)
  444. if err != nil {
  445. return nil, xerr.WithStack(err)
  446. }
  447. outTypeMap := s.OutTypeMap()
  448. auditStatusMap := s.AuditStatusMap()
  449. applicantAtFormat, examineAtFormat := "", ""
  450. if outbound.ApplicantAt > 0 {
  451. applicantAtFormat = time.Unix(outbound.ApplicantAt, 0).Format(model.LayoutTime)
  452. }
  453. if outbound.ExamineAt > 0 {
  454. examineAtFormat = time.Unix(outbound.ExamineAt, 0).Format(model.LayoutTime)
  455. }
  456. return &pasturePb.OutboundDetailResponse{
  457. Code: http.StatusOK,
  458. Message: "ok",
  459. Data: &pasturePb.OutboundApplyDetail{
  460. Id: int32(outbound.Id),
  461. Number: outbound.Number,
  462. OutType: outbound.OutType,
  463. OutTypeName: outTypeMap[outbound.OutType],
  464. AuditStatus: outbound.AuditStatus,
  465. AuditStatusName: auditStatusMap[outbound.AuditStatus],
  466. ApplicantName: outbound.ApplicantName,
  467. ApplicantRemarks: outbound.ApplicantRemarks,
  468. ExamineName: outbound.ExamineName,
  469. ExamineRemarks: outbound.ExamineRemarks,
  470. ApplicantAtFormat: applicantAtFormat,
  471. ExamineAtFormat: examineAtFormat,
  472. GoodsItem: &pasturePb.OutboundApplyItem{
  473. OutType: outbound.OutType,
  474. Goods: model.OutboundDetailSlice(outboundLogs).ToPB(),
  475. ApplicantRemarks: outbound.ApplicantRemarks,
  476. },
  477. },
  478. }, nil
  479. }
  480. func (s *StoreEntry) OutboundDelete(ctx context.Context, id int64) error {
  481. outbound, err := s.GetOutboundById(ctx, id)
  482. if err != nil {
  483. return xerr.WithStack(err)
  484. }
  485. if outbound == nil {
  486. return xerr.Custom("出库单不存在")
  487. }
  488. currUser, err := s.GetCurrentSystemUser(ctx)
  489. if err != nil {
  490. return xerr.Custom("登录信息失效")
  491. }
  492. if !(outbound.AuditStatus == pasturePb.AuditStatus_Pending || outbound.AuditStatus == pasturePb.AuditStatus_Cancel) {
  493. return xerr.Custom("出库单无法删除")
  494. }
  495. if currUser.Id != int64(outbound.ApplicantId) {
  496. return xerr.Custom("非申请人,无权删除出库单")
  497. }
  498. outbound.Delete()
  499. if err = s.DB.Model(new(model.Outbound)).
  500. Select("audit_status").
  501. Updates(outbound).Error; err != nil {
  502. return xerr.WithStack(err)
  503. }
  504. return nil
  505. }
  506. func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error) {
  507. frozenSemenList := make([]*model.FrozenSemen, 0)
  508. var count int64 = 0
  509. pref := s.DB.Table(new(model.FrozenSemen).TableName())
  510. if req.BullId != "" {
  511. pref.Where("bull_id = ?", req.BullId)
  512. }
  513. if req.Producer != "" {
  514. pref.Where("producer = ?", req.Producer)
  515. }
  516. if err := pref.Order("id desc").
  517. Count(&count).Limit(int(pagination.PageSize)).
  518. Offset(int(pagination.PageOffset)).
  519. Find(&frozenSemenList).Error; err != nil {
  520. return nil, xerr.WithStack(err)
  521. }
  522. frozenSemenTypeMap := s.FrozenSemenTypeMap()
  523. unitMap := s.UnitMap()
  524. return &pasturePb.FrozenSemenResponse{
  525. Code: http.StatusOK,
  526. Message: "ok",
  527. Data: &pasturePb.SearchFrozenSemenData{
  528. List: model.FrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
  529. Total: int32(count),
  530. PageSize: pagination.PageSize,
  531. Page: pagination.Page,
  532. },
  533. }, nil
  534. }
  535. func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
  536. currentUser, _ := s.GetCurrentSystemUser(ctx)
  537. req.CowKindName = s.CowKindMap()[req.CowKind]
  538. newFrozenSemen := model.NewFrozenSemen(req, currentUser)
  539. if err := s.DB.Create(newFrozenSemen).Error; err != nil {
  540. return xerr.WithStack(err)
  541. }
  542. return nil
  543. }