package com.template.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.template.annotation.DESRespondSecret;
import com.template.annotation.PassToken;
import com.template.api.DriverStockControllerAPI;
import com.template.common.utils.TimeExchange;
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.*;
import com.template.model.vo.VisitorTjVo;
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.sql.Time;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

@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();
        Integer num = 0;
        for (Tj teacherData:list) {
            if (teacherData.getName().contains("老师")) {
                num = num + teacherData.getNum();
            }
        }
        Tj teacher = new Tj();
        teacher.setName("教师");
        teacher.setNum(num);
        list = list.stream().filter(e -> !e.name.contains("老师") && !e.name.contains("家长")).collect(Collectors.toList());
        list.add(teacher);
        return CommonResult.ok(list);
    }

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public CommonResult getVisitorTj() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
        ArrayList<VisitorTjVo> visitorTjVos = new ArrayList<>();
//  近1年数据
        LocalDateTime localDateTime = LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).plusMonths(1);
        ArrayList<Tj> tjs = new ArrayList<>();
        Tj tj2 = new Tj();
        tj2.setName("家长");
        tj2.setNum(0);
        tjs.add(tj2);
        Tj tj3 = new Tj();
        tj3.setName("其他");
        tj3.setNum(0);
        tjs.add(tj3);
        for (int i = 0; i < 12; i++) {
            VisitorTjVo visitorTjVo = new VisitorTjVo();
            LocalDateTime start = localDateTime.minusMonths(i + 1);
            LocalDateTime end = localDateTime.minusMonths(i);
            String dataTime = start.format(dateTimeFormatter);
            List<Tj> list = driverStockService.getVisitorTj(start, end);
            if (ObjectUtils.isNotEmpty(list) && list.size() > 0) {
                if (list.size()<2) {
                    ArrayList<Tj> tjs2 = new ArrayList<>();
                    for (Tj tj : list) {
                        tjs2.add(tj);
                        String name = tj.getName();
                        if ("家长".equals(name)) {
                            tjs2.add(tj3);
                        }else {
                            tjs2.add(tj2);
                        }
                    }
                    visitorTjVo.setTjs(tjs2);
                }else {
                    visitorTjVo.setTjs(list);
                }
            } else {
                visitorTjVo.setTjs(tjs);
            }

            visitorTjVo.setDataTime(dataTime);
            visitorTjVos.add(visitorTjVo);
        }
        return CommonResult.ok(visitorTjVos);
    }

    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        int year = start.getYear();
        System.out.println("year = " + year);
        int monthValue = start.getMonthValue();
        System.out.println("monthValue = " + monthValue);
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM");
        String format = start.format(dateTimeFormatter2);
        System.out.println("format = " + format);
    }

    @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);
    }

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public CommonResult getEnergyMonth(Integer type) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        List<JSONObject> jList = new ArrayList<>();
        for (int i = -5; i <= 0; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, i);
            String date = simpleDateFormat.format(calendar.getTime());
            List<TjFloat> list = driverStockService.getEnergyMonth(date, type);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("month", date);
            jsonObject.put("data", list);
            jList.add(jsonObject);
        }
        return CommonResult.ok(jList);
    }

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public CommonResult getEnergyDay(Integer type) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<JSONObject> jList = new ArrayList<>();
        for (int i = -11; i <= 0; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, i);
            String date = simpleDateFormat.format(calendar.getTime());
            List<TjFloat> list = driverStockService.getEnergyDay(date, type);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("date", date);
            jsonObject.put("data", list);
            jList.add(jsonObject);
        }
        return CommonResult.ok(jList);
    }

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public CommonResult getDeviceT() {
        List<JSONObject> list = new ArrayList<>();
        JSONObject jsonObject1 = new JSONObject();
        List<TjFloat> allList = driverStockService.getTotalDevice();
        jsonObject1.put("name", "总设备数");
        jsonObject1.put("data", allList);
        list.add(jsonObject1);
        List<TjFloat> onlineList = driverStockService.getOnlineDevice(TimeExchange.getDate());
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("name", "在线设备数");
        jsonObject2.put("data", onlineList);
        list.add(jsonObject2);
        return CommonResult.ok(list);
    }

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public CommonResult getMeterPage(int currentPage, int pageCount, Integer meterType) {
        PageHelper.startPage(currentPage, pageCount);
        PageInfo<MeterPage> list = driverStockService.getMeterDetail(meterType);
        return CommonResult.ok(list);
    }

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

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public CommonResult getUserPage(int currentPage, int pageCount, String key, Integer identityId) {
        PageHelper.startPage(currentPage, pageCount);
        PageInfo<userPage> list = driverStockService.getUserPage(key, identityId);
        return CommonResult.ok(list);
    }

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public CommonResult getScorePer(String examName, String updateUser) {
        List<Tj> list = driverStockService.getScorePer(examName, updateUser);
        return CommonResult.ok(list);
    }

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

    @Override
    @PassToken
    @DESRespondSecret(validated = false)
    public CommonResult getTeacherExam(String cardNo) {
        Tj list = driverStockService.getTeacherExam(cardNo);
        return CommonResult.ok(list);
    }

}
