package com.aegis.web.controller.cases;

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.common.utils.StringUtils;
import com.aegis.cases.domain.Case;
import com.aegis.cases.service.ICaseService;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 案例管理 Controller
 */
@RestController
@RequestMapping("/api/cases")
public class CaseController extends BaseController {

    @Autowired
    private ICaseService caseService;

    /**
     * 查询案例列表
     */
    @PreAuthorize("@ss.hasPermi('case:case:list')")
    @GetMapping
    public TableDataInfo list(@RequestParam(required = false) String title,
                              @RequestParam(required = false) String eventType,
                              @RequestParam(required = false) String eventLevel,
                              @RequestParam(required = false) String tags,
                              @RequestParam(required = false) Boolean isTypical,
                              @RequestParam(required = false) Date beginTime,
                              @RequestParam(required = false) Date endTime) {
        startPage();
        List<Case> list = caseService.listByCondition(title, eventType, eventLevel, tags, isTypical, beginTime, endTime);
        return getDataTable(list);
    }

    /**
     * 获取案例详情
     */
    @PreAuthorize("@ss.hasPermi('case:case:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        Case caseEntity = caseService.getById(id);
        if (caseEntity == null || "2".equals(caseEntity.getDelFlag())) {
            return error("案例不存在或已删除");
        }
        // 增加查看次数
        caseService.incrementViewCount(id);
        return success(caseEntity);
    }

    /**
     * 新增案例
     */
    @PreAuthorize("@ss.hasPermi('case:case:add')")
    @Log(title = "案例管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Case caseEntity) {
        if (caseEntity == null) {
            return error("案例信息不能为空");
        }
        if (StringUtils.isBlank(caseEntity.getTitle())) {
            return error("案例标题不能为空");
        }
        
        // 初始化默认值
        if (caseEntity.getViewCount() == null) {
            caseEntity.setViewCount(0);
        }
        if (caseEntity.getIsTypical() == null) {
            caseEntity.setIsTypical(false);
        }
        
        boolean saved = caseService.save(caseEntity);
        if (saved) {
            return success(caseEntity);
        }
        return error("保存失败");
    }

    /**
     * 修改案例
     */
    @PreAuthorize("@ss.hasPermi('case:case:edit')")
    @Log(title = "案例管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public AjaxResult edit(@PathVariable Long id, @RequestBody Case caseEntity) {
        caseEntity.setId(id);
        return toAjax(caseService.updateById(caseEntity));
    }

    /**
     * 删除案例
     */
    @PreAuthorize("@ss.hasPermi('case:case:remove')")
    @Log(title = "案例管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        Case caseEntity = caseService.getById(id);
        if (caseEntity == null || "2".equals(caseEntity.getDelFlag())) {
            return error("案例不存在或已删除");
        }
        // 逻辑删除
        caseEntity.setDelFlag("2");
        return toAjax(caseService.updateById(caseEntity));
    }

    /**
     * 标记为典型案例
     */
    @PreAuthorize("@ss.hasPermi('case:case:markTypical')")
    @Log(title = "案例管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/mark-typical")
    public AjaxResult markTypical(@PathVariable Long id) {
        boolean result = caseService.markTypical(id);
        if (result) {
            return success("标记成功");
        }
        return error("标记失败");
    }

    /**
     * 取消典型案例标记
     */
    @PreAuthorize("@ss.hasPermi('case:case:markTypical')")
    @Log(title = "案例管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/unmark-typical")
    public AjaxResult unmarkTypical(@PathVariable Long id) {
        boolean result = caseService.unmarkTypical(id);
        if (result) {
            return success("取消标记成功");
        }
        return error("取消标记失败");
    }

    /**
     * 查询典型案例列表
     */
    @PreAuthorize("@ss.hasPermi('case:case:list')")
    @GetMapping("/typical")
    public AjaxResult listTypicalCases() {
        List<Case> list = caseService.listTypicalCases();
        return success(list);
    }
}

