CompanyUsageStatController.java 4.5 KB
// statistics/controller/CompanyUsageStatController.java
package com.aigeo.statistics.controller;

import com.aigeo.statistics.entity.CompanyUsageStat;
import com.aigeo.statistics.service.CompanyUsageStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/company-usage-stats")
public class CompanyUsageStatController {
    
    @Autowired
    private CompanyUsageStatService companyUsageStatService;
    
    @GetMapping
    public List<CompanyUsageStat> getAllUsageStats() {
        return companyUsageStatService.getAllUsageStats();
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<CompanyUsageStat> getUsageStatById(@PathVariable Integer id) {
        Optional<CompanyUsageStat> usageStat = companyUsageStatService.getUsageStatById(id);
        return usageStat.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    @GetMapping("/company/{companyId}")
    public List<CompanyUsageStat> getUsageStatsByCompanyId(@PathVariable Integer companyId) {
        return companyUsageStatService.getUsageStatsByCompanyId(companyId);
    }
    
    @GetMapping("/company/{companyId}/date/{statDate}")
    public ResponseEntity<CompanyUsageStat> getUsageStatByCompanyIdAndStatDate(
            @PathVariable Integer companyId,
            @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") Date statDate) {
        Optional<CompanyUsageStat> usageStat = companyUsageStatService.getUsageStatByCompanyIdAndStatDate(companyId, statDate);
        return usageStat.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    @GetMapping("/company/{companyId}/date-range")
    public List<CompanyUsageStat> getUsageStatsByCompanyIdAndDateRange(
            @PathVariable Integer companyId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        // Convert Date to LocalDate for the first parameter
        java.time.LocalDate localStartDate = startDate.toInstant()
                .atZone(java.time.ZoneId.systemDefault())
                .toLocalDate();
        return companyUsageStatService.getUsageStatsByCompanyIdAndDateRange(companyId, localStartDate, endDate);
    }
    
    @GetMapping("/company/{companyId}/total-article-count")
    public Long getTotalArticleCountByCompanyId(@PathVariable Integer companyId) {
        return companyUsageStatService.getTotalArticleCountByCompanyId(companyId);
    }
    
    @GetMapping("/company/{companyId}/total-ai-token-usage")
    public Long getTotalAiTokenUsageByCompanyId(@PathVariable Integer companyId) {
        return companyUsageStatService.getTotalAiTokenUsageByCompanyId(companyId);
    }
    
    @GetMapping("/company/{companyId}/total-website-count")
    public Long getTotalWebsiteCountByCompanyId(@PathVariable Integer companyId) {
        return companyUsageStatService.getTotalWebsiteCountByCompanyId(companyId);
    }
    
    @PostMapping
    public CompanyUsageStat createUsageStat(@RequestBody CompanyUsageStat usageStat) {
        return companyUsageStatService.saveUsageStat(usageStat);
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<CompanyUsageStat> updateUsageStat(@PathVariable Integer id, 
                                                         @RequestBody CompanyUsageStat usageStatDetails) {
        Optional<CompanyUsageStat> usageStat = companyUsageStatService.getUsageStatById(id);
        if (usageStat.isPresent()) {
            CompanyUsageStat updatedUsageStat = usageStat.get();
            updatedUsageStat.setArticleCount(usageStatDetails.getArticleCount());
            updatedUsageStat.setAiTokenUsage(usageStatDetails.getAiTokenUsage());
            updatedUsageStat.setWebsiteCount(usageStatDetails.getWebsiteCount());
            
            CompanyUsageStat savedUsageStat = companyUsageStatService.saveUsageStat(updatedUsageStat);
            return ResponseEntity.ok(savedUsageStat);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUsageStat(@PathVariable Integer id) {
        companyUsageStatService.deleteUsageStat(id);
        return ResponseEntity.noContent().build();
    }
}