|
|
@@ -14,7 +14,8 @@ from smartfarming.serializers.ascend_serializers import (
|
|
|
)
|
|
|
from django.core.paginator import Paginator
|
|
|
import time
|
|
|
-from django.db.models import Q
|
|
|
+import datetime
|
|
|
+from django.db.models import Q, Count
|
|
|
|
|
|
|
|
|
class BaseAPIView(APIView):
|
|
|
@@ -211,7 +212,9 @@ class MongoPlantInfoAddAPIView(APIView):
|
|
|
plantname = request_data.get("plantname")
|
|
|
pickcode = request_data.get("pickcode")
|
|
|
planttype = request_data.get("planttype")
|
|
|
-
|
|
|
+ plan = MongoPlantInfo.objects.filter(plantname=plantname, planttype=planttype)
|
|
|
+ if plan:
|
|
|
+ return Response({"code": 2, "msg": "该作物种类已存在,请核查"})
|
|
|
plan_id = MongoPlantInfo.objects.create(
|
|
|
batch = batch,
|
|
|
plantname = plantname,
|
|
|
@@ -244,6 +247,9 @@ class MongoPlantInfoUpdateAPIView(APIView):
|
|
|
plantname = request_data.get("plantname")
|
|
|
pickcode = request_data.get("pickcode")
|
|
|
planttype = request_data.get("planttype")
|
|
|
+ plan = MongoPlantInfo.objects.filter(plantname=plantname, planttype=planttype)
|
|
|
+ if plan:
|
|
|
+ return Response({"code": 2, "msg": "该作物种类已存在,请核查"})
|
|
|
MongoPlantInfo.objects.filter(id=plan_id).update(
|
|
|
batch = batch,
|
|
|
plantname = plantname,
|
|
|
@@ -262,7 +268,6 @@ class MongoPlantInfoUpdateAPIView(APIView):
|
|
|
pick_lst = [str(i) for i in c.get("pick")]
|
|
|
pick = ",".join(pick_lst)
|
|
|
mark = c.get("mark")
|
|
|
- print(id, name, start_time, end_time, pest_lst, pest, pick, pick_lst, mark)
|
|
|
if id:
|
|
|
PlanWeekend.objects.filter(id=id, plan_id=plan_id).update(
|
|
|
name=name,
|
|
|
@@ -449,14 +454,67 @@ class CountryModelDeleteAPIView(APIView):
|
|
|
class LandPlanInfoAPIView(APIView):
|
|
|
|
|
|
def post(self, request):
|
|
|
- # 种植采收
|
|
|
+ # 种植采收列表及统计信息
|
|
|
request_data = request.data
|
|
|
plan = request_data.get("plan")
|
|
|
- year = request_data.get("year")
|
|
|
+ year = int(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
|
|
|
- queryset = LandPlanInfo.objects.filter(Q(plan__icontains=plan))
|
|
|
+ start_timestamp = datetime.datetime(year, 1,1,0,0).timestamp()
|
|
|
+ end_timestatmp = datetime.datetime(year, 12,31,23,59).timestamp()
|
|
|
+ plan_ids = []
|
|
|
+ if plan:
|
|
|
+ plan_ids = MongoPlantInfo.objects.filter(plantname__icontains=plan).values_list("id", flat=True)
|
|
|
+ if plan_ids and year:
|
|
|
+ queryset = LandPlanInfo.objects.filter(Q(plan_id__in = plan_ids) & Q(addtime__gte=start_timestamp, addtime__lte=end_timestatmp)).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).order_by("-addtime")
|
|
|
+ elif plan_ids:
|
|
|
+ queryset = LandPlanInfo.objects.filter(plan_id__in = plan_ids).filter(is_delete=1).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().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 = years[:5]
|
|
|
+ # 获取最近5年的作物ID
|
|
|
+ plan_ids = LandPlanInfo.objects.filter(addtime__gte=start_timestamp, addtime__lte=end_timestatmp).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).values("plan_id").annotate(total=Count("plan_id")).order_by("plan_id").values_list("plan_id", "total")
|
|
|
+ inners = {}
|
|
|
+ for k in plan_totals:
|
|
|
+ inners[str(k[0])] = k[1]
|
|
|
+ pid =[]
|
|
|
+ for id in plan_ids:
|
|
|
+ pid.append(inners.get(str(id), 0))
|
|
|
+ counts.append(pid)
|
|
|
+ plannames = []
|
|
|
+ for p in plan_ids:
|
|
|
+ plan = MongoPlantInfo.objects.filter(id=p)
|
|
|
+ if plan:
|
|
|
+ planname = plan.first().plantname + plan.first().planttype
|
|
|
+ else:
|
|
|
+ planname = ""
|
|
|
+ plannames.append(planname)
|
|
|
+ return Response({
|
|
|
+ "code": 0,
|
|
|
+ "msg": "success",
|
|
|
+ "data": serializers.data,
|
|
|
+ "count": total_obj,
|
|
|
+ "charts": {
|
|
|
+ "years": years,
|
|
|
+ "data": counts,
|
|
|
+ "plan": plannames
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+
|