goods.go 20 KB

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