package weaver.interfaces.mingfeng.action;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.tools.HrmDateCheck;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 根据修改信息字段选择的类型，去更新人力资源表对应字段的值
 */
public class HrmInfosChangeAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean basebean = new BaseBean();
        RecordSet rs = new RecordSet();

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

            Integer changeType = -1;

            Integer resourceId = -1;
            String[] workflowFieldsName = {"xghjtzz","xghhyzk","sjgl","xghxl","xghzzmm","xghgzkh"};
            String[] hrmFieldsName = {"residentplace","maritalstatus",actualWorkYearField,"educationlevel","policy","accountid1"};

            MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
            Property[] properties = mainTableInfo.getProperty();
            for ( Property property : properties ) {
                if ( "xgxx".equals(property.getName())) {
                    changeType = Util.getIntValue(Util.null2String(property.getValue()));
                    basebean.writeLog("xgxx: ", changeType);
                }
                if ( "sqr".equals(property.getName())) {
                    resourceId = Util.getIntValue(Util.null2String(property.getValue()));
                    basebean.writeLog("resourceId: ", resourceId);
                }
            }

            if ( changeType >-1 && changeType < 6 && resourceId > -1 ) {
                String workflowFieldName = workflowFieldsName[changeType];
                basebean.writeLog("workflowFieldName: ", workflowFieldName);

                String workflowFieldValue = "";
                for ( Property property : properties ) {
                    if ( workflowFieldName.equals(property.getName())) {
                        workflowFieldValue = Util.null2String(property.getValue());
                        basebean.writeLog("workflowFieldValue: ", workflowFieldValue);
                    }
                }

                if ( StringUtils.isNotBlank(workflowFieldValue)) {
                    String hrmFieldName = hrmFieldsName[changeType];
                    basebean.writeLog("hrmFieldName: ", hrmFieldName);

                    if ( StringUtils.isNotBlank(hrmFieldName)) {

                        if ( actualWorkYearField.equals(hrmFieldName)) {//实际工龄

                            basebean.writeLog("hrmFieldName is actualWorkYearField ");

                            //根据实际工龄，计算出实际参加工作日期
                            String actualWorkYear = workflowFieldValue;
                            basebean.writeLog("actualWorkYear: " + actualWorkYear);

                            String nowDate = DateUtil.format(new Date(),"yyyy-MM-dd");
                            basebean.writeLog("nowDate: " + nowDate);

                            //计算出实际天数
                            Double actualWorkYearDou = Double.parseDouble(workflowFieldValue);
                            long daysNumber = Math.round(actualWorkYearDou * 365.0);
                            Integer daysInt = Integer.parseInt(String.valueOf(-daysNumber));

                            //根据实际工作天数求出实际开始工作日期
                            Calendar cal = Calendar.getInstance();
                            cal.setTime(new Date());
                            cal.add(Calendar.DAY_OF_MONTH, daysInt);
                            String startDateStr = DateUtil.format(cal.getTime(),"yyyy-MM-dd");

                            basebean.writeLog("startDateStr: " + startDateStr);

                            //确认该人员是否已经存在scopeID=3 and scope = 'HrmCustomFieldByInfoType' 的数据
                            boolean isExist =false;
                            String checkSql = "select count(*) as number from cus_fielddata where id = ? and scopeid = 3 and scope = 'HrmCustomFieldByInfoType'";
                            basebean.writeLog("checkSql: " + checkSql);

                            rs.executeQuery(checkSql,resourceId);
                            while (rs.next()) {
                                int number = Util.getIntValue(Util.null2String(rs.getString("number")));
                                if ( number >= 1) {
                                    isExist = true;
                                }
                            }
                            basebean.writeLog("isExist: " + isExist);

                            if (isExist) {
                                String syncSql = "update cus_fielddata set " + actualWorkStartDateField + " =?, "+actualWorkYearField+" = ? where id = ? and scopeid = 3 and scope = 'HrmCustomFieldByInfoType'";
                                basebean.writeLog("syncSql: " + syncSql);
                                basebean.writeLog("resourceId: " + resourceId);
                                basebean.writeLog("workflowFieldValue: " + workflowFieldValue);

                                rs.executeUpdate(syncSql,startDateStr,workflowFieldValue,resourceId);

                            } else {
                                String addSql = " insert into cus_fielddata (id,scopeid,scope,"+actualWorkStartDateField+","+actualWorkYearField+") values (?,?,?,?,?)";
                                basebean.writeLog("syncSql: " + addSql);
                                basebean.writeLog("resourceId: " + resourceId);
                                basebean.writeLog("workflowFieldValue: " + workflowFieldValue);

                                rs.executeUpdate(addSql,resourceId,3,"HrmCustomFieldByInfoType",startDateStr,workflowFieldValue);
                            }

                        } else {
                            String changeValueSql = "update hrmresource set "+hrmFieldName+" = ? where id = ?";
                            basebean.writeLog("changeValueSql: ", changeValueSql);

                            rs.executeUpdate(changeValueSql,workflowFieldValue,resourceId);
                        }

                    }

                }

            }
        } catch (Exception e) {
            basebean.writeLog("HrmInfosChangeAction Exception: " + e);
            return Action.SUCCESS;
        }

        return Action.SUCCESS;
    }
}
