goods.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 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. neckRingIsBindMap := s.NeckRingIsBindMap()
  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(neckRingIsBindMap, 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. var count int64 = 0
  345. outboundList := make([]*model.Outbound, 0)
  346. pref := s.DB.Model(new(model.Outbound)).
  347. Where("pasture_id = ?", userModel.AppPasture.Id)
  348. if req.OutType > 0 {
  349. pref.Where("out_type = ?", req.OutType)
  350. }
  351. if req.StartDayTime > 0 && req.EndDayTime > 0 && req.EndDayTime >= req.StartDayTime {
  352. pref.Where("applicant_at BETWEEN ? AND ?", req.StartDayTime, req.EndDayTime)
  353. }
  354. if req.Number != "" {
  355. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  356. }
  357. if req.AuditStatus > 0 {
  358. pref.Where("audit_status = ?", req.AuditStatus)
  359. }
  360. if req.ApplicantId > 0 {
  361. pref.Where("applicant_id = ?", req.ApplicantId)
  362. }
  363. if req.ExamineId > 0 {
  364. pref.Where("examine_id = ?", req.ExamineId)
  365. }
  366. if err = pref.Order("id desc").
  367. Count(&count).
  368. Limit(int(pagination.PageSize)).
  369. Offset(int(pagination.PageOffset)).
  370. Find(&outboundList).Error; err != nil {
  371. return nil, xerr.WithStack(err)
  372. }
  373. outTypeMap := s.OutTypeMap()
  374. auditStatusMap := s.AuditStatusMap()
  375. return &pasturePb.SearchOutboundApplyResponse{
  376. Code: http.StatusOK,
  377. Msg: "ok",
  378. Data: &pasturePb.SearchOutboundApplyData{
  379. List: model.OutboundSlice(outboundList).ToPB(outTypeMap, auditStatusMap),
  380. Total: int32(count),
  381. PageSize: pagination.PageSize,
  382. Page: pagination.Page,
  383. },
  384. }, nil
  385. }
  386. func (s *StoreEntry) OutboundAudit(ctx context.Context, req *pasturePb.OutboundApplyAuditRequest) error {
  387. userModel, err := s.GetUserModel(ctx)
  388. if err != nil {
  389. return xerr.WithStack(err)
  390. }
  391. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, int64(req.Id))
  392. if err != nil {
  393. return xerr.WithStack(err)
  394. }
  395. if outbound == nil {
  396. return xerr.Custom("出库单不存在")
  397. }
  398. if req.AuditStatus != pasturePb.AuditStatus_Pass && req.AuditStatus != pasturePb.AuditStatus_Reject && req.AuditStatus != pasturePb.AuditStatus_Cancel {
  399. return xerr.Custom("审核状态异常")
  400. }
  401. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  402. return xerr.Custom("异常出库单")
  403. }
  404. outboundDetails, err := s.GetOutboundDetailByOutboundId(ctx, outbound.Id)
  405. if err != nil {
  406. return xerr.WithStack(err)
  407. }
  408. if len(outboundDetails) <= 0 {
  409. return xerr.Custom("出库单商品不存在")
  410. }
  411. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  412. if err = tx.Model(outbound).
  413. Where("id = ?", outbound.Id).
  414. Updates(map[string]interface{}{
  415. "audit_status": req.AuditStatus,
  416. "examine_id": userModel.SystemUser.Id,
  417. "examine_name": userModel.SystemUser.Name,
  418. "examine_remarks": req.ExamineRemarks,
  419. "examine_at": time.Now().Local().Unix(),
  420. }).Error; err != nil {
  421. return xerr.WithStack(err)
  422. }
  423. if req.AuditStatus != pasturePb.AuditStatus_Pass {
  424. return nil
  425. }
  426. tableName := ""
  427. switch outbound.OutType {
  428. case pasturePb.OutType_Drugs:
  429. tableName = new(model.Drugs).TableName()
  430. case pasturePb.OutType_Medical_Equipment:
  431. tableName = new(model.MedicalEquipment).TableName()
  432. default:
  433. return nil
  434. }
  435. for _, v := range outboundDetails {
  436. if err = tx.Table(tableName).
  437. Where("id = ?", v.GoodsId).
  438. Updates(map[string]interface{}{
  439. "inventory": gorm.Expr("inventory - ?", v.Quantity),
  440. }).Error; err != nil {
  441. return xerr.WithStack(err)
  442. }
  443. }
  444. return nil
  445. }); err != nil {
  446. return xerr.WithStack(err)
  447. }
  448. return nil
  449. }
  450. func (s *StoreEntry) OutboundDetail(ctx context.Context, id int64) (*pasturePb.OutboundDetailResponse, error) {
  451. userModel, err := s.GetUserModel(ctx)
  452. if err != nil {
  453. return nil, xerr.WithStack(err)
  454. }
  455. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, id)
  456. if err != nil {
  457. return nil, xerr.WithStack(err)
  458. }
  459. outboundLogs, err := s.GetOutboundDetailByOutboundId(ctx, id)
  460. if err != nil {
  461. return nil, xerr.WithStack(err)
  462. }
  463. outTypeMap := s.OutTypeMap()
  464. auditStatusMap := s.AuditStatusMap()
  465. applicantAtFormat, examineAtFormat := "", ""
  466. if outbound.ApplicantAt > 0 {
  467. applicantAtFormat = time.Unix(outbound.ApplicantAt, 0).Local().Format(model.LayoutTime)
  468. }
  469. if outbound.ExamineAt > 0 {
  470. examineAtFormat = time.Unix(outbound.ExamineAt, 0).Local().Format(model.LayoutTime)
  471. }
  472. return &pasturePb.OutboundDetailResponse{
  473. Code: http.StatusOK,
  474. Msg: "ok",
  475. Data: &pasturePb.OutboundApplyDetail{
  476. Id: int32(outbound.Id),
  477. Number: outbound.Number,
  478. OutType: outbound.OutType,
  479. OutTypeName: outTypeMap[outbound.OutType],
  480. AuditStatus: outbound.AuditStatus,
  481. AuditStatusName: auditStatusMap[outbound.AuditStatus],
  482. ApplicantName: outbound.ApplicantName,
  483. ApplicantRemarks: outbound.ApplicantRemarks,
  484. ExamineName: outbound.ExamineName,
  485. ExamineRemarks: outbound.ExamineRemarks,
  486. ApplicantAtFormat: applicantAtFormat,
  487. ExamineAtFormat: examineAtFormat,
  488. GoodsItem: &pasturePb.OutboundApplyItem{
  489. OutType: outbound.OutType,
  490. Goods: model.OutboundDetailSlice(outboundLogs).ToPB(),
  491. ApplicantRemarks: outbound.ApplicantRemarks,
  492. },
  493. },
  494. }, nil
  495. }
  496. func (s *StoreEntry) OutboundDelete(ctx context.Context, id int64) error {
  497. userModel, err := s.GetUserModel(ctx)
  498. if err != nil {
  499. return xerr.WithStack(err)
  500. }
  501. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, id)
  502. if err != nil {
  503. return xerr.WithStack(err)
  504. }
  505. if outbound == nil {
  506. return xerr.Custom("出库单不存在")
  507. }
  508. if !(outbound.AuditStatus == pasturePb.AuditStatus_Pending || outbound.AuditStatus == pasturePb.AuditStatus_Cancel) {
  509. return xerr.Custom("出库单无法删除")
  510. }
  511. if userModel.SystemUser.Id != int64(outbound.ApplicantId) {
  512. return xerr.Custom("非申请人,无权删除出库单")
  513. }
  514. outbound.Delete()
  515. if err = s.DB.Model(new(model.Outbound)).
  516. Select("audit_status").
  517. Updates(outbound).Error; err != nil {
  518. return xerr.WithStack(err)
  519. }
  520. return nil
  521. }
  522. func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error) {
  523. userModel, err := s.GetUserModel(ctx)
  524. if err != nil {
  525. return nil, xerr.WithStack(err)
  526. }
  527. frozenSemenList := make([]*model.FrozenSemen, 0)
  528. var count int64 = 0
  529. pref := s.DB.Table(new(model.FrozenSemen).TableName()).
  530. Where("pasture_id = ?", userModel.AppPasture.Id)
  531. if req.BullId != "" {
  532. pref.Where("bull_id = ?", req.BullId)
  533. }
  534. if req.Producer != "" {
  535. pref.Where("producer = ?", req.Producer)
  536. }
  537. if err = pref.Order("id desc").
  538. Count(&count).Limit(int(pagination.PageSize)).
  539. Offset(int(pagination.PageOffset)).
  540. Find(&frozenSemenList).Error; err != nil {
  541. return nil, xerr.WithStack(err)
  542. }
  543. frozenSemenTypeMap := s.FrozenSemenTypeMap()
  544. unitMap := s.UnitMap()
  545. return &pasturePb.FrozenSemenResponse{
  546. Code: http.StatusOK,
  547. Msg: "ok",
  548. Data: &pasturePb.SearchFrozenSemenData{
  549. List: model.FrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
  550. Total: int32(count),
  551. PageSize: pagination.PageSize,
  552. Page: pagination.Page,
  553. },
  554. }, nil
  555. }
  556. func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
  557. userModel, err := s.GetUserModel(ctx)
  558. if err != nil {
  559. return xerr.WithStack(err)
  560. }
  561. req.CowKindName = s.CowKindMap()[req.CowKind]
  562. if req.Id > 0 {
  563. histData := &model.FrozenSemen{}
  564. if err = s.DB.Model(new(model.FrozenSemen)).
  565. Where("id = ?", req.Id).
  566. Where("pasture_id = ?", userModel.AppPasture.Id).
  567. First(histData).
  568. Error; err != nil {
  569. return xerr.Customf("该数据不存在:%d", req.Id)
  570. }
  571. histData.UpdateData(req)
  572. if err = s.DB.Model(new(model.FrozenSemen)).
  573. Where("id = ?", req.Id).
  574. Where("pasture_id = ?", userModel.AppPasture.Id).
  575. Updates(histData).Error; err != nil {
  576. return xerr.WithStack(err)
  577. }
  578. } else {
  579. newFrozenSemen := model.NewFrozenSemen(userModel.AppPasture.Id, req, userModel.SystemUser)
  580. if err = s.DB.Create(newFrozenSemen).Error; err != nil {
  581. return xerr.WithStack(err)
  582. }
  583. }
  584. return nil
  585. }