package com.qianhe.zqbf.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.zqbf.domain.ZqbfXmzBfzj;
import com.qianhe.zqbf.domain.ZqbfXmzBfzjQuery;
import com.qianhe.zqbf.domain.ZqbfXmzBfzjVo;
import com.qianhe.zqbf.service.ZqbfXmzBfzjService;
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("/zqbf/bfzj")
public class ZqbfXmzBfzjController extends BaseController {
    @Autowired
    private ZqbfXmzBfzjService zqbfXmzBfzjService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public TableDataInfo list(ZqbfXmzBfzjQuery zqbfXmzBfzjQuery) {
        startPage();
        return getDataTable(zqbfXmzBfzjService.list(zqbfXmzBfzjQuery));
    }

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

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

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

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

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZqbfXmzBfzjQuery zqbfXmzBfzjQuery) {
        List<ZqbfXmzBfzjVo> dataList = zqbfXmzBfzjService.list(zqbfXmzBfzjQuery);
        ExcelUtil<ZqbfXmzBfzjVo> util = new ExcelUtil<>(ZqbfXmzBfzjVo.class);
        util.exportExcel(response, dataList, "sheetName");
    }
}
