calendar_more.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "kpt-pasture/util"
  7. "net/http"
  8. "regexp"
  9. "strconv"
  10. "time"
  11. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  12. "gitee.com/xuyiping_admin/pkg/xerr"
  13. )
  14. func (s *StoreEntry) CalvingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CalvingItemsResponse, error) {
  15. userModel, err := s.GetUserModel(ctx)
  16. if err != nil {
  17. return nil, xerr.WithStack(err)
  18. }
  19. calvingItems := make([]*pasturePb.CalvingItems, 0)
  20. count := int64(0)
  21. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCalving).TableName())).
  22. Select(`a.id,a.cow_id,a.ear_number,a.status,b.breed_status,b.pen_id,ROUND(b.current_weight/1000,2) as current_weight,
  23. DATE_FORMAT(FROM_UNIXTIME(last_mating_at), '%Y-%m-%d') AS mating_at_format,b.day_age,b.last_bull_number as bull_id,
  24. b.pen_name,DATEDIFF(NOW(),FROM_UNIXTIME(last_mating_at)) AS mating_age,DATE_FORMAT(FROM_UNIXTIME(a.plan_day), '%Y-%m-%d') AS plan_day`).
  25. Joins("left join cow as b on a.cow_id = b.id").
  26. Where("a.status = ?", pasturePb.IsShow_No).
  27. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  28. Where("a.pasture_id = ?", userModel.AppPasture.Id)
  29. if req.EndDay != "" {
  30. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  31. pref.Where("a.plan_day <= ?", dateTime)
  32. }
  33. if req.PenId > 0 {
  34. pref.Where("b.pen_id = ?", req.PenId)
  35. }
  36. if err = pref.Order("a.plan_day DESC").
  37. Count(&count).
  38. Limit(int(pagination.PageSize)).
  39. Offset(int(pagination.PageOffset)).
  40. Find(&calvingItems).Error; err != nil {
  41. return nil, xerr.WithStack(err)
  42. }
  43. breedStatusMap := s.CowBreedStatusMap()
  44. for _, v := range calvingItems {
  45. breedStatusName := ""
  46. if breedStatus, ok := breedStatusMap[v.BreedStatus]; ok {
  47. breedStatusName = breedStatus
  48. }
  49. v.BreedStatusName = breedStatusName
  50. }
  51. return &pasturePb.CalvingItemsResponse{
  52. Code: http.StatusOK,
  53. Msg: "ok",
  54. Data: &pasturePb.CalvingItemsData{
  55. Total: int32(count),
  56. Page: pagination.Page,
  57. PageSize: pagination.PageSize,
  58. HeaderSort: []string{"earNumber", "penName", "lact", "breedStatusName", "matingAge", "dayAge", "status",
  59. "bullId", "planDay", "matingAtFormat", "currentWeight"},
  60. Header: map[string]string{
  61. "earNumber": "耳标号",
  62. "breedStatusName": "繁殖状态",
  63. "penName": "栏舍",
  64. "lact": "胎次",
  65. "matingAge": "配后天数",
  66. "dayAge": "日龄",
  67. "status": "是否完成",
  68. "bullId": "配种公牛号",
  69. "planDay": "预产时间",
  70. "matingAtFormat": "配种时间",
  71. "currentWeight": "体重",
  72. },
  73. List: calvingItems,
  74. },
  75. }, nil
  76. }
  77. func (s *StoreEntry) DryMilkCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.DruMilkItemsResponse, error) {
  78. userModel, err := s.GetUserModel(ctx)
  79. if err != nil {
  80. return nil, xerr.WithStack(err)
  81. }
  82. dryMilkItems := make([]*pasturePb.DruMilkItems, 0)
  83. count := int64(0)
  84. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventDryMilk).TableName())).
  85. Select(`a.id,a.cow_id,a.ear_number,a.status,b.breed_status,b.pen_id,b.day_age,
  86. b.last_bull_number as bull_number,b.pen_name,DATEDIFF(NOW(),FROM_UNIXTIME(last_mating_at)) AS mating_age,
  87. DATE_FORMAT(FROM_UNIXTIME(a.plan_day), '%Y-%m-%d') AS plan_day`).
  88. Joins("left join cow as b on a.cow_id = b.id").
  89. Where("a.status = ?", pasturePb.IsShow_No).
  90. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  91. Where("a.pasture_id = ?", userModel.AppPasture.Id)
  92. if req.EndDay != "" {
  93. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  94. pref.Where("a.plan_day <= ?", dateTime)
  95. }
  96. if req.PenId > 0 {
  97. pref.Where("b.pen_id = ?", req.PenId)
  98. }
  99. if err = pref.Order("a.plan_day DESC").
  100. Count(&count).
  101. Limit(int(pagination.PageSize)).
  102. Offset(int(pagination.PageOffset)).
  103. Find(&dryMilkItems).Error; err != nil {
  104. return nil, xerr.WithStack(err)
  105. }
  106. systemBasic, err := s.GetSystemBasicByName(ctx, userModel.AppPasture.Id, model.PregnancyAge)
  107. if err != nil {
  108. return nil, xerr.WithStack(err)
  109. }
  110. breedStatusMap := s.CowBreedStatusMap()
  111. for _, v := range dryMilkItems {
  112. breedStatusName := ""
  113. if breedStatus, ok := breedStatusMap[v.BreedStatus]; ok {
  114. breedStatusName = breedStatus
  115. }
  116. v.BreedStatusName = breedStatusName
  117. v.CalvingAtFormat = time.Now().Local().AddDate(0, 0, int(systemBasic.MinValue-v.PregnancyAge)).Format(model.LayoutDate2)
  118. }
  119. return &pasturePb.DruMilkItemsResponse{
  120. Code: http.StatusOK,
  121. Msg: "ok",
  122. Data: &pasturePb.DruMilkItemsData{
  123. Total: int32(count),
  124. Page: pagination.Page,
  125. PageSize: pagination.PageSize,
  126. HeaderSort: []string{"earNumber", "dayAge", "penName", "lact", "pregnancyAge", "status", "bullNumber", "planDay", "calvingAtFormat"},
  127. Header: map[string]string{
  128. "earNumber": "耳标号",
  129. "breedStatusName": "繁殖状态",
  130. "penName": "栏舍",
  131. "lact": "胎次",
  132. "pregnancyAge": "怀孕天数",
  133. "dayAge": "日龄",
  134. "status": "是否完成",
  135. "bullNumber": "配种公牛号",
  136. "planDay": "干奶时间",
  137. "calvingAtFormat": "预产日期",
  138. },
  139. List: dryMilkItems,
  140. },
  141. }, nil
  142. }
  143. // TreatmentCowList 治疗清单
  144. func (s *StoreEntry) TreatmentCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (interface{}, error) {
  145. userModel, err := s.GetUserModel(ctx)
  146. if err != nil {
  147. return nil, xerr.WithStack(err)
  148. }
  149. diseaseItems := make([]*model.EventCowDisease, 0)
  150. count := int64(0)
  151. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCowDisease).TableName())).
  152. Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
  153. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  154. Where("a.health_status IN (?)", []pasturePb.HealthStatus_Kind{pasturePb.HealthStatus_Disease, pasturePb.HealthStatus_Treatment})
  155. if req.PenId > 0 {
  156. pref.Where("b.pen_id = ?", req.PenId)
  157. }
  158. if err = pref.Order("a.disease_at DESC").
  159. Count(&count).
  160. Limit(int(pagination.PageSize)).
  161. Offset(int(pagination.PageOffset)).
  162. Find(&diseaseItems).Error; err != nil {
  163. return nil, xerr.WithStack(err)
  164. }
  165. return &pasturePb.EventCowDiseaseResponse{
  166. Code: http.StatusOK,
  167. Msg: "ok",
  168. Data: &pasturePb.EventCowDiseaseData{
  169. List: model.EventCowDiseaseSlice(diseaseItems).ToPB(s.HealthStatusMap()),
  170. Total: int32(count),
  171. PageSize: pagination.PageSize,
  172. Page: pagination.Page,
  173. HeaderSort: []string{"earNumber", "penName", "diagnoseName", "healthStatus", "lastPrescriptionName", "treatmentDays", "onsetDays"},
  174. Header: map[string]string{
  175. "earNumber": "耳标",
  176. "diagnoseName": "疾病名称",
  177. "healthStatus": "健康状态",
  178. "lastPrescriptionName": "处方名称",
  179. "treatmentDays": "治疗天数",
  180. "onsetDays": "发病天数",
  181. "penName": "栏舍名称",
  182. },
  183. },
  184. }, nil
  185. }
  186. // WorkOrderCowList 暂时不处理工单业务
  187. func (s *StoreEntry) WorkOrderCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (interface{}, error) {
  188. return nil, nil
  189. }
  190. // Paginate 函数用于对切片进行分页
  191. func Paginate(slice []*pasturePb.CalendarToDoList, req *pasturePb.CalendarToDoRequest, pagination *pasturePb.PaginationModel) ([]*pasturePb.CalendarToDoList, int32) {
  192. newSlice := make([]*pasturePb.CalendarToDoList, 0)
  193. if req.CalendarType > 0 {
  194. calendarTypeName := CalendarTypeMap()[req.CalendarType]
  195. if len(calendarTypeName) > 0 {
  196. re := regexp.MustCompile(`[a-zA-Z]`) // 使用正则表达式替换匹配的字母为空字符串
  197. calendarTypeName = re.ReplaceAllString(calendarTypeName, "")
  198. for _, v := range slice {
  199. if v.CalendarTypeName != calendarTypeName {
  200. continue
  201. }
  202. newSlice = append(newSlice, v)
  203. }
  204. }
  205. } else {
  206. newSlice = append(newSlice, slice...)
  207. }
  208. if req.CowId > 0 {
  209. filteredSlice := make([]*pasturePb.CalendarToDoList, 0)
  210. for _, v := range newSlice {
  211. if v.CowId != req.CowId {
  212. continue
  213. }
  214. filteredSlice = append(filteredSlice, v)
  215. }
  216. newSlice = filteredSlice
  217. }
  218. total := int32(len(newSlice))
  219. // 计算起始索引
  220. start := (pagination.Page - 1) * pagination.PageSize
  221. // 如果起始索引超出切片长度,返回空切片
  222. if start >= int32(len(newSlice)) {
  223. return []*pasturePb.CalendarToDoList{}, total
  224. }
  225. // 计算结束索引
  226. end := start + pagination.PageSize
  227. // 如果结束索引超出切片长度,调整到切片末尾
  228. if end > int32(len(newSlice)) {
  229. end = int32(len(newSlice))
  230. }
  231. // 返回分页后的切片
  232. return newSlice[start:end], total
  233. }
  234. func ProgressList(dMap map[pasturePb.CalendarType_Kind]int32, toDayCompletedCountMap map[pasturePb.CalendarType_Kind]*pasturePb.ProgressList) map[string]*pasturePb.ProgressList {
  235. res := make(map[string]*pasturePb.ProgressList)
  236. for kind, cc := range toDayCompletedCountMap {
  237. incompleteTotal := int32(0)
  238. if it, ok := dMap[kind]; ok {
  239. incompleteTotal = it
  240. }
  241. if res[cc.CalendarName] == nil {
  242. res[cc.CalendarName] = &pasturePb.ProgressList{
  243. CalendarTypeKind: kind,
  244. CalendarName: cc.CalendarName,
  245. Color: model.CalendarTypeColorMap[kind],
  246. CompletedCount: cc.CompletedCount,
  247. IncompleteTotal: incompleteTotal,
  248. }
  249. }
  250. if res[cc.CalendarName].IncompleteTotal > 0 && res[cc.CalendarName].CompletedCount > 0 {
  251. res[cc.CalendarName].Progress = strconv.FormatFloat(
  252. float64(res[cc.CalendarName].CompletedCount)/
  253. float64(res[cc.CalendarName].IncompleteTotal)*100,
  254. 'f',
  255. 2,
  256. 64,
  257. ) + "%"
  258. } else {
  259. res[cc.CalendarName].Progress = "0%"
  260. }
  261. }
  262. return res
  263. }