goods.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "net/http"
  7. "time"
  8. "github.com/nicksnyder/go-i18n/v2/i18n"
  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. userModel, err := s.GetUserModel(ctx)
  15. if err != nil {
  16. return nil, xerr.WithStack(err)
  17. }
  18. drugsList := make([]*model.Drugs, 0)
  19. var count int64 = 0
  20. pref := s.DB.Model(new(model.Drugs)).Where("pasture_id = ?", userModel.AppPasture.Id)
  21. if req.Name != "" {
  22. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  23. }
  24. if req.Usage > 0 {
  25. pref.Where("usage_method = ?", req.Usage)
  26. }
  27. if req.CategoryId > 0 {
  28. pref.Where("category_id = ?", req.CategoryId)
  29. }
  30. if err = pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  31. Find(&drugsList).Error; err != nil {
  32. return nil, xerr.WithStack(err)
  33. }
  34. return &pasturePb.SearchDrugsResponse{
  35. Code: http.StatusOK,
  36. Msg: "ok",
  37. Data: &pasturePb.SearchDrugsData{
  38. List: model.DrugsSlice(drugsList).ToPB(),
  39. Total: int32(count),
  40. PageSize: pagination.PageSize,
  41. Page: pagination.Page,
  42. },
  43. }, nil
  44. }
  45. func (s *StoreEntry) DrugsCreateOrUpdate(ctx context.Context, req *pasturePb.SearchDrugsList) error {
  46. userModel, err := s.GetUserModel(ctx)
  47. if err != nil {
  48. return xerr.WithStack(err)
  49. }
  50. req.CategoryName = s.DrugCategoryMaps()[req.CategoryId]
  51. req.UnitName = s.UnitMap()[req.Unit]
  52. req.UsageName = s.DrugUsageMaps()[req.Usage]
  53. newDrugs := model.NewDrugs(userModel.AppPasture.Id, req, userModel.SystemUser)
  54. if req.Id <= 0 {
  55. if err = s.DB.Create(newDrugs).Error; err != nil {
  56. return xerr.WithStack(err)
  57. }
  58. } else {
  59. if err = s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
  60. return xerr.WithStack(err)
  61. }
  62. }
  63. return nil
  64. }
  65. func (s *StoreEntry) MedicalEquipmentList(ctx context.Context, req *pasturePb.SearchMedicalEquipmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchMedicalEquipmentResponse, error) {
  66. userModel, err := s.GetUserModel(ctx)
  67. if err != nil {
  68. return nil, xerr.WithStack(err)
  69. }
  70. medicalEquipmentList := make([]*model.MedicalEquipment, 0)
  71. var count int64 = 0
  72. pref := s.DB.Model(new(model.MedicalEquipment)).Where("pasture_id = ?", userModel.AppPasture.Id)
  73. if req.Name != "" {
  74. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  75. }
  76. if err = pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  77. Find(&medicalEquipmentList).Error; err != nil {
  78. return nil, xerr.WithStack(err)
  79. }
  80. unitMap := s.UnitMap()
  81. return &pasturePb.SearchMedicalEquipmentResponse{
  82. Code: http.StatusOK,
  83. Msg: "ok",
  84. Data: &pasturePb.SearchMedicalEquipmentData{
  85. List: model.MedicalEquipmentSlice(medicalEquipmentList).ToPB(unitMap),
  86. Total: int32(count),
  87. PageSize: pagination.PageSize,
  88. Page: pagination.Page,
  89. },
  90. }, nil
  91. }
  92. func (s *StoreEntry) MedicalEquipmentCreateOrUpdate(ctx context.Context, req *pasturePb.SearchMedicalEquipmentList) error {
  93. userModel, err := s.GetUserModel(ctx)
  94. if err != nil {
  95. return xerr.WithStack(err)
  96. }
  97. newDrugs := model.NewMedicalEquipment(userModel.AppPasture.Id, req, userModel.SystemUser)
  98. if req.Id <= 0 {
  99. if err = s.DB.Create(newDrugs).Error; err != nil {
  100. return xerr.WithStack(err)
  101. }
  102. } else {
  103. if err = s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
  104. return xerr.WithStack(err)
  105. }
  106. }
  107. return nil
  108. }
  109. func (s *StoreEntry) NeckRingCreateOrUpdate(ctx context.Context, req *pasturePb.NeckRingCreateRequest) error {
  110. userModel, err := s.GetUserModel(ctx)
  111. if err != nil {
  112. return xerr.WithStack(err)
  113. }
  114. if req.Items == nil || len(req.Items) == 0 {
  115. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  116. MessageID: "goods.selectNeckRing",
  117. })
  118. return xerr.Custom(messageId)
  119. }
  120. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  121. for _, item := range req.Items {
  122. number := ""
  123. cowInfo, err := s.GetCowInfoByEarNumber(ctx, userModel.AppPasture.Id, item.EarNumber)
  124. if err != nil {
  125. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  126. MessageID: "cow.cowNotExist",
  127. TemplateData: map[string]interface{}{
  128. "earNumber": item.EarNumber,
  129. },
  130. })
  131. return xerr.Customf(messageId)
  132. }
  133. newNeckRingLog := model.NewNeckRingBindLog(userModel.AppPasture.Id, item.Number, cowInfo, userModel.SystemUser, "")
  134. neckRing, ok := s.NeckRingIsExist(userModel.AppPasture.Id, item.Number)
  135. switch req.Status {
  136. case pasturePb.NeckRingOperationStatus_Bind: // 绑定
  137. if ok && neckRing.IsBind == pasturePb.NeckRingIsBind_Bind {
  138. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  139. MessageID: "goods.neckRingAlreadyBind",
  140. TemplateData: map[string]interface{}{
  141. "neckRingNumber": item.Number,
  142. "earNumber": neckRing.EarNumber,
  143. },
  144. })
  145. return xerr.Customf(messageId)
  146. }
  147. if cowInfo.NeckRingNumber != "" {
  148. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  149. MessageID: "cow.neckRingNumberBind",
  150. TemplateData: map[string]interface{}{
  151. "neckRingNumber": cowInfo.NeckRingNumber,
  152. "earNumber": cowInfo.EarNumber,
  153. },
  154. })
  155. return xerr.Customf(messageId)
  156. }
  157. newNeckRing := model.NewNeckRing(userModel.AppPasture.Id, item.Number, cowInfo, userModel.SystemUser)
  158. if err = tx.Create(newNeckRing).Error; err != nil {
  159. return xerr.WithStack(err)
  160. }
  161. number = item.Number
  162. newNeckRingLog.OperationName = model.OperationNameBind
  163. newNeckRingLog.Remarks = model.DefaultBind
  164. // 解绑
  165. case pasturePb.NeckRingOperationStatus_UnBind:
  166. if ok && neckRing.IsBind != pasturePb.NeckRingIsBind_Bind {
  167. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  168. MessageID: "goods.neckRingNotBind",
  169. TemplateData: map[string]interface{}{
  170. "neckRingNumber": item.Number,
  171. },
  172. })
  173. return xerr.Customf(messageId)
  174. }
  175. if err = tx.Model(new(model.NeckRing)).
  176. Where("id = ?", neckRing.Id).
  177. Updates(map[string]interface{}{
  178. "cow_id": 0,
  179. "ear_number": "",
  180. "wear_at": 0,
  181. "is_bind": pasturePb.NeckRingIsBind_Unbind,
  182. }).Error; err != nil {
  183. return xerr.WithStack(err)
  184. }
  185. newNeckRingLog.OperationName = model.OperationNameUnbind
  186. newNeckRingLog.Remarks = model.DefaultUnBind
  187. // 编辑
  188. case pasturePb.NeckRingOperationStatus_Edit:
  189. if cowInfo.NeckRingNumber != "" && item.Number != cowInfo.NeckRingNumber {
  190. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  191. MessageID: "cow.neckRingNumberBind",
  192. TemplateData: map[string]interface{}{
  193. "neckRingNumber": cowInfo.NeckRingNumber,
  194. "earNumber": cowInfo.EarNumber,
  195. },
  196. })
  197. return xerr.Customf(messageId)
  198. }
  199. if err = tx.Model(new(model.NeckRing)).
  200. Where("neck_ring_number = ?", item.Number).
  201. Updates(map[string]interface{}{
  202. "cow_id": cowInfo.Id,
  203. "ear_number": cowInfo.EarNumber,
  204. "wear_at": time.Now().Local().Unix(),
  205. "is_bind": pasturePb.NeckRingIsBind_Bind,
  206. }).Error; err != nil {
  207. return xerr.WithStack(err)
  208. }
  209. number = item.Number
  210. newNeckRingLog.OperationName = model.OperationNameUpdate
  211. }
  212. if err = tx.Create(newNeckRingLog).Error; err != nil {
  213. return xerr.WithStack(err)
  214. }
  215. if err = tx.Model(new(model.Cow)).
  216. Where("id = ?", cowInfo.Id).
  217. Update("neck_ring_number", number).
  218. Error; err != nil {
  219. return xerr.WithStack(err)
  220. }
  221. }
  222. return nil
  223. }); err != nil {
  224. return xerr.WithStack(err)
  225. }
  226. return nil
  227. }
  228. func (s *StoreEntry) NeckRingList(ctx context.Context, req *pasturePb.SearchNeckRingRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchNeckRingResponse, error) {
  229. userModel, err := s.GetUserModel(ctx)
  230. if err != nil {
  231. return nil, xerr.WithStack(err)
  232. }
  233. neckRingLogList := make([]*model.NeckRingModel, 0)
  234. var count int64 = 0
  235. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.NeckRing).TableName())).
  236. Select("a.*,COALESCE(b.pen_name, '') AS pen_name").
  237. Joins("LEFT JOIN cow as b ON a.cow_id = b.id").
  238. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  239. Where("a.neck_ring_number != ?", "")
  240. if req.IsBind > 0 {
  241. pref.Where("a.is_bind = ?", req.IsBind)
  242. }
  243. if req.ErrorKind > 0 {
  244. pref.Where("a.error_kind = ?", req.ErrorKind)
  245. }
  246. if req.EarNumber != "" {
  247. pref.Where("a.ear_number = ?", req.EarNumber)
  248. }
  249. if req.Number != "" {
  250. pref.Where("a.neck_ring_number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  251. }
  252. if req.PenId > 0 {
  253. pref.Where("b.pen_id = ?", req.PenId)
  254. }
  255. if req.ErrorKind > 0 {
  256. pref.Where("a.error_kind = ?", req.ErrorKind)
  257. }
  258. if err = pref.Order("a.status desc").
  259. Count(&count).
  260. Limit(int(pagination.PageSize)).
  261. Offset(int(pagination.PageOffset)).
  262. Find(&neckRingLogList).Error; err != nil {
  263. return nil, xerr.WithStack(err)
  264. }
  265. neckRingIsBindMap := s.NeckRingIsBindMap()
  266. neckRingStatusMap := s.NeckRingStatusMap()
  267. return &pasturePb.SearchNeckRingResponse{
  268. Code: http.StatusOK,
  269. Msg: "ok",
  270. Data: &pasturePb.SearchNeckRingData{
  271. List: model.NeckRingSlice(neckRingLogList).ToPB(neckRingIsBindMap, neckRingStatusMap),
  272. Total: int32(count),
  273. PageSize: pagination.PageSize,
  274. Page: pagination.Page,
  275. },
  276. }, nil
  277. }
  278. func (s *StoreEntry) OutboundApply(ctx context.Context, req *pasturePb.OutboundApplyItem) error {
  279. userModel, err := s.GetUserModel(ctx)
  280. if err != nil {
  281. return xerr.WithStack(err)
  282. }
  283. if len(req.Goods) <= 0 {
  284. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  285. MessageID: "goods.selectOutGoods",
  286. })
  287. return xerr.Custom(messageId)
  288. }
  289. var outbound *model.Outbound
  290. if req.Id > 0 {
  291. outbound, err = s.GetOutboundById(ctx, userModel.AppPasture.Id, int64(req.Id))
  292. if err != nil || outbound == nil || outbound.Id <= 0 {
  293. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  294. MessageID: "goods.outGoodsNotExist",
  295. })
  296. return xerr.Customf(messageId)
  297. }
  298. if userModel.SystemUser.Id != int64(outbound.ApplicantId) {
  299. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  300. MessageID: "goods.outGoodsNotAllowEdit",
  301. })
  302. return xerr.Custom(messageId)
  303. }
  304. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  305. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  306. MessageID: "goods.outGoodsNotEdit",
  307. })
  308. return xerr.Custom(messageId)
  309. }
  310. } else {
  311. // 创建出库申请
  312. outbound = model.NewOutbound(userModel.AppPasture.Id, req, userModel.SystemUser)
  313. }
  314. goodsItems := make([]*pasturePb.OutboundApplyGoodsItem, 0)
  315. switch req.OutType {
  316. case pasturePb.OutType_Drugs:
  317. for _, v := range req.Goods {
  318. if v.Quantity <= 0 {
  319. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  320. MessageID: "goods.goodsCount",
  321. })
  322. return xerr.Custom(messageId)
  323. }
  324. if v.GoodsId <= 0 {
  325. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  326. MessageID: "goods.selectOutGoods",
  327. })
  328. return xerr.Custom(messageId)
  329. }
  330. newDrugs := &model.Drugs{}
  331. if err = s.DB.Model(new(model.Drugs)).
  332. Where("id = ?", v.GoodsId).
  333. Where("inventory >= ?", v.Quantity).
  334. First(newDrugs).Error; err != nil {
  335. return xerr.WithStack(err)
  336. }
  337. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  338. GoodsId: v.GoodsId,
  339. Quantity: v.Quantity,
  340. Unit: v.Unit,
  341. GoodsName: newDrugs.Name,
  342. Specs: newDrugs.Specs,
  343. Producer: newDrugs.Producer,
  344. BatchNumber: newDrugs.BatchNumber,
  345. Price: float32(newDrugs.Price) / 100,
  346. })
  347. }
  348. case pasturePb.OutType_Medical_Equipment:
  349. for _, v := range req.Goods {
  350. if v.Quantity <= 0 {
  351. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  352. MessageID: "goods.goodsCount",
  353. })
  354. return xerr.Custom(messageId)
  355. }
  356. if v.GoodsId <= 0 {
  357. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  358. MessageID: "goods.selectOutGoods",
  359. })
  360. return xerr.Custom(messageId)
  361. }
  362. newMedicalEquipment := &model.MedicalEquipment{}
  363. if err = s.DB.Model(new(model.Drugs)).
  364. Where("id = ?", v.GoodsId).
  365. Where("inventory >= ?", v.Quantity).
  366. First(newMedicalEquipment).Error; err != nil {
  367. return xerr.WithStack(err)
  368. }
  369. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  370. GoodsId: v.GoodsId,
  371. Quantity: v.Quantity,
  372. Unit: v.Unit,
  373. GoodsName: newMedicalEquipment.Name,
  374. Specs: newMedicalEquipment.Specs,
  375. Producer: newMedicalEquipment.Producer,
  376. BatchNumber: newMedicalEquipment.BatchNumber,
  377. Price: float32(newMedicalEquipment.Price) / 100,
  378. })
  379. }
  380. default:
  381. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  382. MessageID: "goods.unknownOutGoodsType",
  383. })
  384. return xerr.Custom(messageId)
  385. }
  386. unitMap := s.UnitMap()
  387. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  388. if req.Id > 0 {
  389. if err = tx.Model(new(model.Outbound)).
  390. Where("id = ?", req.Id).
  391. Update("applicant_remarks", req.ApplicantRemarks).Error; err != nil {
  392. return xerr.WithStack(err)
  393. }
  394. if err = tx.Model(new(model.OutboundDetail)).
  395. Where("outbound_id = ?", req.Id).
  396. Update("is_delete = ?", pasturePb.IsShow_No).Error; err != nil {
  397. return xerr.WithStack(err)
  398. }
  399. } else {
  400. if err = tx.Create(outbound).Error; err != nil {
  401. return xerr.WithStack(err)
  402. }
  403. }
  404. outboundLog := model.NewOutboundDetailList(outbound.Id, goodsItems, unitMap)
  405. if err = tx.Create(outboundLog).Error; err != nil {
  406. return xerr.WithStack(err)
  407. }
  408. return nil
  409. }); err != nil {
  410. return xerr.WithStack(err)
  411. }
  412. return nil
  413. }
  414. func (s *StoreEntry) OutboundList(ctx context.Context, req *pasturePb.SearchOutboundApplyRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchOutboundApplyResponse, error) {
  415. userModel, err := s.GetUserModel(ctx)
  416. if err != nil {
  417. return nil, xerr.WithStack(err)
  418. }
  419. var count int64 = 0
  420. outboundList := make([]*model.Outbound, 0)
  421. pref := s.DB.Model(new(model.Outbound)).
  422. Where("pasture_id = ?", userModel.AppPasture.Id).
  423. Where("audit_status < ?", pasturePb.AuditStatus_Delete)
  424. if req.OutType > 0 {
  425. pref.Where("out_type = ?", req.OutType)
  426. }
  427. if req.StartDayTime > 0 && req.EndDayTime > 0 && req.EndDayTime >= req.StartDayTime {
  428. pref.Where("applicant_at BETWEEN ? AND ?", req.StartDayTime, req.EndDayTime+86400)
  429. }
  430. if req.Number != "" {
  431. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  432. }
  433. if req.AuditStatus > 0 {
  434. pref.Where("audit_status = ?", req.AuditStatus)
  435. }
  436. if req.ApplicantId > 0 {
  437. pref.Where("applicant_id = ?", req.ApplicantId)
  438. }
  439. if req.ExamineId > 0 {
  440. pref.Where("examine_id = ?", req.ExamineId)
  441. }
  442. if err = pref.Order("id desc").
  443. Count(&count).
  444. Limit(int(pagination.PageSize)).
  445. Offset(int(pagination.PageOffset)).
  446. Find(&outboundList).Error; err != nil {
  447. return nil, xerr.WithStack(err)
  448. }
  449. outTypeMap := s.OutTypeMap()
  450. auditStatusMap := s.AuditStatusMap()
  451. return &pasturePb.SearchOutboundApplyResponse{
  452. Code: http.StatusOK,
  453. Msg: "ok",
  454. Data: &pasturePb.SearchOutboundApplyData{
  455. List: model.OutboundSlice(outboundList).ToPB(outTypeMap, auditStatusMap),
  456. Total: int32(count),
  457. PageSize: pagination.PageSize,
  458. Page: pagination.Page,
  459. },
  460. }, nil
  461. }
  462. func (s *StoreEntry) OutboundAudit(ctx context.Context, req *pasturePb.OutboundApplyAuditRequest) error {
  463. userModel, err := s.GetUserModel(ctx)
  464. if err != nil {
  465. return xerr.WithStack(err)
  466. }
  467. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, int64(req.Id))
  468. if err != nil {
  469. return xerr.WithStack(err)
  470. }
  471. if outbound == nil {
  472. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  473. MessageID: "goods.outGoodsNotExist",
  474. })
  475. return xerr.Custom(messageId)
  476. }
  477. if req.AuditStatus != pasturePb.AuditStatus_Pass && req.AuditStatus != pasturePb.AuditStatus_Reject && req.AuditStatus != pasturePb.AuditStatus_Cancel {
  478. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  479. MessageID: "goods.outGoodsAuditStatusError",
  480. })
  481. return xerr.Custom(messageId)
  482. }
  483. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  484. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  485. MessageID: "goods.outGoodsError",
  486. })
  487. return xerr.Custom(messageId)
  488. }
  489. outboundDetails, err := s.GetOutboundDetailByOutboundId(ctx, outbound.Id)
  490. if err != nil {
  491. return xerr.WithStack(err)
  492. }
  493. if len(outboundDetails) <= 0 {
  494. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  495. MessageID: "goods.outGoodsNotExist",
  496. })
  497. return xerr.Custom(messageId)
  498. }
  499. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  500. if err = tx.Model(outbound).
  501. Where("id = ?", outbound.Id).
  502. Updates(map[string]interface{}{
  503. "audit_status": req.AuditStatus,
  504. "examine_id": userModel.SystemUser.Id,
  505. "examine_name": userModel.SystemUser.Name,
  506. "examine_remarks": req.ExamineRemarks,
  507. "examine_at": time.Now().Local().Unix(),
  508. }).Error; err != nil {
  509. return xerr.WithStack(err)
  510. }
  511. if req.AuditStatus != pasturePb.AuditStatus_Pass {
  512. return nil
  513. }
  514. tableName := ""
  515. switch outbound.OutType {
  516. case pasturePb.OutType_Drugs:
  517. tableName = new(model.Drugs).TableName()
  518. case pasturePb.OutType_Medical_Equipment:
  519. tableName = new(model.MedicalEquipment).TableName()
  520. default:
  521. return nil
  522. }
  523. for _, v := range outboundDetails {
  524. if err = tx.Table(tableName).
  525. Where("id = ?", v.GoodsId).
  526. Updates(map[string]interface{}{
  527. "inventory": gorm.Expr("inventory - ?", v.Quantity),
  528. }).Error; err != nil {
  529. return xerr.WithStack(err)
  530. }
  531. }
  532. return nil
  533. }); err != nil {
  534. return xerr.WithStack(err)
  535. }
  536. return nil
  537. }
  538. func (s *StoreEntry) OutboundDetail(ctx context.Context, id int64) (*pasturePb.OutboundDetailResponse, error) {
  539. userModel, err := s.GetUserModel(ctx)
  540. if err != nil {
  541. return nil, xerr.WithStack(err)
  542. }
  543. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, id)
  544. if err != nil {
  545. return nil, xerr.WithStack(err)
  546. }
  547. outboundLogs, err := s.GetOutboundDetailByOutboundId(ctx, id)
  548. if err != nil {
  549. return nil, xerr.WithStack(err)
  550. }
  551. outTypeMap := s.OutTypeMap()
  552. auditStatusMap := s.AuditStatusMap()
  553. applicantAtFormat, examineAtFormat := "", ""
  554. if outbound.ApplicantAt > 0 {
  555. applicantAtFormat = time.Unix(outbound.ApplicantAt, 0).Local().Format(model.LayoutTime)
  556. }
  557. if outbound.ExamineAt > 0 {
  558. examineAtFormat = time.Unix(outbound.ExamineAt, 0).Local().Format(model.LayoutTime)
  559. }
  560. unitMap := s.UnitMap()
  561. return &pasturePb.OutboundDetailResponse{
  562. Code: http.StatusOK,
  563. Msg: "ok",
  564. Data: &pasturePb.OutboundApplyDetail{
  565. Id: int32(outbound.Id),
  566. Number: outbound.Number,
  567. OutType: outbound.OutType,
  568. OutTypeName: outTypeMap[outbound.OutType],
  569. AuditStatus: outbound.AuditStatus,
  570. AuditStatusName: auditStatusMap[outbound.AuditStatus],
  571. ApplicantName: outbound.ApplicantName,
  572. ApplicantRemarks: outbound.ApplicantRemarks,
  573. ExamineName: outbound.ExamineName,
  574. ExamineRemarks: outbound.ExamineRemarks,
  575. ApplicantAtFormat: applicantAtFormat,
  576. ExamineAtFormat: examineAtFormat,
  577. GoodsItem: &pasturePb.OutboundApplyItem{
  578. OutType: outbound.OutType,
  579. Goods: model.OutboundDetailSlice(outboundLogs).ToPB(unitMap),
  580. ApplicantRemarks: outbound.ApplicantRemarks,
  581. },
  582. },
  583. }, nil
  584. }
  585. func (s *StoreEntry) OutboundDelete(ctx context.Context, id int64) error {
  586. userModel, err := s.GetUserModel(ctx)
  587. if err != nil {
  588. return xerr.WithStack(err)
  589. }
  590. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, id)
  591. if err != nil {
  592. return xerr.WithStack(err)
  593. }
  594. if outbound == nil {
  595. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  596. MessageID: "goods.outGoodsNotExist",
  597. })
  598. return xerr.Custom(messageId)
  599. }
  600. if !(outbound.AuditStatus == pasturePb.AuditStatus_Pending || outbound.AuditStatus == pasturePb.AuditStatus_Cancel) {
  601. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  602. MessageID: "goods.outGoodsNotDelete",
  603. })
  604. return xerr.Custom(messageId)
  605. }
  606. if userModel.SystemUser.Id != int64(outbound.ApplicantId) {
  607. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  608. MessageID: "goods.outGoodsNotAllowDelete",
  609. })
  610. return xerr.Custom(messageId)
  611. }
  612. outbound.Delete()
  613. if err = s.DB.Model(new(model.Outbound)).
  614. Select("audit_status").
  615. Where("id = ?", outbound.Id).
  616. Updates(outbound).Error; err != nil {
  617. return xerr.WithStack(err)
  618. }
  619. return nil
  620. }
  621. func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error) {
  622. userModel, err := s.GetUserModel(ctx)
  623. if err != nil {
  624. return nil, xerr.WithStack(err)
  625. }
  626. frozenSemenList := make([]*model.FrozenSemen, 0)
  627. var count int64 = 0
  628. pref := s.DB.Table(new(model.FrozenSemen).TableName()).
  629. Where("pasture_id = ?", userModel.AppPasture.Id)
  630. if req.BullId != "" {
  631. pref.Where("bull_id = ?", req.BullId)
  632. }
  633. if req.Producer != "" {
  634. pref.Where("producer = ?", req.Producer)
  635. }
  636. if err = pref.Order("id desc").
  637. Count(&count).Limit(int(pagination.PageSize)).
  638. Offset(int(pagination.PageOffset)).
  639. Find(&frozenSemenList).Error; err != nil {
  640. return nil, xerr.WithStack(err)
  641. }
  642. frozenSemenTypeMap := s.FrozenSemenTypeMap()
  643. unitMap := s.UnitMap()
  644. return &pasturePb.FrozenSemenResponse{
  645. Code: http.StatusOK,
  646. Msg: "ok",
  647. Data: &pasturePb.SearchFrozenSemenData{
  648. List: model.FrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
  649. Total: int32(count),
  650. PageSize: pagination.PageSize,
  651. Page: pagination.Page,
  652. },
  653. }, nil
  654. }
  655. func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
  656. userModel, err := s.GetUserModel(ctx)
  657. if err != nil {
  658. return xerr.WithStack(err)
  659. }
  660. req.CowKindName = s.CowKindMap()[req.CowKind]
  661. if req.Id > 0 {
  662. histData := &model.FrozenSemen{}
  663. if err = s.DB.Model(new(model.FrozenSemen)).
  664. Where("id = ?", req.Id).
  665. Where("pasture_id = ?", userModel.AppPasture.Id).
  666. First(histData).
  667. Error; err != nil {
  668. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  669. MessageID: "goods.dataNotExist",
  670. TemplateData: map[string]interface{}{
  671. "id": req.Id,
  672. },
  673. })
  674. return xerr.Customf(messageId)
  675. }
  676. histData.UpdateData(req)
  677. if err = s.DB.Model(new(model.FrozenSemen)).
  678. Where("id = ?", req.Id).
  679. Where("pasture_id = ?", userModel.AppPasture.Id).
  680. Updates(histData).Error; err != nil {
  681. return xerr.WithStack(err)
  682. }
  683. } else {
  684. newFrozenSemen := model.NewFrozenSemen(userModel.AppPasture.Id, req, userModel.SystemUser)
  685. if err = s.DB.Create(newFrozenSemen).Error; err != nil {
  686. return xerr.WithStack(err)
  687. }
  688. }
  689. return nil
  690. }