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 java.util.Date;

/**
 * @description:
 * @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;
        }
        return StaffPO.builder()
                .id(param.getId())
                .planId(param.getPlanId())
                .compId(param.getCompId())
                .deptId(param.getDeptId())
                .jobId(param.getJobId())
                .staffNum(param.getStaffNum())
                .controlPolicy(param.getControlPolicy())
                .permanentNum(param.getPermanentNum())
                .freezeNum(param.getFreezeNum())
                .lackStatus(param.getLackStatus())
                .staffDesc(param.getStaffDesc())
                .description(param.getDescription())
                .deleteType(0)
                .createTime(new Date())
                .updateTime(new Date())
                .creator(employeeId)
                .build();
    }

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

    }


    /**
     * 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;
    }
}
