goods.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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) NeckRingLogCreateOrUpdate(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. newNeckRingLogList := model.NewNeckRingLogList(req.Items, currentUser)
  110. cowIds := make([]int64, 0)
  111. for _, v := range newNeckRingLogList {
  112. cowIds = append(cowIds, v.CowId)
  113. }
  114. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  115. // 解绑脖环号
  116. if err = tx.Model(new(model.NeckRingLog)).
  117. Where("cow_id IN ?", cowIds).
  118. Updates(map[string]interface{}{
  119. "unbind_at": time.Now().Unix(),
  120. "status": pasturePb.NeckRingStatus_Unbind,
  121. "operation_id": currentUser.Id,
  122. "operation_name": currentUser.Name,
  123. }).Error; err != nil {
  124. return xerr.WithStack(err)
  125. }
  126. // 绑定新脖环
  127. if err = tx.Create(newNeckRingLogList).Error; err != nil {
  128. return xerr.WithStack(err)
  129. }
  130. for _, v := range req.Items {
  131. if v.CowId > 0 {
  132. if err = tx.Model(new(model.Cow)).
  133. Where("id = ?", v.CowId).
  134. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  135. Updates(map[string]interface{}{
  136. "neck_ring_number": v.Number,
  137. }).Error; err != nil {
  138. return xerr.WithStack(err)
  139. }
  140. }
  141. }
  142. return nil
  143. }); err != nil {
  144. return xerr.WithStack(err)
  145. }
  146. return nil
  147. }
  148. func (s *StoreEntry) NeckRingLogList(ctx context.Context, req *pasturePb.SearchNeckRingRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchNeckRingResponse, error) {
  149. neckRingLogList := make([]*model.NeckRingLog, 0)
  150. var count int64 = 0
  151. pref := s.DB.Model(new(model.NeckRingLog)).Where("status > ?", pasturePb.NeckRingStatus_Unbind)
  152. if req.Status > 0 {
  153. pref.Where("status = ?", req.Status)
  154. }
  155. if req.CowId > 0 {
  156. pref.Where("cow_id = ?", req.CowId)
  157. }
  158. if req.Number != "" {
  159. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  160. }
  161. if err := pref.Order("id desc").
  162. Count(&count).
  163. Limit(int(pagination.PageSize)).
  164. Offset(int(pagination.PageOffset)).
  165. Find(&neckRingLogList).Error; err != nil {
  166. return nil, xerr.WithStack(err)
  167. }
  168. neckRingStatusMap := s.NeckRingStatusMap()
  169. return &pasturePb.SearchNeckRingResponse{
  170. Code: http.StatusOK,
  171. Message: "ok",
  172. Data: &pasturePb.SearchNeckRingData{
  173. List: model.NeckRingLogSlice(neckRingLogList).ToPB(neckRingStatusMap),
  174. Total: int32(count),
  175. PageSize: pagination.PageSize,
  176. Page: pagination.Page,
  177. },
  178. }, nil
  179. }
  180. func (s *StoreEntry) OutboundApply(ctx context.Context, req *pasturePb.OutboundApplyItem) error {
  181. currentUser, err := s.GetCurrentSystemUser(ctx)
  182. if err != nil {
  183. return xerr.Custom("登录人信息失效")
  184. }
  185. if len(req.Goods) <= 0 {
  186. return xerr.Custom("请选择要出库商品")
  187. }
  188. goodsItems := make([]*pasturePb.OutboundApplyGoodsItem, 0)
  189. switch req.OutType {
  190. case pasturePb.OutType_Drugs:
  191. for _, v := range req.Goods {
  192. if v.Quantity <= 0 {
  193. return xerr.Custom("请填写商品数量")
  194. }
  195. if v.GoodsId <= 0 {
  196. return xerr.Custom("请选择要出库商品")
  197. }
  198. newDrugs := &model.Drugs{}
  199. if err = s.DB.Model(new(model.Drugs)).
  200. Where("id = ?", v.GoodsId).
  201. Where("inventory >= ?", v.Quantity).
  202. First(newDrugs).Error; err != nil {
  203. return xerr.WithStack(err)
  204. }
  205. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  206. GoodsId: v.GoodsId,
  207. Quantity: v.Quantity,
  208. Unit: v.Unit,
  209. GoodsName: newDrugs.Name,
  210. Specs: newDrugs.Specs,
  211. Producer: newDrugs.Producer,
  212. BatchNumber: newDrugs.BatchNumber,
  213. Price: float32(newDrugs.Price) / 100,
  214. })
  215. }
  216. case pasturePb.OutType_Medical_Equipment:
  217. for _, v := range req.Goods {
  218. if v.Quantity <= 0 {
  219. return xerr.Custom("请填写商品数量")
  220. }
  221. if v.GoodsId <= 0 {
  222. return xerr.Custom("请选择要出库商品")
  223. }
  224. newMedicalEquipment := &model.MedicalEquipment{}
  225. if err = s.DB.Model(new(model.Drugs)).
  226. Where("id = ?", v.GoodsId).
  227. Where("inventory >= ?", v.Quantity).
  228. First(newMedicalEquipment).Error; err != nil {
  229. return xerr.WithStack(err)
  230. }
  231. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  232. GoodsId: v.GoodsId,
  233. Quantity: v.Quantity,
  234. Unit: v.Unit,
  235. GoodsName: newMedicalEquipment.Name,
  236. Specs: newMedicalEquipment.Specs,
  237. Producer: newMedicalEquipment.Producer,
  238. BatchNumber: newMedicalEquipment.BatchNumber,
  239. Price: float32(newMedicalEquipment.Price) / 100,
  240. })
  241. }
  242. default:
  243. return xerr.Custom("未知的出库类型")
  244. }
  245. unitMap := s.UnitMap()
  246. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  247. // 创建出库申请
  248. outbound := model.NewOutbound(req, currentUser)
  249. if err = tx.Create(outbound).Error; err != nil {
  250. return xerr.WithStack(err)
  251. }
  252. outboundLog := model.NewOutboundLogList(outbound.Id, goodsItems, unitMap)
  253. if err = tx.Create(outboundLog).Error; err != nil {
  254. return xerr.WithStack(err)
  255. }
  256. return nil
  257. }); err != nil {
  258. return xerr.WithStack(err)
  259. }
  260. return nil
  261. }
  262. func (s *StoreEntry) OutboundList(ctx context.Context, req *pasturePb.SearchOutboundApplyRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchOutboundApplyResponse, error) {
  263. startUnix := util.TimeParseLocalUnix(req.StartDayTime)
  264. endUnix := util.TimeParseLocalEndUnix(req.EndDayTime)
  265. var count int64 = 0
  266. outboundList := make([]*model.Outbound, 0)
  267. pref := s.DB.Model(new(model.Outbound))
  268. if req.OutType > 0 {
  269. pref.Where("out_type = ?", req.OutType)
  270. }
  271. if startUnix > 0 && endUnix > 0 && startUnix <= endUnix {
  272. pref.Where("applicant_at BETWEEN ? AND ?", startUnix, endUnix)
  273. }
  274. if req.Number != "" {
  275. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  276. }
  277. if req.AuditStatus > 0 {
  278. pref.Where("audit_status = ?", req.AuditStatus)
  279. }
  280. if req.ApplicantId > 0 {
  281. pref.Where("applicant_id = ?", req.ApplicantId)
  282. }
  283. if req.ExamineId > 0 {
  284. pref.Where("examine_id = ?", req.ExamineId)
  285. }
  286. if err := pref.Order("id desc").
  287. Count(&count).
  288. Limit(int(pagination.PageSize)).
  289. Offset(int(pagination.PageOffset)).
  290. Find(&outboundList).Error; err != nil {
  291. return nil, xerr.WithStack(err)
  292. }
  293. outTypeMap := s.OutTypeMap()
  294. auditStatusMap := s.AuditStatusMap()
  295. return &pasturePb.SearchOutboundApplyResponse{
  296. Code: http.StatusOK,
  297. Message: "ok",
  298. Data: &pasturePb.SearchOutboundApplyData{
  299. List: model.OutboundSlice(outboundList).ToPB(outTypeMap, auditStatusMap),
  300. Total: int32(count),
  301. PageSize: pagination.PageSize,
  302. Page: pagination.Page,
  303. },
  304. }, nil
  305. }
  306. func (s *StoreEntry) OutboundAudit(ctx context.Context, req *pasturePb.OutboundApplyAuditRequest) error {
  307. outbound, err := s.GetOutboundById(ctx, int64(req.Id))
  308. if err != nil {
  309. return xerr.WithStack(err)
  310. }
  311. if outbound == nil {
  312. return xerr.Custom("出库单不存在")
  313. }
  314. if req.AuditStatus != pasturePb.AuditStatus_Pass && req.AuditStatus != pasturePb.AuditStatus_Reject && req.AuditStatus != pasturePb.AuditStatus_Cancel {
  315. return xerr.Custom("审核状态异常")
  316. }
  317. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  318. return xerr.Custom("异常出库单")
  319. }
  320. currentUser, err := s.GetCurrentSystemUser(ctx)
  321. if err != nil {
  322. return xerr.Custom("登录人信息失效")
  323. }
  324. outboundLogs, err := s.GetOutboundLogsByOutboundId(ctx, outbound.Id)
  325. if err != nil {
  326. return xerr.WithStack(err)
  327. }
  328. if len(outboundLogs) <= 0 {
  329. return xerr.Custom("出库单商品不存在")
  330. }
  331. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  332. if err = tx.Model(outbound).
  333. Where("id = ?", outbound.Id).
  334. Updates(map[string]interface{}{
  335. "audit_status": req.AuditStatus,
  336. "examine_id": currentUser.Id,
  337. "examine_name": currentUser.Name,
  338. "examine_remarks": req.ExamineRemarks,
  339. "examine_at": time.Now().Unix(),
  340. }).Error; err != nil {
  341. return xerr.WithStack(err)
  342. }
  343. if req.AuditStatus != pasturePb.AuditStatus_Pass {
  344. return nil
  345. }
  346. tableName := ""
  347. switch outbound.OutType {
  348. case pasturePb.OutType_Drugs:
  349. tableName = new(model.Drugs).TableName()
  350. case pasturePb.OutType_Medical_Equipment:
  351. tableName = new(model.MedicalEquipment).TableName()
  352. default:
  353. return nil
  354. }
  355. for _, v := range outboundLogs {
  356. if err = tx.Table(tableName).
  357. Where("id = ?", v.GoodsId).
  358. Updates(map[string]interface{}{
  359. "inventory": gorm.Expr("inventory - ?", v.Quantity),
  360. }).Error; err != nil {
  361. return xerr.WithStack(err)
  362. }
  363. }
  364. return nil
  365. }); err != nil {
  366. return xerr.WithStack(err)
  367. }
  368. return nil
  369. }
  370. func (s *StoreEntry) OutboundDetail(ctx context.Context, id int64) (*pasturePb.OutboundDetailResponse, error) {
  371. outbound, err := s.GetOutboundById(ctx, id)
  372. if err != nil {
  373. return nil, xerr.WithStack(err)
  374. }
  375. outboundLogs, err := s.GetOutboundLogsByOutboundId(ctx, id)
  376. if err != nil {
  377. return nil, xerr.WithStack(err)
  378. }
  379. outTypeMap := s.OutTypeMap()
  380. auditStatusMap := s.AuditStatusMap()
  381. applicantAtFormat, examineAtFormat := "", ""
  382. if outbound.ApplicantAt > 0 {
  383. applicantAtFormat = time.Unix(outbound.ApplicantAt, 0).Format(model.LayoutTime)
  384. }
  385. if outbound.ExamineAt > 0 {
  386. examineAtFormat = time.Unix(outbound.ExamineAt, 0).Format(model.LayoutTime)
  387. }
  388. return &pasturePb.OutboundDetailResponse{
  389. Code: http.StatusOK,
  390. Message: "ok",
  391. Data: &pasturePb.OutboundApplyDetail{
  392. Id: int32(outbound.Id),
  393. Number: outbound.Number,
  394. OutType: outbound.OutType,
  395. OutTypeName: outTypeMap[outbound.OutType],
  396. AuditStatus: outbound.AuditStatus,
  397. AuditStatusName: auditStatusMap[outbound.AuditStatus],
  398. ApplicantName: outbound.ApplicantName,
  399. ApplicantRemarks: outbound.ApplicantRemarks,
  400. ExamineName: outbound.ExamineName,
  401. ExamineRemarks: outbound.ExamineRemarks,
  402. ApplicantAtFormat: applicantAtFormat,
  403. ExamineAtFormat: examineAtFormat,
  404. GoodsItem: &pasturePb.OutboundApplyItem{
  405. OutType: outbound.OutType,
  406. Goods: model.OutboundLogSlice(outboundLogs).ToPB(),
  407. ApplicantRemarks: outbound.ApplicantRemarks,
  408. },
  409. },
  410. }, nil
  411. }
  412. func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error) {
  413. frozenSemenList := make([]*model.FrozenSemen, 0)
  414. var count int64 = 0
  415. pref := s.DB.Table(new(model.FrozenSemen).TableName())
  416. if req.BullId != "" {
  417. pref.Where("bull_id = ?", req.BullId)
  418. }
  419. if req.Producer != "" {
  420. pref.Where("producer = ?", req.Producer)
  421. }
  422. if err := pref.Order("id desc").
  423. Count(&count).Limit(int(pagination.PageSize)).
  424. Offset(int(pagination.PageOffset)).
  425. Find(&frozenSemenList).Error; err != nil {
  426. return nil, xerr.WithStack(err)
  427. }
  428. frozenSemenTypeMap := s.FrozenSemenTypeMap()
  429. unitMap := s.UnitMap()
  430. return &pasturePb.FrozenSemenResponse{
  431. Code: http.StatusOK,
  432. Message: "ok",
  433. Data: &pasturePb.SearchFrozenSemenData{
  434. List: model.FrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
  435. Total: int32(count),
  436. PageSize: pagination.PageSize,
  437. Page: pagination.Page,
  438. },
  439. }, nil
  440. }
  441. func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
  442. currentUser, _ := s.GetCurrentSystemUser(ctx)
  443. req.CowKindName = s.CowKindMap()[req.CowKind]
  444. newFrozenSemen := model.NewFrozenSemen(req, currentUser)
  445. if err := s.DB.Create(newFrozenSemen).Error; err != nil {
  446. return xerr.WithStack(err)
  447. }
  448. return nil
  449. }