goods.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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(userModel)[req.CategoryId]
  51. req.UnitName = s.UnitMap(userModel)[req.Unit]
  52. req.UsageName = s.DrugUsageMaps(userModel)[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(userModel)
  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. "status": pasturePb.IsShow_Ok,
  182. "is_bind": pasturePb.NeckRingIsBind_Unbind,
  183. "error_kind": pasturePb.NeckRingNumberError_Invalid,
  184. "error_reason": "",
  185. "describe": "",
  186. }).Error; err != nil {
  187. return xerr.WithStack(err)
  188. }
  189. newNeckRingLog.OperationName = model.OperationNameUnbind
  190. newNeckRingLog.Remarks = model.DefaultUnBind
  191. // 编辑
  192. case pasturePb.NeckRingOperationStatus_Edit:
  193. if cowInfo.NeckRingNumber != "" && item.Number != cowInfo.NeckRingNumber {
  194. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  195. MessageID: "cow.neckRingNumberBind",
  196. TemplateData: map[string]interface{}{
  197. "neckRingNumber": cowInfo.NeckRingNumber,
  198. "earNumber": cowInfo.EarNumber,
  199. },
  200. })
  201. return xerr.Customf(messageId)
  202. }
  203. if err = tx.Model(new(model.NeckRing)).
  204. Where("neck_ring_number = ?", item.Number).
  205. Updates(map[string]interface{}{
  206. "cow_id": cowInfo.Id,
  207. "ear_number": cowInfo.EarNumber,
  208. "wear_at": time.Now().Local().Unix(),
  209. "is_bind": pasturePb.NeckRingIsBind_Bind,
  210. }).Error; err != nil {
  211. return xerr.WithStack(err)
  212. }
  213. number = item.Number
  214. newNeckRingLog.OperationName = model.OperationNameUpdate
  215. }
  216. if err = tx.Create(newNeckRingLog).Error; err != nil {
  217. return xerr.WithStack(err)
  218. }
  219. if err = tx.Model(new(model.Cow)).
  220. Where("id = ?", cowInfo.Id).
  221. Update("neck_ring_number", number).
  222. Error; err != nil {
  223. return xerr.WithStack(err)
  224. }
  225. }
  226. return nil
  227. }); err != nil {
  228. return xerr.WithStack(err)
  229. }
  230. return nil
  231. }
  232. func (s *StoreEntry) NeckRingList(ctx context.Context, req *pasturePb.SearchNeckRingRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchNeckRingResponse, error) {
  233. userModel, err := s.GetUserModel(ctx)
  234. if err != nil {
  235. return nil, xerr.WithStack(err)
  236. }
  237. neckRingLogList := make([]*model.NeckRingModel, 0)
  238. var count int64 = 0
  239. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.NeckRing).TableName())).
  240. Select("a.*,COALESCE(b.pen_name, '') AS pen_name").
  241. Joins("LEFT JOIN cow as b ON a.cow_id = b.id").
  242. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  243. Where("a.neck_ring_number != ?", "")
  244. if req.IsBind > 0 {
  245. pref.Where("a.is_bind = ?", req.IsBind)
  246. }
  247. if req.ErrorKind > 0 {
  248. pref.Where("a.error_kind = ?", req.ErrorKind)
  249. }
  250. if req.EarNumber != "" {
  251. pref.Where("a.ear_number = ?", req.EarNumber)
  252. }
  253. if req.Number != "" {
  254. pref.Where("a.neck_ring_number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  255. }
  256. if req.PenId > 0 {
  257. pref.Where("b.pen_id = ?", req.PenId)
  258. }
  259. if req.ErrorKind > 0 {
  260. pref.Where("a.error_kind = ?", req.ErrorKind)
  261. }
  262. if err = pref.Order("a.status desc").
  263. Count(&count).
  264. Limit(int(pagination.PageSize)).
  265. Offset(int(pagination.PageOffset)).
  266. Find(&neckRingLogList).Error; err != nil {
  267. return nil, xerr.WithStack(err)
  268. }
  269. neckRingIsBindMap := s.NeckRingIsBindMap(userModel)
  270. neckRingStatusMap := s.NeckRingStatusMap(userModel)
  271. return &pasturePb.SearchNeckRingResponse{
  272. Code: http.StatusOK,
  273. Msg: "ok",
  274. Data: &pasturePb.SearchNeckRingData{
  275. List: model.NeckRingSlice(neckRingLogList).ToPB(neckRingIsBindMap, neckRingStatusMap),
  276. Total: int32(count),
  277. PageSize: pagination.PageSize,
  278. Page: pagination.Page,
  279. },
  280. }, nil
  281. }
  282. func (s *StoreEntry) OutboundApply(ctx context.Context, req *pasturePb.OutboundApplyItem) error {
  283. userModel, err := s.GetUserModel(ctx)
  284. if err != nil {
  285. return xerr.WithStack(err)
  286. }
  287. if len(req.Goods) <= 0 {
  288. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  289. MessageID: "goods.selectOutGoods",
  290. })
  291. return xerr.Custom(messageId)
  292. }
  293. var outbound *model.Outbound
  294. if req.Id > 0 {
  295. outbound, err = s.GetOutboundById(ctx, userModel.AppPasture.Id, int64(req.Id))
  296. if err != nil || outbound == nil || outbound.Id <= 0 {
  297. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  298. MessageID: "goods.outGoodsNotExist",
  299. })
  300. return xerr.Customf(messageId)
  301. }
  302. if userModel.SystemUser.Id != int64(outbound.ApplicantId) {
  303. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  304. MessageID: "goods.outGoodsNotAllowEdit",
  305. })
  306. return xerr.Custom(messageId)
  307. }
  308. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  309. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  310. MessageID: "goods.outGoodsNotEdit",
  311. })
  312. return xerr.Custom(messageId)
  313. }
  314. } else {
  315. // 创建出库申请
  316. outbound = model.NewOutbound(userModel.AppPasture.Id, req, userModel.SystemUser)
  317. }
  318. goodsItems := make([]*pasturePb.OutboundApplyGoodsItem, 0)
  319. switch req.OutType {
  320. case pasturePb.OutType_Drugs:
  321. for _, v := range req.Goods {
  322. if v.Quantity <= 0 {
  323. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  324. MessageID: "goods.goodsCount",
  325. })
  326. return xerr.Custom(messageId)
  327. }
  328. if v.GoodsId <= 0 {
  329. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  330. MessageID: "goods.selectOutGoods",
  331. })
  332. return xerr.Custom(messageId)
  333. }
  334. newDrugs := &model.Drugs{}
  335. if err = s.DB.Model(new(model.Drugs)).
  336. Where("id = ?", v.GoodsId).
  337. Where("inventory >= ?", v.Quantity).
  338. First(newDrugs).Error; err != nil {
  339. return xerr.WithStack(err)
  340. }
  341. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  342. GoodsId: v.GoodsId,
  343. Quantity: v.Quantity,
  344. Unit: v.Unit,
  345. GoodsName: newDrugs.Name,
  346. Specs: newDrugs.Specs,
  347. Producer: newDrugs.Producer,
  348. BatchNumber: newDrugs.BatchNumber,
  349. Price: float32(newDrugs.Price) / 100,
  350. })
  351. }
  352. case pasturePb.OutType_Medical_Equipment:
  353. for _, v := range req.Goods {
  354. if v.Quantity <= 0 {
  355. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  356. MessageID: "goods.goodsCount",
  357. })
  358. return xerr.Custom(messageId)
  359. }
  360. if v.GoodsId <= 0 {
  361. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  362. MessageID: "goods.selectOutGoods",
  363. })
  364. return xerr.Custom(messageId)
  365. }
  366. newMedicalEquipment := &model.MedicalEquipment{}
  367. if err = s.DB.Model(new(model.Drugs)).
  368. Where("id = ?", v.GoodsId).
  369. Where("inventory >= ?", v.Quantity).
  370. First(newMedicalEquipment).Error; err != nil {
  371. return xerr.WithStack(err)
  372. }
  373. goodsItems = append(goodsItems, &pasturePb.OutboundApplyGoodsItem{
  374. GoodsId: v.GoodsId,
  375. Quantity: v.Quantity,
  376. Unit: v.Unit,
  377. GoodsName: newMedicalEquipment.Name,
  378. Specs: newMedicalEquipment.Specs,
  379. Producer: newMedicalEquipment.Producer,
  380. BatchNumber: newMedicalEquipment.BatchNumber,
  381. Price: float32(newMedicalEquipment.Price) / 100,
  382. })
  383. }
  384. default:
  385. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  386. MessageID: "goods.unknownOutGoodsType",
  387. })
  388. return xerr.Custom(messageId)
  389. }
  390. unitMap := s.UnitMap(userModel)
  391. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  392. if req.Id > 0 {
  393. if err = tx.Model(new(model.Outbound)).
  394. Where("id = ?", req.Id).
  395. Update("applicant_remarks", req.ApplicantRemarks).Error; err != nil {
  396. return xerr.WithStack(err)
  397. }
  398. if err = tx.Model(new(model.OutboundDetail)).
  399. Where("outbound_id = ?", req.Id).
  400. Update("is_delete = ?", pasturePb.IsShow_No).Error; err != nil {
  401. return xerr.WithStack(err)
  402. }
  403. } else {
  404. if err = tx.Create(outbound).Error; err != nil {
  405. return xerr.WithStack(err)
  406. }
  407. }
  408. outboundLog := model.NewOutboundDetailList(outbound.Id, goodsItems, unitMap)
  409. if err = tx.Create(outboundLog).Error; err != nil {
  410. return xerr.WithStack(err)
  411. }
  412. return nil
  413. }); err != nil {
  414. return xerr.WithStack(err)
  415. }
  416. return nil
  417. }
  418. func (s *StoreEntry) OutboundList(ctx context.Context, req *pasturePb.SearchOutboundApplyRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchOutboundApplyResponse, error) {
  419. userModel, err := s.GetUserModel(ctx)
  420. if err != nil {
  421. return nil, xerr.WithStack(err)
  422. }
  423. var count int64 = 0
  424. outboundList := make([]*model.Outbound, 0)
  425. pref := s.DB.Model(new(model.Outbound)).
  426. Where("pasture_id = ?", userModel.AppPasture.Id).
  427. Where("audit_status < ?", pasturePb.AuditStatus_Delete)
  428. if req.OutType > 0 {
  429. pref.Where("out_type = ?", req.OutType)
  430. }
  431. if req.StartDayTime > 0 && req.EndDayTime > 0 && req.EndDayTime >= req.StartDayTime {
  432. pref.Where("applicant_at BETWEEN ? AND ?", req.StartDayTime, req.EndDayTime+86400)
  433. }
  434. if req.Number != "" {
  435. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  436. }
  437. if req.AuditStatus > 0 {
  438. pref.Where("audit_status = ?", req.AuditStatus)
  439. }
  440. if req.ApplicantId > 0 {
  441. pref.Where("applicant_id = ?", req.ApplicantId)
  442. }
  443. if req.ExamineId > 0 {
  444. pref.Where("examine_id = ?", req.ExamineId)
  445. }
  446. if err = pref.Order("id desc").
  447. Count(&count).
  448. Limit(int(pagination.PageSize)).
  449. Offset(int(pagination.PageOffset)).
  450. Find(&outboundList).Error; err != nil {
  451. return nil, xerr.WithStack(err)
  452. }
  453. outTypeMap := s.OutTypeMap(userModel)
  454. auditStatusMap := s.AuditStatusMap(userModel)
  455. return &pasturePb.SearchOutboundApplyResponse{
  456. Code: http.StatusOK,
  457. Msg: "ok",
  458. Data: &pasturePb.SearchOutboundApplyData{
  459. List: model.OutboundSlice(outboundList).ToPB(outTypeMap, auditStatusMap),
  460. Total: int32(count),
  461. PageSize: pagination.PageSize,
  462. Page: pagination.Page,
  463. },
  464. }, nil
  465. }
  466. func (s *StoreEntry) OutboundAudit(ctx context.Context, req *pasturePb.OutboundApplyAuditRequest) error {
  467. userModel, err := s.GetUserModel(ctx)
  468. if err != nil {
  469. return xerr.WithStack(err)
  470. }
  471. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, int64(req.Id))
  472. if err != nil {
  473. return xerr.WithStack(err)
  474. }
  475. if outbound == nil {
  476. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  477. MessageID: "goods.outGoodsNotExist",
  478. })
  479. return xerr.Custom(messageId)
  480. }
  481. if req.AuditStatus != pasturePb.AuditStatus_Pass && req.AuditStatus != pasturePb.AuditStatus_Reject && req.AuditStatus != pasturePb.AuditStatus_Cancel {
  482. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  483. MessageID: "goods.outGoodsAuditStatusError",
  484. })
  485. return xerr.Custom(messageId)
  486. }
  487. if outbound.AuditStatus != pasturePb.AuditStatus_Pending {
  488. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  489. MessageID: "goods.outGoodsError",
  490. })
  491. return xerr.Custom(messageId)
  492. }
  493. outboundDetails, err := s.GetOutboundDetailByOutboundId(ctx, outbound.Id)
  494. if err != nil {
  495. return xerr.WithStack(err)
  496. }
  497. if len(outboundDetails) <= 0 {
  498. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  499. MessageID: "goods.outGoodsNotExist",
  500. })
  501. return xerr.Custom(messageId)
  502. }
  503. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  504. if err = tx.Model(outbound).
  505. Where("id = ?", outbound.Id).
  506. Updates(map[string]interface{}{
  507. "audit_status": req.AuditStatus,
  508. "examine_id": userModel.SystemUser.Id,
  509. "examine_name": userModel.SystemUser.Name,
  510. "examine_remarks": req.ExamineRemarks,
  511. "examine_at": time.Now().Local().Unix(),
  512. }).Error; err != nil {
  513. return xerr.WithStack(err)
  514. }
  515. if req.AuditStatus != pasturePb.AuditStatus_Pass {
  516. return nil
  517. }
  518. tableName := ""
  519. switch outbound.OutType {
  520. case pasturePb.OutType_Drugs:
  521. tableName = new(model.Drugs).TableName()
  522. case pasturePb.OutType_Medical_Equipment:
  523. tableName = new(model.MedicalEquipment).TableName()
  524. default:
  525. return nil
  526. }
  527. for _, v := range outboundDetails {
  528. if err = tx.Table(tableName).
  529. Where("id = ?", v.GoodsId).
  530. Updates(map[string]interface{}{
  531. "inventory": gorm.Expr("inventory - ?", v.Quantity),
  532. }).Error; err != nil {
  533. return xerr.WithStack(err)
  534. }
  535. }
  536. return nil
  537. }); err != nil {
  538. return xerr.WithStack(err)
  539. }
  540. return nil
  541. }
  542. func (s *StoreEntry) OutboundDetail(ctx context.Context, id int64) (*pasturePb.OutboundDetailResponse, error) {
  543. userModel, err := s.GetUserModel(ctx)
  544. if err != nil {
  545. return nil, xerr.WithStack(err)
  546. }
  547. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, id)
  548. if err != nil {
  549. return nil, xerr.WithStack(err)
  550. }
  551. outboundLogs, err := s.GetOutboundDetailByOutboundId(ctx, id)
  552. if err != nil {
  553. return nil, xerr.WithStack(err)
  554. }
  555. outTypeMap := s.OutTypeMap(userModel)
  556. auditStatusMap := s.AuditStatusMap(userModel)
  557. applicantAtFormat, examineAtFormat := "", ""
  558. if outbound.ApplicantAt > 0 {
  559. applicantAtFormat = time.Unix(outbound.ApplicantAt, 0).Local().Format(model.LayoutTime)
  560. }
  561. if outbound.ExamineAt > 0 {
  562. examineAtFormat = time.Unix(outbound.ExamineAt, 0).Local().Format(model.LayoutTime)
  563. }
  564. unitMap := s.UnitMap(userModel)
  565. return &pasturePb.OutboundDetailResponse{
  566. Code: http.StatusOK,
  567. Msg: "ok",
  568. Data: &pasturePb.OutboundApplyDetail{
  569. Id: int32(outbound.Id),
  570. Number: outbound.Number,
  571. OutType: outbound.OutType,
  572. OutTypeName: outTypeMap[outbound.OutType],
  573. AuditStatus: outbound.AuditStatus,
  574. AuditStatusName: auditStatusMap[outbound.AuditStatus],
  575. ApplicantName: outbound.ApplicantName,
  576. ApplicantRemarks: outbound.ApplicantRemarks,
  577. ExamineName: outbound.ExamineName,
  578. ExamineRemarks: outbound.ExamineRemarks,
  579. ApplicantAtFormat: applicantAtFormat,
  580. ExamineAtFormat: examineAtFormat,
  581. GoodsItem: &pasturePb.OutboundApplyItem{
  582. OutType: outbound.OutType,
  583. Goods: model.OutboundDetailSlice(outboundLogs).ToPB(unitMap),
  584. ApplicantRemarks: outbound.ApplicantRemarks,
  585. },
  586. },
  587. }, nil
  588. }
  589. func (s *StoreEntry) OutboundDelete(ctx context.Context, id int64) error {
  590. userModel, err := s.GetUserModel(ctx)
  591. if err != nil {
  592. return xerr.WithStack(err)
  593. }
  594. outbound, err := s.GetOutboundById(ctx, userModel.AppPasture.Id, id)
  595. if err != nil {
  596. return xerr.WithStack(err)
  597. }
  598. if outbound == nil {
  599. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  600. MessageID: "goods.outGoodsNotExist",
  601. })
  602. return xerr.Custom(messageId)
  603. }
  604. if !(outbound.AuditStatus == pasturePb.AuditStatus_Pending || outbound.AuditStatus == pasturePb.AuditStatus_Cancel) {
  605. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  606. MessageID: "goods.outGoodsNotDelete",
  607. })
  608. return xerr.Custom(messageId)
  609. }
  610. if userModel.SystemUser.Id != int64(outbound.ApplicantId) {
  611. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  612. MessageID: "goods.outGoodsNotAllowDelete",
  613. })
  614. return xerr.Custom(messageId)
  615. }
  616. outbound.Delete()
  617. if err = s.DB.Model(new(model.Outbound)).
  618. Select("audit_status").
  619. Where("id = ?", outbound.Id).
  620. Updates(outbound).Error; err != nil {
  621. return xerr.WithStack(err)
  622. }
  623. return nil
  624. }
  625. func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error) {
  626. userModel, err := s.GetUserModel(ctx)
  627. if err != nil {
  628. return nil, xerr.WithStack(err)
  629. }
  630. frozenSemenList := make([]*model.FrozenSemen, 0)
  631. var count int64 = 0
  632. pref := s.DB.Table(new(model.FrozenSemen).TableName()).
  633. Where("pasture_id = ?", userModel.AppPasture.Id)
  634. if req.BullId != "" {
  635. pref.Where("bull_id = ?", req.BullId)
  636. }
  637. if req.Producer != "" {
  638. pref.Where("producer = ?", req.Producer)
  639. }
  640. if err = pref.Order("id desc").
  641. Count(&count).Limit(int(pagination.PageSize)).
  642. Offset(int(pagination.PageOffset)).
  643. Find(&frozenSemenList).Error; err != nil {
  644. return nil, xerr.WithStack(err)
  645. }
  646. frozenSemenTypeMap := s.FrozenSemenTypeMap(userModel)
  647. unitMap := s.UnitMap(userModel)
  648. return &pasturePb.FrozenSemenResponse{
  649. Code: http.StatusOK,
  650. Msg: "ok",
  651. Data: &pasturePb.SearchFrozenSemenData{
  652. List: model.FrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
  653. Total: int32(count),
  654. PageSize: pagination.PageSize,
  655. Page: pagination.Page,
  656. },
  657. }, nil
  658. }
  659. func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
  660. userModel, err := s.GetUserModel(ctx)
  661. if err != nil {
  662. return xerr.WithStack(err)
  663. }
  664. req.CowKindName = s.CowKindMap(userModel)[req.CowKind]
  665. if req.Id > 0 {
  666. histData := &model.FrozenSemen{}
  667. if err = s.DB.Model(new(model.FrozenSemen)).
  668. Where("id = ?", req.Id).
  669. Where("pasture_id = ?", userModel.AppPasture.Id).
  670. First(histData).
  671. Error; err != nil {
  672. messageId, _ := userModel.LanguageContent.Localize(&i18n.LocalizeConfig{
  673. MessageID: "goods.dataNotExist",
  674. TemplateData: map[string]interface{}{
  675. "id": req.Id,
  676. },
  677. })
  678. return xerr.Customf(messageId)
  679. }
  680. histData.UpdateData(req)
  681. if err = s.DB.Model(new(model.FrozenSemen)).
  682. Where("id = ?", req.Id).
  683. Where("pasture_id = ?", userModel.AppPasture.Id).
  684. Updates(histData).Error; err != nil {
  685. return xerr.WithStack(err)
  686. }
  687. } else {
  688. newFrozenSemen := model.NewFrozenSemen(userModel.AppPasture.Id, req, userModel.SystemUser)
  689. if err = s.DB.Create(newFrozenSemen).Error; err != nil {
  690. return xerr.WithStack(err)
  691. }
  692. }
  693. return nil
  694. }