package com.zjrs.zwnw.zj.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.zwnw.zj.bpo.InitBPO;
import com.zjrs.zwnw.zj.dto.InitDTO;
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.validation.Valid;

@Api("初始数据设置")
@RestController
@RequestMapping("/api/zj/init")
public class InitController {
    private static final Logger LOG = LoggerFactory.getLogger(InitController.class);
    @Resource
    private InitBPO initBPO;

    @ApiOperation(value = "保存")
    @PostMapping(value = "/save")
    public AjaxResponse save(@RequestBody @Valid InitDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            return initBPO.save(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.toString());
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "查询信息")
    @PostMapping(value = "/getInfo")
    public AjaxResponse getInfo() {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            return initBPO.getInfo();
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.toString());
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "查询是否有填报记录")
    @PostMapping(value = "/queryIsTb")
    public AjaxResponse queryIsTb() {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            return initBPO.queryIsTb();
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.toString());
        }
        return ajaxResponse;
    }
}
