package com.zjrs.zwnw.zj.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.zwnw.zj.bpo.QsbsBPO;
import com.zjrs.zwnw.zj.dto.QsDTO;
import com.zjrs.zwnw.zj.dto.QsPageDTO;
import com.zjrs.zwnw.zj.dto.QsbsDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;

@Api("全市博士数据管理")
@RestController
@RequestMapping("/api/zj/qsbs")
public class QsbsController {
    private static final Logger LOG = LoggerFactory.getLogger(QsbsController.class);
    @Resource
    private QsbsBPO qsbsBPO;

    @ApiOperation(value = "新增/修改")
    @PostMapping(value = "/saveOrUpd")
    public AjaxResponse saveOrUpd(@RequestBody @Valid QsbsDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            return qsbsBPO.saveOrUpd(dto);
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.toString());
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "查询信息")
    @PostMapping(value = "/getInfo")
    public AjaxResponse getInfo(@RequestBody @Valid QsDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            return qsbsBPO.getInfo(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.toString());
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "导出全市博士数据")
    @PostMapping(value = "/export")
    public void export(@RequestBody QsDTO dto, HttpServletResponse res) throws IOException {
        LOG.info("导出全市博士数据开始");
        qsbsBPO.export(dto, res);
        LOG.info("导出全市博士数据结束");
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/del")
    public AjaxResponse del(@RequestBody @Valid QsDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            return qsbsBPO.del(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.toString());
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "查询上一年度累计总量")
    @PostMapping(value = "/getTjData")
    public AjaxResponse getTjData() {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
           return qsbsBPO.getTjData();
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.toString());
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "查询列表")
    @PostMapping(value = "/getList")
    public AjaxResponse getList(@RequestBody @Valid QsPageDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = qsbsBPO.getList(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
}
