//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.kingdee.eas.custom.lgl.service;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.BOSObjectFactory;
import com.kingdee.bos.Context;
import com.kingdee.bos.bsf.service.app.IHRMsfService;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.codingrule.CodingRuleManagerFactory;
import com.kingdee.eas.base.codingrule.ICodingRuleManager;
import com.kingdee.eas.base.permission.UserFactory;
import com.kingdee.eas.base.permission.UserInfo;
import com.kingdee.eas.basedata.hraux.DiplomaFactory;
import com.kingdee.eas.basedata.hraux.DiplomaInfo;
import com.kingdee.eas.basedata.hraux.StandingFactory;
import com.kingdee.eas.basedata.hraux.StandingInfo;
import com.kingdee.eas.basedata.org.AdminOrgUnitFactory;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.HROrgUnitFactory;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.basedata.org.PositionCollection;
import com.kingdee.eas.basedata.org.PositionFactory;
import com.kingdee.eas.basedata.org.PositionInfo;
import com.kingdee.eas.basedata.person.Genders;
import com.kingdee.eas.basedata.person.PersonCollection;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.framework.ICoreBase;
import com.kingdee.eas.hr.affair.EmpEnrollBizBillEntryInfo;
import com.kingdee.eas.hr.affair.EmpEnrollBizBillFactory;
import com.kingdee.eas.hr.affair.EmpEnrollBizBillInfo;
import com.kingdee.eas.hr.base.AffairActionReasonFactory;
import com.kingdee.eas.hr.base.AffairActionReasonInfo;
import com.kingdee.eas.hr.base.BaseException;
import com.kingdee.eas.hr.base.EmployeeTypeFactory;
import com.kingdee.eas.hr.base.EmployeeTypeInfo;
import com.kingdee.eas.hr.base.HRBillStateEnum;
import com.kingdee.eas.hr.base.HRBizDefineFactory;
import com.kingdee.eas.hr.base.HRBizDefineInfo;
import com.kingdee.eas.hr.base.VariationReasonFactory;
import com.kingdee.eas.hr.base.VariationReasonInfo;
import com.kingdee.eas.hr.base.util.IDCardCommonUtil;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.compensation.CmpItemFactory;
import com.kingdee.shr.compensation.CmpItemInfo;
import com.kingdee.shr.compensation.FixAdjustSalaryFactory;
import com.kingdee.shr.compensation.FixAdjustSalaryInfo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

public class SaveEmpEnrollBizBillService implements IHRMsfService {
    private static Logger logger = Logger.getLogger("com.kingdee.eas.custom.service.SaveEmpEnrollBizBillService");

