package com.qianhe.zqcz.pwxx.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.exception.BusinessException;
import com.qianhe.common.utils.poi.ExcelUtil;
import com.qianhe.zqcz.pwxx.domain.ZqczPwxx;
import com.qianhe.zqcz.pwxx.domain.ZqczPwxxQuery;
import com.qianhe.zqcz.pwxx.domain.ZqczPwxxVo;
import com.qianhe.zqcz.pwxx.service.ZqczPwxxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@Slf4j
@RestController
@RequestMapping("/zqcz/zqczpwxx")
public class ZqczPwxxController extends BaseController {
    @Autowired
    private ZqczPwxxService zqczPwxxService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public TableDataInfo list(ZqczPwxxQuery zqczPwxxQuery) {
        startPage();
        return getDataTable(zqczPwxxService.list(zqczPwxxQuery));
    }

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

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

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

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

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody ZqczPwxxQuery zqczPwxxQuery) {
        List<ZqczPwxxVo> dataList = zqczPwxxService.list(zqczPwxxQuery);
        ExcelUtil<ZqczPwxxVo> util = new ExcelUtil<>(ZqczPwxxVo.class);
        util.exportExcel(response, dataList, "sheetName");
    }

    /**
     * 导入
     */
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("jcrwid") Long jcrwid, @RequestParam("file") MultipartFile file) {
        try {
            if (jcrwid == null) return warn("直属id不能为空");
            if (file.isEmpty()) return warn("文件不能为空");
            int count = zqczPwxxService.importExcel(jcrwid, file);
            return success("导入成功，共导入 " + count + " 条数据");
        } catch (BusinessException e) {
            log.warn("导入评委失败：" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error("导入评委失败：" + e.getMessage());
            return error(e.getMessage());
        }
    }
}
