package com.luozhihui.project.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@Controller
@RestController
@RequestMapping("/api")
@CrossOrigin
public class GetCountController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @PostMapping("/getcount")
    @ResponseBody
    public Map<String, Object> getCount() {
        Map<String, Object> data = new HashMap<>();
        try {
            // 获取订单成功总金额
            Double totalMoney = jdbcTemplate.queryForObject("SELECT SUM(OrderMoney) AS total_money FROM `order` where OrderStatus = 1", Double.class);
            data.put("total_money", totalMoney != null ? totalMoney : 0);

            // 获取订单总条数和总金额
            Map<String, Object> orderResult = jdbcTemplate.queryForMap("SELECT COUNT(OrderMoney) AS total_orders, SUM(OrderMoney) AS total_moneys FROM `order`");
            data.put("total_orders", orderResult.get("total_orders"));
            data.put("total_moneys", orderResult.get("total_moneys"));

            // 获取今日成功订单总条数与总金额
            Map<String, Object> todaySuccessResult = jdbcTemplate.queryForMap("SELECT COUNT(OrderMoney) AS today_amount, SUM(OrderMoney) AS today_total_money FROM `order` WHERE InTime >= CURDATE() AND OrderStatus = 1");
            data.put("today_amount", todaySuccessResult.get("today_amount"));
            data.put("today_total_money", todaySuccessResult.get("today_total_money"));

            // 获取今日失败订单总条数
            Map<String, Object> todayFailResult = jdbcTemplate.queryForMap("SELECT COUNT(OrderMoney) AS today2_amount , SUM(OrderMoney) AS today2_total_money FROM `order` WHERE InTime >= CURDATE() AND OrderStatus = 0");
            data.put("today2_amount", todayFailResult.get("today2_amount"));
            data.put("today2_total_money", todayFailResult.get("today2_total_money"));

            // 获取昨日订单成功总金额与条数
            Map<String, Object> yesterdaySuccessResult = jdbcTemplate.queryForMap("SELECT COUNT(OrderMoney) AS yes_amount, SUM(OrderMoney) AS yesday_total_money FROM `order` WHERE InTime >= CURDATE() - INTERVAL 1 DAY AND InTime < CURDATE() AND OrderStatus = 1");
            data.put("yes_amount", yesterdaySuccessResult.get("yes_amount"));
            data.put("yesday_total_money", yesterdaySuccessResult.get("yesday_total_money"));

            // 获取昨日订单失败总条数
            Map<String, Object> yesterdayFailResult = jdbcTemplate.queryForMap("SELECT COUNT(OrderMoney) AS yes2_amount FROM `order` WHERE InTime >= CURDATE() - INTERVAL 1 DAY AND InTime < CURDATE() AND OrderStatus = 0");
            data.put("yes2_amount", yesterdayFailResult.get("yes2_amount"));

            // 获取今日日租金额
            Double todayTotalDay = jdbcTemplate.queryForObject("SELECT SUM(OrderMoney) AS today_total_day FROM `order` WHERE InTime >= CURDATE() AND OrderStatus = 1 AND Mode = 1", Double.class);
            data.put("today_total_day", todayTotalDay != null ? todayTotalDay : 0);

            // 获取今日时租金额
            Double todayTotalHour = jdbcTemplate.queryForObject("SELECT SUM(OrderMoney) AS today_total_hour FROM `order` WHERE InTime >= CURDATE() AND OrderStatus = 1 AND Mode = 0", Double.class);
            data.put("today_total_hour", todayTotalHour != null ? todayTotalHour : 0);
            // 获取用户总数
            Double totalUser = jdbcTemplate.queryForObject("SELECT COUNT(UserId) AS total_user FROM `user` ", Double.class);
            data.put("total_user", totalUser != null ? totalUser : 0);
            // 获取车辆总数
            Double totalCar = jdbcTemplate.queryForObject("SELECT COUNT(CarId) AS total_car FROM `car` ", Double.class);
            data.put("total_car", totalCar != null ? totalCar : 0);
            // 获取维修总数
            Double totalRepair = jdbcTemplate.queryForObject("SELECT COUNT(RepairId) AS total_repair FROM `repair` ", Double.class);
            data.put("total_repair", totalRepair != null ? totalRepair : 0);

            return new HashMap<String, Object>() {{
                put("status", 1);
                put("message", "获取成功！");
                put("data", data);
            }};
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<String, Object>() {{
                put("status", 200);
                put("message", e.getMessage());
                put("data", null);
            }};
        }
    }
}
