package com.zjrs.ggfw.rlzysc.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
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.annotation.VisitLog;
import com.zjrs.ggfw.rlzysc.bpo.GrqzBPO;
import com.zjrs.ggfw.rlzysc.bpo.JlxxBPO;
import com.zjrs.ggfw.rlzysc.dto.*;
import com.zjrs.ggfw.rlzysc.dto.grzxjlxx.ZjQueryDTO;
import com.zjrs.ggfw.rlzysc.utils.RlzyscUtils;
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/jlxx")
public class JlxxController {
    private static final Logger LOG = LoggerFactory.getLogger(JlxxController.class);
    @Resource
    private JlxxBPO jlxxBPO;

    @Resource
    private GrqzBPO grqzBPO;


    @ApiOperation(value = "获取简历详细信息个人使用", notes = "获取简历详细信息")
    @PostMapping("/search/query")
    public AjaxResponse getjlxxDetailForGr(@RequestBody @Valid JlxxWhDTO dto) {
        //小程序仅个人登录使用改接口，公共服务个人单位均使用该接口
        LOG.info("获取简历详细信息开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ZjQueryDTO dto1 = new ZjQueryDTO();
            dto1.setZjlsh(dto.getRsrlzyjlxxlsh());
            dto1.setFlag("view");

            JljbxxDTO jljbxxdto = new JljbxxDTO();
            jljbxxdto.setFlag("view");
            jljbxxdto.setZjlsh(dto.getRsrlzyjlxxlsh());
            jljbxxdto.setJlssr(dto.getJlssr());

            Map jljbxx = grqzBPO.getJljbxx(jljbxxdto);
            String fbzt = jljbxx.get("fbzt") == null ? "0" : jljbxx.get("fbzt").toString();
            String zjhm = (String) jljbxx.get("aac147");
            if (StringUtils.isNotBlank(zjhm) && !fbzt.equals("0")) {
                if (zjhm.length() == 18) {
                    jljbxx.put("aac147", zjhm.replaceAll("(\\d{3})\\d{13}(\\d{2})", "$1*************$2"));
                } else if (zjhm.length() == 15){
                    jljbxx.put("aac147", zjhm.replaceAll("(\\d{3})\\d{10}(\\d{2})", "$1**********$2"));
                }
            }
            String sjhm = (String) jljbxx.get("sjhm");
            if (StringUtils.isNotBlank(sjhm) && !fbzt.equals("0")){
                jljbxx.put("sjhm",sjhm.replaceAll("(\\d{3})\\d{8}","$1*********"));
            }

            String yx = (String) jljbxx.get("yx");
            if (StringUtils.isNotBlank(yx) && !fbzt.equals("0")){
                jljbxx.put("yx", RlzyscUtils.str2SercurityStr(yx, 2,0));
            }

            String aac006 = String.valueOf(jljbxx.get("aac006"));
            if (StringUtils.isNotBlank(aac006) && !fbzt.equals("0")){
                jljbxx.put("aac006", RlzyscUtils.str2SercurityStr(aac006, 2,1));
            }

            Map jlqzyx = grqzBPO.getJlqzyx(dto1);
            List<JlxxGzjyUDTO> gzjyList = this.grqzBPO.getgzjyList(dto);
            List<JlxxJyjlUDTO> jyjlList = this.grqzBPO.getjyjlList(dto);
            List<JlxxPxjlUDTO> pxjlList = this.grqzBPO.getpxjlList(dto);
            List<JlxxZyjnUDTO> zyjnList = this.grqzBPO.getzyjnList(dto);
            List<JlxxShzsUDTO> shzsList = this.grqzBPO.getshzsList(dto);
            List<JlxxHjqkUDTO> hjqkList = this.grqzBPO.gethjqkList(dto);
            List<JlxxZcqkUDTO> zcqkList = this.grqzBPO.getzcqkList(dto);
            ajaxResponse.setData("gzjyList", gzjyList);
            ajaxResponse.setData("jyjlList", jyjlList);
            ajaxResponse.setData("pxjlList", pxjlList);
            ajaxResponse.setData("zyjnList", zyjnList);
            ajaxResponse.setData("shzsList", shzsList);
            ajaxResponse.setData("hjqkList", hjqkList);
            ajaxResponse.setData("zcqkList", zcqkList);
            ajaxResponse.setData("jljbxx",jljbxx);
            ajaxResponse.setData("jlqzyx",jlqzyx);
        } 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;
    }

    @VisitLog(type = "01")
    @ApiOperation(value = "获取简历详细信息单位使用", notes = "获取简历详细信息")
    @PostMapping("/search/queryfordw")
    public AjaxResponse getjlxxDetailForDw(@RequestBody @Valid JlxxWhDTO dto) {
        //小程序仅个人登录使用改接口，公共服务个人单位均使用该接口
        LOG.info("获取简历详细信息开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ZjQueryDTO dto1 = new ZjQueryDTO();
            dto1.setZjlsh(dto.getRsrlzyjlxxlsh());
            dto1.setFlag("view");
            Map jljbxx = grqzBPO.getJljbxxForDw(dto1);
            Map jlqzyx = grqzBPO.getJlqzyxForDw(dto1);
            List<JlxxGzjyUDTO> gzjyList = this.grqzBPO.getgzjyList(dto);
            List<JlxxJyjlUDTO> jyjlList = this.grqzBPO.getjyjlList(dto);
            List<JlxxPxjlUDTO> pxjlList = this.grqzBPO.getpxjlList(dto);
            List<JlxxZyjnUDTO> zyjnList = this.grqzBPO.getzyjnList(dto);
            List<JlxxShzsUDTO> shzsList = this.grqzBPO.getshzsList(dto);
            List<JlxxHjqkUDTO> hjqkList = this.grqzBPO.gethjqkList(dto);
            List<JlxxZcqkUDTO> zcqkList = this.grqzBPO.getzcqkList(dto);
            ajaxResponse.setData("gzjyList", gzjyList);
            ajaxResponse.setData("jyjlList", jyjlList);
            ajaxResponse.setData("pxjlList", pxjlList);
            ajaxResponse.setData("zyjnList", zyjnList);
            ajaxResponse.setData("shzsList", shzsList);
            ajaxResponse.setData("hjqkList", hjqkList);
            ajaxResponse.setData("zcqkList", zcqkList);
            ajaxResponse.setData("jljbxx",jljbxx);
            ajaxResponse.setData("jlqzyx",jlqzyx);
        } 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 getJlxxList(@RequestBody @Valid JlxxQueryDTO 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.jlxxBPO.getJlxxList(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 getRecentJlxxList() {
        LOG.info("获取最近浏览简历信息列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            List<JlxxBPO> result = this.jlxxBPO.getRecentJlxxList();
            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;
    }

    /**
     * 附件简历上传
     *
     * @param dto
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "个人简历附件上传", notes = "个人简历附件上传")
    @PostMapping("/file/upload")
    public AjaxResponse uploadPhoto(@RequestBody @Valid JlxxFileUploadDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();

        //校验入参
        if (bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }

        try {
            this.jlxxBPO.uploadFile(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;
    }
}

