goods.go 20 KB

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