    public Object process(Context arg0, Map arg1) throws EASBizException, BOSException {
        System.out.println("开始执行入职单接口...");
        new HashMap();
        String name = (String)arg1.get("name");
        String position = (String)arg1.get("position");
        String joinDate = (String)arg1.get("joinDate");
        String idcard = (String)arg1.get("idcard");
        String sex = (String)arg1.get("sex");
        String birthDate = (String)arg1.get("birthDate");
        String tel = (String)arg1.get("tel");
        String rank = (String)arg1.get("rank");
        String personLev = (String)arg1.get("personLev");
        String lowerDep = (String)arg1.get("lowerDep");
        String probation = (String)arg1.get("probation");
        String becomeDate = (String)arg1.get("becomeDate");
        String yearBonus = (String)arg1.get("yearBonus");
        String applier = (String)arg1.get("applier");
        String applyDate = (String)arg1.get("applyDate");
        String oaNumber = (String)arg1.get("oaNumber");
        String endlishName = (String)arg1.get("englishName");
        String diplNumber = (String)arg1.get("diplNumber");
        String hRBizDefine = (String)arg1.get("hRBizDefine");
        String zwjb = (String)arg1.get("zwjb");
        String sapDep = (String)arg1.get("sapDep");
        String yybmbm = (String)arg1.get("yybmbm");
        String yybm = (String)arg1.get("yybm");
        if (StringUtils.isNotBlank(sapDep) && sapDep.indexOf("-") > 0) {
            String[] sapDeps = sapDep.split("-");
            if(sapDeps.length == 2){
                yybmbm = sapDeps[0];
                yybm = sapDeps[1];
            }else  if(sapDeps.length == 1){
                yybmbm = sapDeps[0];
            }
        }

        String groupClass = (String)arg1.get("groupClass");
        String groupClassDetails = (String)arg1.get("groupClassDetails");
        String reimLevel = (String)arg1.get("reimLevel");
        String bpcbmfldm = (String)arg1.get("bpcbmfldm");
        String fyfl1 = (String)arg1.get("fyfl1");
        String fyfl2 = (String)arg1.get("fyfl2");
        String bmfl3 = (String)arg1.get("bmfl3");
        String bmfl4 = (String)arg1.get("bmfl4");
        if (StringUtils.isNotBlank(groupClass) && groupClass.indexOf("-") > 0) {
            String[] groupClasss = groupClass.split("-");
            if (groupClasss.length == 3) {
                fyfl1 = groupClasss[0];
                fyfl2 = groupClasss[1];
                bmfl3 = groupClasss[2];
            }else  if(groupClasss.length == 2) {
                fyfl1 = groupClasss[0];
                fyfl2 = groupClasss[1];
            }else  if(groupClasss.length == 1) {
                fyfl1 = groupClasss[0];
            }

        }

        System.out.println("获取的参数值：" + ",name=" + name + ",position=" + position + ",joinDate=" + joinDate + ",idcard=" + idcard + ",sex=" + sex + ",birthDate=" + birthDate + ",tel=" + tel + ",rank=" + rank + ",personLev=" + personLev + ",lowerDep=" + lowerDep + ",probation=" + probation + ",becomeDate=" + becomeDate + ",yearBonus=" + yearBonus + ",applier=" + applier + ",applyDate=" + applyDate + ",oaNumber=" + oaNumber);
        Map resultMap = this.createEmpEnrollBizBill(arg0, name, position, joinDate, idcard, sex, birthDate, tel, rank, personLev, lowerDep, probation, becomeDate, yearBonus, applier, applyDate, oaNumber, diplNumber, endlishName, hRBizDefine, zwjb, yybmbm, groupClass, reimLevel, yybm, bpcbmfldm, groupClassDetails, fyfl1, fyfl2, bmfl3, groupClassDetails);
        System.out.println("执行入职单接口结束...");
        return resultMap;
    }

