sql.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "kpt-pasture/model"
  7. "kpt-pasture/store/kptstore"
  8. operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
  9. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  10. "gitee.com/xuyiping_admin/pkg/xerr"
  11. "gorm.io/gorm"
  12. )
  13. // GetCurrentUserName 获取当前用户名
  14. func (s *StoreEntry) GetCurrentUserName(ctx context.Context) (string, error) {
  15. userNameInter := ctx.Value(CurrentUserName)
  16. if userNameInter == nil {
  17. return "", xerr.Customf("cannot userName")
  18. }
  19. if userName, ok := userNameInter.(string); ok {
  20. return userName, nil
  21. } else {
  22. return "", xerr.Customf("waring userName")
  23. }
  24. }
  25. func (s *StoreEntry) GetCurrentSystemUser(ctx context.Context) (*model.SystemUser, error) {
  26. // 解析token
  27. userName, err := s.GetCurrentUserName(ctx)
  28. if err != nil {
  29. return nil, xerr.WithStack(err)
  30. }
  31. // 根据用户token获取用户数据
  32. systemUser := &model.SystemUser{Name: userName}
  33. if err = s.DB.Model(new(model.SystemUser)).
  34. Where("name = ?", userName).
  35. Where("is_show = ? and is_delete = ?", pasturePb.IsShow_Ok, pasturePb.IsShow_Ok).
  36. First(systemUser).Error; err != nil {
  37. if errors.Is(err, gorm.ErrRecordNotFound) {
  38. return nil, xerr.Custom("当前登录用户数据不存在")
  39. }
  40. return nil, xerr.WithStack(err)
  41. }
  42. return systemUser, nil
  43. }
  44. func (s *StoreEntry) GetSystemUserById(ctx context.Context, pastureId, userId int64) (*model.SystemUser, error) {
  45. systemUser := &model.SystemUser{Id: userId}
  46. if err := s.DB.Model(new(model.SystemUser)).First(systemUser).Error; err != nil {
  47. if errors.Is(err, gorm.ErrRecordNotFound) {
  48. return nil, xerr.Customf("该系统用户数据不存在: %d", userId)
  49. }
  50. return nil, xerr.WithStack(err)
  51. }
  52. return systemUser, nil
  53. }
  54. func (s *StoreEntry) SystemDeptList(ctx context.Context, pastureId int64) ([]*model.SystemDept, error) {
  55. deptList := make([]*model.SystemDept, 0)
  56. if err := s.DB.Model(new(model.SystemDept)).
  57. Where("is_delete = ?", pasturePb.IsShow_Ok).
  58. Find(&deptList).Error; err != nil {
  59. return nil, xerr.WithStack(err)
  60. }
  61. return deptList, nil
  62. }
  63. func (s *StoreEntry) GetPenById(ctx context.Context, pastureId int64, penId int32) (*model.Pen, error) {
  64. penData := &model.Pen{Id: penId}
  65. if err := s.DB.Model(new(model.Pen)).First(penData).Error; err != nil {
  66. if errors.Is(err, gorm.ErrRecordNotFound) {
  67. return nil, xerr.Customf("该栏舍数据不存在: %d", penId)
  68. }
  69. return nil, xerr.WithStack(err)
  70. }
  71. return penData, nil
  72. }
  73. func (s *StoreEntry) GetPenList(ctx context.Context, pastureId int64) ([]*model.Pen, error) {
  74. penList := make([]*model.Pen, 0)
  75. if err := s.DB.Model(new(model.Pen)).
  76. Where("is_delete = ?", pasturePb.IsShow_Ok).
  77. Where("pasture_id = ?", pastureId).
  78. Find(&penList).Error; err != nil {
  79. return nil, xerr.WithStack(err)
  80. }
  81. return penList, nil
  82. }
  83. func (s *StoreEntry) PenMap(ctx context.Context, pastureId int64) map[int32]*model.Pen {
  84. penList, _ := s.GetPenList(ctx, pastureId)
  85. penMap := make(map[int32]*model.Pen)
  86. for _, v := range penList {
  87. penMap[v.Id] = v
  88. }
  89. return penMap
  90. }
  91. func (s *StoreEntry) GetCowInfoByCowId(ctx context.Context, pastureId, cowId int64) (*model.Cow, error) {
  92. cowData := &model.Cow{Id: cowId}
  93. if err := s.DB.Model(new(model.Cow)).
  94. Where("pasture_id = ?", pastureId).
  95. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  96. First(cowData).Error; err != nil {
  97. if errors.Is(err, gorm.ErrRecordNotFound) {
  98. return nil, xerr.Customf("该牛只数据不存在: %d", cowId)
  99. }
  100. return nil, xerr.WithStack(err)
  101. }
  102. return cowData, nil
  103. }
  104. func (s *StoreEntry) GetCowInfoByCowIds(ctx context.Context, pastureId int64, cowIds []int64) ([]*model.Cow, error) {
  105. cowList := make([]*model.Cow, 0)
  106. if err := s.DB.Model(&model.Cow{}).
  107. Where("pasture_id = ?", pastureId).
  108. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  109. Where("id IN ?", cowIds).
  110. Find(&cowList).Error; err != nil {
  111. return nil, xerr.WithStack(err)
  112. }
  113. return cowList, nil
  114. }
  115. func (s *StoreEntry) GetTransferReasonInfo(ctx context.Context, reasonId int64) (*model.ConfigTransferPenReason, error) {
  116. configTransferPenReasonData := &model.ConfigTransferPenReason{
  117. Id: reasonId,
  118. }
  119. if err := s.DB.Model(new(model.ConfigTransferPenReason)).First(configTransferPenReasonData).Error; err != nil {
  120. if errors.Is(err, gorm.ErrRecordNotFound) {
  121. return nil, xerr.Customf("该转群原因数据不存在: %d", reasonId)
  122. }
  123. return nil, xerr.WithStack(err)
  124. }
  125. return configTransferPenReasonData, nil
  126. }
  127. func (s *StoreEntry) GetCowWeightByLastSecond(ctx context.Context, pastureId, cowId, lastWeightAt int64) (*model.EventWeight, error) {
  128. cowWeightData := &model.EventWeight{}
  129. if err := s.DB.Model(new(model.EventWeight)).
  130. Where("pasture_id = ?", pastureId).
  131. Where("cow_id = ?", cowId).
  132. Where("weight_at < ?", lastWeightAt).
  133. First(cowWeightData).Error; err != nil {
  134. if errors.Is(err, gorm.ErrRecordNotFound) {
  135. return nil, xerr.Customf("该牛只体重数据不存在: %d", cowId)
  136. }
  137. return nil, xerr.WithStack(err)
  138. }
  139. return cowWeightData, nil
  140. }
  141. func (s *StoreEntry) GetDrugsById(ctx context.Context, pastureId, drugsId int64) (*model.Drugs, error) {
  142. drugs := &model.Drugs{Id: drugsId}
  143. if err := s.DB.Model(new(model.Drugs)).
  144. Where("pasture_id = ?", pastureId).
  145. First(drugs).Error; err != nil {
  146. if errors.Is(err, gorm.ErrRecordNotFound) {
  147. return nil, xerr.Customf("该药品数据不存在: %d", drugsId)
  148. }
  149. return nil, xerr.WithStack(err)
  150. }
  151. return drugs, nil
  152. }
  153. func (s *StoreEntry) DiseaseTypeList(ctx context.Context) ([]*model.ConfigDiseaseType, error) {
  154. diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
  155. if err := s.DB.Model(new(model.ConfigDiseaseType)).
  156. Where("is_show = ?", pasturePb.IsShow_Ok).
  157. Find(&diseaseTypeList).Error; err != nil {
  158. return nil, xerr.WithStack(err)
  159. }
  160. return diseaseTypeList, nil
  161. }
  162. func (s *StoreEntry) GetDiseaseById(ctx context.Context, pastureId int64, diseaseId int32) (*model.Disease, error) {
  163. newDisease := &model.Disease{}
  164. if err := s.DB.Model(new(model.Disease)).
  165. Where("pasture_id = ?", pastureId).
  166. Where("is_show = ?", pasturePb.IsShow_Ok).
  167. Where("id = ?", diseaseId).
  168. First(newDisease).Error; err != nil {
  169. return nil, xerr.WithStack(err)
  170. }
  171. return newDisease, nil
  172. }
  173. func (s *StoreEntry) DiseaseListByIds(ctx context.Context, pastureId int64, ids []int32) ([]*model.Disease, error) {
  174. diseaseList := make([]*model.Disease, 0)
  175. if err := s.DB.Model(new(model.Disease)).
  176. Where("pasture_id = ?", pastureId).
  177. Where("is_show = ?", pasturePb.IsShow_Ok).
  178. Where("id IN ?", ids).Find(&diseaseList).Error; err != nil {
  179. return nil, xerr.WithStack(err)
  180. }
  181. return diseaseList, nil
  182. }
  183. func (s *StoreEntry) GetPrescriptionById(ctx context.Context, pastureId int64, id int32) (*model.Prescription, error) {
  184. newPrescription := &model.Prescription{}
  185. if err := s.DB.Model(new(model.Prescription)).
  186. Where("id = ?", id).
  187. Where("pasture_id = ?", pastureId).
  188. Where("is_show = ?", pasturePb.IsShow_Ok).
  189. First(&newPrescription).Error; err != nil {
  190. return nil, xerr.WithStack(err)
  191. }
  192. return newPrescription, nil
  193. }
  194. func (s *StoreEntry) PrescriptionDrugsByPrescriptionId(ctx context.Context, pastureId int64, prescriptionId int32) ([]*model.PrescriptionDrugs, error) {
  195. newPrescriptionDrugsList := make([]*model.PrescriptionDrugs, 0)
  196. if err := s.DB.Model(new(model.PrescriptionDrugs)).
  197. Where("pasture_id = ?", pastureId).
  198. Where("prescription_id = ?", prescriptionId).
  199. Where("is_show = ?", pasturePb.IsShow_Ok).
  200. Find(&newPrescriptionDrugsList).Error; err != nil {
  201. return nil, xerr.WithStack(err)
  202. }
  203. return newPrescriptionDrugsList, nil
  204. }
  205. func (s *StoreEntry) GetImmunizationById(ctx context.Context, pastureId, planId int64) (*model.ImmunizationPlan, error) {
  206. immunizationPlan := &model.ImmunizationPlan{Id: planId}
  207. if err := s.DB.Model(new(model.ImmunizationPlan)).
  208. Where("pasture_id = ?", pastureId).
  209. Where("is_show = ?", pasturePb.IsShow_Ok).
  210. First(immunizationPlan).Error; err != nil {
  211. if errors.Is(err, gorm.ErrRecordNotFound) {
  212. return nil, xerr.Customf("该免疫计划不存在: %d", planId)
  213. }
  214. return nil, xerr.WithStack(err)
  215. }
  216. return immunizationPlan, nil
  217. }
  218. func (s *StoreEntry) GetWorkOrderSubByWorkOrderId(ctx context.Context, workOrderId int64) ([]*model.WorkOrderSub, error) {
  219. workOrderSubList := make([]*model.WorkOrderSub, 0)
  220. if err := s.DB.Model(new(model.WorkOrderSub)).
  221. Where("work_order_id = ?", workOrderId).
  222. Find(&workOrderSubList).Error; err != nil {
  223. return nil, xerr.WithStack(err)
  224. }
  225. return workOrderSubList, nil
  226. }
  227. func (s *StoreEntry) GetEventCowSameTimeByCowId(ctx context.Context, pastureId, cowId int64) (*model.EventCowSameTime, error) {
  228. eventCowSameTime := &model.EventCowSameTime{}
  229. if err := s.DB.Model(new(model.EventCowSameTime)).
  230. Where("cow_id = ?", cowId).
  231. Where("pasture_id = ?", pastureId).
  232. Where("status = ?", pasturePb.IsShow_No).
  233. First(eventCowSameTime).Error; err != nil {
  234. return nil, xerr.WithStack(err)
  235. }
  236. return eventCowSameTime, nil
  237. }
  238. func (s *StoreEntry) SearchSystemDeptListByIds(ctx context.Context, ids []int64) ([]*model.SystemDept, error) {
  239. systemDeptList := make([]*model.SystemDept, 0)
  240. if err := s.DB.Model(new(model.SystemDept)).
  241. Where("is_show = ?", operationPb.IsShow_OK).
  242. Find(&systemDeptList, ids).Error; err != nil {
  243. return nil, xerr.WithStack(err)
  244. }
  245. return systemDeptList, nil
  246. }
  247. func (s *StoreEntry) GetSystemDeptListById(ctx context.Context, id int64) (*model.SystemDept, error) {
  248. systemDept := &model.SystemDept{Id: id}
  249. if err := s.DB.Model(new(model.SystemDept)).First(systemDept).Error; err != nil {
  250. return nil, xerr.WithStack(err)
  251. }
  252. return systemDept, nil
  253. }
  254. func (s *StoreEntry) GetSystemBasicByName(ctx context.Context, name string) (*model.SystemBasic, error) {
  255. systemBasic := &model.SystemBasic{}
  256. if err := s.DB.Model(new(model.SystemBasic)).
  257. Where("name = ?", name).
  258. Where("is_show = ?", pasturePb.IsShow_Ok).
  259. First(systemBasic).Error; err != nil {
  260. return nil, xerr.WithStack(err)
  261. }
  262. return systemBasic, nil
  263. }
  264. // FindLastEventMatingByCowId 根据cowId获取最近一次配种表信息
  265. func (s *StoreEntry) FindLastEventMatingByCowId(ctx context.Context, pastureId, cowId int64) (*model.EventMating, bool, error) {
  266. newEventMating := &model.EventMating{}
  267. if err := s.DB.Model(new(model.EventMating)).
  268. Where("cow_id = ?", cowId).
  269. Where("pasture_id = ?", pastureId).
  270. Order("id desc").
  271. First(newEventMating).Error; err != nil {
  272. if errors.Is(err, gorm.ErrRecordNotFound) {
  273. return nil, false, nil
  274. }
  275. return nil, false, xerr.WithStack(err)
  276. }
  277. return newEventMating, true, nil
  278. }
  279. func (s *StoreEntry) FindEventEstrusByCowId(ctx context.Context, pastureId, cowId int64) (*model.EventEstrus, bool, error) {
  280. newEventEstrus := &model.EventEstrus{}
  281. if err := s.DB.Model(new(model.EventEstrus)).
  282. Where("cow_id = ?", cowId).
  283. Where("pasture_id = ?", pastureId).
  284. Where("is_show = ? ", pasturePb.IsShow_Ok).
  285. First(newEventEstrus).Error; err != nil {
  286. if errors.Is(err, gorm.ErrRecordNotFound) {
  287. return nil, false, nil
  288. } else {
  289. return nil, false, xerr.WithStack(err)
  290. }
  291. }
  292. return newEventEstrus, true, nil
  293. }
  294. func (s *StoreEntry) GetOutboundById(ctx context.Context, id int64) (*model.Outbound, error) {
  295. res := &model.Outbound{}
  296. if err := s.DB.Model(new(model.Outbound)).
  297. Where("id = ?", id).First(res).Error; err != nil {
  298. return nil, xerr.WithStack(err)
  299. }
  300. return res, nil
  301. }
  302. func (s *StoreEntry) GetOutboundDetailByOutboundId(ctx context.Context, id int64) ([]*model.OutboundDetail, error) {
  303. list := make([]*model.OutboundDetail, 0)
  304. if err := s.DB.Model(new(model.OutboundDetail)).
  305. Where("outbound_id = ?", id).
  306. Where("is_delete = ?", pasturePb.IsShow_Ok).
  307. Find(&list).Error; err != nil {
  308. return nil, xerr.WithStack(err)
  309. }
  310. return list, nil
  311. }
  312. func (s *StoreEntry) FindEventPregnantCheckIsExIstByCowId(
  313. ctx context.Context,
  314. cow *model.Cow,
  315. ) (*model.EventPregnantCheck, bool, error) {
  316. newEventPregnantCheck := &model.EventPregnantCheck{}
  317. if err := s.DB.Model(new(model.EventMating)).
  318. Where("cow_id = ?", cow.Id).
  319. Where("lact = ?", cow.Lact).
  320. Where("status = ?", pasturePb.IsShow_No).
  321. Order("id desc").
  322. First(newEventPregnantCheck).Error; err != nil {
  323. if errors.Is(err, gorm.ErrRecordNotFound) {
  324. return nil, false, nil
  325. } else {
  326. return nil, false, xerr.WithStack(err)
  327. }
  328. }
  329. return newEventPregnantCheck, true, nil
  330. }
  331. func (s *StoreEntry) DiseaseMaps(ctx context.Context) (map[int64]*model.Disease, error) {
  332. res := make(map[int64]*model.Disease)
  333. list := make([]*model.Disease, 0)
  334. if err := s.DB.Model(new(model.Disease)).Where("is_show = ?", pasturePb.IsShow_Ok).Find(&list).Error; err != nil {
  335. return nil, xerr.WithStack(err)
  336. }
  337. for _, v := range list {
  338. res[v.Id] = v
  339. }
  340. return res, nil
  341. }
  342. func (s *StoreEntry) NeckRingIsExist(ctx context.Context, pastureId int64, number string) (*model.NeckRing, bool) {
  343. neckRing := &model.NeckRing{}
  344. if err := s.DB.Model(new(model.NeckRing)).
  345. Where("neck_ring_number = ?", number).
  346. Where("pasture_id = ?", pastureId).
  347. First(neckRing).Error; err != nil {
  348. if errors.Is(err, gorm.ErrRecordNotFound) {
  349. return nil, false
  350. } else {
  351. return nil, false
  352. }
  353. }
  354. return neckRing, true
  355. }
  356. func (s *StoreEntry) GetEventLogList(
  357. cowId int64, lact int32,
  358. eventCategoryId pasturePb.EventCategory_Kind,
  359. paginationModel *pasturePb.PaginationModel,
  360. ) ([]*model.EventCowLog, error) {
  361. eventLogList := make([]*model.EventCowLog, 0)
  362. newEventCowLog := &model.EventCowLog{CowId: cowId}
  363. pref := s.DB.Table(newEventCowLog.TableName()).
  364. Where("cow_id = ?", cowId)
  365. if lact >= 0 {
  366. pref.Where("lact = ?", lact)
  367. }
  368. if eventCategoryId > 0 {
  369. pref.Where("event_category_id = ?", eventCategoryId)
  370. }
  371. if err := pref.Order("id desc").
  372. Offset(int(paginationModel.PageOffset)).
  373. Limit(int(paginationModel.PageSize)).
  374. Find(&eventLogList).Error; err != nil {
  375. return nil, xerr.WithStack(err)
  376. }
  377. return eventLogList, nil
  378. }
  379. func GetCowPenInfoByCowId(DB *kptstore.DB, cowId int64) *model.Pen {
  380. penData := &model.Pen{}
  381. if err := DB.Model(new(model.Cow)).
  382. Select("b.*").
  383. Table(fmt.Sprintf("%s as a", new(model.Cow).TableName())).
  384. Joins(fmt.Sprintf("left join %s as b on a.pen_id = b.id", new(model.Pen).TableName())).
  385. Where("a.id = ?", cowId).
  386. Where("a.pen_id > ?", 0).
  387. First(penData).Error; err != nil {
  388. return nil
  389. }
  390. return penData
  391. }
  392. func GetCowInfoByCowId(DB *kptstore.DB, cowId int64) *model.Cow {
  393. cowData := &model.Cow{}
  394. if err := DB.Model(new(model.Cow)).
  395. Where("id = ?", cowId).
  396. First(cowData).Error; err != nil {
  397. return nil
  398. }
  399. return cowData
  400. }
  401. func GetCowLastEvent(DB *kptstore.DB, cowId int64, eventCategoryId pasturePb.EventCategory_Kind) *model.EventCowLog {
  402. newEventCowLog := &model.EventCowLog{CowId: cowId}
  403. pref := DB.Table(newEventCowLog.TableName()).
  404. Where("cow_id = ?", cowId)
  405. if eventCategoryId > 0 {
  406. pref.Where("event_category_id = ?", eventCategoryId)
  407. }
  408. if err := pref.Order("id desc").First(newEventCowLog); err != nil {
  409. return nil
  410. }
  411. return newEventCowLog
  412. }