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

import com.engine.organization.entity.staff.param.StaffParams;
import com.engine.organization.entity.staff.po.StaffPO;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author:dxfeng
 * @createTime: 2022/05/25
 * @version: 1.0
 */
public class StaffBO {
    public static StaffPO convertParamToPO(StaffParams 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) {
        List<Integer> numberF = Arrays.asList(staffPO.getFreezeNum(),staffPO.getPermanentNum());
        int sum = numberF.stream()
                .mapToInt(Integer::intValue)
                .sum();
        staffPO.setLackStatus(parseLackStatus(null == staffPO.getStaffNum() ? 0 : staffPO.getStaffNum(),sum));
//        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 sums
     * @return
     */
    private static int parseLackStatus(int staffNum, int sums) {
        if (staffNum == sums) {
            // 满员
            return 2;
        }
        if (staffNum > sums) {
            // 缺编
            return 1;
        }
        // 超编
        return 3;
    }
}