    public Map<Object, Object> createEmpEnrollBizBill(Context ctx, String name, String position, String joinDate, String idcard, String sex, String birthDate, String tel, String rank, String personLev, String lowerDep, String probation, String becomeDate, String yearBonus, String applier, String applyDate, String oaNumber, String diplNumber, String endlishName, String hRBizDefine, String zwjb, String yybmbm, String groupClass, String reimLevel, String yybm, String bpcbmfldm, String groupClassDetails, String fyfl1, String fyfl2, String bmfl3, String bmfl4) throws BOSException {
        Map<Object, Object> reurnMap = new HashMap();
        String msg = null;
        String fid = null;
        String fnumber = null;
        StringBuffer reason = new StringBuffer();
        String msgType = null;
        String rankSql = null;
        String xjbmSql = null;
        String number = null;
        String cp = null;
        Date cpDate = null;
        EmpEnrollBizBillInfo info = new EmpEnrollBizBillInfo();
        EmpEnrollBizBillEntryInfo enInfo = new EmpEnrollBizBillEntryInfo();
        enInfo.put("yybmbm", yybmbm);
        enInfo.put("groupClass", groupClass);
        PersonInfo ccPeInfo = new PersonInfo();
        if (zwjb != null && "" != zwjb) {
            ICoreBase coreBase = (ICoreBase)BOSObjectFactory.createBOSObject(ctx, new BOSObjectType("DCBAAD05"));
            CoreBaseCollection coreBaseColl = coreBase.getCollection(" where number = '" + zwjb + "'");
            if (coreBaseColl.size() > 0) {
                enInfo.put("zwjb", coreBaseColl.get(0));
            }
        }

        enInfo.put("reimLevel", reimLevel);
        enInfo.put("yybm", yybm);
        enInfo.put("bpcbmfldm", bpcbmfldm);
        enInfo.put("groupClassDetails", groupClassDetails);
        enInfo.put("fyfl1", fyfl1);
        enInfo.put("fyfl2", fyfl2);
        enInfo.put("bmfl3", bmfl3);
        enInfo.put("bmfl4", bmfl4);
        info.setNumber("rz" + System.currentTimeMillis());
        info.setBillState(HRBillStateEnum.SAVED);

        try {
            if (applier != null && applier != "") {
                PersonCollection apCol = PersonFactory.getLocalInstance(ctx).getPersonCollection(" where number = '" + applier + "'");
                if (apCol.size() > 0) {
                    info.setApplier(apCol.get(0));
                } else {
                    reason.append("创建人编码获取信息有误，请检查传入参数applier");
                }
            } else {
                reason.append("创建人编码为空，请检查传入参数applier");
            }

            HROrgUnitInfo hrInfo = HROrgUnitFactory.getLocalInstance(ctx).getHROrgUnitInfo(new ObjectUuidPK("00000000-0000-0000-0000-000000000000CCE7AED4"));
            info.setHrOrgUnit(hrInfo);
            if (applyDate != null && applyDate != "") {
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                info.setApplyDate(format1.parse(applyDate));
            } else {
                info.setApplyDate(new Date());
            }

            if (name != null && name != "") {
                enInfo.setEmpName(name);
                ccPeInfo.setName(name);
            } else {
                reason.append("员工姓名为空，请检查传入参数name");
            }

            new PositionInfo();
            new PositionCollection();
            new AdminOrgUnitInfo();
            AdminOrgUnitInfo companyInfo = new AdminOrgUnitInfo();
            if (position != null && position != "") {
                PositionCollection poCol = PositionFactory.getLocalInstance(ctx).getPositionCollection(" where number = '" + position + "'");
                if (poCol.size() > 0) {
                    PositionInfo poInfo = poCol.get(0);
                    AdminOrgUnitInfo adInfo = AdminOrgUnitFactory.getLocalInstance(ctx).getAdminOrgUnitInfo(new ObjectUuidPK(poInfo.getAdminOrgUnit().getId()));
                    companyInfo = AdminOrgUnitFactory.getLocalInstance(ctx).getAdminOrgUnitInfo(new ObjectUuidPK(adInfo.getCompany().getId()));
                    enInfo.setPosition(poInfo);
                    enInfo.setAdminOrg(adInfo);
                    enInfo.setCompany(companyInfo);
                } else {
                    reason.append("职位编码获取信息有误，请检查传入参数position");
                }
            } else {
                reason.append("职位编码为空，请检查传入参数position");
            }

            if (joinDate != null && joinDate != "") {
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                enInfo.setBizDate(format1.parse(joinDate));
            } else {
                reason.append("入职日期为空，请检查传入参数joinDate");
            }

            if (idcard != null && idcard != "") {
                try {
                    new IDCardCommonUtil(idcard);
                    enInfo.setIDCardNo(idcard);
                    ccPeInfo.setIdCardNO(idcard);
                } catch (BaseException e) {
                    e.printStackTrace();
                    reason.append(e.getMessage());
                }
            } else {
                reason.append("身份证号码为空，请检查传入参数idcard");
            }

            if (sex != null && sex != "") {
                if (sex.equals("女")) {
                    enInfo.setGender(Genders.Female);
                    ccPeInfo.setGender(Genders.Female);
                } else {
                    enInfo.setGender(Genders.Male);
                    ccPeInfo.setGender(Genders.Male);
                }
            } else {
                reason.append("性别为空，请检查传入参数sex");
            }

            if (birthDate != null && birthDate != "") {
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                enInfo.setBirthday(format1.parse(birthDate));
                ccPeInfo.setBirthday(format1.parse(birthDate));
            } else {
                reason.append("出生日期为空，请检查传入参数birthDate");
            }

            if (tel != null && tel != "") {
                enInfo.setTelNum(tel);
                ccPeInfo.setCell(tel);
            } else {
                reason.append("手机号码为空，请检查传入参数tel");
            }

            StandingInfo sdInfo = new StandingInfo();
            if (rank != null && rank != "") {
                if (rank.equals("工人")) {
                    sdInfo = StandingFactory.getLocalInstance(ctx).getStandingInfo(" where name = '工人'");
                    enInfo.setStanding(sdInfo);
                    ccPeInfo.setStanding(sdInfo);
                } else if (rank.equals("职员")) {
                    sdInfo = StandingFactory.getLocalInstance(ctx).getStandingInfo(" where name = '职员'");
                    enInfo.setStanding(sdInfo);
                    ccPeInfo.setStanding(sdInfo);
                }
            } else {
                reason.append("入职员工身份类型为空，请检查传入参数rank");
            }

            new EmployeeTypeInfo();
            if (personLev != null && personLev != "") {
                EmployeeTypeInfo emInfo;
                if (personLev.indexOf("试用") >= 0) {
                    emInfo = EmployeeTypeFactory.getLocalInstance(ctx).getEmployeeTypeInfo(" where name = '试用员工'");
                } else {
                    emInfo = EmployeeTypeFactory.getLocalInstance(ctx).getEmployeeTypeInfo(" where name = '正式员工'");
                }

                enInfo.setEmpType(emInfo);
            } else {
                reason.append("入职员工用工关系状态为空，请检查传入参数personLev");
            }

            new HRBizDefineInfo();
            StringBuilder sql = new StringBuilder("select p.fid,p.FEmployeeTypeID,pp.FPrimaryPositionID,pp.FPersonDep from t_bd_person p ");
            sql.append(" left join T_HR_PersonPosition pp on pp.fpersonid=p.fid ");
            sql.append(" where p.FIDCardNO='" + idcard + "'");
            IRowSet rs9 = DbUtil.executeQuery(ctx, sql.toString());
            if (rs9.next()) {
                PersonInfo personInfo = new PersonInfo();
                personInfo.setId(BOSUuid.read(rs9.getString("fid")));
                new PositionInfo();
                PositionInfo positionInfo = PositionFactory.getLocalInstance(ctx).getPositionInfo(new ObjectUuidPK(rs9.getString("FPrimaryPositionID")));
                AdminOrgUnitInfo adminOrgUnitInfo = AdminOrgUnitFactory.getLocalInstance(ctx).getAdminOrgUnitInfo(new ObjectUuidPK(rs9.getString("FPersonDep")));
                enInfo.setOldPosition(positionInfo);
                enInfo.setOldAdminOrg(adminOrgUnitInfo);
                EmployeeTypeInfo employeeTypeInfo = EmployeeTypeFactory.getLocalInstance(ctx).getEmployeeTypeInfo(new ObjectUuidPK(rs9.getString("FEmployeeTypeID")));
                enInfo.setOldEmpType(employeeTypeInfo);
                HRBizDefineInfo hrdInfo = HRBizDefineFactory.getLocalInstance(ctx).getHRBizDefineCollection(" where number='SHR02'").get(0);
                enInfo.setHrBizDefine(hrdInfo);
                enInfo.setPerson(personInfo);
                enInfo.setUseOldNumber(false);
                enInfo.setEnrollAgain(true);
                enInfo.setIsPrimary(false);
            } else {
                enInfo.setHrBizDefine(HRBizDefineFactory.getLocalInstance(ctx).getHRBizDefineInfo(new ObjectUuidPK("DawAAAApC9DmaL7Z")));
            }

            new AffairActionReasonInfo();
            AffairActionReasonInfo affInfo = AffairActionReasonFactory.getLocalInstance(ctx).getAffairActionReasonInfo(new ObjectUuidPK("1wXho31/Tsi0LhiLKfCQ+JYRae4="));
            enInfo.setAffairActionReason(affInfo);
            new VariationReasonInfo();
            VariationReasonInfo varInfo = VariationReasonFactory.getLocalInstance(ctx).getVariationReasonInfo(new ObjectUuidPK("3LGpEFtuQoWg7hgx2AmOeOas36w="));
            enInfo.setVariationReason(varInfo);
            if (diplNumber != null) {
                new DiplomaInfo();
                DiplomaInfo dipl = DiplomaFactory.getLocalInstance(ctx).getDiplomaInfo(" where number ='" + diplNumber + "'");
                enInfo.setString("diploma", dipl.getId().toString());
                enInfo.setBoolean("isHighest", true);
            }

            if (probation != null && probation != "") {
                enInfo.setProbation(Integer.valueOf(probation));
            } else {
                reason.append("试用期为空，请检查传入参数probation");
            }

            if (becomeDate != null && becomeDate != "") {
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                enInfo.setPlanFormalDate(format1.parse(becomeDate));
            } else {
                reason.append("预计转正日期为空，请检查传入参数becomeDate");
            }

            if (yearBonus == null || yearBonus == "") {
                reason.append("年终奖系数为空，请检查传入参数yearBonus");
            }

            if (null != endlishName && !"".equals(endlishName)) {
                enInfo.setString("endlishName", endlishName);
            } else {
                enInfo.setString("endlishName", (String)null);
            }

            enInfo.setString("oaNumber", oaNumber);
            enInfo.setId(BOSUuid.create("04BEFD73"));
            info.setId(BOSUuid.create("B41CAA3F"));
            info.setIsMultiEntry(false);
            info.getEntrys().add(enInfo);
            System.out.println("info:" + info);
            if (reason.length() <= 0) {
                enInfo.setUseOldNumber(false);
                String qz = companyInfo.getFax();
                ICodingRuleManager iCodingRuleManager = CodingRuleManagerFactory.getLocalInstance(ctx);
                number = iCodingRuleManager.getNumber(ccPeInfo, hrInfo.getId().toString());
                enInfo.setEmpNumber(qz + number);
            }

            rankSql = "UPDATE T_BD_PERSON SET FSTANDINGID='" + sdInfo.getId() + "'  WHERE FNUMBER = '" + number + "'";
            xjbmSql = "UPDATE T_BD_PERSON SET CFSubordinateDepar=(SELECT FID FROM CT_CUS_Xjbm where FNumber = '" + lowerDep + "') WHERE FNUMBER = '" + number + "'";
        } catch (Exception e) {
            e.printStackTrace();
            reason.append(e.getMessage());
        }

        enInfo.setBill(info);
        if (reason.length() > 0) {
            msgType = "0";
            msg = "失败";
        } else {
            try {
                IObjectPK pk = EmpEnrollBizBillFactory.getLocalInstance(ctx).save(info);
                fid = pk.toString();
            } catch (EASBizException e) {
                reason.append(e.getMessage());
            } catch (BOSException e) {
                reason.append(e.getMessage());
            }

            if (reason.length() > 0) {
                msgType = "0";
                msg = "失败";
            } else {
                msg = "成功";
                msgType = "1";
                String nuSql = "select FNumber from T_HR_EmpEnrollBizBill where FID = '" + fid + "'";

                try {
                    IRowSet nuSet = DbUtil.executeQuery(ctx, nuSql.toString());
                    if (nuSet.next()) {
                        fnumber = nuSet.getString("FNumber");
                    }
                } catch (BOSException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                this.setOtherParam(ctx, lowerDep, yearBonus, fid);
            }
        }

        reurnMap.put("reason", reason.toString());
        reurnMap.put("msgType", msgType);
        reurnMap.put("msg", msg);
        reurnMap.put("fid", fid);
        reurnMap.put("fnumber", fnumber);
        return reurnMap;
    }

    private void setOtherParam(Context ctx, String lowerDep, String yearBonus, String id) {
        String xjbmSql = "UPDATE T_HR_EmpEnrollBizBillEntry SET CFXjbmID = (SELECT FID FROM CT_CUS_Xjbm where FNumber = '" + lowerDep + "')  WHERE FBillID = '" + id + "' ";
        String nzjSql = "UPDATE T_HR_EmpEnrollBizBillEntry SET CFNzjfaID = (SELECT FID FROM CT_MP_Nzjfa where FName_l2 = '" + yearBonus + "')  WHERE FBillID = '" + id + "' ";

        try {
            DbUtil.execute(ctx, xjbmSql);
            DbUtil.execute(ctx, nzjSql);
        } catch (BOSException e) {
            e.printStackTrace();
        }

    }

    private void setCompProgram(Context ctx, String compProgram, Date cpDate, String perNumber, String applier) {
        int woMon = 0;
        new CmpItemInfo();
        UserInfo usInfo = new UserInfo();

        try {
            Properties prop = new Properties();
            prop.load(new FileInputStream(System.getProperty("EAS_HOME") + "/server/properties/lgl/cmpItem.properties"));
            String nums = prop.getProperty("number1");
            CmpItemInfo cmInfo = CmpItemFactory.getLocalInstance(ctx).getCmpItemInfo("where number='" + nums + "'");
            String sqls = "select us.FID ID from T_PM_USER us INNER JOIN T_BD_PERSON pe ON US.FPERSONID = PE.FID WHERE pe.FNUMBER = '" + applier + "'";
            IRowSet asSet = DbUtil.executeQuery(ctx, sqls.toString());
            if (asSet.next()) {
                usInfo = UserFactory.getLocalInstance(ctx).getUserInfo(new ObjectUuidPK(asSet.getString("ID")));
            }

            woMon = Integer.parseInt(compProgram);
            FixAdjustSalaryInfo info = new FixAdjustSalaryInfo();
            info.setId(BOSUuid.create("0715640C"));
            info.setCmpItem(cmInfo);
            info.setMoney(new BigDecimal(woMon));
            info.setEffectDay(cpDate);
            info.setCreateTime(new Timestamp(System.currentTimeMillis()));
            info.setCreator(usInfo);
            PersonInfo personInfo = PersonFactory.getLocalInstance(ctx).getPersonCollection(" where number = '" + perNumber + "'").get(0);
            info.setPerson(personInfo);
            FixAdjustSalaryFactory.getLocalInstance(ctx).save(info);
        } catch (EASBizException e) {
            e.printStackTrace();
        } catch (BOSException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
