package com.zjrs.ggfw.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.ggfw.rlzysc.bpo.MsgGlBPO;
import com.zjrs.ggfw.rlzysc.dto.msgl.MsgQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.msgl.MsgUDTO;
import com.zjrs.ggfw.rlzysc.dto.msgl.MsgglZtDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.validation.Valid;
import java.util.List;

@Api("面试官管理")
@RestController
@RequestMapping("/api/rlzysc/msgl")
public class MsgGlController {

    private static final Logger LOG = LoggerFactory.getLogger(MsgGlController.class);

    @Autowired
    private MsgGlBPO msglBPO;

    @ApiOperation(value = "面试官管理列表列表", notes = "面试官管理列表列表")
    @PostMapping("/msggl/list")
    public AjaxResponse getMsggllist(@RequestBody @Valid MsgQueryDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            StringBuffer errmsg = new StringBuffer();
            errmsg.append("");
            for (int i = 0; i < fieldErrors.size(); i++) {
                if (i == 0) {
                    errmsg.append(fieldErrors.get(i).getDefaultMessage());
                } else {
                    errmsg.append("；" + fieldErrors.get(i).getDefaultMessage());
                }
            }
            ajaxResponse.setErrorMsg(errmsg.toString());
            return ajaxResponse;
        }
        try {
            ajaxResponse.setData("data", msglBPO.getMsggllist(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("/msggl/save")
    public AjaxResponse saveMsgglInfo(@RequestBody @Valid MsgUDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            StringBuffer errmsg = new StringBuffer();
            errmsg.append("");
            for (int i = 0; i < fieldErrors.size(); i++) {
                if (i == 0) {
                    errmsg.append(fieldErrors.get(i).getDefaultMessage());
                } else {
                    errmsg.append("；" + fieldErrors.get(i).getDefaultMessage());
                }
            }
            ajaxResponse.setErrorMsg(errmsg.toString());
            return ajaxResponse;
        }
        try {
            ajaxResponse.setData("data", msglBPO.saveMsgglInfo(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("/msggl/delete")
    public AjaxResponse delMsggl(@RequestBody @Valid MsgglZtDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            StringBuffer errmsg = new StringBuffer();
            errmsg.append("");
            for (int i = 0; i < fieldErrors.size(); i++) {
                if (i == 0) {
                    errmsg.append(fieldErrors.get(i).getDefaultMessage());
                } else {
                    errmsg.append("；" + fieldErrors.get(i).getDefaultMessage());
                }
            }
            ajaxResponse.setErrorMsg(errmsg.toString());
            return ajaxResponse;
        }
        try {
            msglBPO.delMsggl(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("/msggl/query")
    public AjaxResponse getMsgglInfo(@RequestBody @Valid MsgglZtDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            StringBuffer errmsg = new StringBuffer();
            errmsg.append("");
            for (int i = 0; i < fieldErrors.size(); i++) {
                if (i == 0) {
                    errmsg.append(fieldErrors.get(i).getDefaultMessage());
                } else {
                    errmsg.append("；" + fieldErrors.get(i).getDefaultMessage());
                }
            }
            ajaxResponse.setErrorMsg(errmsg.toString());
            return ajaxResponse;
        }
        try {
            ajaxResponse.setData("data", msglBPO.getMsgglInfo(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("/msggl/select")
    public AjaxResponse getMsgXzList(@RequestBody @Valid MsgglZtDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            StringBuffer errmsg = new StringBuffer();
            errmsg.append("");
            for (int i = 0; i < fieldErrors.size(); i++) {
                if (i == 0) {
                    errmsg.append(fieldErrors.get(i).getDefaultMessage());
                } else {
                    errmsg.append("；" + fieldErrors.get(i).getDefaultMessage());
                }
            }
            ajaxResponse.setErrorMsg(errmsg.toString());
            return ajaxResponse;
        }
        try {
            ajaxResponse.setData("data", msglBPO.getMsgXzList(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;
    }
}

