| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- from rest_framework.views import APIView
- from rest_framework.response import Response
- from smartfarming.models.ascend import MongoBase, MongoLandInfo, MongoPlantInfo, MongoAreaJob, LandPlanInfo, CountryModel, PlanWeekend
- from smartfarming.models.user import DeviceUser
- from smartfarming.models.device import MongoDevice
- from smartfarming.models.worm_forecast import MongoCBDphoto
- from smartfarming.serializers.ascend_serializers import (
- LandPlanInfoSerializers
- )
- from django.core.paginator import Paginator
- import time
- import datetime
- from django.db.models import Q, Sum, Count
- from django.conf import settings
- class LandPlanInfoAPIView(APIView):
- def post(self, request):
- # 种植采收列表及统计信息
- request_data = request.data
- plan = request_data.get("plan")
- year = request_data.get("year")
- page_num = int(request_data.get("pagenum")) if request_data.get("pagenum") else 1
- page_size = int(request_data.get("pagesize")) if request_data.get("pagesize") else 10
- start_timestamp, end_timestatmp = 0, 0
- if year:
- start_timestamp = datetime.datetime(int(year), 1,1,0,0).timestamp()
- end_timestatmp = datetime.datetime(int(year), 12,31,23,59).timestamp()
- plan_ids_head = []
- if plan :
- plan_ids_head = MongoPlantInfo.objects.filter(plantname__icontains=plan).values_list("id", flat=True)
- if plan_ids_head and year:
- queryset = LandPlanInfo.objects.filter(Q(plan_id__in = plan_ids_head) & Q(addtime__gte=start_timestamp, addtime__lte=end_timestatmp)).exclude(recovery_time=0).filter(is_delete=1).order_by("-addtime")
- elif year:
- queryset = LandPlanInfo.objects.filter(addtime__gte=start_timestamp, addtime__lte=end_timestatmp).filter(is_delete=1).exclude(recovery_time=0).order_by("-addtime")
- elif plan_ids_head:
- queryset = LandPlanInfo.objects.filter(plan_id__in = plan_ids_head).filter(is_delete=1).exclude(recovery_time=0).order_by("-addtime")
- else:
- queryset = LandPlanInfo.objects.filter(is_delete=1).exclude(recovery_time=0).order_by("-addtime")
- total_obj = queryset.count()
- paginator = Paginator(queryset, page_size)
- page_obj = paginator.get_page(page_num)
- serializers = LandPlanInfoSerializers(page_obj, many=True)
- # 折线图
- year_value = LandPlanInfo.objects.all().exclude(recovery_time=0).values_list("addtime", flat=True).order_by("-addtime")
- years = []
- for year in year_value:
- y = datetime.datetime.fromtimestamp(year).year
- if y not in years:
- years.append(y)
- years.reverse()
- # 获取最近5年的作物ID
- end = datetime.datetime(int(years[-1]) -5, 12,31,23,59).timestamp()
- start = datetime.datetime(int(years[-1]), 12,31,23,59).timestamp()
- plan_ids = LandPlanInfo.objects.filter(addtime__gte=end, addtime__lte=start).exclude(recovery_time=0).distinct().values_list("plan_id", flat=True).order_by("plan_id")
- counts = []
- # 组织具体数据
- for i in years:
- start_timestamp = datetime.datetime(i, 1,1,0,0).timestamp()
- end_timestatmp = datetime.datetime(i, 12,31,23,59).timestamp()
- plan_totals = LandPlanInfo.objects.filter(
- addtime__gte=start_timestamp,
- addtime__lte=end_timestatmp).exclude(recovery_time=0).values("plan_id").annotate(total=Sum("recovery_kg")).order_by("plan_id").values_list("plan_id", "total")
- inners = {}
- for k in plan_totals:
- inners[str(k[0])] = k[1]
- pid =[]
- for j in plan_ids:
- pid.append(inners.get(str(j), 0))
- counts.append(pid)
- transpose_matrix = [[row[i] for row in counts] for i in range(len(counts[0]))]
- plannames = []
- # 组织农作物
- for p in plan_ids:
- plan_obj = MongoPlantInfo.objects.filter(id=p)
- if plan_obj:
- planname = plan_obj.first().plantname +"-"+ plan_obj.first().planttype
- else:
- planname = ""
- plannames.append(planname)
- return Response({
- "code": 0,
- "msg": "success",
- "data": serializers.data,
- "count": total_obj,
- "charts": {
- "years": years,
- "data": transpose_matrix,
- "plan": plannames
- }
- })
- class PlanNameAPIView(APIView):
- def post(self, request):
- try:
- # 作物名字
- plan_ids = LandPlanInfo.objects.filter(is_delete=1).exclude(recovery_time=0).values_list("plan_id", flat=True).order_by("-addtime")
- plans = MongoPlantInfo.objects.filter(is_delete=1, id__in=plan_ids).values_list("plantname", flat=True).distinct()
- return Response({"code": 0, "msg": "success", "data": plans})
- except Exception as e:
- return Response({"code": 0, "msg": "success", "data": []})
-
- class PlanAreaAPIView(APIView):
- def post(self, request):
- # 种植面积与作物个数统计
- land_plan_ids = LandPlanInfo.objects.filter(status="采收").filter(recovery_kg=0).values("land_id", "plan_id")
- plan_land = []
- plans = []
- plan_area = 0
- for i in land_plan_ids:
- landarea = MongoLandInfo.objects.get(id=i.get("land_id")).landarea
- plant = MongoPlantInfo.objects.get(id=i.get("plan_id"))
- plantname = f"{plant.plantname}({plant.planttype})"
- plan_land.append(
- {
- "name": plantname,
- "value": round(float(landarea), 2)
- }
- )
- plans.append(plantname) if plantname not in plans else None
- plan_area += round(float(landarea), 2)
- return Response({"code": 0, "msg": "success", "data": {"lands_area": round(plan_area, 2), "plans_count": len(plans), "p_list": plan_land}})
-
- class DeviceCountAPIView(APIView):
- def post(self, request):
- # 统计设备在线或离线统计
- device_status = MongoDevice.objects.values("device_status").annotate(total=Count("id"))
- online = 0
- offline = 0
- for k in device_status:
- if k.get("device_status") == 1:
- online = k.get("total")
- if k.get("device_status") == 0:
- offline = k.get("total")
- # 设备分类
- device_type = MongoDevice.objects.values("device_type_id").annotate(total=Count("id"))
- config_dict = settings.CONFIG.get("device_type_zh")
- device_type = {config_dict.get(str(result["device_type_id"])): result["total"] for result in device_type}
- return Response({
- "code": 0,
- "msg": "success",
- "data": {
- "device_status": {"online": online, "offline": offline},
- "device_category": device_type
- }
- })
- class RecentPestCountAPIView(APIView):
- def post(self, request):
- # 统计最近一个月的害虫排名
- MongoCBDphoto
|