sql.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.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{
  45. Id: userId,
  46. }
  47. if err := s.DB.First(systemUser).Error; err != nil {
  48. if errors.Is(err, gorm.ErrRecordNotFound) {
  49. return nil, xerr.Customf("该系统用户数据不存在: %d", userId)
  50. }
  51. return nil, xerr.WithStack(err)
  52. }
  53. return systemUser, nil
  54. }
  55. func (s *StoreEntry) SystemDeptList(ctx context.Context) ([]*model.SystemDept, error) {
  56. deptList := make([]*model.SystemDept, 0)
  57. if err := s.DB.Where("is_delete = ?", pasturePb.IsShow_Ok).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.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.Where("is_delete = ?", pasturePb.IsShow_Ok).Find(&penList).Error; err != nil {
  75. return nil, xerr.WithStack(err)
  76. }
  77. return penList, nil
  78. }
  79. func (s *StoreEntry) PenMap(ctx context.Context) map[int32]*model.Pen {
  80. penList, _ := s.GetPenList(ctx)
  81. penMap := make(map[int32]*model.Pen)
  82. for _, v := range penList {
  83. penMap[v.Id] = v
  84. }
  85. return penMap
  86. }
  87. func (s *StoreEntry) GetCowList(ctx context.Context) ([]*model.Cow, error) {
  88. cowList := make([]*model.Cow, 0)
  89. if err := s.DB.Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).Find(&cowList).Error; err != nil {
  90. return nil, xerr.WithStack(err)
  91. }
  92. return cowList, nil
  93. }
  94. func (s *StoreEntry) GetCowInfoByCowId(ctx context.Context, cowId int64) (*model.Cow, error) {
  95. cowData := &model.Cow{Id: cowId}
  96. if err := s.DB.Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).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, cowIds []int64) ([]*model.Cow, error) {
  105. cowList := make([]*model.Cow, 0)
  106. if err := s.DB.Model(&model.Cow{}).
  107. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  108. Where("id IN ?", cowIds).
  109. Find(&cowList).Error; err != nil {
  110. return nil, xerr.WithStack(err)
  111. }
  112. return cowList, nil
  113. }
  114. func (s *StoreEntry) GetTransferReasonInfo(ctx context.Context, reasonId int64) (*model.ConfigTransferPenReason, error) {
  115. configTransferPenReasonData := &model.ConfigTransferPenReason{
  116. Id: reasonId,
  117. }
  118. if err := s.DB.First(configTransferPenReasonData).Error; err != nil {
  119. if errors.Is(err, gorm.ErrRecordNotFound) {
  120. return nil, xerr.Customf("该转群原因数据不存在: %d", reasonId)
  121. }
  122. return nil, xerr.WithStack(err)
  123. }
  124. return configTransferPenReasonData, nil
  125. }
  126. func (s *StoreEntry) GetCowWeightByLastSecond(ctx context.Context, cowId, lastWeightAt int64) (*model.EventWeight, error) {
  127. cowWeightData := &model.EventWeight{}
  128. if err := s.DB.Where("cow_id = ?", cowId).Where("weight_at < ?", lastWeightAt).First(cowWeightData).Error; err != nil {
  129. if errors.Is(err, gorm.ErrRecordNotFound) {
  130. return nil, xerr.Customf("该牛只体重数据不存在: %d", cowId)
  131. }
  132. return nil, xerr.WithStack(err)
  133. }
  134. return cowWeightData, nil
  135. }
  136. func (s *StoreEntry) GetDrugsById(ctx context.Context, id int64) (*model.Drugs, error) {
  137. drugs := &model.Drugs{Id: id}
  138. if err := s.DB.First(drugs).Error; err != nil {
  139. if errors.Is(err, gorm.ErrRecordNotFound) {
  140. return nil, xerr.Customf("该药品数据不存在: %d", id)
  141. }
  142. return nil, xerr.WithStack(err)
  143. }
  144. return drugs, nil
  145. }
  146. func (s *StoreEntry) DiseaseTypeList(ctx context.Context) ([]*model.ConfigDiseaseType, error) {
  147. diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
  148. if err := s.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&diseaseTypeList).Error; err != nil {
  149. return nil, xerr.WithStack(err)
  150. }
  151. return diseaseTypeList, nil
  152. }
  153. func (s *StoreEntry) GetDiseaseById(ctx context.Context, id int32) (*model.Disease, error) {
  154. newDisease := &model.Disease{}
  155. if err := s.DB.Where("id = ?", id).First(newDisease).Error; err != nil {
  156. return nil, xerr.WithStack(err)
  157. }
  158. return newDisease, nil
  159. }
  160. func (s *StoreEntry) DiseaseListByIds(ctx context.Context, ids []int32) ([]*model.Disease, error) {
  161. diseaseList := make([]*model.Disease, 0)
  162. if err := s.DB.Where("id IN ?", ids).Find(&diseaseList).Error; err != nil {
  163. return nil, xerr.WithStack(err)
  164. }
  165. return diseaseList, nil
  166. }
  167. func (s *StoreEntry) GetPrescriptionById(ctx context.Context, id int32) (*model.Prescription, error) {
  168. newPrescription := &model.Prescription{}
  169. if err := s.DB.Where("id = ?", id).
  170. Where("is_show = ?", pasturePb.IsShow_Ok).
  171. First(&newPrescription).Error; err != nil {
  172. return nil, xerr.WithStack(err)
  173. }
  174. return newPrescription, nil
  175. }
  176. func (s *StoreEntry) PrescriptionDrugsByPrescriptionId(ctx context.Context, prescriptionId int32) ([]*model.PrescriptionDrugs, error) {
  177. newPrescriptionDrugsList := make([]*model.PrescriptionDrugs, 0)
  178. if err := s.DB.Where("prescription_id = ?", prescriptionId).
  179. Where("is_show = ?", pasturePb.IsShow_Ok).
  180. Find(&newPrescriptionDrugsList).Error; err != nil {
  181. return nil, xerr.WithStack(err)
  182. }
  183. return newPrescriptionDrugsList, nil
  184. }
  185. func (s *StoreEntry) GetImmunizationById(ctx context.Context, id int64) (*model.ImmunizationPlan, error) {
  186. immunizationPlan := &model.ImmunizationPlan{Id: id}
  187. if err := s.DB.First(immunizationPlan).Error; err != nil {
  188. if errors.Is(err, gorm.ErrRecordNotFound) {
  189. return nil, xerr.Customf("该免疫计划不存在: %d", id)
  190. }
  191. return nil, xerr.WithStack(err)
  192. }
  193. return immunizationPlan, nil
  194. }
  195. func (s *StoreEntry) GetWorkOrderSubByWorkOrderId(ctx context.Context, workOrderId int64) ([]*model.WorkOrderSub, error) {
  196. workOrderSubList := make([]*model.WorkOrderSub, 0)
  197. if err := s.DB.Where("work_order_id = ?", workOrderId).Find(&workOrderSubList).Error; err != nil {
  198. return nil, xerr.WithStack(err)
  199. }
  200. return workOrderSubList, nil
  201. }
  202. func (s *StoreEntry) GetSameTimeById(ctx context.Context, id int64) (*model.SameTime, error) {
  203. sameTime := &model.SameTime{Id: id}
  204. if err := s.DB.Where("is_show = ?", pasturePb.IsShow_Ok).First(sameTime).Error; err != nil {
  205. return nil, xerr.WithStack(err)
  206. }
  207. return sameTime, nil
  208. }
  209. func (s *StoreEntry) SearchSystemDeptListByIds(ctx context.Context, ids []int64) ([]*model.SystemDept, error) {
  210. systemDeptList := make([]*model.SystemDept, 0)
  211. if err := s.DB.Model(new(model.SystemDept)).
  212. Where("is_show = ?", operationPb.IsShow_OK).
  213. Find(&systemDeptList, ids).Error; err != nil {
  214. return nil, xerr.WithStack(err)
  215. }
  216. return systemDeptList, nil
  217. }
  218. func (s *StoreEntry) GetSystemDeptListById(ctx context.Context, id int64) (*model.SystemDept, error) {
  219. systemDept := &model.SystemDept{Id: id}
  220. if err := s.DB.Model(new(model.SystemDept)).First(systemDept).Error; err != nil {
  221. return nil, xerr.WithStack(err)
  222. }
  223. return systemDept, nil
  224. }
  225. func (s *StoreEntry) GetSystemBasicByName(ctx context.Context, name string) (*model.SystemBasic, error) {
  226. systemBasic := &model.SystemBasic{}
  227. if err := s.DB.Where("name = ?", name).Where("is_show = ?", pasturePb.IsShow_Ok).First(systemBasic).Error; err != nil {
  228. return nil, xerr.WithStack(err)
  229. }
  230. return systemBasic, nil
  231. }
  232. // UpdateCowPenId 更新牛舍Id
  233. func (s *StoreEntry) UpdateCowPenId(ctx context.Context, cowId, penId int64) {
  234. if penId == 0 || cowId == 0 {
  235. return
  236. }
  237. cow, err := s.GetCowInfoByCowId(ctx, cowId)
  238. if err != nil {
  239. zaplog.Error("UpdateCowPenId", zap.Any("cowId", err))
  240. return
  241. }
  242. if err = s.DB.Model(new(model.Cow)).Where("id = ?", cow.Id).Update("pen_id", penId).Error; err != nil {
  243. zaplog.Error("UpdateCowPenId", zap.Any("cow_id", cowId), zap.Any("pen_id", penId), zap.Any("err", err))
  244. }
  245. }
  246. func (s *StoreEntry) GetLastEventMating(ctx context.Context, cowId int64) (*model.EventMating, error) {
  247. res := &model.EventMating{}
  248. if err := s.DB.Where("cow_id = ?", cowId).Order("reality_day desc").First(res).Error; err != nil {
  249. if errors.Is(err, gorm.ErrRecordNotFound) {
  250. return res, nil
  251. } else {
  252. return nil, xerr.WithStack(err)
  253. }
  254. }
  255. return res, nil
  256. }