goods.go 20 KB

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