from smartfarming.models.pest_count import MongoCBDPestWarning
from smartfarming.models.device import MongoDevice
from kedong.decoration import kedong_deco, PortError


@kedong_deco(login_required=True)
def user_cbd_pest_warning_record_list(request):
    """ 
    测报灯预警记录列表
    
    page            非必传(str)              页码数
    page_size       非必传(str)              条数
    warning_type    必传(str)                1目标种类预警 2、指定害虫数量预警  3 害虫数量总和预警 4综合预警
    start_time      非必传(str)              开始时间  时间戳
    end_time        非必传(str)              结束时间  时间戳
    """
    
    page = request.POST.get("page","1")
    page_size = request.POST.get("page_size","10")
    warning_type = request.POST.get("warning_type")
    start_time = request.POST.get("start_time")
    end_time = request.POST.get("end_time")
    
    if page and page_size:
        if page.isdigit() and page_size.isdigit():
            page = int(page)
            page_size = int(page_size)
        else:
            raise PortError("page&page_size","参数有误")
    else:
        page = 1
        page_size = 10
 
    start = max((page - 1), 0) * page_size
    end = page * page_size
    cbd_pest_warnig_query = MongoCBDPestWarning.objects.filter().order_by("-id")
    if warning_type:
        if warning_type not in("1","2","3","4"):
            raise PortError(warning_type,"参数超出范围")
        cbd_pest_warnig_query = cbd_pest_warnig_query.filter(warning_types=warning_type)
    
    if start_time and end_time:
        if not (start_time.isdigit() and end_time.isdigit()):
            raise PortError("start_time&end_time","参数有误") 
        cbd_pest_warnig_query = cbd_pest_warnig_query.filter(upltime__range=(int(start_time),int(end_time)))
    total = cbd_pest_warnig_query.count()
    
    data = []
    for xx in cbd_pest_warnig_query[start:end]:
        data.append({
            "id":xx.id,
            "device_id":xx.device_id,
            "warning_type":xx.warning_types,
            "warning_name": xx.warning_name,
            "warning_content": xx.warning_content,
            "upltime":xx.upltime,
            "send_user":xx.send_user,
            "status":xx.status,
        })
    return {"data" : data, "total" : total}



@kedong_deco(login_required=True)
def user_cbd_pest_warning_record_read(request):
    """
    测报灯预警已读
    参数
    id                       必传(str)              预警id
    req                      非必传(str)            一键已读必传 "all"   单个已读无需传递
    """
    req = request.POST.get("req")
    cbd_pest_warnig_query = MongoCBDPestWarning.objects.filter(user_id=request.myuser.id)
    if req and req=="all":
        cbd_pest_warnig_query.update(status=1)
    else:
        d_id = request.POST.get("id")
        if not d_id:
            raise PortError("id","参数缺失")
        try:
            cbd_pest_warnig_query = cbd_pest_warnig_query.get(id=d_id)
        except:
            raise PortError("id","未找到此数据")
        cbd_pest_warnig_query.status = 1
        cbd_pest_warnig_query.save()
    return True


@kedong_deco(login_required=True)
def user_device_map_location(request):
    """ 
    测报灯、气象站设备地图位置
    参数
    device_ids                  必传(str)                 设备号用多个,进行连接
    
    """
    
    device_ids = request.POST.get("device_ids")
    if not device_ids:
        raise PortError("device_ids","参数缺失")
    device_list = device_ids.split(",")
    data = []
    device_query = MongoDevice.objects.filter(device_type_id__in=[3,5]).filter(device_id__in=device_list)
    for xx in device_query:
        data.append({
            "device_id" : xx.device_id,
            "device_type_id" : xx.device_type_id,
            "device_name" : xx.device_name,
            "lat" : xx.lat,
            "lng" : xx.lng,
            "uptime" : xx.uptime
        })
    return data