goods.go 18 KB

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