package com.template.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.template.annotation.DESRespondSecret;
import com.template.api.SmartCarAccessControllerAPI;
import com.template.api.SmartFaceDiscernControllerAPI;
import com.template.common.utils.*;
import com.template.config.ControlConfig;
import com.template.model.pojo.SmartCarAccess;
import com.template.model.pojo.SmartFaceDiscern;
import com.template.model.pojo.SmartQrcode;
import com.template.model.pojo.SmartUser;
import com.template.model.result.CommonResult;
import com.template.services.SmartCarAccessService;
import com.template.services.SmartFaceDiscernService;
import com.template.services.SmartQrcodeService;
import com.template.services.SmartUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


import static com.template.common.utils.AesTestOne.decrypt;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-01-18
 */
@RestController
//返回参数加密注解
@DESRespondSecret
public class SmartFaceDiscernController implements SmartFaceDiscernControllerAPI {
    private static Logger logger = LoggerFactory.getLogger(SmartFaceDiscernController.class);

    @Autowired
    SmartFaceDiscernService smartFaceDiscernService;

    @Autowired
    SmartQrcodeService smartQrcodeService;

    @Autowired
    SmartCarAccessService smartCarAccessService;

    @Override
    @DESRespondSecret(validated = true)
    public CommonResult track(Integer id, int dateTime) {
        JSONObject date = getDate(dateTime);

        String stateTime = date.getString("stateTime");
        String endTime = date.getString("endTime");

        List<SmartFaceDiscern> smartFaceDiscerns = smartFaceDiscernService.track(stateTime, endTime, id);

        return CommonResult.ok(smartFaceDiscerns);
    }

    @Override
    @DESRespondSecret(validated = true)
    public CommonResult access() {

        LocalDateTime now = LocalDateTime.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < 30; i++) {
            LocalDateTime stateTime = now.minusDays(i + 1);
            LocalDateTime endTime = now.minusDays(i);
            String dateTime = endTime.format(dateTimeFormatter);
            LambdaQueryWrapper<SmartFaceDiscern> faceDiscernWrapper = new LambdaQueryWrapper<>();
            faceDiscernWrapper.between(SmartFaceDiscern::getDateTime, stateTime, endTime)
                    .ne(SmartFaceDiscern::getType, "人脸比对");
            int faceDiscernCount = smartFaceDiscernService.count(faceDiscernWrapper);

            LambdaQueryWrapper<SmartQrcode> qrcodeWrapper = new LambdaQueryWrapper<>();
            qrcodeWrapper.between(SmartQrcode::getCreateTime, stateTime, endTime);
            int qrcodeCount = smartQrcodeService.count(qrcodeWrapper);

            LambdaQueryWrapper<SmartCarAccess> smartCarAccessWrapper = new LambdaQueryWrapper<>();
            smartCarAccessWrapper.between(SmartCarAccess::getEnterTime, stateTime, endTime);
            int carCount = smartCarAccessService.count(smartCarAccessWrapper);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("date", dateTime);
            jsonObject.put("face", faceDiscernCount);
            jsonObject.put("code", qrcodeCount);
            jsonObject.put("car", carCount);
            jsonArray.add(jsonObject);

        }

        return CommonResult.ok(jsonArray);
    }


    public static JSONObject getDate(Integer date) {
        JSONObject jsonObject = new JSONObject();

        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime stateTime = now.withHour(0).withMinute(0).withSecond(0).minusDays(date);

        LocalDateTime endTime = now.withHour(23).withMinute(59).withSecond(59).minusDays(date);

        jsonObject.put("stateTime", stateTime.format(dateTimeFormatter1));
        jsonObject.put("endTime", endTime.format(dateTimeFormatter1));

        return jsonObject;

    }

    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (int i = 1; i <= 30; i++) {
            LocalDateTime endTime = now.minusDays(i);
            String dateTime = endTime.format(dateTimeFormatter);
            logger.info("dateTime = " + dateTime);

        }
    }

}

