package com.qianhe.zqcz.ryxx.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.ryxx.domain.ZqczRyxx;
import com.qianhe.zqcz.ryxx.domain.ZqczRyxxQuery;
import com.qianhe.zqcz.ryxx.domain.ZqczRyxxVo;
import com.qianhe.zqcz.ryxx.service.ZqczRyxxService;
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/ryxx")
public class ZqczRyxxController extends BaseController {
    @Autowired
    private ZqczRyxxService zqczRyxxService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public TableDataInfo list(ZqczRyxxQuery zqczRyxxQuery) {
        startPage();
        return getDataTable(zqczRyxxService.list(zqczRyxxQuery));
    }

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

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

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

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

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody ZqczRyxxQuery zqczRyxxQuery) {
        List<ZqczRyxxVo> dataList = zqczRyxxService.list(zqczRyxxQuery);
        ExcelUtil<ZqczRyxxVo> util = new ExcelUtil<>(ZqczRyxxVo.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 = zqczRyxxService.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());
        }
    }
}
