sql.go 17 KB

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