calendar_more.go 9.9 KB

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