package weaver.cronjob.mingfeng;

import com.weaver.file.Prop;
import weaver.conn.RecordSet;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 定时  所有人员 根据身份证号定时刷新出生日和年龄
 */
public class UserCustomFieldService{
    private final Logger log = LoggerFactory.getLogger(UserCustomFieldService.class);
    private static final String agefield_cus_name = Prop.getPropValue("UserCustomFieldCron","age");
    public synchronized void execute() {
        RecordSet recordSeta = new RecordSet();
        RecordSet recordSetb = new RecordSet();
        RecordSet recordSetc = new RecordSet();
        RecordSet recordSetd = new RecordSet();
        String sql = "select id,certificatenum from HrmResource where certificatenum is not null and certificatenum != ''";
        recordSeta.executeQuery(sql);
        while (recordSeta.next()) {
            String id = recordSeta.getString("id");
            String certificatenum = recordSeta.getString("certificatenum").trim();
            if(certificatenum.length() == 18) {
                String birthday = getBirthDay(certificatenum);
                if(birthday.length()>0) {
                    int age = calculateAge(birthday);
                    if(age >=0) {
                        //刷新值
                        String agestr= String.valueOf(age);
                        String sql1 = "update HrmResource set birthday = '"+birthday+"' where id = "+id;
                        recordSetb.executeUpdate(sql1);
                        log.error("howec::::::::::::::::1:"+sql1);
                        String sql3 = "select count(id) as nums from cus_fielddata where scopeid = 1 and scope = 'HrmCustomFieldByInfoType' and id = "+id;
                        recordSetd.executeQuery(sql3);
                        recordSetd.next();
                        int nums = recordSetd.getInt("nums");
                        if(nums>0) {
                            String sql2 = "update cus_fielddata set "+agefield_cus_name+" = '"+agestr+"' where scopeid = 1 and scope = 'HrmCustomFieldByInfoType' and id = "+id;
                            recordSetc.executeUpdate(sql2);
                            log.error("howec::::::::::::::::2:"+sql2);
                        }else {
                            String sql4 = "insert into cus_fielddata("+agefield_cus_name+",scopeid,scope,id) values(?,?,?,?)";
                            recordSetc.executeUpdate(sql4,agestr,1,"HrmCustomFieldByInfoType",id);
                        }
                    }
                }
            }
        }

    }

    public static int calculateAge(String birthDate){
        try{
            Calendar currentDate = Calendar.getInstance();
            Calendar dateOfBirth = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date dob = sdf.parse(birthDate);
            dateOfBirth.setTime(dob);
            int years = currentDate.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR);
            if (currentDate.get(Calendar.MONTH) < dateOfBirth.get(Calendar.MONTH) ||
                    (currentDate.get(Calendar.MONTH) == dateOfBirth.get(Calendar.MONTH) &&
                            currentDate.get(Calendar.DAY_OF_MONTH) < dateOfBirth.get(Calendar.DAY_OF_MONTH))) {
                years--;
            }
            return years;
        }catch (Exception e){
            return -1;
        }
    }
    public static String getBirthDay(String sfzh){
        try {
            String birthdayString = sfzh.substring(6, 14); // 截取身份证号的前六位数字作为出生日期
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            Date birthday = dateFormat.parse(birthdayString); // 解析为日期对象
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = dateFormat1.format(birthday);
            return dateString;
        }catch (Exception e){
            return "";
        }
    }
}
