package com.engine.recruit.entity.resume;

import cn.hutool.core.convert.Convert;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.recruit.conn.CheckRepeatResume;
import com.engine.recruit.conn.ModeBrowserCommonInfo;
import com.engine.recruit.conn.RecruitDataMap;
import com.engine.recruit.constant.RecruitConstant;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import weaver.formmode.recruit.modeexpand.util.RecruitModeUtil;
import weaver.general.BaseBean;
import weaver.hrm.User;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:dxfeng
 * @createTime: 2023/11/10
 * @version: 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class QllResumePo {

    private String id;
    private String xm;
    private String xb;
    private String nl;
    private String gzjy;
    private String xjzd;
    private String sjhm;
    private String yx;
    private String ypzw;
    private String grys;
    private String qzyx;
    private String gzjl;
    private String xmjl;
    private String jyjl;
    private String zgxl;
    private String zyjn;
    private String yynl;
    private String zs;
    private String jlfj;
    private String rksj;
    private String tdsj;
    private String lyqd;
    private String gjrsjh;

    private int userId;


    /**
     * 解析JSON数组，下载文件并入库
     *
     * @return
     */
    public String getJlfj() {
        if (StringUtils.isBlank(jlfj)) {
            return null;
        }
        String resumeId = null;
        BaseBean baseBean = new BaseBean();
        try {
            JSONArray jsonArray = JSON.parseArray(jlfj);
            JSONObject jsonObject = (JSONObject) jsonArray.get(0);
            String name = jsonObject.getString("name");
            String content = jsonObject.getString("content");
            baseBean.writeLog("千里聆简历附件解析，id=" + id + ",jslf=" + jlfj);
            HttpResponse response = HttpRequest.get(content).execute();
            if (response.isOk()) {
                String disposition = response.header("Content-Disposition");
                if (StringUtils.isNotBlank(disposition)) {
                    InputStream inputStream = response.bodyStream();
                    int imageFileId = RecruitModeUtil.generateImageFileId(inputStream, name);
                    int docId = RecruitModeUtil.createDocId(Convert.toInt(RecruitConstant.APPLICANTS_RESUMES_CATEGORY), imageFileId, new User(userId));
                    resumeId = String.valueOf(docId);
                } else {
                    baseBean.writeLog("千里聆附件下载失败" + response.body());
                }
            } else {
                baseBean.writeLog("千里聆附件下载失败,URL响应失败");
            }
        } catch (Exception e) {
            baseBean.writeLog("千里聆附件下载失败", e);
        }
        return resumeId;
    }

    public String getLyqd() {
        return ModeBrowserCommonInfo.getResumeSource(lyqd);
    }

    public String getXb() {
        if ("男".equals(xb)) {
            return "0";
        } else if ("女".equals(xb)) {
            return "1";
        }
        return null;
    }

    public String getGzjy() {
        if (StringUtils.isBlank(gzjy)) {
            return null;
        }
        int parseInt = Integer.parseInt(gzjy);
        String year = parseInt + "年";
        if (0 == parseInt) {
            year = "1年以内";
        }
        if (parseInt > 20) {
            year = "20年以上";
        }
        return ModeBrowserCommonInfo.getWorkExperience(year);
    }

    public String getZgxl() {
        return ModeBrowserCommonInfo.getEducationLevelId(zgxl);
    }


    public List<RecruitDataMap<Object>> getGzjl() {
        List<RecruitDataMap<Object>> list = new ArrayList<>();
        if (StringUtils.isNotBlank(gzjl)) {
            try {
                JSONArray jsonArray = JSONArray.parseArray(gzjl);
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String gsmc = jsonObject.getString("公司名称:");
                    String zwmc = jsonObject.getString("职位名称:");
                    String sj = jsonObject.getString("时间:");
                    String gznr = jsonObject.getString("工作内容:");
                    RecruitDataMap<Object> map = CheckRepeatResume.getDateRange(sj, false);
                    map.put("gsmc", gsmc);
                    map.put("gw", zwmc);
                    map.put("gzzz", formatStr(gznr));
                    list.add(map);
                }
            } catch (Exception e) {
                new BaseBean().writeLog("工作经历解析失败", e);
            }
        }
        return list;
    }

    public List<RecruitDataMap<Object>> getXmjl() {
        List<RecruitDataMap<Object>> list = new ArrayList<>();
        if (StringUtils.isNotBlank(xmjl)) {
            try {
                JSONArray jsonArray = JSONArray.parseArray(xmjl);
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String xmmc = jsonObject.getString("项目名称:");
                    //String rzgs = jsonObject.getString("任职公司:");
                    String sj = jsonObject.getString("时间:");
                    String xmnr = jsonObject.getString("项目内容:");
                    RecruitDataMap<Object> map = CheckRepeatResume.getDateRange(sj, false);
                    map.put("xmmc", xmmc);
                    map.put("xmms", formatStr(xmnr));
                    list.add(map);
                }
            } catch (Exception e) {
                new BaseBean().writeLog("项目经历解析失败", e);
            }
        }
        return list;
    }

    public List<RecruitDataMap<Object>> getJyjl() {
        List<RecruitDataMap<Object>> list = new ArrayList<>();
        if (StringUtils.isNotBlank(jyjl)) {
            try {
                JSONArray jsonArray = JSONArray.parseArray(jyjl);
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String byyx = jsonObject.getString("毕业院校:");
                    String xl = jsonObject.getString("学历:");
                    String sj = jsonObject.getString("时间:");
                    String zy = jsonObject.getString("专业:");
                    RecruitDataMap<Object> map = CheckRepeatResume.getDateRange(sj, true);
                    map.put("xxmc", byyx);
                    map.put("xl", ModeBrowserCommonInfo.getEducationLevelId(xl));
                    map.put("zy", zy);
                    list.add(map);
                }
            } catch (Exception e) {
                new BaseBean().writeLog("教育经历解析失败", e);
            }
        }
        return list;
    }


    private String formatStr(String str) {
        if (StringUtils.isBlank(str)) {
            return "";
        }
        return str.replaceAll("[\\pC]", "").replace("\\n", "\n").replace("\\r", "");
    }
}
