package com.zksy.web.controller.risk;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.domain.RiskAssessment;
import com.zksy.base.dto.RiskPageInDTO;
import com.zksy.base.service.RiskAssessmentService;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.annotation.Log;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/risk")
@Api(tags = "管网风险评估")
public class RiskController {

    @Autowired
    private RiskAssessmentService service;

    @GetMapping("/findByPage")
    @ApiOperation("风险评估分页查询")
    @Anonymous
    public AjaxResult findByPage(RiskPageInDTO dto) {
        Page<RiskAssessment> page = service.findByPage(dto);
        return AjaxResult.success(page);
    }

    @GetMapping("/{riskId}")
    @ApiOperation("风险评估详情")
    @Anonymous
    public AjaxResult getById(@PathVariable String riskId) {
        RiskAssessment entity = service.getById(riskId);
        return entity != null ? AjaxResult.success(entity) : AjaxResult.error("风险评估信息不存在");
    }

    @PostMapping("/save")
    @ApiOperation("新增风险评估")
    @Log(title = "新增风险评估", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody RiskAssessment entity) {
        boolean saved = service.save(entity);
        return saved ? AjaxResult.success("新增成功", saved) : AjaxResult.error("新增失败");
    }

    @PutMapping("/updateById")
    @ApiOperation("修改风险评估")
    @Log(title = "修改风险评估", businessType = BusinessType.UPDATE)
    public AjaxResult updateById(@RequestBody RiskAssessment entity) {
        boolean updated = service.updateById(entity);
        return updated ? AjaxResult.success("修改成功", updated) : AjaxResult.error("修改失败");
    }

    @DeleteMapping("/deleteById")
    @ApiOperation("删除风险评估")
    @Log(title = "删除风险评估", businessType = BusinessType.DELETE)
    public AjaxResult deleteById(@RequestParam String id) {
        boolean deleted = service.removeById(id);
        return deleted ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @GetMapping("/four-color-map")
    @ApiOperation("管线风险评估四色图数据")
    @Anonymous
    public AjaxResult getFourColorMap() {
        return AjaxResult.success(service.getFourColorMapData());
    }
}
