goods.go 20 KB

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