package com.engine.organization.entity.staff.bo;

import com.engine.organization.entity.staff.param.StaffSearchParam;
import com.engine.organization.entity.staff.po.StaffPO;
import com.engine.organization.transmethod.StaffTransMethod;
import weaver.general.Util;

import java.util.Date;

/**
 * @author:dxfeng
 * @createTime: 2022/05/25
 * @version: 1.0
 */
public class StaffBO {
    public static StaffPO convertParamToPO(StaffSearchParam param, Long employeeId) {
        if (param == null) {
            return null;
        }
        StaffPO staffPO = StaffPO.builder()
                .id(param.getId())
                .planId(param.getPlanId())
                .compId(param.getCompId())
                .ecCompany(param.getEcCompany())
                .deptId(param.getDeptId())
                .ecDepartment(param.getEcDepartment())
                .jobId(param.getJobId())
                .staffNum(param.getStaffNum())
                .controlPolicy(param.getControlPolicy())
                .permanentNum(param.getPermanentNum() == null ? 0 : param.getPermanentNum())
                .freezeNum(param.getFreezeNum() == null ? 0 : param.getFreezeNum())
                .lackStatus(param.getLackStatus())
                .staffDesc(param.getStaffDesc())
                .description(param.getDescription())
                .deleteType(0)
                .createTime(new Date())
                .updateTime(new Date())
                .creator(employeeId)
                .build();
        buildStaffDesc(staffPO);
        return staffPO;
    }

    public static void buildStaffDesc(StaffPO staffPO) {
        staffPO.setLackStatus(parseLackStatus(null == staffPO.getStaffNum() ? 0 : staffPO.getStaffNum(), null == staffPO.getPermanentNum() ? 0 : staffPO.getPermanentNum()));
        String sb = "编制数：" + parseNull(staffPO.getStaffNum()) +
                ",在编数：" + parseNull(staffPO.getPermanentNum()) +
                ",冻结数：" + parseNull(staffPO.getFreezeNum()) +
                ",缺编状态：" + StaffTransMethod.getLackSpan(Util.null2String(staffPO.getLackStatus()));
        staffPO.setStaffDesc(sb);

    }


    /**
     * null 转换为 0
     *
     * @param number
     * @return
     */
    private static Integer parseNull(Integer number) {
        return null == number ? 0 : number;

    }

    /**
     * 转换缺编状态
     *
     * @param staffNum
     * @param permanentNum
     * @return
     */
    private static int parseLackStatus(int staffNum, int permanentNum) {
        if (staffNum == permanentNum) {
            // 满员
            return 2;
        }
        if (staffNum > permanentNum) {
            // 缺编
            return 1;
        }
        // 超编
        return 3;
    }
}
