goods.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. userModel, err := s.GetUserModel(ctx)
  15. if err != nil {
  16. return nil, xerr.WithStack(err)
  17. }
  18. drugsList := make([]*model.Drugs, 0)
  19. var count int64 = 0
  20. pref := s.DB.Model(new(model.Drugs)).Where("pasture_id = ?", userModel.AppPasture.Id)
  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. Msg: "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. userModel, err := s.GetUserModel(ctx)
  47. if err != nil {
  48. return xerr.WithStack(err)
  49. }
  50. req.CategoryName = s.DrugCategoryMaps()[req.CategoryId]
  51. req.UnitName = s.UnitMap()[req.Unit]
  52. req.UsageName = s.DrugUsageMaps()[req.Usage]
  53. newDrugs := model.NewDrugs(userModel.AppPasture.Id, req, userModel.SystemUser)
  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. userModel, err := s.GetUserModel(ctx)
  67. if err != nil {
  68. return nil, xerr.WithStack(err)
  69. }
  70. medicalEquipmentList := make([]*model.MedicalEquipment, 0)
  71. var count int64 = 0
  72. pref := s.DB.Model(new(model.MedicalEquipment)).Where("pasture_id = ?", userModel.AppPasture.Id)
  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. Msg: "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. userModel, err := s.GetUserModel(ctx)
  94. if err != nil {
  95. return xerr.WithStack(err)
  96. }
  97. newDrugs := model.NewMedicalEquipment(userModel.AppPasture.Id, req, userModel.SystemUser)
  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. userModel, err := s.GetUserModel(ctx)
  111. if err != nil {
  112. return xerr.WithStack(err)
  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, userModel.AppPasture.Id, int64(v.CowId))
  123. if err != nil {
  124. return xerr.Customf("该牛不存在")
  125. }
  126. neckRing, ok := s.NeckRingIsExist(ctx, userModel.AppPasture.Id, v.Number)
  127. if !ok {
  128. newNeckRing := model.NewNeckRing(userModel.AppPasture.Id, v.Number, int64(v.CowId), userModel.SystemUser)
  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(userModel.AppPasture.Id, v.Number, int64(v.CowId), userModel.SystemUser)
  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. userModel, err := s.GetUserModel(ctx)
  197. if err != nil {
  198. return nil, xerr.WithStack(err)
  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 = ?", userModel.AppPasture.Id).
  205. Where("neck_ring_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. Msg: "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. userModel, err := s.GetUserModel(ctx)
  236. if err != nil {
  237. return xerr.WithStack(err)
  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, userModel.AppPasture.Id, int64(req.Id))
  245. if err != nil || outbound == nil || outbound.Id <= 0 {
  246. return xerr.Customf("该出库单不存在")
  247. }
  248. if userModel.SystemUser.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(userModel.AppPasture.Id, req, userModel.SystemUser)
  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. userModel, err := s.GetUserModel(ctx)
  345. if err != nil {
  346. return nil, xerr.WithStack(err)
  347. }
  348. startUnix := util.TimeParseLocalUnix(req.StartDayTime)
  349. endUnix := util.TimeParseLocalEndUnix(req.EndDayTime)
  350. var count int64 = 0
  351. outboundList := make([]*model.Outbound, 0)
  352. pref := s.DB.Model(new(model.Outbound)).Where("pasture_id = ?", userModel.AppPasture.Id)
  353. if req.OutType > 0 {
  354. pref.Where("out_type = ?", req.OutType)
  355. }
  356. if startUnix > 0 && endUnix > 0 && startUnix <= endUnix {
  357. pref.Where("applicant_at BETWEEN ? AND ?", startUnix, endUnix)
  358. }
  359. if req.Number != "" {
  360. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  361. }
  362. if req.AuditStatus > 0 {
  363. pref.Where("audit_status = ?", req.AuditStatus)
  364. }
  365. if req.ApplicantId > 0 {
  366. pref.Where("applicant_id = ?", req.ApplicantId)
  367. }
  368. if req.ExamineId > 0 {
  369. pref.Where("examine_id = ?", req.ExamineId)
  370. }
  371. if err = pref.Order("id desc").
  372. Count(&count).
  373. Limit(int(pagination.PageSize)).
  374. Offset(int(pagination.PageOffset)).
  375. Find(&outboundList).Error; err != nil {
  376. return nil, xerr.WithStack(err)
  377. }
  378. outTypeMap := s.OutTypeMap()
  379. auditStatusMap := s.AuditStatusMap()
  380. return &pasturePb.SearchOutboundApplyResponse{
  381. Code: http.StatusOK,
  382. Msg: "ok",
  383. Data: &pasturePb.SearchOutboundApplyData{
  384. List: model.OutboundSlice(outboundList).ToPB(outTypeMap, auditStatusMap),
  385. Total: int32(count),
  386. PageSize: pagination.PageSize,
  387. Page: pagination.Page,
  388. },
  389. }, nil
  390. }
  391. func (s *StoreEntry) OutboundAudit(ctx context.Context, req *pasturePb.OutboundApplyAuditRequest) error {
  392. userModel, err := s.GetUserModel(ctx)
  393. if err != nil {
  394. return xerr.WithStack(err)
  395. }
  396. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, int64(req.Id))
  397. if err != nil {
  398. return xerr.WithStack(err)
  399. }
  400. if outbound == nil {
  401. return xerr.Custom("出库单不存在")
  402. }
  403. if req.AuditStatus != pasturePb.AuditStatus_Pass && req.AuditStatus != pasturePb.AuditStatus_Reject && req.AuditStatus != pasturePb.AuditStatus_Cancel {
  404. return xerr.Custom("审核状态异常")
  405. }
  406. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  407. return xerr.Custom("异常出库单")
  408. }
  409. outboundDetails, err := s.GetOutboundDetailByOutboundId(ctx, outbound.Id)
  410. if err != nil {
  411. return xerr.WithStack(err)
  412. }
  413. if len(outboundDetails) <= 0 {
  414. return xerr.Custom("出库单商品不存在")
  415. }
  416. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  417. if err = tx.Model(outbound).
  418. Where("id = ?", outbound.Id).
  419. Updates(map[string]interface{}{
  420. "audit_status": req.AuditStatus,
  421. "examine_id": userModel.SystemUser.Id,
  422. "examine_name": userModel.SystemUser.Name,
  423. "examine_remarks": req.ExamineRemarks,
  424. "examine_at": time.Now().Unix(),
  425. }).Error; err != nil {
  426. return xerr.WithStack(err)
  427. }
  428. if req.AuditStatus != pasturePb.AuditStatus_Pass {
  429. return nil
  430. }
  431. tableName := ""
  432. switch outbound.OutType {
  433. case pasturePb.OutType_Drugs:
  434. tableName = new(model.Drugs).TableName()
  435. case pasturePb.OutType_Medical_Equipment:
  436. tableName = new(model.MedicalEquipment).TableName()
  437. default:
  438. return nil
  439. }
  440. for _, v := range outboundDetails {
  441. if err = tx.Table(tableName).
  442. Where("id = ?", v.GoodsId).
  443. Updates(map[string]interface{}{
  444. "inventory": gorm.Expr("inventory - ?", v.Quantity),
  445. }).Error; err != nil {
  446. return xerr.WithStack(err)
  447. }
  448. }
  449. return nil
  450. }); err != nil {
  451. return xerr.WithStack(err)
  452. }
  453. return nil
  454. }
  455. func (s *StoreEntry) OutboundDetail(ctx context.Context, id int64) (*pasturePb.OutboundDetailResponse, error) {
  456. userModel, err := s.GetUserModel(ctx)
  457. if err != nil {
  458. return nil, xerr.WithStack(err)
  459. }
  460. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, id)
  461. if err != nil {
  462. return nil, xerr.WithStack(err)
  463. }
  464. outboundLogs, err := s.GetOutboundDetailByOutboundId(ctx, id)
  465. if err != nil {
  466. return nil, xerr.WithStack(err)
  467. }
  468. outTypeMap := s.OutTypeMap()
  469. auditStatusMap := s.AuditStatusMap()
  470. applicantAtFormat, examineAtFormat := "", ""
  471. if outbound.ApplicantAt > 0 {
  472. applicantAtFormat = time.Unix(outbound.ApplicantAt, 0).Format(model.LayoutTime)
  473. }
  474. if outbound.ExamineAt > 0 {
  475. examineAtFormat = time.Unix(outbound.ExamineAt, 0).Format(model.LayoutTime)
  476. }
  477. return &pasturePb.OutboundDetailResponse{
  478. Code: http.StatusOK,
  479. Msg: "ok",
  480. Data: &pasturePb.OutboundApplyDetail{
  481. Id: int32(outbound.Id),
  482. Number: outbound.Number,
  483. OutType: outbound.OutType,
  484. OutTypeName: outTypeMap[outbound.OutType],
  485. AuditStatus: outbound.AuditStatus,
  486. AuditStatusName: auditStatusMap[outbound.AuditStatus],
  487. ApplicantName: outbound.ApplicantName,
  488. ApplicantRemarks: outbound.ApplicantRemarks,
  489. ExamineName: outbound.ExamineName,
  490. ExamineRemarks: outbound.ExamineRemarks,
  491. ApplicantAtFormat: applicantAtFormat,
  492. ExamineAtFormat: examineAtFormat,
  493. GoodsItem: &pasturePb.OutboundApplyItem{
  494. OutType: outbound.OutType,
  495. Goods: model.OutboundDetailSlice(outboundLogs).ToPB(),
  496. ApplicantRemarks: outbound.ApplicantRemarks,
  497. },
  498. },
  499. }, nil
  500. }
  501. func (s *StoreEntry) OutboundDelete(ctx context.Context, id int64) error {
  502. userModel, err := s.GetUserModel(ctx)
  503. if err != nil {
  504. return xerr.WithStack(err)
  505. }
  506. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, id)
  507. if err != nil {
  508. return xerr.WithStack(err)
  509. }
  510. if outbound == nil {
  511. return xerr.Custom("出库单不存在")
  512. }
  513. if !(outbound.AuditStatus == pasturePb.AuditStatus_Pending || outbound.AuditStatus == pasturePb.AuditStatus_Cancel) {
  514. return xerr.Custom("出库单无法删除")
  515. }
  516. if userModel.SystemUser.Id != int64(outbound.ApplicantId) {
  517. return xerr.Custom("非申请人,无权删除出库单")
  518. }
  519. outbound.Delete()
  520. if err = s.DB.Model(new(model.Outbound)).
  521. Select("audit_status").
  522. Updates(outbound).Error; err != nil {
  523. return xerr.WithStack(err)
  524. }
  525. return nil
  526. }
  527. func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error) {
  528. frozenSemenList := make([]*model.FrozenSemen, 0)
  529. var count int64 = 0
  530. pref := s.DB.Table(new(model.FrozenSemen).TableName())
  531. if req.BullId != "" {
  532. pref.Where("bull_id = ?", req.BullId)
  533. }
  534. if req.Producer != "" {
  535. pref.Where("producer = ?", req.Producer)
  536. }
  537. if err := pref.Order("id desc").
  538. Count(&count).Limit(int(pagination.PageSize)).
  539. Offset(int(pagination.PageOffset)).
  540. Find(&frozenSemenList).Error; err != nil {
  541. return nil, xerr.WithStack(err)
  542. }
  543. frozenSemenTypeMap := s.FrozenSemenTypeMap()
  544. unitMap := s.UnitMap()
  545. return &pasturePb.FrozenSemenResponse{
  546. Code: http.StatusOK,
  547. Msg: "ok",
  548. Data: &pasturePb.SearchFrozenSemenData{
  549. List: model.FrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
  550. Total: int32(count),
  551. PageSize: pagination.PageSize,
  552. Page: pagination.Page,
  553. },
  554. }, nil
  555. }
  556. func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
  557. userModel, err := s.GetUserModel(ctx)
  558. if err != nil {
  559. return xerr.WithStack(err)
  560. }
  561. req.CowKindName = s.CowKindMap()[req.CowKind]
  562. newFrozenSemen := model.NewFrozenSemen(userModel.AppPasture.Id, req, userModel.SystemUser)
  563. if err := s.DB.Create(newFrozenSemen).Error; err != nil {
  564. return xerr.WithStack(err)
  565. }
  566. return nil
  567. }