package com.ruoyi.api.controller.info;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.api.controller.common.AbstractApiController;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.FilePathSplicingType;
import com.ruoyi.common.filepathsplicing.FilePathSplicing;
import com.ruoyi.info.management.domain.bo.TeacherDepartmentRelBo;
import com.ruoyi.info.management.domain.vo.TeacherDepartmentRelVo;
import com.ruoyi.info.management.service.ITeacherDepartmentRelService;
import com.ruoyi.info.order.domain.bo.OrderBo;
import com.ruoyi.info.order.domain.vo.OrderVo;
import com.ruoyi.info.order.enums.CancellationMethod;
import com.ruoyi.info.order.enums.OrderStatus;
import com.ruoyi.info.order.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2025-09-20
 */
@Validated
@Api(value = "管理老师端订单控制器", tags = {"管理老师端订单管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/teacher/order")
public class ApiTeacherOrderController extends AbstractApiController {

    private final IOrderService iOrderService;
    private final ITeacherDepartmentRelService teacherDepartmentRelService;

    /**
     * 分页查询订单列表
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("查询订单列表")
    @GetMapping("/page")
    public TableDataInfo<OrderVo> page(@Validated(QueryGroup.class) OrderBo bo, PageQuery pageQuery) {
        //获取老师科室
        TeacherDepartmentRelBo teacherDepartmentRelBo = new TeacherDepartmentRelBo();
        teacherDepartmentRelBo.setTeacherId(getTeacherId());
        List<TeacherDepartmentRelVo> teacherDepartmentRelVos = teacherDepartmentRelService.queryList(teacherDepartmentRelBo);
        if (ObjectUtil.isEmpty(teacherDepartmentRelVos)) {
            return TableDataInfo.build();
        }
        bo.setHospitalDepartmentIds(teacherDepartmentRelVos.stream().map(TeacherDepartmentRelVo::getDepartmentId).collect(Collectors.toList()));
        return iOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取订单详细信息
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("获取订单详细信息")
    @GetMapping("/info/{id}")
    public R<OrderVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.ok(iOrderService.queryById(id));
    }

    @ApiOperation("取消订单")
    @RepeatSubmit()
    @PostMapping("/cancel/{id}")
    public R<Void> cancel(@ApiParam("主键")
                              @NotNull(message = "主键不能为空")
                              @PathVariable("id") Long id) {
        return toAjax(iOrderService.cancel(id, null, CancellationMethod.teacher_canceled) ? 1 : 0);
    }

    @ApiOperation("分配护工")
    @RepeatSubmit()
    @PostMapping("/assignWorker")
    public R<Void> assignWorker(@RequestBody OrderBo bo) {
        return toAjax(iOrderService.assignWorker(bo, getTeacherId()) ? 1 : 0);
    }

    @ApiOperation("获取护工电话")
    @GetMapping("/getWorkerPhone/{id}")
    public R<String> getWorkerPhone(@ApiParam("主键")
                                        @NotNull(message = "主键不能为空")
                                        @PathVariable("id") Long id) {
        return R.ok(iOrderService.getWorkerPhone(id, getTeacherId()));
    }
}
