package com.zjrs.ggfw.rlzysc.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 org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.ggfw.rlzysc.bpo.JyzxBPO;
import com.zjrs.ggfw.rlzysc.dto.JyzdAddDTO;
import com.zjrs.ggfw.rlzysc.dto.JyzxQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.VRsRlzyJyzxFjxxDTO;
import com.zjrs.ggfw.rlzysc.dto.YrzdAddDTO;
import com.zjrs.ggfw.rlzysc.po.RsRlzyFwggPO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

@Api("就业咨询")
@RestController
@RequestMapping("/api/rlzysc/jyzx")
public class JyzxController {
    private static final Logger LOG = LoggerFactory.getLogger(JyzxController.class);
    @Resource
    private JyzxBPO jyzxBPO;

    @ApiOperation(value = "获取就业资讯列表", notes = "获取就业资讯列表")
    @PostMapping("/list")
    public AjaxResponse getJyzxList(@RequestBody JyzxQueryDTO dto) {
        LOG.info("获取就业资讯列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = this.jyzxBPO.getJyzxList(dto,pagenum,pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("获取就业资讯列表结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取就业资讯详情", notes = "获取就业资讯详情")
    @GetMapping("/detail/{id}")
    public AjaxResponse getJyzxDetail(@PathVariable("id") String id) {
        LOG.info("获取就业资讯详情开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            RsRlzyFwggPO result = this.jyzxBPO.getJyzxDetail(id);
            List<VRsRlzyJyzxFjxxDTO> fjxxResults = this.jyzxBPO.getJyzxFjxxForList(id);
            ajaxResponse.setData("data", result);
            ajaxResponse.setData("fjxx_data", fjxxResults);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("获取就业资讯详情结束");
        return ajaxResponse;
    }


    @ApiOperation(value = "就业指导报名", notes = "就业指导报名")
    @PostMapping("/jyzd/add")
    public AjaxResponse addJyzd(@Valid @RequestBody JyzdAddDTO dto, BindingResult bindingResult) {
        LOG.info("就业指导报名开始");
        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 {
            this.jyzxBPO.addJyzd(dto);
            ajaxResponse.setAppcode("0");
            ajaxResponse.setMsg("操作成功");
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("就业指导报名结束");
        return ajaxResponse;
    }
    @ApiOperation(value = "用人指导报名", notes = "用人指导报名")
    @PostMapping("/jyzd/addyrzd")
    public AjaxResponse addYrzd(@Valid @RequestBody YrzdAddDTO dto, BindingResult bindingResult) {
        LOG.info("用人指导报名开始");
        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 {
            this.jyzxBPO.addYrzd(dto);
            ajaxResponse.setAppcode("0");
            ajaxResponse.setMsg("操作成功");
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("用人指导报名结束");
        return ajaxResponse;
    }
}

