package com.qianhe.zqbfbdw.bfzj.controller;

import com.qianhe.common.core.controller.BaseController;
import com.qianhe.common.core.domain.AjaxResult;
import com.qianhe.common.core.page.TableDataInfo;
import com.qianhe.common.utils.poi.ExcelUtil;
import com.qianhe.zqbfbdw.bfzj.domain.ZqbfBdwBfzj;
import com.qianhe.zqbfbdw.bfzj.domain.ZqbfBdwBfzjQuery;
import com.qianhe.zqbfbdw.bfzj.domain.ZqbfBdwBfzjVo;
import com.qianhe.zqbfbdw.bfzj.service.ZqbfBdwBfzjService;
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("/zqbfbdw/bfzj")
public class ZqbfBdwBfzjController extends BaseController {
    @Autowired
    private ZqbfBdwBfzjService zqbfBdwBfzjService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public TableDataInfo list(ZqbfBdwBfzjQuery zqbfBdwBfzjQuery) {
        startPage();
        return getDataTable(zqbfBdwBfzjService.list(zqbfBdwBfzjQuery));
    }

    /**
     * 查询详情
     */
    @GetMapping("/{id}")
    public AjaxResult info(@PathVariable Long id) {
        try {
            return success(zqbfBdwBfzjService.info(id));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 新增
     */
    @PostMapping
    public AjaxResult add(@RequestBody ZqbfBdwBfzj zqbfBdwBfzj) {
        try {
            return toAjax(zqbfBdwBfzjService.add(zqbfBdwBfzj, getUsername()));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ZqbfBdwBfzj zqbfBdwBfzj) {
        try {
            return toAjax(zqbfBdwBfzjService.edit(zqbfBdwBfzj, getUsername()));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        try {
            return toAjax(zqbfBdwBfzjService.delete(ids));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZqbfBdwBfzjQuery zqbfBdwBfzjQuery) {
        List<ZqbfBdwBfzjVo> dataList = zqbfBdwBfzjService.list(zqbfBdwBfzjQuery);
        ExcelUtil<ZqbfBdwBfzjVo> util = new ExcelUtil<>(ZqbfBdwBfzjVo.class);
        util.exportExcel(response, dataList, "sheetName");
    }
}
