package com.ygtx.gxt.controller;

import com.ygtx.common.annotation.Log;
import com.ygtx.common.core.controller.BaseController;
import com.ygtx.common.core.domain.AjaxResult;
import com.ygtx.common.core.page.TableDataInfo;
import com.ygtx.common.enums.BusinessType;
import com.ygtx.common.utils.SecurityUtils;
import com.ygtx.gxt.domain.GxtRepairOrder;
import com.ygtx.gxt.domain.GxtWorkOrder;
import com.ygtx.gxt.domain.OrderScoreInfo;
import com.ygtx.gxt.service.IGxtOrderMineService;
import com.ygtx.gxt.service.IGxtOrderScoreService;
import com.ygtx.gxt.service.IGxtRepairOrderService;
import com.ygtx.gxt.service.IGxtWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

/**
 * 我的工单Controller
 * 
 * @author ouyj
 * @date 2025-11-27
 */
@RestController
@RequestMapping("/gxt/orderMine")
@Api(tags = "我的工单管理")
public class GxtOrderMineController extends BaseController
{
    @Autowired
    private IGxtRepairOrderService gxtRepairOrderService;

    @Autowired
    private IGxtWorkOrderService gxtWorkOrderService;
    
    @Autowired
    private IGxtOrderMineService gxtOrderMineService;

    /**
     * 查询我创建的工单列表（合并维修工单和维保工单）
     */
    @ApiOperation("查询我创建的工单列表（合并维修工单和维保工单）")
    @GetMapping("/listMyCreate")
    public TableDataInfo listMyCreate(GxtRepairOrder repairOrder, GxtWorkOrder workOrder)
    {
        // 开启分页插件自动分页
        startPage();
        // 查询合并的工单列表
        List<OrderScoreInfo> list = gxtOrderMineService.selectUnionOrderListMyCreate(repairOrder, workOrder);
        return getDataTable(list);
    }

    /**
     * 查询我处理的工单列表（合并维修工单和维保工单）
     */
    @ApiOperation("查询我处理的工单列表（合并维修工单和维保工单）")
    @GetMapping("/listMyDone")
    public TableDataInfo listMyDone(GxtRepairOrder repairOrder, GxtWorkOrder workOrder)
    {
        // 开启分页插件自动分页
        startPage();
        // 查询合并的工单列表
        List<OrderScoreInfo> list = gxtOrderMineService.selectUnionOrderListMyDone(repairOrder, workOrder);
        return getDataTable(list);
    }

    /**
     * 查询我相关的工单列表（合并维修工单和维保工单）
     */
    @ApiOperation("查询我相关的工单列表（合并维修工单和维保工单）")
    @GetMapping("/listMyRelate")
    public TableDataInfo listMyRelate(GxtRepairOrder repairOrder, GxtWorkOrder workOrder)
    {
        // 开启分页插件自动分页
        startPage();
        // 查询合并的工单列表
        List<OrderScoreInfo> list = gxtOrderMineService.selectUnionOrderListMyRelate(repairOrder, workOrder);
        return getDataTable(list);
    }

    /**
     * 查询我的待办工单列表（合并维修工单和维保工单）
     */
    @ApiOperation("查询我的待办工单列表（合并维修工单和维保工单）")
    @GetMapping("/listMyTodo")
    public TableDataInfo listMyTodo(GxtRepairOrder repairOrder, GxtWorkOrder workOrder)
    {
        // 开启分页插件自动分页
        startPage();
        // 查询合并的工单列表
        List<OrderScoreInfo> list = gxtOrderMineService.selectUnionOrderListMyTodo(repairOrder, workOrder, null);
        return getDataTable(list);
    }

    /**
     * 查询我的待评分工单列表（合并维修工单和维保工单）
     */
    @ApiOperation("查询我的待评分工单列表（合并维修工单和维保工单）")
    @GetMapping("/listMyRate")
    public TableDataInfo listMyRate(GxtRepairOrder repairOrder, GxtWorkOrder workOrder)
    {
        // 开启分页插件自动分页
        startPage();
        // 查询合并的工单列表
        List<OrderScoreInfo> list = gxtOrderMineService.selectUnionOrderListMyRate(repairOrder, workOrder);
        return getDataTable(list);
    }

    /**
     * 获取工单详细信息
     */
    @ApiOperation("获取工单详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "orderType", value = "工单类型", required = true, dataType = "Integer", paramType = "path", dataTypeClass = Integer.class),
        @ApiImplicitParam(name = "orderId", value = "工单ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    })
    @GetMapping(value = "/{orderType}/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderType") Integer orderType, @PathVariable("orderId") Long orderId)
    {
        if (orderType == 1) {
            // 维修工单
            GxtRepairOrder order = gxtRepairOrderService.selectGxtRepairOrderById(orderId);
            return success(order);
        } else if (orderType == 2) {
            // 维保工单
            GxtWorkOrder order = gxtWorkOrderService.selectGxtWorkOrderById(orderId);
            return success(order);
        }
        return AjaxResult.error("无效的工单类型");
    }
}