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.DwzpBPO;
import com.zjrs.ggfw.rlzysc.bpo.ZwxxBPO;
import com.zjrs.ggfw.rlzysc.dto.QyxxDTO;
import com.zjrs.ggfw.rlzysc.dto.ZwxxListDTO;
import com.zjrs.ggfw.rlzysc.dto.ZwxxQueryDTO;
import com.zjrs.ggfw.rlzysc.po.RsRlzyZwxxPO;
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;
import java.util.Map;

@Api("职位信息")
@RestController
@RequestMapping("/api/rlzysc/zyxx")
public class ZwxxController {
    private static final Logger LOG = LoggerFactory.getLogger(ZwxxController.class);
    @Resource
    private ZwxxBPO zwxxBPO;
    @Resource
    private DwzpBPO dwzpBPO;

    @ApiOperation(value = "获取职位详细信息", notes = "获取职位详细信息")
    @PostMapping("/search/query")
    public AjaxResponse getZyxxById(@RequestBody ZwxxQueryDTO dto){
        LOG.info("获取职位详细信息开始");
            AjaxResponse ajaxResponse = new AjaxResponse();
        try{
            RsRlzyZwxxPO zwxx = this.zwxxBPO.getZyxxById(dto);//职业信息以及企业信息
            String rsrlzyqyxxlsh = zwxx.getRsrlzyqyxxlsh();
//            ZjQueryDTO queryDto = new ZjQueryDTO();
//            queryDto.setZjlsh(rsrlzyqyxxlsh);
//            queryDto.setFlag("view");

            QyxxDTO qyxxDTO = new QyxxDTO();
            qyxxDTO.setFlag("view");
            qyxxDTO.setQyssr(null);
            qyxxDTO.setZjlsh(rsrlzyqyxxlsh);

            Map qyxx = dwzpBPO.getQyxx(qyxxDTO);
            List<RsRlzyZwxxPO> xszw = this.zwxxBPO.getXszw(zwxx.getZpgz(),zwxx.getRsrlzyzwxxlsh());//通过该职位的职位类型查找5条相似的职位
            ajaxResponse.setData("zwxx",zwxx);
            ajaxResponse.setData("qyxx", qyxx);
            ajaxResponse.setData("xszw",xszw);
        } 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("/search/list")
    public AjaxResponse getZyxxList(@RequestBody @Valid ZwxxQueryDTO dto, BindingResult bindingResult){
        LOG.info("获取职位信息列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        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 {
            PageResult pageResult = this.zwxxBPO.getZyxxList(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 = "获取最近浏览职位信息列表")
    @PostMapping("/search/recent/list")
    public AjaxResponse getRecentZyxxList(){
        LOG.info("获取最近浏览职位信息列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            List<ZwxxListDTO> result = this.zwxxBPO.getRecentZyxxList();
            ajaxResponse.setData("data", result);
        } 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;
    }


}

