goods.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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.Table(fmt.Sprintf("%s as a", new(model.NeckRing).TableName())).
  197. Select("a.*,COALESCE(b.pen_name, '') AS pen_name").
  198. Joins("LEFT JOIN cow as b ON a.cow_id = b.id").
  199. Where("a.status >= ?", pasturePb.NeckRingStatus_Unbind).
  200. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  201. Where("a.neck_ring_number != ''")
  202. if req.Status > 0 {
  203. pref.Where("a.status = ?", req.Status)
  204. }
  205. if req.CowId > 0 {
  206. pref.Where("a.cow_id = ?", req.CowId)
  207. }
  208. if req.Number != "" {
  209. pref.Where("a.number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  210. }
  211. if err = pref.Order("a.id desc").
  212. Count(&count).
  213. Limit(int(pagination.PageSize)).
  214. Offset(int(pagination.PageOffset)).
  215. Find(&neckRingLogList).Error; err != nil {
  216. return nil, xerr.WithStack(err)
  217. }
  218. neckRingStatusMap := s.NeckRingStatusMap()
  219. return &pasturePb.SearchNeckRingResponse{
  220. Code: http.StatusOK,
  221. Msg: "ok",
  222. Data: &pasturePb.SearchNeckRingData{
  223. List: model.NeckRingSlice(neckRingLogList).ToPB(neckRingStatusMap),
  224. Total: int32(count),
  225. PageSize: pagination.PageSize,
  226. Page: pagination.Page,
  227. },
  228. }, nil
  229. }
  230. func (s *StoreEntry) OutboundApply(ctx context.Context, req *pasturePb.OutboundApplyItem) error {
  231. userModel, err := s.GetUserModel(ctx)
  232. if err != nil {
  233. return xerr.WithStack(err)
  234. }
  235. if len(req.Goods) <= 0 {
  236. return xerr.Custom("请选择要出库商品")
  237. }
  238. var outbound *model.Outbound
  239. if req.Id > 0 {
  240. outbound, err = s.GetOutboundById(ctx, userModel.AppPasture.Id, int64(req.Id))
  241. if err != nil || outbound == nil || outbound.Id <= 0 {
  242. return xerr.Customf("该出库单不存在")
  243. }
  244. if userModel.SystemUser.Id != int64(outbound.ApplicantId) {
  245. return xerr.Custom("非申请人,无权修改该出库单")
  246. }
  247. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  248. return xerr.Custom("该出库单不能修改")
  249. }
  250. } else {
  251. // 创建出库申请
  252. outbound = model.NewOutbound(userModel.AppPasture.Id, req, userModel.SystemUser)
  253. }
  254. goodsItems := make([]*pasturePb.OutboundApplyGoodsItem, 0)
  255. switch req.OutType {
  256. case pasturePb.OutType_Drugs:
  257. for _, v := range req.Goods {
  258. if v.Quantity <= 0 {
  259. return xerr.Custom("请填写商品数量")
  260. }
  261. if v.GoodsId <= 0 {
  262. return xerr.Custom("请选择要出库商品")
  263. }
  264. newDrugs := &model.Drugs{}
  265. if err = s.DB.Model(new(model.Drugs)).
  266. Where("id = ?", v.GoodsId).
  267. Where("inventory >= ?", v.Quantity).
  268. First(newDrugs).Error; err != nil {
  269. return xerr.WithStack(err)
  270. }
  271. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  272. GoodsId: v.GoodsId,
  273. Quantity: v.Quantity,
  274. Unit: v.Unit,
  275. GoodsName: newDrugs.Name,
  276. Specs: newDrugs.Specs,
  277. Producer: newDrugs.Producer,
  278. BatchNumber: newDrugs.BatchNumber,
  279. Price: float32(newDrugs.Price) / 100,
  280. })
  281. }
  282. case pasturePb.OutType_Medical_Equipment:
  283. for _, v := range req.Goods {
  284. if v.Quantity <= 0 {
  285. return xerr.Custom("请填写商品数量")
  286. }
  287. if v.GoodsId <= 0 {
  288. return xerr.Custom("请选择要出库商品")
  289. }
  290. newMedicalEquipment := &model.MedicalEquipment{}
  291. if err = s.DB.Model(new(model.Drugs)).
  292. Where("id = ?", v.GoodsId).
  293. Where("inventory >= ?", v.Quantity).
  294. First(newMedicalEquipment).Error; err != nil {
  295. return xerr.WithStack(err)
  296. }
  297. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  298. GoodsId: v.GoodsId,
  299. Quantity: v.Quantity,
  300. Unit: v.Unit,
  301. GoodsName: newMedicalEquipment.Name,
  302. Specs: newMedicalEquipment.Specs,
  303. Producer: newMedicalEquipment.Producer,
  304. BatchNumber: newMedicalEquipment.BatchNumber,
  305. Price: float32(newMedicalEquipment.Price) / 100,
  306. })
  307. }
  308. default:
  309. return xerr.Custom("未知的出库类型")
  310. }
  311. unitMap := s.UnitMap()
  312. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  313. if req.Id > 0 {
  314. if err = tx.Model(new(model.Outbound)).
  315. Where("id = ?", req.Id).
  316. Update("applicant_remarks", req.ApplicantRemarks).Error; err != nil {
  317. return xerr.WithStack(err)
  318. }
  319. if err = tx.Model(new(model.OutboundDetail)).
  320. Where("outbound_id = ?", req.Id).
  321. Update("is_delete = ?", pasturePb.IsShow_No).Error; err != nil {
  322. return xerr.WithStack(err)
  323. }
  324. } else {
  325. if err = tx.Create(outbound).Error; err != nil {
  326. return xerr.WithStack(err)
  327. }
  328. }
  329. outboundLog := model.NewOutboundDetailList(outbound.Id, goodsItems, unitMap)
  330. if err = tx.Create(outboundLog).Error; err != nil {
  331. return xerr.WithStack(err)
  332. }
  333. return nil
  334. }); err != nil {
  335. return xerr.WithStack(err)
  336. }
  337. return nil
  338. }
  339. func (s *StoreEntry) OutboundList(ctx context.Context, req *pasturePb.SearchOutboundApplyRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchOutboundApplyResponse, error) {
  340. userModel, err := s.GetUserModel(ctx)
  341. if err != nil {
  342. return nil, xerr.WithStack(err)
  343. }
  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)).Where("pasture_id = ?", userModel.AppPasture.Id)
  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. Msg: "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. userModel, err := s.GetUserModel(ctx)
  389. if err != nil {
  390. return xerr.WithStack(err)
  391. }
  392. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, int64(req.Id))
  393. if err != nil {
  394. return xerr.WithStack(err)
  395. }
  396. if outbound == nil {
  397. return xerr.Custom("出库单不存在")
  398. }
  399. if req.AuditStatus != pasturePb.AuditStatus_Pass && req.AuditStatus != pasturePb.AuditStatus_Reject && req.AuditStatus != pasturePb.AuditStatus_Cancel {
  400. return xerr.Custom("审核状态异常")
  401. }
  402. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  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": userModel.SystemUser.Id,
  418. "examine_name": userModel.SystemUser.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. userModel, err := s.GetUserModel(ctx)
  453. if err != nil {
  454. return nil, xerr.WithStack(err)
  455. }
  456. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, id)
  457. if err != nil {
  458. return nil, xerr.WithStack(err)
  459. }
  460. outboundLogs, err := s.GetOutboundDetailByOutboundId(ctx, id)
  461. if err != nil {
  462. return nil, xerr.WithStack(err)
  463. }
  464. outTypeMap := s.OutTypeMap()
  465. auditStatusMap := s.AuditStatusMap()
  466. applicantAtFormat, examineAtFormat := "", ""
  467. if outbound.ApplicantAt > 0 {
  468. applicantAtFormat = time.Unix(outbound.ApplicantAt, 0).Format(model.LayoutTime)
  469. }
  470. if outbound.ExamineAt > 0 {
  471. examineAtFormat = time.Unix(outbound.ExamineAt, 0).Format(model.LayoutTime)
  472. }
  473. return &pasturePb.OutboundDetailResponse{
  474. Code: http.StatusOK,
  475. Msg: "ok",
  476. Data: &pasturePb.OutboundApplyDetail{
  477. Id: int32(outbound.Id),
  478. Number: outbound.Number,
  479. OutType: outbound.OutType,
  480. OutTypeName: outTypeMap[outbound.OutType],
  481. AuditStatus: outbound.AuditStatus,
  482. AuditStatusName: auditStatusMap[outbound.AuditStatus],
  483. ApplicantName: outbound.ApplicantName,
  484. ApplicantRemarks: outbound.ApplicantRemarks,
  485. ExamineName: outbound.ExamineName,
  486. ExamineRemarks: outbound.ExamineRemarks,
  487. ApplicantAtFormat: applicantAtFormat,
  488. ExamineAtFormat: examineAtFormat,
  489. GoodsItem: &pasturePb.OutboundApplyItem{
  490. OutType: outbound.OutType,
  491. Goods: model.OutboundDetailSlice(outboundLogs).ToPB(),
  492. ApplicantRemarks: outbound.ApplicantRemarks,
  493. },
  494. },
  495. }, nil
  496. }
  497. func (s *StoreEntry) OutboundDelete(ctx context.Context, id int64) error {
  498. userModel, err := s.GetUserModel(ctx)
  499. if err != nil {
  500. return xerr.WithStack(err)
  501. }
  502. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, id)
  503. if err != nil {
  504. return xerr.WithStack(err)
  505. }
  506. if outbound == nil {
  507. return xerr.Custom("出库单不存在")
  508. }
  509. if !(outbound.AuditStatus == pasturePb.AuditStatus_Pending || outbound.AuditStatus == pasturePb.AuditStatus_Cancel) {
  510. return xerr.Custom("出库单无法删除")
  511. }
  512. if userModel.SystemUser.Id != int64(outbound.ApplicantId) {
  513. return xerr.Custom("非申请人,无权删除出库单")
  514. }
  515. outbound.Delete()
  516. if err = s.DB.Model(new(model.Outbound)).
  517. Select("audit_status").
  518. Updates(outbound).Error; err != nil {
  519. return xerr.WithStack(err)
  520. }
  521. return nil
  522. }
  523. func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error) {
  524. frozenSemenList := make([]*model.FrozenSemen, 0)
  525. var count int64 = 0
  526. pref := s.DB.Table(new(model.FrozenSemen).TableName())
  527. if req.BullId != "" {
  528. pref.Where("bull_id = ?", req.BullId)
  529. }
  530. if req.Producer != "" {
  531. pref.Where("producer = ?", req.Producer)
  532. }
  533. if err := pref.Order("id desc").
  534. Count(&count).Limit(int(pagination.PageSize)).
  535. Offset(int(pagination.PageOffset)).
  536. Find(&frozenSemenList).Error; err != nil {
  537. return nil, xerr.WithStack(err)
  538. }
  539. frozenSemenTypeMap := s.FrozenSemenTypeMap()
  540. unitMap := s.UnitMap()
  541. return &pasturePb.FrozenSemenResponse{
  542. Code: http.StatusOK,
  543. Msg: "ok",
  544. Data: &pasturePb.SearchFrozenSemenData{
  545. List: model.FrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
  546. Total: int32(count),
  547. PageSize: pagination.PageSize,
  548. Page: pagination.Page,
  549. },
  550. }, nil
  551. }
  552. func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
  553. userModel, err := s.GetUserModel(ctx)
  554. if err != nil {
  555. return xerr.WithStack(err)
  556. }
  557. req.CowKindName = s.CowKindMap()[req.CowKind]
  558. newFrozenSemen := model.NewFrozenSemen(userModel.AppPasture.Id, req, userModel.SystemUser)
  559. if err := s.DB.Create(newFrozenSemen).Error; err != nil {
  560. return xerr.WithStack(err)
  561. }
  562. return nil
  563. }