package com.zjrs.zwnw.rlzysc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.zwnw.rlzysc.bpo.JyzxglBPO;
import com.zjrs.zwnw.rlzysc.dto.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
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;
import java.util.List;

/*
就业咨询管理菜单
 */
@Api("就业咨询管理")
@RestController
@RequestMapping("/api/rlzysc/jyzxgl")
public class JyzxglController {
    private static final Logger LOG = LoggerFactory.getLogger(JyzxglController.class);

    @Resource
    private JyzxglBPO jyzxglBPO;

    /*
    获取列表
     */
    @ApiOperation(value = "获取列表", notes = "获取列表")
    @PostMapping(value = "/getJyzxglList")
    public AjaxResponse getJyzxglList(@RequestBody @Valid JyzxglQueryDTO dto){
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("data", jyzxglBPO.getJyzxglList(dto));
            ajaxResponse.setData("pagenum", dto.getPagenum());
            ajaxResponse.setData("pagesize", dto.getPagesize());
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /*
    更新发布状态
     */
    @ApiOperation(value = "更新发布状态", notes = "更新发布状态")
    @PostMapping(value = "/updateJyzxFbzt")
    public AjaxResponse updateJyzxFbzt(@RequestBody @Valid ZjUDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("zjlsh", jyzxglBPO.updateJyzxFbzt(dto));
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    /*
    删除就业咨询信息
     */
    @ApiOperation(value = "删除就业咨询信息", notes = "删除就业咨询信息")
    @PostMapping(value = "/delJyzxXx")
    public AjaxResponse delJyzxXx(@RequestBody @Valid ZjDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("zjlsh", jyzxglBPO.delJyzxXx(dto));
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /*
    获取就业咨询信息
     */
    @ApiOperation(value = "获取就业咨询信息", notes = "获取就业咨询信息")
    @PostMapping(value = "/getJyzxXx")
    public AjaxResponse getJyzxXx(@RequestBody @Valid ZjQueryDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("data", jyzxglBPO.getJyzxXx(dto));
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    /*
    保存就业咨询信息
     */
    @ApiOperation(value = "保存就业咨询信息", notes = "保存就业咨询信息")
    @PostMapping(value = "/saveJyzxXx")
    public AjaxResponse saveJyzxXx(@RequestBody @Valid JyzxglUDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("zjlsh", jyzxglBPO.saveJyzxXx(dto));
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /*
    获取列表
     */
    @ApiOperation(value = "获取列表", notes = "获取列表")
    @PostMapping(value = "/fjxx/list")
    public AjaxResponse queryFjxxList(@RequestBody @Valid ZjQueryListDTO dto, BindingResult bindingResult){
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            String errmsg = "";
            for (int i=0;i<fieldErrors.size();i++){
                if (i == 0){
                    errmsg = errmsg + fieldErrors.get(i).getDefaultMessage();
                } else {
                    errmsg = errmsg + "；" + fieldErrors.get(i).getDefaultMessage();
                }
            }
            ajaxResponse.setErrorMsg(errmsg);
            return ajaxResponse;
        }
        try {
            ajaxResponse.setData("data", jyzxglBPO.queryFjxxList(dto));
            ajaxResponse.setData("pagenum", dto.getPagenum());
            ajaxResponse.setData("pagesize", dto.getPagesize());
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /*
    保存就业咨询信息
     */
    @ApiOperation(value = "保存附件信息", notes = "保存附件信息")
    @PostMapping(value = "/fjxx/save")
    public AjaxResponse saveFjxx(@RequestBody @Valid RsRlzyJyzxFjxxDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("zjlsh", jyzxglBPO.saveFjxx(dto));
            ajaxResponse.setMsg("保存成功");
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /*
    保存就业咨询信息
     */
    @ApiOperation(value = "保存附件信息", notes = "保存附件信息")
    @PostMapping(value = "/fjxx/delete")
    public AjaxResponse deleteFjxx(@RequestBody @Valid ZjDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            jyzxglBPO.deleteFjxx(dto);
            ajaxResponse.setMsg("删除成功");
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
}
