package com.jeeplus.modules.wexinpackage.access.util.access;

import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.wexinpackage.access.entity.ChangeNumber;
import com.jeeplus.modules.wexinpackage.access.entity.WeChatUser;
import com.jeeplus.modules.workstaff.entity.WorkStaffBasicInfo;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class ContactUtil {
    //通讯录事件类型
    public final static String CHANGE_CONTACT = "change_contact";
    public final static String UPDATE_USER = "update_user";
    public final static String CREATE_USER = "create_user";
    public final static String DELETE_USER = "delete_user";

    //map集合转换成实体类
    public static <T> T map2Object(Map<String, String> map, Class<T> clazz) {
        if (map == null) {
            return null;
        }
        T obj = null;
        try {
            // 使用newInstance来创建对象
            obj = clazz.newInstance();
            // 获取类中的所有字段
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                int mod = field.getModifiers();
                // 判断是拥有某个修饰符
                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                    continue;
                }
                // 当字段使用private修饰时，需要加上
                field.setAccessible(true);
                // 获取参数类型名字
                String filedTypeName = field.getType().getName();
                // 判断是否为时间类型，使用equalsIgnoreCase比较字符串，不区分大小写
                // 给obj的属性赋值
                field.set(obj, map.get(field.getName()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
    //人员登记类转换
    public static WeChatUser replaceUser(WorkStaffBasicInfo workStaffBasicInfo){
        WeChatUser weChatUser = new WeChatUser();
        weChatUser.setUserid(workStaffBasicInfo.getNo());
        weChatUser.setName(workStaffBasicInfo.getName());
        weChatUser.setDepartment(1);
        weChatUser.setMobile(workStaffBasicInfo.getMobile());
        weChatUser.setEmail(workStaffBasicInfo.getEmail());
        weChatUser.setGender("1");
        weChatUser.setPosition(workStaffBasicInfo.getDuty());
        return weChatUser;
    }

    //获取保存到系统中对象
    public static WorkStaffBasicInfo getCreateWorkStaffBasicInfo(String roleId,Office office,WorkStaffBasicInfo workStaffBasicInfo, ChangeNumber changeNumber){

        Office company = new Office();
        company.setName("兴光项目");
        company.setId("ffe5af5b557e46f28b000c94716e228a");
        workStaffBasicInfo.setCompany(company);
        workStaffBasicInfo.setOffice(office);
//        String idCard = "";
//        workStaffBasicInfo.setIdCard(idCard);//身份证 -
        workStaffBasicInfo.setName(changeNumber.getName());//姓名 -- 微信姓名
        workStaffBasicInfo.setGender(changeNumber.getGender());//性别  --微信姓别性别，1表示男性，2表示女性
        workStaffBasicInfo.setNation("01");//民族
//        workStaffBasicInfo = ContactUtil.getDateAndAge(workStaffBasicInfo,idCard);//获取出生日期以及年龄
        workStaffBasicInfo.setCalendar("01");//公里农历
        workStaffBasicInfo.setEmail(changeNumber.getEmail());//邮箱
        workStaffBasicInfo.setMobile(changeNumber.getMobile());//移动电话
        workStaffBasicInfo.setPhone(changeNumber.getTelephone());//座机
        workStaffBasicInfo.setStatus("正式");//默认正式
        workStaffBasicInfo.setEntryDate(new Date());//入职日期
        workStaffBasicInfo.setMarriage("未婚");
        workStaffBasicInfo.setPolitics("普通居民");
        workStaffBasicInfo.setExSoldier("0");
        workStaffBasicInfo.setNotifyFlag("1");
        workStaffBasicInfo.setDelFlag("0");
        workStaffBasicInfo.setRoleId(roleId);
        workStaffBasicInfo.setIsNewRecord(false);
        return workStaffBasicInfo;
    }

    //获取创建日期，以及年龄
    public static WorkStaffBasicInfo getDateAndAge(WorkStaffBasicInfo workStaffBasicInfo,String idCard){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd");
        Date date = new Date();
        //得到当前时间日期
        if (null != idCard && !idCard.equals("")){
            String year = idCard.substring(6,10);//获取年
            String month = idCard.substring(10,12);//获取月
            String day = idCard.substring(12,14);//获取日
            String data = year+"-"+month+"-"+day;
            try {
                date = simpleDateFormat.parse(data);
                int ageByBirth = getAgeByBirth(date);
                workStaffBasicInfo.setBirthday(date);//出生日期
                workStaffBasicInfo.setAge(ageByBirth);//年龄
            } catch (ParseException e) {
                e.printStackTrace();
            }
            workStaffBasicInfo.setAge(25);//获取年龄
        }
        return workStaffBasicInfo;
    }

    /**
     * 根据用户生日精确计算年龄
     * 用Calender对象取得当前日期对象--从对象中分别取出年月日
     * @author Administrator
     *
     */
    private static int getAgeByBirth(Date birthday){
        //Calendar：日历
        /*从Calendar对象中或得一个Date对象*/
        Calendar cal = Calendar.getInstance();
        /*把出生日期放入Calendar类型的bir对象中，进行Calendar和Date类型进行转换*/
        Calendar bir = Calendar.getInstance();
        bir.setTime(birthday);
        /*如果生日大于当前日期，则抛出异常：出生日期不能大于当前日期*/
        if(cal.before(birthday)){
            throw new IllegalArgumentException("The birthday is before Now,It's unbelievable");
        }
        /*取出当前年月日*/
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayNow = cal.get(Calendar.DAY_OF_MONTH);
        /*取出出生年月日*/
        int yearBirth = bir.get(Calendar.YEAR);
        int monthBirth = bir.get(Calendar.MONTH);
        int dayBirth = bir.get(Calendar.DAY_OF_MONTH);
        /*大概年龄是当前年减去出生年*/
        int age = yearNow - yearBirth;
        /*如果出当前月小与出生月，或者当前月等于出生月但是当前日小于出生日，那么年龄age就减一岁*/
        if(monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)){
            age--;
        }
        return age;
    }
}
