sql.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "kpt-pasture/model"
  6. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  7. "go.uber.org/zap"
  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)).Where("name = ?", userName).
  34. Where("is_show = ? and is_delete = ?", pasturePb.IsShow_Ok, pasturePb.IsShow_Ok).
  35. First(systemUser).Error; err != nil {
  36. if errors.Is(err, gorm.ErrRecordNotFound) {
  37. return nil, xerr.Custom("当前登录用户数据不存在")
  38. }
  39. return nil, xerr.WithStack(err)
  40. }
  41. return systemUser, nil
  42. }
  43. func (s *StoreEntry) GetSystemUserById(ctx context.Context, userId int64) (*model.SystemUser, error) {
  44. systemUser := &model.SystemUser{Id: userId}
  45. if err := s.DB.Model(new(model.SystemUser)).First(systemUser).Error; err != nil {
  46. if errors.Is(err, gorm.ErrRecordNotFound) {
  47. return nil, xerr.Customf("该系统用户数据不存在: %d", userId)
  48. }
  49. return nil, xerr.WithStack(err)
  50. }
  51. return systemUser, nil
  52. }
  53. func (s *StoreEntry) SystemDeptList(ctx context.Context) ([]*model.SystemDept, error) {
  54. deptList := make([]*model.SystemDept, 0)
  55. if err := s.DB.Model(new(model.SystemDept)).
  56. Where("is_delete = ?", pasturePb.IsShow_Ok).
  57. Find(&deptList).Error; err != nil {
  58. return nil, xerr.WithStack(err)
  59. }
  60. return deptList, nil
  61. }
  62. func (s *StoreEntry) GetPenById(ctx context.Context, penId int32) (*model.Pen, error) {
  63. penData := &model.Pen{Id: penId}
  64. if err := s.DB.Model(new(model.Pen)).First(penData).Error; err != nil {
  65. if errors.Is(err, gorm.ErrRecordNotFound) {
  66. return nil, xerr.Customf("该栏舍数据不存在: %d", penId)
  67. }
  68. return nil, xerr.WithStack(err)
  69. }
  70. return penData, nil
  71. }
  72. func (s *StoreEntry) GetPenList(ctx context.Context) ([]*model.Pen, error) {
  73. penList := make([]*model.Pen, 0)
  74. if err := s.DB.Model(new(model.Pen)).
  75. Where("is_delete = ?", pasturePb.IsShow_Ok).
  76. Find(&penList).Error; err != nil {
  77. return nil, xerr.WithStack(err)
  78. }
  79. return penList, nil
  80. }
  81. func (s *StoreEntry) PenMap(ctx context.Context) map[int32]*model.Pen {
  82. penList, _ := s.GetPenList(ctx)
  83. penMap := make(map[int32]*model.Pen)
  84. for _, v := range penList {
  85. penMap[v.Id] = v
  86. }
  87. return penMap
  88. }
  89. func (s *StoreEntry) GetCowList(ctx context.Context, cowIds []int64) ([]*model.Cow, error) {
  90. cowList := make([]*model.Cow, 0)
  91. if err := s.DB.Model(new(model.Cow)).
  92. Where("id IN ?", cowIds).
  93. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  94. Find(&cowList).Error; err != nil {
  95. return nil, xerr.WithStack(err)
  96. }
  97. return cowList, nil
  98. }
  99. func (s *StoreEntry) GetCowInfoByCowId(ctx context.Context, cowId int64) (*model.Cow, error) {
  100. cowData := &model.Cow{Id: cowId}
  101. if err := s.DB.Model(new(model.Cow)).Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).First(cowData).Error; err != nil {
  102. if errors.Is(err, gorm.ErrRecordNotFound) {
  103. return nil, xerr.Customf("该牛只数据不存在: %d", cowId)
  104. }
  105. return nil, xerr.WithStack(err)
  106. }
  107. return cowData, nil
  108. }
  109. func (s *StoreEntry) GetCowInfoByCowIds(ctx context.Context, cowIds []int64) ([]*model.Cow, error) {
  110. cowList := make([]*model.Cow, 0)
  111. if err := s.DB.Model(&model.Cow{}).
  112. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  113. Where("id IN ?", cowIds).
  114. Find(&cowList).Error; err != nil {
  115. return nil, xerr.WithStack(err)
  116. }
  117. return cowList, nil
  118. }
  119. func (s *StoreEntry) GetTransferReasonInfo(ctx context.Context, reasonId int64) (*model.ConfigTransferPenReason, error) {
  120. configTransferPenReasonData := &model.ConfigTransferPenReason{
  121. Id: reasonId,
  122. }
  123. if err := s.DB.Model(new(model.ConfigTransferPenReason)).First(configTransferPenReasonData).Error; err != nil {
  124. if errors.Is(err, gorm.ErrRecordNotFound) {
  125. return nil, xerr.Customf("该转群原因数据不存在: %d", reasonId)
  126. }
  127. return nil, xerr.WithStack(err)
  128. }
  129. return configTransferPenReasonData, nil
  130. }
  131. func (s *StoreEntry) GetCowWeightByLastSecond(ctx context.Context, cowId, lastWeightAt int64) (*model.EventWeight, error) {
  132. cowWeightData := &model.EventWeight{}
  133. if err := s.DB.Model(new(model.EventWeight)).Where("cow_id = ?", cowId).Where("weight_at < ?", lastWeightAt).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, id int64) (*model.Drugs, error) {
  142. drugs := &model.Drugs{Id: id}
  143. if err := s.DB.Model(new(model.Drugs)).First(drugs).Error; err != nil {
  144. if errors.Is(err, gorm.ErrRecordNotFound) {
  145. return nil, xerr.Customf("该药品数据不存在: %d", id)
  146. }
  147. return nil, xerr.WithStack(err)
  148. }
  149. return drugs, nil
  150. }
  151. func (s *StoreEntry) DiseaseTypeList(ctx context.Context) ([]*model.ConfigDiseaseType, error) {
  152. diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
  153. if err := s.DB.Model(new(model.ConfigDiseaseType)).Where("is_show = ?", pasturePb.IsShow_Ok).Find(&diseaseTypeList).Error; err != nil {
  154. return nil, xerr.WithStack(err)
  155. }
  156. return diseaseTypeList, nil
  157. }
  158. func (s *StoreEntry) GetDiseaseById(ctx context.Context, id int32) (*model.Disease, error) {
  159. newDisease := &model.Disease{}
  160. if err := s.DB.Model(new(model.Disease)).Where("id = ?", id).First(newDisease).Error; err != nil {
  161. return nil, xerr.WithStack(err)
  162. }
  163. return newDisease, nil
  164. }
  165. func (s *StoreEntry) DiseaseListByIds(ctx context.Context, ids []int32) ([]*model.Disease, error) {
  166. diseaseList := make([]*model.Disease, 0)
  167. if err := s.DB.Model(new(model.Disease)).Where("id IN ?", ids).Find(&diseaseList).Error; err != nil {
  168. return nil, xerr.WithStack(err)
  169. }
  170. return diseaseList, nil
  171. }
  172. func (s *StoreEntry) GetPrescriptionById(ctx context.Context, id int32) (*model.Prescription, error) {
  173. newPrescription := &model.Prescription{}
  174. if err := s.DB.Model(new(model.Prescription)).Where("id = ?", id).
  175. Where("is_show = ?", pasturePb.IsShow_Ok).
  176. First(&newPrescription).Error; err != nil {
  177. return nil, xerr.WithStack(err)
  178. }
  179. return newPrescription, nil
  180. }
  181. func (s *StoreEntry) PrescriptionDrugsByPrescriptionId(ctx context.Context, prescriptionId int32) ([]*model.PrescriptionDrugs, error) {
  182. newPrescriptionDrugsList := make([]*model.PrescriptionDrugs, 0)
  183. if err := s.DB.Model(new(model.PrescriptionDrugs)).Where("prescription_id = ?", prescriptionId).
  184. Where("is_show = ?", pasturePb.IsShow_Ok).
  185. Find(&newPrescriptionDrugsList).Error; err != nil {
  186. return nil, xerr.WithStack(err)
  187. }
  188. return newPrescriptionDrugsList, nil
  189. }
  190. func (s *StoreEntry) GetImmunizationById(ctx context.Context, id int64) (*model.ImmunizationPlan, error) {
  191. immunizationPlan := &model.ImmunizationPlan{Id: id}
  192. if err := s.DB.Model(new(model.ImmunizationPlan)).First(immunizationPlan).Error; err != nil {
  193. if errors.Is(err, gorm.ErrRecordNotFound) {
  194. return nil, xerr.Customf("该免疫计划不存在: %d", id)
  195. }
  196. return nil, xerr.WithStack(err)
  197. }
  198. return immunizationPlan, nil
  199. }
  200. func (s *StoreEntry) GetWorkOrderSubByWorkOrderId(ctx context.Context, workOrderId int64) ([]*model.WorkOrderSub, error) {
  201. workOrderSubList := make([]*model.WorkOrderSub, 0)
  202. if err := s.DB.Model(new(model.WorkOrderSub)).Where("work_order_id = ?", workOrderId).Find(&workOrderSubList).Error; err != nil {
  203. return nil, xerr.WithStack(err)
  204. }
  205. return workOrderSubList, nil
  206. }
  207. func (s *StoreEntry) GetSameTimeById(ctx context.Context, id int64) (*model.SameTime, error) {
  208. sameTime := &model.SameTime{Id: id}
  209. if err := s.DB.Model(new(model.SameTime)).Where("is_show = ?", pasturePb.IsShow_Ok).First(sameTime).Error; err != nil {
  210. return nil, xerr.WithStack(err)
  211. }
  212. return sameTime, nil
  213. }
  214. func (s *StoreEntry) SearchSystemDeptListByIds(ctx context.Context, ids []int64) ([]*model.SystemDept, error) {
  215. systemDeptList := make([]*model.SystemDept, 0)
  216. if err := s.DB.Model(new(model.SystemDept)).
  217. Where("is_show = ?", operationPb.IsShow_OK).
  218. Find(&systemDeptList, ids).Error; err != nil {
  219. return nil, xerr.WithStack(err)
  220. }
  221. return systemDeptList, nil
  222. }
  223. func (s *StoreEntry) GetSystemDeptListById(ctx context.Context, id int64) (*model.SystemDept, error) {
  224. systemDept := &model.SystemDept{Id: id}
  225. if err := s.DB.Model(new(model.SystemDept)).First(systemDept).Error; err != nil {
  226. return nil, xerr.WithStack(err)
  227. }
  228. return systemDept, nil
  229. }
  230. func (s *StoreEntry) GetSystemBasicByName(ctx context.Context, name string) (*model.SystemBasic, error) {
  231. systemBasic := &model.SystemBasic{}
  232. if err := s.DB.Model(new(model.SystemBasic)).
  233. Where("name = ?", name).
  234. Where("is_show = ?", pasturePb.IsShow_Ok).
  235. First(systemBasic).Error; err != nil {
  236. return nil, xerr.WithStack(err)
  237. }
  238. return systemBasic, nil
  239. }
  240. // UpdateCowPenId 更新牛舍Id
  241. func (s *StoreEntry) UpdateCowPenId(ctx context.Context, cowId, penId int64) {
  242. if penId == 0 || cowId == 0 {
  243. return
  244. }
  245. cow, err := s.GetCowInfoByCowId(ctx, cowId)
  246. if err != nil {
  247. zaplog.Error("UpdateCowPenId", zap.Any("cowId", err))
  248. return
  249. }
  250. if err = s.DB.Model(new(model.Cow)).Where("id = ?", cow.Id).Update("pen_id", penId).Error; err != nil {
  251. zaplog.Error("UpdateCowPenId", zap.Any("cow_id", cowId), zap.Any("pen_id", penId), zap.Any("err", err))
  252. }
  253. }
  254. func (s *StoreEntry) GetLastEventMating(ctx context.Context, cowId int64) (*model.EventMating, error) {
  255. res := &model.EventMating{}
  256. if err := s.DB.Model(new(model.EventMating)).
  257. Where("cow_id = ?", cowId).
  258. Order("reality_day desc").
  259. First(res).Error; err != nil {
  260. if errors.Is(err, gorm.ErrRecordNotFound) {
  261. return res, nil
  262. } else {
  263. return nil, xerr.WithStack(err)
  264. }
  265. }
  266. return res, nil
  267. }
  268. func (s *StoreEntry) GetOutboundById(ctx context.Context, id int64) (*model.Outbound, error) {
  269. res := &model.Outbound{}
  270. if err := s.DB.Model(new(model.Outbound)).Where("id = ?", id).First(res).Error; err != nil {
  271. return nil, xerr.WithStack(err)
  272. }
  273. return res, nil
  274. }
  275. func (s *StoreEntry) GetOutboundLogsByOutboundId(ctx context.Context, id int64) ([]*model.OutboundLog, error) {
  276. list := make([]*model.OutboundLog, 0)
  277. if err := s.DB.Model(new(model.OutboundLog)).Where("outbound_id = ?", id).Find(&list).Error; err != nil {
  278. return nil, xerr.WithStack(err)
  279. }
  280. return list, nil
  281. }
  282. func (s *StoreEntry) GetEventMatingIsExIstByCowId(
  283. ctx context.Context,
  284. cow *model.Cow,
  285. ) (*model.EventMating, error) {
  286. newEventMating := &model.EventMating{}
  287. if err := s.DB.Model(new(model.EventMating)).
  288. Where("cow_id = ?", cow.Id).
  289. Where("lact = ?", cow.Lact).
  290. Order("id desc").
  291. First(newEventMating).Error; err != nil {
  292. if !errors.Is(err, gorm.ErrRecordNotFound) {
  293. return nil, xerr.WithStack(err)
  294. }
  295. }
  296. return newEventMating, nil
  297. }
  298. func (s *StoreEntry) GetEventPregnantCheckIsExIstByCowId(
  299. ctx context.Context,
  300. cow *model.Cow,
  301. ) (*model.EventPregnantCheck, error) {
  302. newEventPregnantCheck := &model.EventPregnantCheck{}
  303. if err := s.DB.Model(new(model.EventMating)).
  304. Where("cow_id = ?", cow.Id).
  305. Where("lact = ?", cow.Lact).
  306. Where("status = ?", pasturePb.IsShow_No).
  307. Order("id desc").
  308. First(newEventPregnantCheck).Error; err != nil {
  309. if !errors.Is(err, gorm.ErrRecordNotFound) {
  310. return nil, xerr.WithStack(err)
  311. }
  312. }
  313. return newEventPregnantCheck, nil
  314. }
  315. func (s *StoreEntry) DiseaseMaps(ctx context.Context) (map[int64]*model.Disease, error) {
  316. res := make(map[int64]*model.Disease)
  317. list := make([]*model.Disease, 0)
  318. if err := s.DB.Model(new(model.Disease)).Where("is_show = ?", pasturePb.IsShow_Ok).Find(&list).Error; err != nil {
  319. return nil, xerr.WithStack(err)
  320. }
  321. for _, v := range list {
  322. res[v.Id] = v
  323. }
  324. return res, nil
  325. }