package com.qianhe.zqcz.xmgl.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.xmgl.domain.ZqczXmgl;
import com.qianhe.zqcz.xmgl.domain.ZqczXmglQuery;
import com.qianhe.zqcz.xmgl.domain.ZqczXmglVo;
import com.qianhe.zqcz.xmgl.service.ZqczXmglService;
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/xmgl")
public class ZqczXmglController extends BaseController {
    @Autowired
    private ZqczXmglService zqczXmglService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public TableDataInfo list(ZqczXmglQuery zqczXmglQuery) {
        startPage();
        return getDataTable(zqczXmglService.list(zqczXmglQuery));
    }

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

    /**
     * 新增
     */
    @PostMapping
    public AjaxResult add(@RequestBody ZqczXmgl zqczXmgl) {
        try {
            return toAjax(zqczXmglService.add(zqczXmgl, getUsername()));
        } catch (BusinessException e) {
            log.warn("最强操作-项目管理-新增异常：" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ZqczXmgl zqczXmgl) {
        try {
            return toAjax(zqczXmglService.edit(zqczXmgl, getUsername()));
        } catch (BusinessException e) {
            log.warn("最强操作-项目管理-修改异常：" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

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

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZqczXmglQuery zqczXmglQuery) {
        List<ZqczXmglVo> dataList = zqczXmglService.list(zqczXmglQuery);
        ExcelUtil<ZqczXmglVo> util = new ExcelUtil<>(ZqczXmglVo.class);
        util.exportExcel(response, dataList, "sheetName");
    }

    /**
     * 导入
     */
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) return warn("文件不能为空");
            int count = zqczXmglService.importExcel(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());
        }
    }

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