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.MsGlBPO;
import com.zjrs.ggfw.rlzysc.dto.grzxjlxx.ZjDTO;
import com.zjrs.ggfw.rlzysc.dto.msgl.*;
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 MsGlController {

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

    @Autowired
    private MsGlBPO msglBPO;

    @ApiOperation(value = "面试管理保存", notes = "面试管理保存")
    @PostMapping("/save")
    public AjaxResponse saveMsglInfo(@RequestBody @Valid MsUDTO 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.saveMsglInfo(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("/list")
    public AjaxResponse getMsgllist(@RequestBody @Valid MsQueryDTO 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.getMsgllist(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("/listforgr")
    public AjaxResponse getMsgllistForGr(@RequestBody @Valid MsQueryDTO 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.getMsgllistForGr(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("/updatemsjg")
//    public AjaxResponse updatemsjg(@RequestBody @Valid MsglZtUDTO 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.updatemsjg(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("/query")
    public AjaxResponse getMsglInfo(@RequestBody @Valid ZjDTO 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.getMsglInfo(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("/queryforydd")
    public AjaxResponse getMsglInfoForYdd(@RequestBody @Valid ZjDTO 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.getMsglInfoForYdd(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("/update")
    public AjaxResponse updatemsgl(@RequestBody @Valid MsglZtUDTO 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.updatemsgl(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("/msglist")
    public AjaxResponse getMsglMsglist(@RequestBody @Valid MsglZtListDTO 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.getMsglMsglist(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("/qyxx/query")
    public AjaxResponse queryQyxx(@RequestBody @Valid MsglZtUDTO 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.queryQyxx(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("/msjxx/valid")
    public AjaxResponse validMsjxx(@RequestBody @Valid MsyqglDTO 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.validMsjxx(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("/sendLqtzs")
    public AjaxResponse sendLqtzs(@RequestBody @Valid LqtzsDTO 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.sendLqtzs(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 = "查询面试管理token信息", notes = "查询面试管理token信息")
    @PostMapping("/jwt_token")
    public AjaxResponse getJwtToken(@RequestBody @Valid MsglZtUDTO 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.getJwtToken(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;
    }

}

