goods.go 19 KB

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