package com.ruoyi.api.controller.info;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.api.controller.common.AbstractApiController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.FilePathSplicingType;
import com.ruoyi.common.filepathsplicing.FilePathSplicing;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.date.LocalDateTimeUtils;
import com.ruoyi.info.management.domain.TeacherDepartmentRel;
import com.ruoyi.info.management.domain.vo.ManagementTeacherBenchVo;
import com.ruoyi.info.management.domain.vo.ManagementTeacherVo;
import com.ruoyi.info.management.service.IManagementTeacherService;
import com.ruoyi.info.management.service.ITeacherDepartmentRelService;
import com.ruoyi.info.nursing.domain.bo.NursingWorkerBo;
import com.ruoyi.info.nursing.domain.vo.NursingWorkerVo;
import com.ruoyi.info.order.domain.bo.OrderCountBo;
import com.ruoyi.info.order.domain.bo.OrderRefundCountBo;
import com.ruoyi.info.order.enums.OrderStatus;
import com.ruoyi.info.order.enums.RefundStatus;
import com.ruoyi.info.order.service.IOrderRefundService;
import com.ruoyi.info.order.service.IOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * @author fucheng.bai
 * @date 2025/10/9
 */
@Validated
@Api(value = "API 管理老师控制器", tags = {"管理老师管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/info/managementTeacher")
public class ApiManagementTeacherController extends AbstractApiController {

    private final IManagementTeacherService managementTeacherService;

    private final ITeacherDepartmentRelService teacherDepartmentRelService;

    private final IOrderService orderService;

    private final IOrderRefundService orderRefundService;

    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("管理老师工作台信息")
    @GetMapping("/workbench")
    public R<ManagementTeacherBenchVo> workbench() {
        Long teacherId = getTeacherId();

        ManagementTeacherBenchVo managementTeacherBenchVo = new ManagementTeacherBenchVo();

        // 获取基本信息
        ManagementTeacherVo managementTeacherVo = managementTeacherService.queryById(teacherId);
        BeanCopyUtils.copy(managementTeacherVo, managementTeacherBenchVo);

        // 获取管理老师管理科室列表
        List<TeacherDepartmentRel> teacherDepartmentRelList = teacherDepartmentRelService.queryByTeacherId(teacherId);
        Set<Long> departmentIds = CollectionUtils.convertSet(teacherDepartmentRelList, TeacherDepartmentRel::getDepartmentId);

        // 订单总数
        Integer orderCount = orderService.queryOrderCount(new OrderCountBo(departmentIds));

        // 今日订单数
        Date todayBegin = DateUtils.toDate(LocalDateTimeUtils.getToday());
        Date todayEnd = LocalDateTimeUtils.getEndOfDate(todayBegin);
        Integer todayCount =  orderService.queryOrderCount(new OrderCountBo(departmentIds, todayBegin, todayEnd));

        // 待确认数量
        Integer waitConfirmedCount = orderService.queryOrderCount(new OrderCountBo(departmentIds, OrderStatus.wait_confirmed.getCode()));

        // 退款售后数量，这里只查退款中的数量
        Integer waitRefundCount = orderRefundService.queryOrderRefundCount(new OrderRefundCountBo(departmentIds, RefundStatus.wait.getCode()));

        managementTeacherBenchVo.setOrderCount(orderCount);
        managementTeacherBenchVo.setTodayCount(todayCount);
        managementTeacherBenchVo.setWaitConfirmedCount(waitConfirmedCount);
        managementTeacherBenchVo.setWaitRefundCount(waitRefundCount);
        return R.ok(managementTeacherBenchVo);
    }



}
