|
@@ -6,7 +6,7 @@
|
|
@Email :
|
|
@Email :
|
|
@Software: PyCharm
|
|
@Software: PyCharm
|
|
"""
|
|
"""
|
|
-import datetime
|
|
|
|
|
|
+from datetime import datetime, timedelta
|
|
import json
|
|
import json
|
|
import time
|
|
import time
|
|
|
|
|
|
@@ -37,14 +37,16 @@ class FeedDiaryView(View):
|
|
return self.get_feed_diary_data(request_dict, response)
|
|
return self.get_feed_diary_data(request_dict, response)
|
|
elif operation == 'queryStatistics': # 查询喂养统计
|
|
elif operation == 'queryStatistics': # 查询喂养统计
|
|
return self.get_statistics_data(request_dict, response)
|
|
return self.get_statistics_data(request_dict, response)
|
|
- elif operation == 'addOrEdit': # 查询喂养统计
|
|
|
|
|
|
+ elif operation == 'addOrEdit': # 添加或编辑喂养日记
|
|
return self.add_or_edit_feed_diary(request_dict, response)
|
|
return self.add_or_edit_feed_diary(request_dict, response)
|
|
- elif operation == 'delete': # 查询喂养统计
|
|
|
|
|
|
+ elif operation == 'delete': # 删除喂养日记
|
|
return self.delete_feed_diary(request_dict, response)
|
|
return self.delete_feed_diary(request_dict, response)
|
|
- elif operation == 'getFeedList': # 查询喂养列表
|
|
|
|
|
|
+ elif operation == 'getFeedList': # 查询喂养类型列表
|
|
return self.get_feed_list(request_dict, response)
|
|
return self.get_feed_list(request_dict, response)
|
|
elif operation == 'feedTypeConfig':
|
|
elif operation == 'feedTypeConfig':
|
|
return self.feed_type_config(request_dict, response)
|
|
return self.feed_type_config(request_dict, response)
|
|
|
|
+ elif operation == 'getDailyRecordStatus': # 查询天数记录
|
|
|
|
+ return self.get_daily_record_status(request_dict, response)
|
|
else:
|
|
else:
|
|
return response.json(414)
|
|
return response.json(414)
|
|
|
|
|
|
@@ -73,7 +75,44 @@ class FeedDiaryView(View):
|
|
feed_diary = feed_diary.filter(feed_type=feed_type)
|
|
feed_diary = feed_diary.filter(feed_type=feed_type)
|
|
feed_diary = feed_diary.values('feed_type__name', 'feed_type__icon_url', 'feed_content', 'date_time',
|
|
feed_diary = feed_diary.values('feed_type__name', 'feed_type__icon_url', 'feed_content', 'date_time',
|
|
'id', 'feed_type')
|
|
'id', 'feed_type')
|
|
- return response.json(0, list(feed_diary))
|
|
|
|
|
|
+ feed_diary = list(feed_diary)
|
|
|
|
+
|
|
|
|
+ # 统计各类型的喂养次数
|
|
|
|
+ feed_statistics = {}
|
|
|
|
+ for item in feed_diary:
|
|
|
|
+ feed_type_id = item['feed_type']
|
|
|
|
+ feed_type_name = item['feed_type__name']
|
|
|
|
+
|
|
|
|
+ # 初始化统计项
|
|
|
|
+ if feed_type_id not in feed_statistics:
|
|
|
|
+ feed_statistics[feed_type_id] = {
|
|
|
|
+ "feed_type_name": feed_type_name,
|
|
|
|
+ "frequency": 0,
|
|
|
|
+ "total": 0 # 仅对类型1/2/3有效
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ # 更新统计
|
|
|
|
+ feed_statistics[feed_type_id]["frequency"] += 1
|
|
|
|
+
|
|
|
|
+ # 仅对类型1/2/3计算total
|
|
|
|
+ if item['feed_type'] in [1, 2, 3] and "total" in item.get("feed_content", {}):
|
|
|
|
+ feed_statistics[feed_type_id]["total"] += item["feed_content"]["total"]
|
|
|
|
+
|
|
|
|
+ # 转换统计结果为列表
|
|
|
|
+ feed_statistics_list = [
|
|
|
|
+ {
|
|
|
|
+ "feed_type": feed_type_id,
|
|
|
|
+ "feed_type_name": data["feed_type_name"],
|
|
|
|
+ "frequency": str(data["frequency"]),
|
|
|
|
+ "total": str(data["total"])
|
|
|
|
+ }
|
|
|
|
+ for feed_type_id, data in feed_statistics.items()
|
|
|
|
+ ]
|
|
|
|
+
|
|
|
|
+ return response.json(0, {
|
|
|
|
+ 'feed_diary': feed_diary,
|
|
|
|
+ 'feed_statistics': feed_statistics_list # 改为列表形式,更清晰
|
|
|
|
+ })
|
|
except Exception as e:
|
|
except Exception as e:
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
|
|
@@ -226,3 +265,61 @@ class FeedDiaryView(View):
|
|
return response.json(0, {"feed_type_text": feed_type_text})
|
|
return response.json(0, {"feed_type_text": feed_type_text})
|
|
except Exception as e:
|
|
except Exception as e:
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
+
|
|
|
|
+ @classmethod
|
|
|
|
+ def get_daily_record_status(cls, request_dict, response):
|
|
|
|
+ """
|
|
|
|
+ 获取记录的日期
|
|
|
|
+ @param request_dict: 请求参数
|
|
|
|
+ @request_dict baby_id: 宝宝id
|
|
|
|
+ @request_dict timestamp: 时间戳
|
|
|
|
+ @param response: 响应对象
|
|
|
|
+ @return: response
|
|
|
|
+ """
|
|
|
|
+ baby_id = request_dict.get('baby_id')
|
|
|
|
+ start_time = request_dict.get('start_time')
|
|
|
|
+ end_time = request_dict.get('end_time')
|
|
|
|
+
|
|
|
|
+ if not all([baby_id, start_time, end_time]):
|
|
|
|
+ return response.json(444, {'error': '缺少baby_id、start_time或end_time参数'})
|
|
|
|
+
|
|
|
|
+ try:
|
|
|
|
+ # 转换为整数
|
|
|
|
+ start_time = int(start_time)
|
|
|
|
+ end_time = int(end_time)
|
|
|
|
+
|
|
|
|
+ records = FeedDiary.objects.filter(
|
|
|
|
+ baby_id=baby_id,
|
|
|
|
+ date_time__gte=start_time,
|
|
|
|
+ date_time__lte=end_time
|
|
|
|
+ ).values('date_time')
|
|
|
|
+ record_dates = {
|
|
|
|
+ datetime.fromtimestamp(r['date_time']).strftime('%Y-%m-%d')
|
|
|
|
+ for r in records
|
|
|
|
+ }
|
|
|
|
+ # 生成时间段内所有日期
|
|
|
|
+ all_dates = []
|
|
|
|
+ current_date = datetime.fromtimestamp(start_time).date()
|
|
|
|
+ end_date = datetime.fromtimestamp(end_time).date()
|
|
|
|
+
|
|
|
|
+ while current_date <= end_date:
|
|
|
|
+ all_dates.append(current_date.strftime('%Y-%m-%d'))
|
|
|
|
+ current_date += timedelta(days=1)
|
|
|
|
+
|
|
|
|
+ # 构建返回结果
|
|
|
|
+ result = []
|
|
|
|
+ for date in all_dates:
|
|
|
|
+ result.append({
|
|
|
|
+ 'date': date,
|
|
|
|
+ 'has_record': date in record_dates
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ return response.json(0, {
|
|
|
|
+ 'baby_id': baby_id,
|
|
|
|
+ 'days': result
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ except ValueError as e:
|
|
|
|
+ return response.json(444, {'error': '时间戳格式不正确'})
|
|
|
|
+ except Exception as e:
|
|
|
|
+ return response.json(500, {'error': f'服务器错误: {str(e)}'})
|