package com.zksy.web.controller.manhole;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.domain.*;
import com.zksy.base.service.*;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.core.controller.BaseController;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.core.domain.AjaxResult2;
import com.zksy.manhole.dto.in.WorkOrderAddInDTO;
import com.zksy.manhole.dto.in.WorkOrderFinishedInDTO;
import com.zksy.manhole.dto.in.WorkOrderOperInDTO;
import com.zksy.manhole.dto.in.WorkOrderPageInDTO;
import com.zksy.manhole.dto.in.WorkOrderProcessingInDTO;
import com.zksy.manhole.dto.out.EquipmentStatusOutDTO;
import com.zksy.manhole.dto.out.WorkOrderLogOutDTO;
import com.zksy.manhole.dto.out.WorkOrderOutDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 窨井盖监测系统-工单处置管理Controller
 * 
 * @author xianggx
 * @date 2026-05-06
 */
@RestController
@RequestMapping("/manhole/workOrder")
@Api(tags = "窨井盖监测系统-工单处置管理")
public class WorkOrderController extends BaseController
{
    @Autowired
    private WorkOrderService workOrderService;

    @Autowired
    private WorkOrderLogService workOrderLogService;

    @Autowired
    private EquipmentBaseService equipmentBaseService;

    @Autowired
    private EquipmentStatusService equipmentStatusService;

    @Autowired
    private EquipmentTypeService equipmentTypeService;

    /**
     * 分页查询工单信息列表
     */
    @Anonymous
    @PostMapping(value = "/findByPage")
    @ApiOperation(value = "分页查询工单信息列表")
    public AjaxResult2<Page<WorkOrderOutDTO>> findByPage(@RequestBody WorkOrderPageInDTO pageInDTO)
    {
        return AjaxResult2.success(workOrderService.findByPage(pageInDTO));
    }

    /**
     * 获取工单信息详情
     */
    @Anonymous
    @GetMapping(value = "/getById/{id}")
    @ApiOperation(value = "获取工单信息详情")
    public AjaxResult2<WorkOrderOutDTO> getById(@PathVariable("id") Long id)
    {
        WorkOrder workOrder = workOrderService.getById(id);
        if(workOrder == null){
            return AjaxResult2.error("工单ID不存在");
        }
        WorkOrderOutDTO outDTO = BeanUtil.copyProperties(workOrder, WorkOrderOutDTO.class);
        // 查询设备状态
        EquipmentStatus equipmentStatus = equipmentStatusService.lambdaQuery()
                .eq(EquipmentStatus::getEquipmentId, outDTO.getDeviceId())
                .one();
        if(equipmentStatus != null){
            EquipmentStatusOutDTO equipmentStatusOutDTO = BeanUtil.copyProperties(equipmentStatus, EquipmentStatusOutDTO.class);
            outDTO.setEquipmentStatus(equipmentStatusOutDTO);
        }
        // 查询设备类别名称
        EquipmentBase equipmentBase = equipmentBaseService.getById(outDTO.getDeviceId());
        if(equipmentBase != null){
            outDTO.setDeviceCode(equipmentBase.getEquipmentCode());
            EquipmentType equipmentType = equipmentTypeService.getById(equipmentBase.getEquipmentTypeId());
            if(equipmentType != null){
                outDTO.setEquipmentTypeName(equipmentType.getTypeName());
            }
        }
        // 查询工单操作日志
        List<WorkOrderLog> workOrderLogList = workOrderLogService.list(new LambdaQueryWrapper<WorkOrderLog>()
                .eq(WorkOrderLog::getOrderId, outDTO.getOrderId())
                .orderByAsc(WorkOrderLog::getOperTime));
        if(CollUtil.isNotEmpty(workOrderLogList)){
            List<WorkOrderLogOutDTO> workOrderLogOutDTOList = BeanUtil.copyToList(workOrderLogList, WorkOrderLogOutDTO.class);
            outDTO.setWorkOrderLogList(workOrderLogOutDTOList);
        }
        return AjaxResult2.success(outDTO);
    }

    /**
     * 经办工单分页查询（处理中：待接单、已接单、处理中、待验收）
     */
    @Anonymous
    @PostMapping(value = "/findProcessingPage")
    @ApiOperation(value = "经办工单分页查询")
    public AjaxResult2<Page<WorkOrderOutDTO>> findProcessingPage(@RequestBody WorkOrderProcessingInDTO inDTO) {
        return AjaxResult2.success(workOrderService.findProcessingPage(inDTO));
    }

    /**
     * 办结工单分页查询（已办结：已结案、已驳回）
     */
    @Anonymous
    @PostMapping(value = "/findFinishedPage")
    @ApiOperation(value = "办结工单分页查询")
    public AjaxResult2<Page<WorkOrderOutDTO>> findFinishedPage(@RequestBody WorkOrderFinishedInDTO inDTO) {
        return AjaxResult2.success(workOrderService.findFinishedPage(inDTO));
    }

    /**
     * 新增工单
     */
    @Anonymous
    @PostMapping(value = "/addWorkOrder")
    @ApiOperation(value = "新增工单")
    public AjaxResult addWorkOrder(@RequestBody WorkOrderAddInDTO inDTO) {
        boolean result = workOrderService.addWorkOrder(inDTO);
        return result ? AjaxResult.success("新增成功", result)
                : AjaxResult.error("新增失败", result);
    }

    /**
     * 操作工单
     * @param inDTO
     * @return
     */
    @Anonymous
    @PostMapping("/operWorkOrder")
    @ApiOperation(value = "操作工单")
    public AjaxResult operWorkOrder(@RequestBody WorkOrderOperInDTO inDTO) {
        boolean result = workOrderService.operWorkOrder(inDTO);
        return result ? AjaxResult.success("操作成功",result)
                : AjaxResult.error("操作失败",result);
    }
}
