package com.aegis.web.controller.emergency.duty;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.aegis.common.annotation.Log;
import com.aegis.common.core.controller.BaseController;
import com.aegis.common.core.domain.AjaxResult;
import com.aegis.common.core.page.TableDataInfo;
import com.aegis.common.enums.BusinessType;
import com.aegis.emergency.alert.domain.DutyAlertTransfer;
import com.aegis.emergency.alert.dto.TransferApproveRequest;
import com.aegis.emergency.alert.dto.TransferRejectRequest;
import com.aegis.emergency.alert.dto.TransferFeedbackRequest;
import com.aegis.emergency.alert.dto.TransferProcessRequest;
import com.aegis.emergency.alert.service.DutyAlertTransferService;

/**
 * 转警管理Controller
 * 
 * @author aegis
 */
@RestController
@RequestMapping("/duty/transfers")
public class DutyAlertTransferController extends BaseController {

    @Autowired
    private DutyAlertTransferService dutyAlertTransferService;

    /**
     * 查询转警列表（支持分页）
     */
    @PreAuthorize("@ss.hasPermi('duty:transfer:list')")
    @GetMapping("/list")
    public TableDataInfo list(DutyAlertTransfer condition) {
        startPage();
        List<DutyAlertTransfer> list = dutyAlertTransferService.listByCondition(condition);
        return getDataTable(list);
    }

    /**
     * 根据ID查询转警详情
     */
    @PreAuthorize("@ss.hasPermi('duty:transfer:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        DutyAlertTransfer transfer = dutyAlertTransferService.getById(id);
        if (transfer == null) {
            return error("转警记录不存在或已删除");
        }
        return success(transfer);
    }

    /**
     * 根据警情ID查询转警记录
     */
    @PreAuthorize("@ss.hasPermi('duty:transfer:query')")
    @GetMapping("/alert/{alertId}")
    public AjaxResult getByAlertId(@PathVariable Long alertId) {
        DutyAlertTransfer transfer = dutyAlertTransferService.getByAlertId(alertId);
        // 没有转警记录是正常情况，返回成功但 data 为 null
        return success(transfer);
    }

    /**
     * 审核通过转警申请
     */
    @PreAuthorize("@ss.hasPermi('duty:transfer:approve')")
    @Log(title = "转警管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/approve")
    public AjaxResult approve(@PathVariable Long id, @RequestBody(required = false) TransferApproveRequest request) {
        String reviewComment = request != null ? request.getReviewComment() : null;
        boolean success = dutyAlertTransferService.approveTransfer(id, reviewComment);
        if (success) {
            return success("转警申请已审核通过");
        } else {
            return error("审核操作失败");
        }
    }

    /**
     * 审核驳回转警申请
     */
    @PreAuthorize("@ss.hasPermi('duty:transfer:approve')")
    @Log(title = "转警管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/reject")
    public AjaxResult reject(@PathVariable Long id, @RequestBody(required = false) TransferRejectRequest request) {
        String reviewComment = request != null ? request.getReviewComment() : null;
        boolean success = dutyAlertTransferService.rejectTransfer(id, reviewComment);
        if (success) {
            return success("转警申请已驳回");
        } else {
            return error("驳回操作失败");
        }
    }

    /**
     * 录入反馈
     */
    @PreAuthorize("@ss.hasPermi('duty:transfer:edit')")
    @Log(title = "转警管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/feedback")
    public AjaxResult feedback(@PathVariable Long id, @RequestBody TransferFeedbackRequest request) {
        if (request == null || request.getFeedback() == null || request.getFeedback().trim().isEmpty()) {
            return error("反馈内容不能为空");
        }
        Date feedbackTime = request.getFeedbackTime() != null ? request.getFeedbackTime() : new Date();
        boolean success = dutyAlertTransferService.recordFeedback(id, request.getFeedback(), feedbackTime);
        if (success) {
            return success("反馈已录入");
        } else {
            return error("反馈录入失败");
        }
    }

    /**
     * 标记为已处理
     */
    @PreAuthorize("@ss.hasPermi('duty:transfer:edit')")
    @Log(title = "转警管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/process")
    public AjaxResult process(@PathVariable Long id, @RequestBody(required = false) TransferProcessRequest request) {
        String remark = request != null ? request.getRemark() : null;
        boolean success = dutyAlertTransferService.markProcessed(id, remark);
        if (success) {
            return success("转警记录已标记为已处理");
        } else {
            return error("标记操作失败");
        }
    }
}