package com.yuchen.saas.park.service.controller.work;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.dto.work.*;
import com.yuchen.saas.park.api.entity.work.SaasWorkOrder;
import com.yuchen.saas.park.api.vo.work.*;
import com.yuchen.saas.park.service.service.work.ISaasWorkOrderService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 工单主表 前端控制器
 * </p>
 *
 * @author caiwq
 * @since 2024-04-09
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/saasWorkOrder")
public class SaasWorkOrderController {

    private final ISaasWorkOrderService baseService;


    /**
     * 新增工单
     * @param workOrderDto 工单Dto
     * @return TRUE/FALSE
     */
    @PostMapping("/add")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增工单", notes = "新增工单")
    public R<SaasWorkOrder> add(@RequestBody @Validated SaasWorkOrderDto workOrderDto) {
        return R.data(baseService.add(workOrderDto));
    }

    /**
     * 转派
     * @param workOrderDto 转派Dto
     * @return TRUE/FALSE
     */
    @PostMapping("/assign")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "转派", notes = "转派")
    public R<Boolean> assign(@RequestBody @Validated SaasWorkOrderDto workOrderDto) {
        return R.data(baseService.assign(workOrderDto));
    }

    /**
     * 转派
     * @param workOrderAssignDto 转派Dto
     * @return TRUE/FALSE
     */
    @PostMapping("/assignV2")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "转派", notes = "转派")
    public R<Boolean> assignV2(@RequestBody @Validated SaasWorkOrderAssignDto workOrderAssignDto) {
        return R.data(baseService.assignV2(workOrderAssignDto));
    }

    /**
     * 开始处理
     * @param id 主键ID
     * @return TRUE/FALSE
     */
    @GetMapping("/startHandle")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "开始处理", notes = "开始处理")
    public R<Boolean> startHandle(@ApiParam(name = "id", value = "主键ID", required = true) @RequestParam("id") Long id) {
        return R.data(baseService.startHandle(id));
    }

    /**
     * 提交处理
     * @param submitHandleDto 提交处理Dto
     * @return TRUE/FALSE
     */
    @PostMapping("/submitHandle")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "提交处理", notes = "提交处理")
    public R<Boolean> submitHandle(@RequestBody @Validated SaasWorkOrderSubmitHandleDto submitHandleDto) {
        return R.data(baseService.submitHandle(submitHandleDto));
    }

    /**
     * 回单
     * @param workOrderReceiptDto 回单Dto
     * @return TRUE/FALSE
     */
    @PostMapping("/receipt")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "回单", notes = "回单")
    public R<Boolean> receipt(@RequestBody @Validated SaasWorkOrderReceiptDto workOrderReceiptDto) {
        return R.data(baseService.receipt(workOrderReceiptDto));
    }

    /**
     * 确认
     * @param workOrderConfirmDto 确认Dto
     * @return TRUE/FALSE
     */
    @PostMapping("/confirm")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "确认", notes = "确认")
    public R<Boolean> confirm(@RequestBody @Validated SaasWorkOrderConfirmDto workOrderConfirmDto) {
        return R.data(baseService.confirm(workOrderConfirmDto));
    }

    /**
     * 关闭
     * @param workOrderCloseDto 关闭Dto
     * @return TRUE/FALSE
     */
    @PostMapping("/close")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "关闭", notes = "关闭")
    public R<Boolean> close(@RequestBody @Validated SaasWorkOrderCloseDto workOrderCloseDto) {
        return R.data(baseService.close(workOrderCloseDto));
    }

    /**
     * 工单详情
     * @param id 消息ID
     * @return 消息详情Vo
     */
    @GetMapping("/getDetail/{id}")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "工单详情", notes = "工单详情")
    public R<SaasWorkOrderVo> getDetail(@ApiParam(name = "id", value = "主键ID", required = true) @PathVariable("id") Long id) {
        return R.data(baseService.getDetail(id));
    }

    /**
     * 工单列表数量统计
     * @return 工单列表数量统计Vo
     */
    @GetMapping("/statisticsAuditCount")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "工单列表数量统计", notes = "工单列表数量统计")
    public R<SaasWorkOrderStatisticsVo> statisticsAuditCount(@ApiParam(name = "workOrderCategory", value = "工单大类(1:报事报修 2:设备报修 3:告警运维)", required = true) @RequestParam(required = true) Integer workOrderCategory,
                                                             @ApiParam(name = "dispatchUserId", value = "指派人ID", required = false) @RequestParam(required = false) Long dispatchUserId) {
        return R.data(baseService.statisticsAuditCount(workOrderCategory, dispatchUserId));
    }

    /**
     * 报事报修工单列表(分页)
     * @param queryDto 报事报修工单列表查询参数
     * @return 报事报修工单列表Vo
     */
    @PostMapping("/getMatterRepairList")
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "报事报修工单列表(分页)", notes = "报事报修工单列表(分页)")
    public R<NestPage<SaasWorkOrderMatterRepairListVo>> getMatterRepairList(@RequestBody SaasWorkOrderMatterRepairQueryDto queryDto) {
        return R.data(baseService.getMatterRepairList(queryDto));
    }

    /**
     * 我的报事报修工单列表(分页)
     * @param queryDto 报事报修工单列表查询参数
     * @return 报事报修工单列表Vo
     */
    @PostMapping("/getMyMatterRepairList")
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "我的报事报修工单列表(分页)", notes = "我的报事报修工单列表(分页)")
    public R<NestPage<SaasWorkOrderMatterRepairListVo>> getMyMatterRepairList(@RequestBody SaasWorkOrderMatterRepairQueryDto queryDto) {
        queryDto.setDispatchUserId(AuthUtil.getUser().getOrgUserId());
        return R.data(baseService.getMatterRepairList(queryDto));
    }

    /**
     * 导出报事报修工单
     * @return page 分页数据
     */
    @PostMapping("/exportMatterRepair")
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "导出报事报修工单", notes = "导出报事报修工单")
    public void exportMatterRepair(@RequestBody SaasWorkOrderMatterRepairQueryDto queryDto, HttpServletResponse response) {
        baseService.exportMatterRepair(queryDto, response);
    }

    /**
     * 设备报修工单列表(分页)
     * @param queryDto 设备报修工单列表查询参数
     * @return 报事报修工单列表Vo
     */
    @PostMapping("/getDeviceRepairList")
    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "设备报修工单列表(分页)", notes = "设备报修工单列表(分页)")
    public R<NestPage<SaasWorkOrderDeviceRepairListVo>> getDeviceRepairList(@RequestBody SaasWorkOrderDeviceRepairQueryDto queryDto) {
        return R.data(baseService.getDeviceRepairList(queryDto));
    }

    /**
     * 我的设备报修工单列表(分页)
     * @param queryDto 设备报修工单列表查询参数
     * @return 报事报修工单列表Vo
     */
    @PostMapping("/getMyDeviceRepairList")
    @ApiOperationSupport(order = 15)
    @ApiOperation(value = "我的设备报修工单列表(分页)", notes = "我的设备报修工单列表(分页)")
    public R<NestPage<SaasWorkOrderDeviceRepairListVo>> getMyDeviceRepairList(@RequestBody SaasWorkOrderDeviceRepairQueryDto queryDto) {
        queryDto.setDispatchUserId(AuthUtil.getUser().getOrgUserId());
        return R.data(baseService.getDeviceRepairList(queryDto));
    }

    /**
     * 导出设备报修工单
     * @return page 分页数据
     */
    @PostMapping("/exportDeviceRepair")
    @ApiOperationSupport(order = 16)
    @ApiOperation(value = "导出设备报修工单", notes = "导出设备报修工单")
    public void exportDeviceRepair(@RequestBody SaasWorkOrderDeviceRepairQueryDto queryDto, HttpServletResponse response) {
        baseService.exportDeviceRepair(queryDto, response);
    }

    /**
     * 告警运维工单列表(分页)
     * @param queryDto 报事报修工单列表查询参数
     * @return 报事报修工单列表Vo
     */
    @PostMapping("/getAlarmOperationList")
    @ApiOperationSupport(order = 17)
    @ApiOperation(value = "告警运维工单列表(分页)", notes = "告警运维工单列表(分页)")
    public R<NestPage<SaasWorkOrderAlarmOperationListVo>> getAlarmOperationList(@RequestBody SaasWorkOrderAlarmOperationQueryDto queryDto) {
        return R.data(baseService.getAlarmOperationList(queryDto));
    }

    /**
     * 我的告警运维工单列表(分页)
     * @param queryDto 报事报修工单列表查询参数
     * @return 报事报修工单列表Vo
     */
    @PostMapping("/getMyAlarmOperationList")
    @ApiOperationSupport(order = 18)
    @ApiOperation(value = "我的告警运维工单列表(分页)", notes = "我的告警运维工单列表(分页)")
    public R<NestPage<SaasWorkOrderAlarmOperationListVo>> getMyAlarmOperationList(@RequestBody SaasWorkOrderAlarmOperationQueryDto queryDto) {
        queryDto.setDispatchUserId(AuthUtil.getUser().getOrgUserId());
        return R.data(baseService.getAlarmOperationList(queryDto));
    }

    /**
     * 导出告警运维工单
     * @return page 分页数据
     */
    @PostMapping("/exportAlarmOperation")
    @ApiOperationSupport(order = 19)
    @ApiOperation(value = "导出告警运维工单", notes = "导出告警运维工单")
    public void exportAlarmOperation(@RequestBody SaasWorkOrderAlarmOperationQueryDto queryDto, HttpServletResponse response) {
        baseService.exportAlarmOperation(queryDto, response);
    }

}
