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

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.DutyAlertConvert;
import com.aegis.emergency.alert.dto.ConvertApproveRequest;
import com.aegis.emergency.alert.dto.ConvertRejectRequest;
import com.aegis.emergency.alert.service.DutyAlertConvertService;

/**
 * 转事件管理Controller
 * 
 * @author aegis
 */
@RestController
@RequestMapping("/duty/converts")
public class DutyAlertConvertController extends BaseController {

    @Autowired
    private DutyAlertConvertService dutyAlertConvertService;

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

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

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

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

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