package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.common.config.LoginContextManager;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.provider.service.IServiceOrderService;
import com.fs.provider.vo.ServiceOrderStatisticsReqVO;
import com.fs.provider.vo.ServiceOrderStatisticsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务单统计 Controller
 *
 * @author fs
 */
@Slf4j
@Api(tags = "服务单统计")
@RestController
@RequestMapping("/app/statistics/serviceOrder")
@RequiredArgsConstructor
public class ServiceOrderStatisticsController extends BaseController {

    private final IServiceOrderService serviceOrderService;

    /**
     * 服务单统计汇总（按部门分组）
     *
     * 统计字段：
     * - 归属部门
     * - 服务单数
     * - 任务数
     * - 客服数
     * - 医生数
     * - 总金额
     */
    @Login
    @ApiOperation("服务单统计汇总")
    @GetMapping("/summary")
    public AjaxResult getStatisticsSummary(ServiceOrderStatisticsReqVO reqVO) {
        // 获取当前登录用户的公司ID
        Long companyId = LoginContextManager.getCurrentCompanyId();
        reqVO.setCompanyId(companyId);

        log.info("服务单统计汇总查询, companyId={}, reqVO={}", companyId, reqVO);

        List<ServiceOrderStatisticsVO> statistics = serviceOrderService.getServiceOrderStatistics(reqVO);

        return AjaxResult.success(statistics);
    }

    @Login
    @ApiOperation("服务单统计汇总-服务单数量")
    @GetMapping("/summaryTotal")
    public AjaxResult getStatisticsSummaryTotal(ServiceOrderStatisticsReqVO reqVO) {
        // 获取当前登录用户的公司ID
        Long companyId = LoginContextManager.getCurrentCompanyId();
        reqVO.setCompanyId(companyId);

        log.info("服务单统计汇总-服务单数量, companyId={}, reqVO={}", companyId, reqVO);

        Long serviceOrderStatCount = serviceOrderService.selectServiceOrderStatCount(reqVO);

        return AjaxResult.success(serviceOrderStatCount);
    }

    /**
     * 服务单统计汇总（指定部门）
     */
    @Login
    @ApiOperation("指定部门统计")
    @GetMapping("/summary/{deptId}")
    public AjaxResult getStatisticsByDept(@PathVariable Long deptId, ServiceOrderStatisticsReqVO reqVO) {
        // 获取当前登录用户的公司ID
        Long companyId = LoginContextManager.getCurrentCompanyId();
        reqVO.setCompanyId(companyId);
        reqVO.setDeptId(deptId);

        log.info("指定部门统计查询, companyId={}, deptId={}, reqVO={}", companyId, deptId, reqVO);

        List<ServiceOrderStatisticsVO> statistics = serviceOrderService.getServiceOrderStatistics(reqVO);

        // 返回单个结果或空
        if (statistics != null && !statistics.isEmpty()) {
            return AjaxResult.success(statistics.get(0));
        }

        return AjaxResult.success(null);
    }
}
