package com.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.annotation.DESRespondSecret;
import com.template.annotation.PassToken;
import com.template.api.DriverStockControllerAPI;
import com.template.model.pojo.SmartUser;
import com.template.model.pojo.SmartWarning;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.tongji.Tj;
import com.template.model.tongji.TjFloat;
import com.template.services.DriverStockService;
import com.template.services.SmartUserService;
import com.template.services.SmartWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

@Controller
public class DriverStockController implements DriverStockControllerAPI {

    @Autowired
    public DriverStockService driverStockService;
    @Autowired
    public SmartWarningService smartWarningService;
    @Autowired
    public SmartUserService smartUserService;

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public CommonResult getClassTj() {
        List<Tj> list = driverStockService.getClassTj();
        return CommonResult.ok(list);
    }

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public CommonResult getVisitorTj() {
        List<Tj> list = driverStockService.getVisitorTj();
        return CommonResult.ok(list);
    }

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public CommonResult queryPageSmartWarning(int currentPage, int pageCount, String name, String state) {
        PageUtils<SmartWarning> result = smartWarningService.queryPageSmartWarnings(currentPage, pageCount, name, state);

        return CommonResult.ok(result);
    }

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public CommonResult getEnergyTj() {
        List<TjFloat> list = driverStockService.getEnergyTj();
        return CommonResult.ok(list);
    }

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public CommonResult getEnergyTjAvg() {
        List<TjFloat> list = driverStockService.getEnergyTj();
        QueryWrapper<SmartUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        Integer userNum = smartUserService.list(queryWrapper).size();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setNum(list.get(i).getNum()/userNum);
        }
        return CommonResult.ok(list);
    }

}
