goods.go 19 KB

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