goods.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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("cowId 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("quantity >= ?", 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. })
  210. }
  211. case pasturePb.OutType_Medical_Equipment:
  212. for _, v := range req.Goods {
  213. if v.Quantity <= 0 {
  214. return xerr.Custom("请填写商品数量")
  215. }
  216. if v.GoodsId <= 0 {
  217. return xerr.Custom("请选择要出库商品")
  218. }
  219. newMedicalEquipment := &model.MedicalEquipment{}
  220. if err = s.DB.Model(new(model.Drugs)).
  221. Where("id = ?", v.GoodsId).
  222. Where("quantity >= ?", v.Quantity).
  223. First(newMedicalEquipment).Error; err != nil {
  224. return xerr.WithStack(err)
  225. }
  226. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  227. GoodsId: v.GoodsId,
  228. Quantity: v.Quantity,
  229. Unit: v.Unit,
  230. })
  231. }
  232. default:
  233. return xerr.Custom("未知的出库类型")
  234. }
  235. unitMap := s.UnitMap()
  236. outbound := model.NewOutbound(req, currentUser)
  237. outboundLog := model.NewOutboundLogList(goodsItems, unitMap)
  238. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  239. // 创建出库申请
  240. if err = tx.Create(outbound).Error; err != nil {
  241. return xerr.WithStack(err)
  242. }
  243. if err = tx.Create(outboundLog).Error; err != nil {
  244. return xerr.WithStack(err)
  245. }
  246. return nil
  247. }); err != nil {
  248. return xerr.WithStack(err)
  249. }
  250. return nil
  251. }
  252. func (s *StoreEntry) OutboundList(ctx context.Context, req *pasturePb.SearchOutboundApplyRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchOutboundApplyResponse, error) {
  253. startUnix := util.TimeParseLocalUnix(req.StartDayTime)
  254. endUnix := util.TimeParseLocalEndUnix(req.EndDayTime)
  255. var count int64 = 0
  256. outboundList := make([]*model.Outbound, 0)
  257. pref := s.DB.Model(new(model.Outbound))
  258. if req.OutType > 0 {
  259. pref.Where("out_type = ?", req.OutType)
  260. }
  261. if startUnix > 0 && endUnix > 0 && startUnix <= endUnix {
  262. pref.Where("applicant_at BETWEEN ? AND ?", startUnix, endUnix)
  263. }
  264. if req.Number != "" {
  265. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  266. }
  267. if req.AuditStatus > 0 {
  268. pref.Where("audit_status = ?", req.AuditStatus)
  269. }
  270. if req.ApplicantId > 0 {
  271. pref.Where("applicant_id = ?", req.ApplicantId)
  272. }
  273. if req.ExamineId > 0 {
  274. pref.Where("examine_id = ?", req.ExamineId)
  275. }
  276. if err := pref.Order("id desc").
  277. Count(&count).
  278. Limit(int(pagination.PageSize)).
  279. Offset(int(pagination.PageOffset)).
  280. Find(&outboundList).Error; err != nil {
  281. return nil, xerr.WithStack(err)
  282. }
  283. outTypeMap := s.OutTypeMap()
  284. auditStatusMap := s.AuditStatusMap()
  285. return &pasturePb.SearchOutboundApplyResponse{
  286. Code: http.StatusOK,
  287. Message: "ok",
  288. Data: &pasturePb.SearchOutboundApplyData{
  289. List: model.OutboundSlice(outboundList).ToPB(outTypeMap, auditStatusMap),
  290. Total: int32(count),
  291. PageSize: pagination.PageSize,
  292. Page: pagination.Page,
  293. },
  294. }, nil
  295. }
  296. func (s *StoreEntry) OutboundAudit(ctx context.Context, req *pasturePb.OutboundApplyAuditRequest) error {
  297. outbound, err := s.GetOutboundById(ctx, int64(req.Id))
  298. if err != nil {
  299. return xerr.WithStack(err)
  300. }
  301. if outbound == nil {
  302. return xerr.Custom("出库单不存在")
  303. }
  304. if req.AuditStatus != pasturePb.AuditStatus_Pass && req.AuditStatus != pasturePb.AuditStatus_Reject && req.AuditStatus != pasturePb.AuditStatus_Cancel {
  305. return xerr.Custom("审核状态异常")
  306. }
  307. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  308. return xerr.Custom("异常出库单")
  309. }
  310. currentUser, err := s.GetCurrentSystemUser(ctx)
  311. if err != nil {
  312. return xerr.Custom("登录人信息失效")
  313. }
  314. outboundLogs, err := s.GetOutboundLogsByOutboundId(ctx, int64(outbound.Id))
  315. if err != nil {
  316. return xerr.WithStack(err)
  317. }
  318. if len(outboundLogs) <= 0 {
  319. return xerr.Custom("出库单商品不存在")
  320. }
  321. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  322. if err = tx.Model(outbound).
  323. Where("id = ?", outbound.Id).
  324. Updates(map[string]interface{}{
  325. "audit_status": req.AuditStatus,
  326. "examine_id": currentUser.Id,
  327. "examine_name": currentUser.Name,
  328. "examine_remarks": req.ExamineRemarks,
  329. "examine_at": time.Now().Unix(),
  330. }).Error; err != nil {
  331. return xerr.WithStack(err)
  332. }
  333. if req.AuditStatus != pasturePb.AuditStatus_Pass {
  334. return nil
  335. }
  336. tableName := ""
  337. switch outbound.OutType {
  338. case pasturePb.OutType_Drugs:
  339. tableName = new(model.Drugs).TableName()
  340. case pasturePb.OutType_Medical_Equipment:
  341. tableName = new(model.MedicalEquipment).TableName()
  342. default:
  343. return nil
  344. }
  345. for _, v := range outboundLogs {
  346. if err = tx.Table(tableName).
  347. Where("id = ?", v.GoodsId).
  348. Updates(map[string]interface{}{
  349. "quantity": gorm.Expr("quantity - ?", v.Quantity),
  350. }).Error; err != nil {
  351. return xerr.WithStack(err)
  352. }
  353. }
  354. return nil
  355. }); err != nil {
  356. return xerr.WithStack(err)
  357. }
  358. return nil
  359. }
  360. func (s *StoreEntry) OutboundDetail(ctx context.Context, id int64) (*pasturePb.OutboundDetailResponse, error) {
  361. outbound, err := s.GetOutboundById(ctx, id)
  362. if err != nil {
  363. return nil, xerr.WithStack(err)
  364. }
  365. outboundLogs, err := s.GetOutboundLogsByOutboundId(ctx, id)
  366. if err != nil {
  367. return nil, xerr.WithStack(err)
  368. }
  369. outTypeMap := s.OutTypeMap()
  370. auditStatusMap := s.AuditStatusMap()
  371. applicantAtFormat, examineAtFormat := "", ""
  372. if outbound.ApplicantAt > 0 {
  373. applicantAtFormat = time.Unix(outbound.ApplicantAt, 0).Format(model.LayoutTime)
  374. }
  375. if outbound.ExamineAt > 0 {
  376. examineAtFormat = time.Unix(outbound.ExamineAt, 0).Format(model.LayoutTime)
  377. }
  378. return &pasturePb.OutboundDetailResponse{
  379. Code: http.StatusOK,
  380. Message: "ok",
  381. Data: &pasturePb.OutboundApplyDetail{
  382. Id: outbound.Id,
  383. Number: outbound.Number,
  384. OutType: outbound.OutType,
  385. OutTypeName: outTypeMap[outbound.OutType],
  386. AuditStatus: outbound.AuditStatus,
  387. AuditStatusName: auditStatusMap[outbound.AuditStatus],
  388. ApplicantName: outbound.ApplicantName,
  389. ApplicantRemarks: outbound.ApplicantRemarks,
  390. ExamineName: outbound.ExamineName,
  391. ExamineRemarks: outbound.ExamineRemarks,
  392. ApplicantAtFormat: applicantAtFormat,
  393. ExamineAtFormat: examineAtFormat,
  394. GoodsItem: &pasturePb.OutboundApplyItem{
  395. OutType: outbound.OutType,
  396. Goods: model.OutboundLogSlice(outboundLogs).ToPB(),
  397. ApplicantRemarks: outbound.ApplicantRemarks,
  398. },
  399. },
  400. }, nil
  401. }
  402. func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error) {
  403. frozenSemenList := make([]*model.FrozenSemen, 0)
  404. var count int64 = 0
  405. pref := s.DB.Table(new(model.FrozenSemen).TableName())
  406. if req.BullId != "" {
  407. pref.Where("bull_id = ?", req.BullId)
  408. }
  409. if req.Producer != "" {
  410. pref.Where("producer = ?", req.Producer)
  411. }
  412. if err := pref.Order("id desc").
  413. Count(&count).Limit(int(pagination.PageSize)).
  414. Offset(int(pagination.PageOffset)).
  415. Find(&frozenSemenList).Error; err != nil {
  416. return nil, xerr.WithStack(err)
  417. }
  418. frozenSemenTypeMap := s.FrozenSemenTypeMap()
  419. unitMap := s.UnitMap()
  420. return &pasturePb.FrozenSemenResponse{
  421. Code: http.StatusOK,
  422. Message: "ok",
  423. Data: &pasturePb.SearchFrozenSemenData{
  424. List: model.FrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
  425. Total: int32(count),
  426. PageSize: pagination.PageSize,
  427. Page: pagination.Page,
  428. },
  429. }, nil
  430. }
  431. func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
  432. currentUser, _ := s.GetCurrentSystemUser(ctx)
  433. req.CowKindName = s.CowKindMap()[req.CowKind]
  434. newFrozenSemen := model.NewFrozenSemen(req, currentUser)
  435. if err := s.DB.Create(newFrozenSemen).Error; err != nil {
  436. return xerr.WithStack(err)
  437. }
  438. return nil
  439. }