package com.ruoyi.project.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.project.domain.DeptProofreadStatsVo;
import com.ruoyi.project.domain.TrsResultsStatsSimpleVo;
import com.ruoyi.project.service.ITrsResultsStatsSimpleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 审校统计Controller
 */
@Controller
@RequestMapping("/project/stats")
public class TrsResultsStatsSimpleController extends BaseController {

    private String prefix = "project/stats";

    @Autowired
    private ITrsResultsStatsSimpleService trsResultsStatsSimpleService;

    /**
     * 统计页面
     */
    @GetMapping()
    public String stats() {
        return prefix + "/stats";
    }

    /**
     * 获取统计数据
     */
    @PostMapping("/getStats")
    @ResponseBody
    public AjaxResult getStats(String beginTime, String endTime) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date begin = beginTime != null && !beginTime.isEmpty() ? sdf.parse(beginTime) : null;
            Date end = endTime != null && !endTime.isEmpty() ? sdf.parse(endTime) : null;

            TrsResultsStatsSimpleVo stats = trsResultsStatsSimpleService.getStats(begin, end);
            return AjaxResult.success(stats);
        } catch (ParseException e) {
            return AjaxResult.error("日期格式错误");
        }
    }

    /**
     * 获取最近30天统计数据
     */
    @PostMapping("/getLast30DaysStats")
    @ResponseBody
    public AjaxResult getLast30DaysStats() {
        TrsResultsStatsSimpleVo stats = trsResultsStatsSimpleService.getLast30DaysStats();
        return AjaxResult.success(stats);
    }

    /**
     * 获取部门活跃用户详情
     */
    @PostMapping("/getDeptUsers")
    @ResponseBody
    public AjaxResult getDeptUsers(Long deptId, String beginTime, String endTime) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date begin = beginTime != null && !beginTime.isEmpty() ? sdf.parse(beginTime) : null;
            Date end = endTime != null && !endTime.isEmpty() ? sdf.parse(endTime) : null;

            List<TrsResultsStatsSimpleVo.UserActiveVo> list = trsResultsStatsSimpleService.getDeptUsers(deptId, begin, end);
            return AjaxResult.success(list);
        } catch (ParseException e) {
            return AjaxResult.error("日期格式错误");
        }
    }

    /**
     * 获取部门校对次数统计
     */
    @PostMapping("/getDeptProofreadStats")
    @ResponseBody
    public AjaxResult getDeptProofreadStats(String beginTime, String endTime) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date begin = beginTime != null && !beginTime.isEmpty() ? sdf.parse(beginTime) : null;
            Date end = endTime != null && !endTime.isEmpty() ? sdf.parse(endTime) : null;

            List<DeptProofreadStatsVo> list = trsResultsStatsSimpleService.getDeptProofreadStats(begin, end);
            return AjaxResult.success(list);
        } catch (ParseException e) {
            return AjaxResult.error("日期格式错误");
        }
    }
}