package com.qianhe.zqcz.zsrw.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.zqcz.zsrw.domain.ZqczZsrw;
import com.qianhe.zqcz.zsrw.domain.ZqczZsrwQuery;
import com.qianhe.zqcz.zsrw.domain.ZqczZsrwVo;
import com.qianhe.zqcz.zsrw.service.ZqczZsrwService;
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("/zqcz/zqczzsrw")
public class ZqczZsrwController extends BaseController {
    @Autowired
    private ZqczZsrwService zqczZsrwService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public TableDataInfo list(ZqczZsrwQuery zqczZsrwQuery) {
        startPage();
        List<ZqczZsrwVo> dataList = zqczZsrwService.list(zqczZsrwQuery);
        return getDataTable(dataList);
    }

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

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

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

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

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZqczZsrwQuery zqczZsrwQuery) {
        List<ZqczZsrwVo> dataList = zqczZsrwService.list(zqczZsrwQuery);
        ExcelUtil<ZqczZsrwVo> util = new ExcelUtil<>(ZqczZsrwVo.class);
        util.exportExcel(response, dataList, "sheetName");
    }
}
