package com.example.controller;

import com.example.common.Result;
import com.example.common.config.TokenUtils;
import com.example.entity.Account;
import com.example.service.SymptomRecordService;
import jakarta.annotation.Resource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * 症状记录统计控制器
 */
@RestController
@RequestMapping("/symptomStatistics")
public class SymptomStatisticsController {

    @Resource
    private SymptomRecordService symptomRecordService;
    
    /**
     * 获取症状记录统计数据
     */
    @GetMapping
    public Result getStatistics(
            @RequestParam(required = false) Integer patientId,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        
        // 获取当前登录用户
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null) {
            return Result.error("401", "请先登录");
        }
        
        Map<String, Object> result;
        
        // 根据用户角色获取不同的统计数据
        switch (currentUser.getRole()) {
            case "admin":
                // 管理员可以查看所有数据或指定患者的数据
                result = symptomRecordService.getStatisticsData(patientId, startDate, endDate);
                break;
            case "doctor":
                // 如果请求包含patientId且该患者属于当前医生，则返回该患者的数据
                // 否则返回该医生的所有患者数据
                if (patientId != null) {
                    result = symptomRecordService.getStatisticsData(patientId, startDate, endDate);
                } else {
                    result = symptomRecordService.getStatisticsByDoctor(currentUser.getId(), startDate, endDate);
                }
                break;
            case "patient":
                // 患者只能查看自己的数据
                result = symptomRecordService.getStatisticsData(currentUser.getId(), startDate, endDate);
                break;
            default:
                return Result.error("403", "无权限访问");
        }
        
        return Result.success(result);
    }
} 