goods.go 20 KB

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