package com.qianhe.zqcz.jcdw.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.jcdw.domain.ZqczJcdw;
import com.qianhe.zqcz.jcdw.domain.ZqczJcdwQuery;
import com.qianhe.zqcz.jcdw.domain.ZqczJcdwVo;
import com.qianhe.zqcz.jcdw.service.ZqczJcdwService;
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/zqczjcdw")
public class ZqczJcdwController extends BaseController {
    @Autowired
    private ZqczJcdwService zqczJcdwService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public TableDataInfo list(ZqczJcdwQuery zqczJcdwQuery) {
        startPage();
        List<ZqczJcdwVo> dataList = zqczJcdwService.list(zqczJcdwQuery);
        return getDataTable(dataList);
    }

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

    /**
     * 新增
     */
    @PostMapping
    public AjaxResult add(@RequestBody ZqczJcdw zqczJcdw) {
        try {
            return toAjax(zqczJcdwService.add(zqczJcdw));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ZqczJcdw zqczJcdw) {
        try {
            return toAjax(zqczJcdwService.edit(zqczJcdw));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

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

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZqczJcdwQuery zqczJcdwQuery) {
        List<ZqczJcdwVo> dataList = zqczJcdwService.list(zqczJcdwQuery);
        ExcelUtil<ZqczJcdwVo> util = new ExcelUtil<>(ZqczJcdwVo.class);
        util.exportExcel(response, dataList, "sheetName");
    }

    /**
     * 上传文件 批量新增
     */
    @PostMapping("/batchAdd")
    public AjaxResult batchAdd(@RequestParam("zsrwid") Long zsrwid, @RequestParam("file") MultipartFile file) {
        try {
            if (zsrwid == null) return warn("直属id不能为空");
            if (file.isEmpty()) return warn("文件不能为空");
            int count = zqczJcdwService.batchAdd(zsrwid, 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());
        }
    }

    /**
     * 通过直属任务ID 删除
     */
    @DeleteMapping("/deleteZsrwid/{zsrwid}")
    public AjaxResult deleteZsrwid(@PathVariable Long zsrwid) {
        try {
            return toAjax(zqczJcdwService.deleteZsrwid(zsrwid));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }
}
