package com.zksy.web.controller.gasbasic;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.domain.PipeNetworkBase;
import com.zksy.base.dto.PipeNetworkExportDTO;
import com.zksy.base.dto.PipeNetworkPageInDTO;
import com.zksy.base.service.PipeNetworkBaseService;
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 com.zksy.common.utils.poi.ExcelUtil;
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.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/pipe-network")
@Api(tags = "燃气管网基础数据")
public class GasPipeNetworkController {

    @Autowired
    private PipeNetworkBaseService service;

    @GetMapping("/findByPage")
    @ApiOperation("管网分页查询")
    @Anonymous
    public AjaxResult findByPage(PipeNetworkPageInDTO dto) {
        Page<PipeNetworkBase> page = service.queryByPage(dto);
        return AjaxResult.success(page);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("根据ID查询管网")
    @Anonymous
    public AjaxResult getById(@PathVariable String id) {
        PipeNetworkBase entity = service.getById(id);
        return entity != null ? AjaxResult.success(entity) : AjaxResult.error("管网信息不存在");
    }

    @GetMapping("/options")
    @ApiOperation("管网下拉选项")
    @Anonymous
    public AjaxResult options() {
        return AjaxResult.success(service.getOptions());
    }

    @PostMapping("/save")
    @ApiOperation("新增管网")
    @Log(title = "新增管网基础数据", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody PipeNetworkBase 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 PipeNetworkBase entity) {
        boolean updated = service.updateWithCheck(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("/export")
    @ApiOperation("导出Excel")
    @Anonymous
    public void exportData(HttpServletResponse response,
                           @RequestParam(required = false) String networkName,
                           @RequestParam(required = false) String status) {
        List<PipeNetworkExportDTO> list = service.exportData(networkName, status);
        ExcelUtil<PipeNetworkExportDTO> util = new ExcelUtil<>(PipeNetworkExportDTO.class);
        util.exportExcel(response, list, "燃气管网数据");
    }
}
