package jnpf.personnels.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import jnpf.model.personnels.dto.staff.field.SubMulitFieldValDto;
import jnpf.model.personnels.dto.staff.roster.WorkerGroupDataDto;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class PersonnelStaffUtils {
    public static String convertSexName(String value) {
        if ("1".equals(value)) {
            return "男";
        } else if ("2".equals(value)) {
            return "女";
        } else {
            return "";
        }
    }

    public static List<WorkerGroupDataDto> buildWorkerGroupDataDtoList(String affiliatedGroupData) {
        if (StringUtils.isEmpty(affiliatedGroupData)) {
            return CollectionUtil.newArrayList();
        }
        Gson gson = new Gson();
        Type type = new TypeToken<List<List<SubMulitFieldValDto>>>() {
        }.getType();
        List<List<SubMulitFieldValDto>> dataList = gson.fromJson(affiliatedGroupData, type);

        List<WorkerGroupDataDto> groupList = new ArrayList<>();
        for (List<SubMulitFieldValDto> subList : dataList) {
            WorkerGroupDataDto workerGroupDataDto = new WorkerGroupDataDto();
            for (SubMulitFieldValDto fieldValDto : subList) {
                if (fieldValDto.getId().equals("affiliatedOrg")) {
                    workerGroupDataDto.setAffiliatedOrg(fieldValDto.getUserValue());
                } else if (fieldValDto.getId().equals("affiliatedShop")) {
                    workerGroupDataDto.setAffiliatedShop(fieldValDto.getUserValue());
                } else if (fieldValDto.getId().equals("reportsTo")) {
                    workerGroupDataDto.setReportsTo(fieldValDto.getUserValue());
                } else if (fieldValDto.getId().equals("affiliatedPosition")) {
                    workerGroupDataDto.setAffiliatedPosition(fieldValDto.getUserValue());
                } else if (fieldValDto.getId().equals("affiliatedRank")) {
                    workerGroupDataDto.setAffiliatedRank(fieldValDto.getUserValue());
                }
            }
            groupList.add(workerGroupDataDto);

        }
        return groupList;
    }

    public static int calculatePeriodDays(String probationPeriod, String probationPeriodDay) {
        int maxDay = 0;
        if (probationPeriod.equals("101")) {//1个月内
            maxDay = Integer.valueOf(probationPeriodDay);
        } else if (probationPeriod.equals("102")) {
            maxDay = 30;
        } else if (probationPeriod.equals("103")) {
            maxDay = 30 * 2;
        } else if (probationPeriod.equals("104")) {
            maxDay = 30 * 3;
        } else if (probationPeriod.equals("105")) {
            maxDay = 30 * 4;
        } else if (probationPeriod.equals("106")) {
            maxDay = 30 * 5;
        } else if (probationPeriod.equals("107")) {
            maxDay = 30 * 6;
        } else if (probationPeriod.equals("108")) {
            maxDay = 30 * 7;
        } else if (probationPeriod.equals("109")) {
            maxDay = 30 * 8;
        }
        return maxDay;
    }


    public static Date getTodayDate() {
        Calendar calendar = Calendar.getInstance(); // 获取当前日期时间的Calendar实例
        calendar.set(Calendar.HOUR_OF_DAY, 0); // 设置小时为0点
        calendar.set(Calendar.MINUTE, 0); // 设置分钟为0分
        calendar.set(Calendar.SECOND, 0); // 设置秒数为0秒
        calendar.set(Calendar.MILLISECOND, 0); // 设置毫秒为0

        return calendar.getTime();
    }
}
