package com.ruoyi.app.report;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.system.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.time.Year;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import jakarta.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/report/statistics")
public class StatisticsController extends BaseController {

    @Autowired
    private StatisticsService statisticsService;

    @Anonymous
    @GetMapping("/index")
    public AjaxResult index(HttpServletRequest httpServletRequest) {
        // 设置时区为UTC+7
        TimeZone utc7 = TimeZone.getTimeZone("GMT+07:00");

        // 今年第一天
        Calendar calendar = Calendar.getInstance(utc7);
        calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date nowYearFirstDay = calendar.getTime();
        // 当前时间
        Date nowDate = new Date();

        // 访问次数
        int accessCount = statisticsService.getAccessCount(Year.now().getValue());
        // 卖家总数量
        int totalNumberSeller = statisticsService.countUsersByUserTypeAndCreatedAt(1, null);
        // 新卖家数量
        int newNumberSeller = statisticsService.countUsersByUserTypeAndCreatedAt(1, nowYearFirstDay);
        // 产品总数
        int totalNumberProduct = statisticsService.countPosFoodByCreatedAt(null);
        // 新产品总数
        int newNumberProduct = statisticsService.countPosFoodByCreatedAt(nowYearFirstDay);
        // 今年总订单数
        int totalNumberOrder = statisticsService.countOrderByNowYear(nowYearFirstDay);
        // 今年成功订单数
        int successNumberOrder = statisticsService.countOrderSuccessByNowYear(nowYearFirstDay);
        // 今年失败订单数
        int failNumberOrder = statisticsService.countOrderFailByNowYear(nowYearFirstDay);
        // 总成交额
        Double totalTurnover = statisticsService.totalTurnoverByNowYear(nowYearFirstDay);

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        SimpleDateFormat sdfymd = new SimpleDateFormat("dd/MM/yyyy");
        sdf.setTimeZone(utc7);
        sdfymd.setTimeZone(utc7);
        String formattedNowYearFirstDay = sdfymd.format(nowYearFirstDay);
        String formattedNowDate = sdf.format(nowDate);

        Map<String, Object> result = new HashMap<>();
        result.put("accessCount", accessCount);
        result.put("nowYearFirstDay", formattedNowYearFirstDay);
        result.put("nowDate", formattedNowDate);
        result.put("totalNumberSeller", totalNumberSeller);
        result.put("newNumberSeller", newNumberSeller);
        result.put("totalNumberProduct", totalNumberProduct);
        result.put("newNumberProduct", newNumberProduct);
        result.put("totalNumberOrder", totalNumberOrder);
        result.put("successNumberOrder", successNumberOrder);
        result.put("failNumberOrder", failNumberOrder);
        result.put("totalTurnover", totalTurnover);

        return AjaxResult.success("Nhận thành công", result);
    }
}
