calendar_more.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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/100,2) as current_weight,DATE_FORMAT(FROM_UNIXTIME(last_mating_at), '%Y-%m-%d') AS mating_at_format,
  23. b.day_age,b.last_bull_number as bull_id,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`).
  24. Joins("left join cow as b on a.cow_id = b.id").
  25. Where("a.status = ?", pasturePb.IsShow_No).
  26. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  27. Where("a.pasture_id = ?", userModel.AppPasture.Id)
  28. if req.EndDay != "" {
  29. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  30. pref.Where("a.plan_day <= ?", dateTime)
  31. }
  32. if req.PenId > 0 {
  33. pref.Where("b.pen_id = ?", req.PenId)
  34. }
  35. if err = pref.Order("a.plan_day DESC").
  36. Count(&count).
  37. Limit(int(pagination.PageSize)).
  38. Offset(int(pagination.PageOffset)).
  39. Find(&calvingItems).Error; err != nil {
  40. return nil, xerr.WithStack(err)
  41. }
  42. breedStatusMap := s.CowBreedStatusMap()
  43. for _, v := range calvingItems {
  44. breedStatusName := ""
  45. if breedStatus, ok := breedStatusMap[v.BreedStatus]; ok {
  46. breedStatusName = breedStatus
  47. }
  48. v.BreedStatusName = breedStatusName
  49. }
  50. return &pasturePb.CalvingItemsResponse{
  51. Code: http.StatusOK,
  52. Msg: "ok",
  53. Data: &pasturePb.CalvingItemsData{
  54. Total: int32(count),
  55. Page: pagination.Page,
  56. PageSize: pagination.PageSize,
  57. Header: map[string]string{
  58. "id": "编号",
  59. "cowId": "牛号",
  60. "earNumber": "耳标号",
  61. "breedStatusName": "繁殖状态",
  62. "penName": "栏舍",
  63. "lact": "胎次",
  64. "matingAge": "配后天数",
  65. "dayAge": "日龄",
  66. "status": "是否完成",
  67. "bullId": "配种公牛号",
  68. "planDay": "预产时间",
  69. "matingAtFormat": "配种时间",
  70. "currentWeight": "体重",
  71. },
  72. List: calvingItems,
  73. },
  74. }, nil
  75. }
  76. func (s *StoreEntry) DryMilkCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.DruMilkItemsResponse, error) {
  77. userModel, err := s.GetUserModel(ctx)
  78. if err != nil {
  79. return nil, xerr.WithStack(err)
  80. }
  81. dryMilkItems := make([]*pasturePb.DruMilkItems, 0)
  82. count := int64(0)
  83. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventDryMilk).TableName())).
  84. Select(`a.id,a.cow_id,a.ear_number,a.status,b.breed_status,b.pen_id,b.day_age,b.last_bull_number as bull_number,
  85. 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`).
  86. Joins("left join cow as b on a.cow_id = b.id").
  87. Where("a.status = ?", pasturePb.IsShow_No).
  88. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  89. Where("a.pasture_id = ?", userModel.AppPasture.Id)
  90. if req.EndDay != "" {
  91. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  92. pref.Where("a.plan_day <= ?", dateTime)
  93. }
  94. if req.PenId > 0 {
  95. pref.Where("b.pen_id = ?", req.PenId)
  96. }
  97. if err = pref.Order("a.plan_day DESC").
  98. Count(&count).
  99. Limit(int(pagination.PageSize)).
  100. Offset(int(pagination.PageOffset)).
  101. Find(&dryMilkItems).Error; err != nil {
  102. return nil, xerr.WithStack(err)
  103. }
  104. systemBasic, err := s.GetSystemBasicByName(ctx, userModel.AppPasture.Id, model.PregnancyAge)
  105. if err != nil {
  106. return nil, xerr.WithStack(err)
  107. }
  108. breedStatusMap := s.CowBreedStatusMap()
  109. for _, v := range dryMilkItems {
  110. breedStatusName := ""
  111. if breedStatus, ok := breedStatusMap[v.BreedStatus]; ok {
  112. breedStatusName = breedStatus
  113. }
  114. v.BreedStatusName = breedStatusName
  115. v.CalvingAtFormat = time.Now().AddDate(0, 0, int(systemBasic.MinValue-v.PregnancyAge)).Format(model.LayoutDate2)
  116. }
  117. return &pasturePb.DruMilkItemsResponse{
  118. Code: http.StatusOK,
  119. Msg: "ok",
  120. Data: &pasturePb.DruMilkItemsData{
  121. Total: int32(count),
  122. Page: pagination.Page,
  123. PageSize: pagination.PageSize,
  124. Header: map[string]string{
  125. "id": "编号",
  126. "cowId": "牛号",
  127. "earNumber": "耳标号",
  128. "breedStatusName": "繁殖状态",
  129. "penName": "栏舍",
  130. "lact": "胎次",
  131. "pregnancyAge": "怀孕天数",
  132. "dayAge": "日龄",
  133. "status": "是否完成",
  134. "bullNumber": "配种公牛号",
  135. "planDay": "干奶时间",
  136. "calvingAtFormat": "预产日期",
  137. },
  138. List: dryMilkItems,
  139. },
  140. }, nil
  141. }
  142. // TreatmentCowList 治疗清单
  143. func (s *StoreEntry) TreatmentCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (interface{}, error) {
  144. userModel, err := s.GetUserModel(ctx)
  145. if err != nil {
  146. return nil, xerr.WithStack(err)
  147. }
  148. diseaseItems := make([]*model.EventCowDisease, 0)
  149. count := int64(0)
  150. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCowDisease).TableName())).
  151. Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
  152. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  153. Where("a.health_status IN (?)", []pasturePb.HealthStatus_Kind{pasturePb.HealthStatus_Disease, pasturePb.HealthStatus_Treatment})
  154. if req.PenId > 0 {
  155. pref.Where("b.pen_id = ?", req.PenId)
  156. }
  157. if err = pref.Order("a.disease_at DESC").
  158. Count(&count).
  159. Limit(int(pagination.PageSize)).
  160. Offset(int(pagination.PageOffset)).
  161. Find(&diseaseItems).Error; err != nil {
  162. return nil, xerr.WithStack(err)
  163. }
  164. return &pasturePb.EventCowDiseaseResponse{
  165. Code: http.StatusOK,
  166. Msg: "ok",
  167. Data: &pasturePb.EventCowDiseaseData{
  168. List: model.EventCowDiseaseSlice(diseaseItems).ToPB(s.HealthStatusMap()),
  169. Total: int32(count),
  170. PageSize: pagination.PageSize,
  171. Page: pagination.Page,
  172. Header: map[string]string{
  173. "id": "编号",
  174. "cowId": "牛号",
  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(dataList []*pasturePb.CalendarToDoList, toDayCompletedCountMap map[pasturePb.CalendarType_Kind]*pasturePb.ProgressList) map[string]*pasturePb.ProgressList {
  235. res := make(map[string]*pasturePb.ProgressList)
  236. dMap := map[pasturePb.CalendarType_Kind][]*pasturePb.CalendarToDoList{}
  237. for _, v := range dataList {
  238. dMap[v.CalendarType] = append(dMap[v.CalendarType], v)
  239. }
  240. for kind, cc := range toDayCompletedCountMap {
  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: int32(len(dMap[kind])),
  248. }
  249. }
  250. if res[cc.CalendarName].IncompleteTotal > 0 && res[cc.CalendarName].CompletedCount > 0 {
  251. res[cc.CalendarName].Progress = strconv.FormatFloat(float64(res[cc.CalendarName].CompletedCount)/float64(res[cc.CalendarName].IncompleteTotal)*100, 'f', 2, 64)
  252. } else {
  253. res[cc.CalendarName].Progress = "0%"
  254. }
  255. }
  256. return res
  257. }