goods.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "kpt-pasture/util"
  7. "net/http"
  8. "time"
  9. "gorm.io/gorm"
  10. "gitee.com/xuyiping_admin/pkg/xerr"
  11. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  12. )
  13. func (s *StoreEntry) DrugsList(ctx context.Context, req *pasturePb.SearchDrugsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchDrugsResponse, error) {
  14. drugsList := make([]*model.Drugs, 0)
  15. var count int64 = 0
  16. pref := s.DB.Model(new(model.Drugs))
  17. if req.Name != "" {
  18. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  19. }
  20. if req.Usage > 0 {
  21. pref.Where("usage_method = ?", req.Usage)
  22. }
  23. if req.CategoryId > 0 {
  24. pref.Where("category_id = ?", req.CategoryId)
  25. }
  26. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  27. Find(&drugsList).Error; err != nil {
  28. return nil, xerr.WithStack(err)
  29. }
  30. return &pasturePb.SearchDrugsResponse{
  31. Code: http.StatusOK,
  32. Message: "ok",
  33. Data: &pasturePb.SearchDrugsData{
  34. List: model.DrugsSlice(drugsList).ToPB(),
  35. Total: int32(count),
  36. PageSize: pagination.PageSize,
  37. Page: pagination.Page,
  38. },
  39. }, nil
  40. }
  41. func (s *StoreEntry) DrugsCreateOrUpdate(ctx context.Context, req *pasturePb.SearchDrugsList) error {
  42. currentUser, err := s.GetCurrentSystemUser(ctx)
  43. if err != nil {
  44. return xerr.Custom("登录人信息失效")
  45. }
  46. req.CategoryName = s.DrugCategoryMaps()[req.CategoryId]
  47. req.UnitName = s.UnitMap()[req.Unit]
  48. req.UnitName = s.DrugUsageMaps()[req.Usage]
  49. newDrugs := model.NewDrugs(req, currentUser)
  50. if req.Id <= 0 {
  51. if err = s.DB.Create(newDrugs).Error; err != nil {
  52. return xerr.WithStack(err)
  53. }
  54. } else {
  55. if err = s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
  56. return xerr.WithStack(err)
  57. }
  58. }
  59. return nil
  60. }
  61. func (s *StoreEntry) MedicalEquipmentList(ctx context.Context, req *pasturePb.SearchMedicalEquipmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchMedicalEquipmentResponse, error) {
  62. medicalEquipmentList := make([]*model.MedicalEquipment, 0)
  63. var count int64 = 0
  64. pref := s.DB.Model(new(model.MedicalEquipment))
  65. if req.Name != "" {
  66. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  67. }
  68. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  69. Find(&medicalEquipmentList).Error; err != nil {
  70. return nil, xerr.WithStack(err)
  71. }
  72. unitMap := s.UnitMap()
  73. return &pasturePb.SearchMedicalEquipmentResponse{
  74. Code: http.StatusOK,
  75. Message: "ok",
  76. Data: &pasturePb.SearchMedicalEquipmentData{
  77. List: model.MedicalEquipmentSlice(medicalEquipmentList).ToPB(unitMap),
  78. Total: int32(count),
  79. PageSize: pagination.PageSize,
  80. Page: pagination.Page,
  81. },
  82. }, nil
  83. }
  84. func (s *StoreEntry) MedicalEquipmentCreateOrUpdate(ctx context.Context, req *pasturePb.SearchMedicalEquipmentList) error {
  85. currentUser, err := s.GetCurrentSystemUser(ctx)
  86. if err != nil {
  87. return xerr.Custom("登录人信息失效")
  88. }
  89. newDrugs := model.NewMedicalEquipment(req, currentUser)
  90. if req.Id <= 0 {
  91. if err = s.DB.Create(newDrugs).Error; err != nil {
  92. return xerr.WithStack(err)
  93. }
  94. } else {
  95. if err = s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
  96. return xerr.WithStack(err)
  97. }
  98. }
  99. return nil
  100. }
  101. func (s *StoreEntry) NeckRingCreateOrUpdate(ctx context.Context, req *pasturePb.NeckRingCreateRequest) error {
  102. currentUser, err := s.GetCurrentSystemUser(ctx)
  103. if err != nil {
  104. return xerr.Custom("登录人信息失效")
  105. }
  106. if req.Items == nil || len(req.Items) == 0 {
  107. return xerr.Custom("请选择要脖环数据")
  108. }
  109. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  110. for _, v := range req.Items {
  111. switch req.Status {
  112. // 绑定
  113. case pasturePb.NeckRingOperationStatus_Bind:
  114. _, err = s.GetCowInfoByCowId(ctx, int64(v.CowId))
  115. if err != nil {
  116. return xerr.Customf("该牛不存在")
  117. }
  118. neckRing, ok := s.NeckRingIsExist(ctx, v.Number)
  119. if !ok {
  120. newNeckRing := model.NewNeckRing(v.Number, int64(v.CowId), currentUser)
  121. if err = tx.Create(newNeckRing).Error; err != nil {
  122. return xerr.WithStack(err)
  123. }
  124. }
  125. if ok {
  126. neckRing.Update(int64(v.CowId))
  127. if err = tx.Model(new(model.NeckRing)).
  128. Select("cow_id,wear_at").
  129. Where("id = ?", neckRing.Id).
  130. Updates(neckRing).Error; err != nil {
  131. return xerr.WithStack(err)
  132. }
  133. }
  134. newNeckRingLog := model.NewNeckRingBindLog(v.Number, int64(v.CowId), currentUser)
  135. if err = tx.Create(newNeckRingLog).Error; err != nil {
  136. return xerr.WithStack(err)
  137. }
  138. if err = tx.Model(new(model.Cow)).
  139. Where("id = ?", v.CowId).
  140. Update("neck_ring_number", v.Number).
  141. Error; err != nil {
  142. return xerr.WithStack(err)
  143. }
  144. // 解绑
  145. case pasturePb.NeckRingOperationStatus_UnBind:
  146. if err = tx.Model(new(model.NeckRing)).
  147. Where("number = ?", v.Number).
  148. Updates(map[string]interface{}{
  149. "wear_at": 0,
  150. "cow_id": 0,
  151. "status": pasturePb.NeckRingStatus_Unbind,
  152. }).Error; err != nil {
  153. return xerr.WithStack(err)
  154. }
  155. if err = tx.Model(new(model.Cow)).
  156. Where("neck_ring_number = ?", v.Number).
  157. Updates(map[string]interface{}{
  158. "neck_ring_number": "",
  159. }).Error; err != nil {
  160. return xerr.WithStack(err)
  161. }
  162. if err = tx.Model(new(model.NeckRingBindLog)).
  163. Where("number = ?", v.Number).
  164. Update("un_bind_at", time.Now().Unix()).Error; err != nil {
  165. return xerr.WithStack(err)
  166. }
  167. // 编辑
  168. case pasturePb.NeckRingOperationStatus_Edit:
  169. if err = tx.Model(new(model.NeckRing)).
  170. Where("cow_id = ?", v.CowId).
  171. Update("number", v.Number).Error; err != nil {
  172. return xerr.WithStack(err)
  173. }
  174. if err = tx.Model(new(model.Cow)).
  175. Where("id = ?", v.CowId).
  176. Update("neck_ring_number", v.Number).Error; err != nil {
  177. return xerr.WithStack(err)
  178. }
  179. }
  180. }
  181. return nil
  182. }); err != nil {
  183. return xerr.WithStack(err)
  184. }
  185. return nil
  186. }
  187. func (s *StoreEntry) NeckRingList(ctx context.Context, req *pasturePb.SearchNeckRingRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchNeckRingResponse, error) {
  188. neckRingLogList := make([]*model.NeckRing, 0)
  189. var count int64 = 0
  190. pref := s.DB.Model(new(model.NeckRing)).
  191. Where("status >= ?", pasturePb.NeckRingStatus_Unbind).
  192. Where("number != ''")
  193. if req.Status > 0 {
  194. pref.Where("status = ?", req.Status)
  195. }
  196. if req.CowId > 0 {
  197. pref.Where("cow_id = ?", req.CowId)
  198. }
  199. if req.Number != "" {
  200. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  201. }
  202. if err := pref.Order("id desc").
  203. Count(&count).
  204. Limit(int(pagination.PageSize)).
  205. Offset(int(pagination.PageOffset)).
  206. Find(&neckRingLogList).Error; err != nil {
  207. return nil, xerr.WithStack(err)
  208. }
  209. neckRingStatusMap := s.NeckRingStatusMap()
  210. return &pasturePb.SearchNeckRingResponse{
  211. Code: http.StatusOK,
  212. Message: "ok",
  213. Data: &pasturePb.SearchNeckRingData{
  214. List: model.NeckRingSlice(neckRingLogList).ToPB(neckRingStatusMap, s.DB, GetCowPenInfoByCowId),
  215. Total: int32(count),
  216. PageSize: pagination.PageSize,
  217. Page: pagination.Page,
  218. },
  219. }, nil
  220. }
  221. func (s *StoreEntry) OutboundApply(ctx context.Context, req *pasturePb.OutboundApplyItem) error {
  222. currentUser, err := s.GetCurrentSystemUser(ctx)
  223. if err != nil {
  224. return xerr.Custom("登录人信息失效")
  225. }
  226. if len(req.Goods) <= 0 {
  227. return xerr.Custom("请选择要出库商品")
  228. }
  229. goodsItems := make([]*pasturePb.OutboundApplyGoodsItem, 0)
  230. switch req.OutType {
  231. case pasturePb.OutType_Drugs:
  232. for _, v := range req.Goods {
  233. if v.Quantity <= 0 {
  234. return xerr.Custom("请填写商品数量")
  235. }
  236. if v.GoodsId <= 0 {
  237. return xerr.Custom("请选择要出库商品")
  238. }
  239. newDrugs := &model.Drugs{}
  240. if err = s.DB.Model(new(model.Drugs)).
  241. Where("id = ?", v.GoodsId).
  242. Where("inventory >= ?", v.Quantity).
  243. First(newDrugs).Error; err != nil {
  244. return xerr.WithStack(err)
  245. }
  246. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  247. GoodsId: v.GoodsId,
  248. Quantity: v.Quantity,
  249. Unit: v.Unit,
  250. GoodsName: newDrugs.Name,
  251. Specs: newDrugs.Specs,
  252. Producer: newDrugs.Producer,
  253. BatchNumber: newDrugs.BatchNumber,
  254. Price: float32(newDrugs.Price) / 100,
  255. })
  256. }
  257. case pasturePb.OutType_Medical_Equipment:
  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. newMedicalEquipment := &model.MedicalEquipment{}
  266. if err = s.DB.Model(new(model.Drugs)).
  267. Where("id = ?", v.GoodsId).
  268. Where("inventory >= ?", v.Quantity).
  269. First(newMedicalEquipment).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: newMedicalEquipment.Name,
  277. Specs: newMedicalEquipment.Specs,
  278. Producer: newMedicalEquipment.Producer,
  279. BatchNumber: newMedicalEquipment.BatchNumber,
  280. Price: float32(newMedicalEquipment.Price) / 100,
  281. })
  282. }
  283. default:
  284. return xerr.Custom("未知的出库类型")
  285. }
  286. unitMap := s.UnitMap()
  287. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  288. // 创建出库申请
  289. outbound := model.NewOutbound(req, currentUser)
  290. if err = tx.Create(outbound).Error; err != nil {
  291. return xerr.WithStack(err)
  292. }
  293. outboundLog := model.NewOutboundLogList(outbound.Id, goodsItems, unitMap)
  294. if err = tx.Create(outboundLog).Error; err != nil {
  295. return xerr.WithStack(err)
  296. }
  297. return nil
  298. }); err != nil {
  299. return xerr.WithStack(err)
  300. }
  301. return nil
  302. }
  303. func (s *StoreEntry) OutboundList(ctx context.Context, req *pasturePb.SearchOutboundApplyRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchOutboundApplyResponse, error) {
  304. startUnix := util.TimeParseLocalUnix(req.StartDayTime)
  305. endUnix := util.TimeParseLocalEndUnix(req.EndDayTime)
  306. var count int64 = 0
  307. outboundList := make([]*model.Outbound, 0)
  308. pref := s.DB.Model(new(model.Outbound))
  309. if req.OutType > 0 {
  310. pref.Where("out_type = ?", req.OutType)
  311. }
  312. if startUnix > 0 && endUnix > 0 && startUnix <= endUnix {
  313. pref.Where("applicant_at BETWEEN ? AND ?", startUnix, endUnix)
  314. }
  315. if req.Number != "" {
  316. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  317. }
  318. if req.AuditStatus > 0 {
  319. pref.Where("audit_status = ?", req.AuditStatus)
  320. }
  321. if req.ApplicantId > 0 {
  322. pref.Where("applicant_id = ?", req.ApplicantId)
  323. }
  324. if req.ExamineId > 0 {
  325. pref.Where("examine_id = ?", req.ExamineId)
  326. }
  327. if err := pref.Order("id desc").
  328. Count(&count).
  329. Limit(int(pagination.PageSize)).
  330. Offset(int(pagination.PageOffset)).
  331. Find(&outboundList).Error; err != nil {
  332. return nil, xerr.WithStack(err)
  333. }
  334. outTypeMap := s.OutTypeMap()
  335. auditStatusMap := s.AuditStatusMap()
  336. return &pasturePb.SearchOutboundApplyResponse{
  337. Code: http.StatusOK,
  338. Message: "ok",
  339. Data: &pasturePb.SearchOutboundApplyData{
  340. List: model.OutboundSlice(outboundList).ToPB(outTypeMap, auditStatusMap),
  341. Total: int32(count),
  342. PageSize: pagination.PageSize,
  343. Page: pagination.Page,
  344. },
  345. }, nil
  346. }
  347. func (s *StoreEntry) OutboundAudit(ctx context.Context, req *pasturePb.OutboundApplyAuditRequest) error {
  348. outbound, err := s.GetOutboundById(ctx, int64(req.Id))
  349. if err != nil {
  350. return xerr.WithStack(err)
  351. }
  352. if outbound == nil {
  353. return xerr.Custom("出库单不存在")
  354. }
  355. if req.AuditStatus != pasturePb.AuditStatus_Pass && req.AuditStatus != pasturePb.AuditStatus_Reject && req.AuditStatus != pasturePb.AuditStatus_Cancel {
  356. return xerr.Custom("审核状态异常")
  357. }
  358. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  359. return xerr.Custom("异常出库单")
  360. }
  361. currentUser, err := s.GetCurrentSystemUser(ctx)
  362. if err != nil {
  363. return xerr.Custom("登录人信息失效")
  364. }
  365. outboundLogs, err := s.GetOutboundLogsByOutboundId(ctx, outbound.Id)
  366. if err != nil {
  367. return xerr.WithStack(err)
  368. }
  369. if len(outboundLogs) <= 0 {
  370. return xerr.Custom("出库单商品不存在")
  371. }
  372. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  373. if err = tx.Model(outbound).
  374. Where("id = ?", outbound.Id).
  375. Updates(map[string]interface{}{
  376. "audit_status": req.AuditStatus,
  377. "examine_id": currentUser.Id,
  378. "examine_name": currentUser.Name,
  379. "examine_remarks": req.ExamineRemarks,
  380. "examine_at": time.Now().Unix(),
  381. }).Error; err != nil {
  382. return xerr.WithStack(err)
  383. }
  384. if req.AuditStatus != pasturePb.AuditStatus_Pass {
  385. return nil
  386. }
  387. tableName := ""
  388. switch outbound.OutType {
  389. case pasturePb.OutType_Drugs:
  390. tableName = new(model.Drugs).TableName()
  391. case pasturePb.OutType_Medical_Equipment:
  392. tableName = new(model.MedicalEquipment).TableName()
  393. default:
  394. return nil
  395. }
  396. for _, v := range outboundLogs {
  397. if err = tx.Table(tableName).
  398. Where("id = ?", v.GoodsId).
  399. Updates(map[string]interface{}{
  400. "inventory": gorm.Expr("inventory - ?", v.Quantity),
  401. }).Error; err != nil {
  402. return xerr.WithStack(err)
  403. }
  404. }
  405. return nil
  406. }); err != nil {
  407. return xerr.WithStack(err)
  408. }
  409. return nil
  410. }
  411. func (s *StoreEntry) OutboundDetail(ctx context.Context, id int64) (*pasturePb.OutboundDetailResponse, error) {
  412. outbound, err := s.GetOutboundById(ctx, id)
  413. if err != nil {
  414. return nil, xerr.WithStack(err)
  415. }
  416. outboundLogs, err := s.GetOutboundLogsByOutboundId(ctx, id)
  417. if err != nil {
  418. return nil, xerr.WithStack(err)
  419. }
  420. outTypeMap := s.OutTypeMap()
  421. auditStatusMap := s.AuditStatusMap()
  422. applicantAtFormat, examineAtFormat := "", ""
  423. if outbound.ApplicantAt > 0 {
  424. applicantAtFormat = time.Unix(outbound.ApplicantAt, 0).Format(model.LayoutTime)
  425. }
  426. if outbound.ExamineAt > 0 {
  427. examineAtFormat = time.Unix(outbound.ExamineAt, 0).Format(model.LayoutTime)
  428. }
  429. return &pasturePb.OutboundDetailResponse{
  430. Code: http.StatusOK,
  431. Message: "ok",
  432. Data: &pasturePb.OutboundApplyDetail{
  433. Id: int32(outbound.Id),
  434. Number: outbound.Number,
  435. OutType: outbound.OutType,
  436. OutTypeName: outTypeMap[outbound.OutType],
  437. AuditStatus: outbound.AuditStatus,
  438. AuditStatusName: auditStatusMap[outbound.AuditStatus],
  439. ApplicantName: outbound.ApplicantName,
  440. ApplicantRemarks: outbound.ApplicantRemarks,
  441. ExamineName: outbound.ExamineName,
  442. ExamineRemarks: outbound.ExamineRemarks,
  443. ApplicantAtFormat: applicantAtFormat,
  444. ExamineAtFormat: examineAtFormat,
  445. GoodsItem: &pasturePb.OutboundApplyItem{
  446. OutType: outbound.OutType,
  447. Goods: model.OutboundLogSlice(outboundLogs).ToPB(),
  448. ApplicantRemarks: outbound.ApplicantRemarks,
  449. },
  450. },
  451. }, nil
  452. }
  453. func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error) {
  454. frozenSemenList := make([]*model.FrozenSemen, 0)
  455. var count int64 = 0
  456. pref := s.DB.Table(new(model.FrozenSemen).TableName())
  457. if req.BullId != "" {
  458. pref.Where("bull_id = ?", req.BullId)
  459. }
  460. if req.Producer != "" {
  461. pref.Where("producer = ?", req.Producer)
  462. }
  463. if err := pref.Order("id desc").
  464. Count(&count).Limit(int(pagination.PageSize)).
  465. Offset(int(pagination.PageOffset)).
  466. Find(&frozenSemenList).Error; err != nil {
  467. return nil, xerr.WithStack(err)
  468. }
  469. frozenSemenTypeMap := s.FrozenSemenTypeMap()
  470. unitMap := s.UnitMap()
  471. return &pasturePb.FrozenSemenResponse{
  472. Code: http.StatusOK,
  473. Message: "ok",
  474. Data: &pasturePb.SearchFrozenSemenData{
  475. List: model.FrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
  476. Total: int32(count),
  477. PageSize: pagination.PageSize,
  478. Page: pagination.Page,
  479. },
  480. }, nil
  481. }
  482. func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
  483. currentUser, _ := s.GetCurrentSystemUser(ctx)
  484. req.CowKindName = s.CowKindMap()[req.CowKind]
  485. newFrozenSemen := model.NewFrozenSemen(req, currentUser)
  486. if err := s.DB.Create(newFrozenSemen).Error; err != nil {
  487. return xerr.WithStack(err)
  488. }
  489. return nil
  490. }