package weaver.interfaces.mingfeng.job;

import org.apache.commons.lang.StringUtils;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.ArrayList;
import java.util.List;

public class ActualWorkYearJob extends BaseCronJob {

    BaseBean basebean = new BaseBean();

    @Override
    public void execute() {

        basebean.writeLog("----------ActualWorkYearJob Start----------");

        RecordSet rs = new RecordSet();
        //根据实际参加工作时间计算实际工龄

        String actualWorkYearField = basebean.getPropValue("syn_othersys_mfkj","ActualWorkYear");//实际工龄
        String actualWorkStartDateField =basebean.getPropValue("syn_othersys_mfkj","ActualWorkStartDate");//实际参加工作时间

        List<String> resList = new ArrayList<String>();
        String acqResSql = "select id from hrmresource";
        rs.executeQuery(acqResSql);
        while (rs.next()) {
            String id = Util.null2String(rs.getString("id"));
            if ( StringUtils.isNotBlank(id) ) {
                resList.add(id);
            }
        }

        if ( !resList.isEmpty()) {
            for (String id : resList) {
                calculateActualWorkYear( actualWorkStartDateField, actualWorkYearField, id, rs);
            }
        }

    }

    /**
     * 计算实际工龄
     */
    private void calculateActualWorkYear(String actualWorkStartDateField, String actualWorkYearField, String resourceId, RecordSet rs) {
        String sql = "";

        try{
            if(Util.getIntValue(resourceId)<=0){
                //无效的人员id
                return;
            }
            String currentdate = DateUtil.getCurrentDate();
            String actualWorkstartdate = "";

            String status = "";
            String dismissdate = "";
            double actualWorkyear = 0;

            sql = " select a.status ,b." + actualWorkStartDateField +" " +
                    "from HrmResource a " +
                    "left join cus_fielddata b " +
                    "on b.id = a.id and b.scopeid = 3 and scope = 'HrmCustomFieldByInfoType' " +
                    "where a.id = ?";
            rs.executeQuery(sql,resourceId);
            if(rs.next()){
                actualWorkstartdate = Util.null2String(rs.getString(actualWorkStartDateField));
                status = Util.null2String(rs.getString("status"));
            }

            if("5".equals(status)) {
                sql = " SELECT MAX(changedate) as dismissdate FROM HrmStatusHistory WHERE resourceid= ? ";
                rs.executeQuery(sql, resourceId);
                if (rs.next()) {
                    dismissdate = rs.getString("dismissdate");
                }
            }

            //离职人员只计算到离职日期
            currentdate = dismissdate.length()>0?dismissdate:currentdate;

            if( actualWorkstartdate.length()>0){
                actualWorkstartdate = DateUtil.compDate(currentdate,actualWorkstartdate)>0?currentdate:actualWorkstartdate;
                actualWorkyear = DateUtil.dayDiff(actualWorkstartdate,currentdate)/365.0;
            }
            actualWorkyear = Math.round(actualWorkyear * 100.0) / 100.0;

            //更新工龄 司龄
            sql = " UPDATE cus_fielddata SET " + actualWorkYearField + "=? where id = ? ";
            rs.executeUpdate(sql,actualWorkyear,resourceId);

        }catch (Exception e){
            basebean.writeLog("ActualWorkYearJob calculateActualWorkYear Exception: " + e);
        }

    }


}
