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.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.OrderRefundBo;
import com.ruoyi.info.order.domain.vo.OrderRefundVo;
import com.ruoyi.info.order.enums.OrderOperatorType;
import com.ruoyi.info.order.service.IOrderRefundService;
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.List;
import java.util.stream.Collectors;

/**
 * 售后订单Controller
 *
 * @author ruoyi
 * @date 2025-09-29
 */
@Validated
@Api(value = "售后订单控制器", tags = {"售后订单管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/teacher/orderRefund")
public class ApiTeacherOrderRefundController extends AbstractApiController {

    private final IOrderRefundService iOrderRefundService;
    private final ITeacherDepartmentRelService teacherDepartmentRelService;

    /**
     * 分页查询售后订单列表
     */
    @ApiOperation("查询售后订单列表")
    @GetMapping("/page")
    public TableDataInfo<OrderRefundVo> page(@Validated(QueryGroup.class) OrderRefundBo 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 iOrderRefundService.queryPageList(bo, pageQuery);
    }

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

    /**
     * 同意或拒绝售后订单
     */
    @ApiOperation("同意或拒绝售后订单")
    @RepeatSubmit()
    @PostMapping("/agreeOrRefuse")
    public R<Void> agreeOrRefuse(@RequestBody OrderRefundBo bo) {
        bo.setOperatorId(getTeacherId());
        bo.setOperatorType(OrderOperatorType.teacher.getCode());
        return toAjax(iOrderRefundService.agreeOrRefuse(bo) ? 1 : 0);
    }

}
