package com.qianhe.zqcz.pftb.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.MtExcelUtil;
import com.qianhe.zqcz.pftb.domain.ZqczPftb;
import com.qianhe.zqcz.pftb.domain.ZqczPftbQuery;
import com.qianhe.zqcz.pftb.service.ZqczPftbService;
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;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/zqcz/pftb")
public class ZqczPftbController extends BaseController {
    @Autowired
    private ZqczPftbService zqczPftbService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public AjaxResult list(ZqczPftbQuery zqczPftbQuery) {
        try {
            return success(zqczPftbService.list(zqczPftbQuery));
        } catch (BusinessException e) {
            log.warn("最强操作-评分查询失败：" + e.getMessage());
            return warn(e.getMessage());
        } catch (Exception e) {
            log.error("最强操作-评分查询失败：" + e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 新增
     */
    @PostMapping
    public AjaxResult add(@RequestBody ZqczPftb zqczPftb) {
        try {
            return toAjax(zqczPftbService.add(zqczPftb, getUsername()));
        } catch (Exception e) {
            log.error("最强操作-评分新增失败：" + e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody ZqczPftbQuery zqczPftbQuery) {
        zqczPftbService.export(response, zqczPftbQuery);
    }

    /**
     * 导入
     */
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("zsrwid") Long zsrwid, @RequestParam("jcrwid") Long jcrwid, @RequestParam("file") MultipartFile file) {
        try {
            int count = zqczPftbService.importExcel(zsrwid, 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());
        }
    }
}
