package com.engine.organization.entity.resume.po;

import lombok.Data;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.IOUtils;
import weaver.file.ImageFileManager;
import weaver.general.GCONST;
import weaver.general.Util;

import java.io.*;
import java.nio.file.Files;
import java.util.Base64;

/**
 * @author:dxfeng
 * @createTime: 2022/12/29
 * @version: 1.0
 */
@Data
public class PersonnelResumePO {
    private Integer id;
    private String lastName;
    private String sex;
    private String birthday;
    // resourceImageId;
    private String image;
    private String imageId;
    private String nativePlace;
    // policy;
    private String politics;
    // departmentid;
    private String department;
    // maritalStatus;
    private String marriage;
    private String jobTitle;
    private String companyStartDate;
    private String workStartDate;
    // certificatenum
    private String idCard;
    // homeaddress
    private String address;
    private String telephone;
    private String email;
    private String selfStatement;

    public String getSex() {
        if ("1".equals(sex)) {
            return "女";
        }
        return "男";
    }

    public String getImage() {
        if (StringUtils.isBlank(image)) {
            return "";
        }
        ImageFileManager manager = new ImageFileManager();
        manager.getImageFileInfoById(Util.getIntValue(image));
        InputStream inputStream = manager.getInputStream();
        String imageStr = "";
        try {
            String outPutPath = GCONST.getRootPath() + "hrm" + File.separator + "import" + File.separator + "template" + File.separator
                    + manager.getImageFileName();
            File f = new File(outPutPath);
            if (!f.exists()) {
                String substring = outPutPath.substring(0, outPutPath.lastIndexOf(File.separator));
                File file = new File(substring);
                if (file.mkdirs()) {
                    boolean newFile = f.createNewFile();
                    if (!newFile) {
                        throw new IOException(outPutPath + "文件创建失败");
                    }
                }
            }
            Thumbnails.of(inputStream).forceSize(100, 120).toFile(f);
            InputStream fileInputStream = Files.newInputStream(f.toPath());
            imageStr = "data:image/" + manager.getImageFileType() + ";base64," + Base64.getEncoder().encodeToString(IOUtils.toByteArray(fileInputStream));
            // 删除文件
            f.delete();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return imageStr;
    }


    public String getMarriage() {
        if (StringUtils.isBlank(marriage)) {
            return "未婚";
        }
        switch (marriage) {
            case "1":
                return "已婚";
            case "2":
                return "离异";
            case "0":
            default:
                return "未婚";
        }
    }
}
