goods.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "net/http"
  7. "time"
  8. "gorm.io/gorm"
  9. "gitee.com/xuyiping_admin/pkg/xerr"
  10. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  11. )
  12. func (s *StoreEntry) DrugsList(ctx context.Context, req *pasturePb.SearchDrugsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchDrugsResponse, error) {
  13. userModel, err := s.GetUserModel(ctx)
  14. if err != nil {
  15. return nil, xerr.WithStack(err)
  16. }
  17. drugsList := make([]*model.Drugs, 0)
  18. var count int64 = 0
  19. pref := s.DB.Model(new(model.Drugs)).Where("pasture_id = ?", userModel.AppPasture.Id)
  20. if req.Name != "" {
  21. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  22. }
  23. if req.Usage > 0 {
  24. pref.Where("usage_method = ?", req.Usage)
  25. }
  26. if req.CategoryId > 0 {
  27. pref.Where("category_id = ?", req.CategoryId)
  28. }
  29. if err = pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  30. Find(&drugsList).Error; err != nil {
  31. return nil, xerr.WithStack(err)
  32. }
  33. return &pasturePb.SearchDrugsResponse{
  34. Code: http.StatusOK,
  35. Msg: "ok",
  36. Data: &pasturePb.SearchDrugsData{
  37. List: model.DrugsSlice(drugsList).ToPB(),
  38. Total: int32(count),
  39. PageSize: pagination.PageSize,
  40. Page: pagination.Page,
  41. },
  42. }, nil
  43. }
  44. func (s *StoreEntry) DrugsCreateOrUpdate(ctx context.Context, req *pasturePb.SearchDrugsList) error {
  45. userModel, err := s.GetUserModel(ctx)
  46. if err != nil {
  47. return xerr.WithStack(err)
  48. }
  49. req.CategoryName = s.DrugCategoryMaps()[req.CategoryId]
  50. req.UnitName = s.UnitMap()[req.Unit]
  51. req.UsageName = s.DrugUsageMaps()[req.Usage]
  52. newDrugs := model.NewDrugs(userModel.AppPasture.Id, req, userModel.SystemUser)
  53. if req.Id <= 0 {
  54. if err = s.DB.Create(newDrugs).Error; err != nil {
  55. return xerr.WithStack(err)
  56. }
  57. } else {
  58. if err = s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
  59. return xerr.WithStack(err)
  60. }
  61. }
  62. return nil
  63. }
  64. func (s *StoreEntry) MedicalEquipmentList(ctx context.Context, req *pasturePb.SearchMedicalEquipmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchMedicalEquipmentResponse, error) {
  65. userModel, err := s.GetUserModel(ctx)
  66. if err != nil {
  67. return nil, xerr.WithStack(err)
  68. }
  69. medicalEquipmentList := make([]*model.MedicalEquipment, 0)
  70. var count int64 = 0
  71. pref := s.DB.Model(new(model.MedicalEquipment)).Where("pasture_id = ?", userModel.AppPasture.Id)
  72. if req.Name != "" {
  73. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  74. }
  75. if err = pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  76. Find(&medicalEquipmentList).Error; err != nil {
  77. return nil, xerr.WithStack(err)
  78. }
  79. unitMap := s.UnitMap()
  80. return &pasturePb.SearchMedicalEquipmentResponse{
  81. Code: http.StatusOK,
  82. Msg: "ok",
  83. Data: &pasturePb.SearchMedicalEquipmentData{
  84. List: model.MedicalEquipmentSlice(medicalEquipmentList).ToPB(unitMap),
  85. Total: int32(count),
  86. PageSize: pagination.PageSize,
  87. Page: pagination.Page,
  88. },
  89. }, nil
  90. }
  91. func (s *StoreEntry) MedicalEquipmentCreateOrUpdate(ctx context.Context, req *pasturePb.SearchMedicalEquipmentList) error {
  92. userModel, err := s.GetUserModel(ctx)
  93. if err != nil {
  94. return xerr.WithStack(err)
  95. }
  96. newDrugs := model.NewMedicalEquipment(userModel.AppPasture.Id, req, userModel.SystemUser)
  97. if req.Id <= 0 {
  98. if err = s.DB.Create(newDrugs).Error; err != nil {
  99. return xerr.WithStack(err)
  100. }
  101. } else {
  102. if err = s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
  103. return xerr.WithStack(err)
  104. }
  105. }
  106. return nil
  107. }
  108. func (s *StoreEntry) NeckRingCreateOrUpdate(ctx context.Context, req *pasturePb.NeckRingCreateRequest) error {
  109. userModel, err := s.GetUserModel(ctx)
  110. if err != nil {
  111. return xerr.WithStack(err)
  112. }
  113. if req.Items == nil || len(req.Items) == 0 {
  114. return xerr.Custom("请选择要脖环数据")
  115. }
  116. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  117. for _, item := range req.Items {
  118. number := ""
  119. cowInfo, err := s.GetCowInfoByEarNumber(ctx, userModel.AppPasture.Id, item.EarNumber)
  120. if err != nil {
  121. return xerr.Customf("该牛: %d 不存在", item.CowId)
  122. }
  123. newNeckRingLog := model.NewNeckRingBindLog(userModel.AppPasture.Id, item.Number, cowInfo, userModel.SystemUser)
  124. neckRing, ok, err := s.NeckRingIsExist(ctx, userModel.AppPasture.Id, item.Number)
  125. switch req.Status {
  126. case pasturePb.NeckRingOperationStatus_Bind: // 绑定
  127. if err != nil {
  128. return xerr.WithStack(err)
  129. }
  130. if ok && neckRing.Status == pasturePb.NeckRingStatus_Bind {
  131. return xerr.Customf("该脖环: %s已经绑定牛只: %s", item.Number, neckRing.EarNumber)
  132. }
  133. newNeckRing := model.NewNeckRing(userModel.AppPasture.Id, item.Number, cowInfo, userModel.SystemUser)
  134. if err = tx.Create(newNeckRing).Error; err != nil {
  135. return xerr.WithStack(err)
  136. }
  137. number = item.Number
  138. newNeckRingLog.OperationStatusName = "绑定"
  139. // 解绑
  140. case pasturePb.NeckRingOperationStatus_UnBind:
  141. if ok && neckRing.Status != pasturePb.NeckRingStatus_Bind {
  142. return xerr.Customf("该脖环: %s,未绑定牛只", item.Number)
  143. }
  144. if err = tx.Model(new(model.NeckRing)).
  145. Where("id = ?", neckRing.Id).
  146. Updates(map[string]interface{}{
  147. "cow_id": 0,
  148. "ear_number": "",
  149. "wear_at": 0,
  150. "status": pasturePb.NeckRingStatus_Unbind,
  151. }).Error; err != nil {
  152. return xerr.WithStack(err)
  153. }
  154. newNeckRingLog.OperationStatusName = "解绑"
  155. // 编辑
  156. case pasturePb.NeckRingOperationStatus_Edit:
  157. if err = tx.Model(new(model.NeckRing)).
  158. Where("neck_ring_number = ?", item.Number).
  159. Updates(map[string]interface{}{
  160. "cow_id": cowInfo.Id,
  161. "ear_number": cowInfo.EarNumber,
  162. "wear_at": time.Now().Unix(),
  163. "status": pasturePb.NeckRingStatus_Bind,
  164. }).Error; err != nil {
  165. return xerr.WithStack(err)
  166. }
  167. number = item.Number
  168. newNeckRingLog.OperationStatusName = "编辑"
  169. default:
  170. continue
  171. }
  172. if err = tx.Create(newNeckRingLog).Error; err != nil {
  173. return xerr.WithStack(err)
  174. }
  175. if err = tx.Model(new(model.Cow)).
  176. Where("id = ?", item.CowId).
  177. Update("neck_ring_number", number).
  178. Error; err != nil {
  179. return xerr.WithStack(err)
  180. }
  181. }
  182. return nil
  183. }); err != nil {
  184. return xerr.WithStack(err)
  185. }
  186. return nil
  187. }
  188. func (s *StoreEntry) NeckRingList(ctx context.Context, req *pasturePb.SearchNeckRingRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchNeckRingResponse, error) {
  189. userModel, err := s.GetUserModel(ctx)
  190. if err != nil {
  191. return nil, xerr.WithStack(err)
  192. }
  193. neckRingLogList := make([]*model.NeckRing, 0)
  194. var count int64 = 0
  195. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.NeckRing).TableName())).
  196. Select("a.*,COALESCE(b.pen_name, '') AS pen_name").
  197. Joins("LEFT JOIN cow as b ON a.cow_id = b.id").
  198. Where("a.status >= ?", pasturePb.NeckRingStatus_Unbind).
  199. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  200. Where("a.neck_ring_number != ''")
  201. if req.Status > 0 {
  202. pref.Where("a.status = ?", req.Status)
  203. }
  204. if req.CowId > 0 {
  205. pref.Where("a.cow_id = ?", req.CowId)
  206. }
  207. if req.Number != "" {
  208. pref.Where("a.number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  209. }
  210. if err = pref.Order("a.id desc").
  211. Count(&count).
  212. Limit(int(pagination.PageSize)).
  213. Offset(int(pagination.PageOffset)).
  214. Find(&neckRingLogList).Error; err != nil {
  215. return nil, xerr.WithStack(err)
  216. }
  217. neckRingStatusMap := s.NeckRingStatusMap()
  218. return &pasturePb.SearchNeckRingResponse{
  219. Code: http.StatusOK,
  220. Msg: "ok",
  221. Data: &pasturePb.SearchNeckRingData{
  222. List: model.NeckRingSlice(neckRingLogList).ToPB(neckRingStatusMap),
  223. Total: int32(count),
  224. PageSize: pagination.PageSize,
  225. Page: pagination.Page,
  226. },
  227. }, nil
  228. }
  229. func (s *StoreEntry) OutboundApply(ctx context.Context, req *pasturePb.OutboundApplyItem) error {
  230. userModel, err := s.GetUserModel(ctx)
  231. if err != nil {
  232. return xerr.WithStack(err)
  233. }
  234. if len(req.Goods) <= 0 {
  235. return xerr.Custom("请选择要出库商品")
  236. }
  237. var outbound *model.Outbound
  238. if req.Id > 0 {
  239. outbound, err = s.GetOutboundById(ctx, userModel.AppPasture.Id, int64(req.Id))
  240. if err != nil || outbound == nil || outbound.Id <= 0 {
  241. return xerr.Customf("该出库单不存在")
  242. }
  243. if userModel.SystemUser.Id != int64(outbound.ApplicantId) {
  244. return xerr.Custom("非申请人,无权修改该出库单")
  245. }
  246. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  247. return xerr.Custom("该出库单不能修改")
  248. }
  249. } else {
  250. // 创建出库申请
  251. outbound = model.NewOutbound(userModel.AppPasture.Id, req, userModel.SystemUser)
  252. }
  253. goodsItems := make([]*pasturePb.OutboundApplyGoodsItem, 0)
  254. switch req.OutType {
  255. case pasturePb.OutType_Drugs:
  256. for _, v := range req.Goods {
  257. if v.Quantity <= 0 {
  258. return xerr.Custom("请填写商品数量")
  259. }
  260. if v.GoodsId <= 0 {
  261. return xerr.Custom("请选择要出库商品")
  262. }
  263. newDrugs := &model.Drugs{}
  264. if err = s.DB.Model(new(model.Drugs)).
  265. Where("id = ?", v.GoodsId).
  266. Where("inventory >= ?", v.Quantity).
  267. First(newDrugs).Error; err != nil {
  268. return xerr.WithStack(err)
  269. }
  270. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  271. GoodsId: v.GoodsId,
  272. Quantity: v.Quantity,
  273. Unit: v.Unit,
  274. GoodsName: newDrugs.Name,
  275. Specs: newDrugs.Specs,
  276. Producer: newDrugs.Producer,
  277. BatchNumber: newDrugs.BatchNumber,
  278. Price: float32(newDrugs.Price) / 100,
  279. })
  280. }
  281. case pasturePb.OutType_Medical_Equipment:
  282. for _, v := range req.Goods {
  283. if v.Quantity <= 0 {
  284. return xerr.Custom("请填写商品数量")
  285. }
  286. if v.GoodsId <= 0 {
  287. return xerr.Custom("请选择要出库商品")
  288. }
  289. newMedicalEquipment := &model.MedicalEquipment{}
  290. if err = s.DB.Model(new(model.Drugs)).
  291. Where("id = ?", v.GoodsId).
  292. Where("inventory >= ?", v.Quantity).
  293. First(newMedicalEquipment).Error; err != nil {
  294. return xerr.WithStack(err)
  295. }
  296. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  297. GoodsId: v.GoodsId,
  298. Quantity: v.Quantity,
  299. Unit: v.Unit,
  300. GoodsName: newMedicalEquipment.Name,
  301. Specs: newMedicalEquipment.Specs,
  302. Producer: newMedicalEquipment.Producer,
  303. BatchNumber: newMedicalEquipment.BatchNumber,
  304. Price: float32(newMedicalEquipment.Price) / 100,
  305. })
  306. }
  307. default:
  308. return xerr.Custom("未知的出库类型")
  309. }
  310. unitMap := s.UnitMap()
  311. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  312. if req.Id > 0 {
  313. if err = tx.Model(new(model.Outbound)).
  314. Where("id = ?", req.Id).
  315. Update("applicant_remarks", req.ApplicantRemarks).Error; err != nil {
  316. return xerr.WithStack(err)
  317. }
  318. if err = tx.Model(new(model.OutboundDetail)).
  319. Where("outbound_id = ?", req.Id).
  320. Update("is_delete = ?", pasturePb.IsShow_No).Error; err != nil {
  321. return xerr.WithStack(err)
  322. }
  323. } else {
  324. if err = tx.Create(outbound).Error; err != nil {
  325. return xerr.WithStack(err)
  326. }
  327. }
  328. outboundLog := model.NewOutboundDetailList(outbound.Id, goodsItems, unitMap)
  329. if err = tx.Create(outboundLog).Error; err != nil {
  330. return xerr.WithStack(err)
  331. }
  332. return nil
  333. }); err != nil {
  334. return xerr.WithStack(err)
  335. }
  336. return nil
  337. }
  338. func (s *StoreEntry) OutboundList(ctx context.Context, req *pasturePb.SearchOutboundApplyRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchOutboundApplyResponse, error) {
  339. userModel, err := s.GetUserModel(ctx)
  340. if err != nil {
  341. return nil, xerr.WithStack(err)
  342. }
  343. var count int64 = 0
  344. outboundList := make([]*model.Outbound, 0)
  345. pref := s.DB.Model(new(model.Outbound)).
  346. Where("pasture_id = ?", userModel.AppPasture.Id)
  347. if req.OutType > 0 {
  348. pref.Where("out_type = ?", req.OutType)
  349. }
  350. if req.StartDayTime > 0 && req.EndDayTime > 0 && req.EndDayTime >= req.StartDayTime {
  351. pref.Where("applicant_at BETWEEN ? AND ?", req.StartDayTime, req.EndDayTime)
  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. userModel, err := s.GetUserModel(ctx)
  523. if err != nil {
  524. return nil, xerr.WithStack(err)
  525. }
  526. frozenSemenList := make([]*model.FrozenSemen, 0)
  527. var count int64 = 0
  528. pref := s.DB.Table(new(model.FrozenSemen).TableName()).
  529. Where("pasture_id = ?", userModel.AppPasture.Id)
  530. if req.BullId != "" {
  531. pref.Where("bull_id = ?", req.BullId)
  532. }
  533. if req.Producer != "" {
  534. pref.Where("producer = ?", req.Producer)
  535. }
  536. if err = pref.Order("id desc").
  537. Count(&count).Limit(int(pagination.PageSize)).
  538. Offset(int(pagination.PageOffset)).
  539. Find(&frozenSemenList).Error; err != nil {
  540. return nil, xerr.WithStack(err)
  541. }
  542. frozenSemenTypeMap := s.FrozenSemenTypeMap()
  543. unitMap := s.UnitMap()
  544. return &pasturePb.FrozenSemenResponse{
  545. Code: http.StatusOK,
  546. Msg: "ok",
  547. Data: &pasturePb.SearchFrozenSemenData{
  548. List: model.FrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
  549. Total: int32(count),
  550. PageSize: pagination.PageSize,
  551. Page: pagination.Page,
  552. },
  553. }, nil
  554. }
  555. func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
  556. userModel, err := s.GetUserModel(ctx)
  557. if err != nil {
  558. return xerr.WithStack(err)
  559. }
  560. req.CowKindName = s.CowKindMap()[req.CowKind]
  561. if req.Id > 0 {
  562. histData := &model.FrozenSemen{}
  563. if err = s.DB.Model(new(model.FrozenSemen)).
  564. Where("id = ?", req.Id).
  565. Where("pasture_id = ?", userModel.AppPasture.Id).
  566. First(histData).
  567. Error; err != nil {
  568. return xerr.Customf("该数据不存在:%d", req.Id)
  569. }
  570. histData.UpdateData(req)
  571. if err = s.DB.Model(new(model.FrozenSemen)).
  572. Where("id = ?", req.Id).
  573. Where("pasture_id = ?", userModel.AppPasture.Id).
  574. Updates(histData).Error; err != nil {
  575. return xerr.WithStack(err)
  576. }
  577. } else {
  578. newFrozenSemen := model.NewFrozenSemen(userModel.AppPasture.Id, req, userModel.SystemUser)
  579. if err = s.DB.Create(newFrozenSemen).Error; err != nil {
  580. return xerr.WithStack(err)
  581. }
  582. }
  583. return nil
  584. }