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

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

import com.google.common.collect.Maps;
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.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.EmpEnrollBizBillCollection;
import com.kingdee.eas.hr.affair.EmpEnrollBizBillEntryCollection;
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.affair.EmpHireBizBillCollection;
import com.kingdee.eas.hr.affair.EmpHireBizBillEntryInfo;
import com.kingdee.eas.hr.affair.EmpHireBizBillFactory;
import com.kingdee.eas.hr.affair.EmpHireBizBillInfo;
import com.kingdee.eas.hr.affair.FluctuationBizBillCollection;
import com.kingdee.eas.hr.affair.FluctuationBizBillEntryCollection;
import com.kingdee.eas.hr.affair.FluctuationBizBillEntryInfo;
import com.kingdee.eas.hr.affair.FluctuationBizBillFactory;
import com.kingdee.eas.hr.affair.FluctuationBizBillInfo;
import com.kingdee.eas.hr.affair.ResignBizBillCollection;
import com.kingdee.eas.hr.affair.ResignBizBillFactory;
import com.kingdee.eas.hr.affair.ResignBizBillInfo;
import com.kingdee.eas.hr.base.ApproveTypeEnum;
import com.kingdee.eas.hr.base.HRBillStateEnum;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;

public class SubmitEffHrService implements IHRMsfService {
    public Object process(Context arg0, Map arg1) throws EASBizException, BOSException {
        String types = (String)arg1.get("types");
        String id = (String)arg1.get("id");
        Map<Object, Object> reurnMap = Maps.newHashMapWithExpectedSize(10);
        System.out.println("人事事务单据审批通过接口：" + arg1);
        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];
            }

        }

        if ("rz".equals(types)) {
            String probation = (String)arg1.get("probation");
            String yearBonus = (String)arg1.get("yearBonus");
            String becomeDate = (String)arg1.get("becomeDate");
            String becomeMon = (String)arg1.get("becomeMon");
            String compDate = (String)arg1.get("compDate");
            String bizDate = (String)arg1.get("bizDate");
            reurnMap = this.submitEffectRZ(arg0, id, becomeMon, compDate, probation, yearBonus, becomeDate, bizDate, zwjb, yybmbm, groupClass, reimLevel, yybm, bpcbmfldm, groupClassDetails, fyfl1, fyfl2, bmfl3, groupClassDetails);
        } else if ("lz".equals(types)) {
            String bizDate = (String)arg1.get("compDate");
            reurnMap = this.submitEffectLZ(arg0, id, bizDate);
        } else if ("zz".equals(types)) {
            String becomeMon = (String)arg1.get("becomeMon");
            String compDate = (String)arg1.get("compDate");
            reurnMap = this.submitEffectZZ(arg0, id, becomeMon, compDate);
        } else if ("DD".equals(types)) {
            String becomeMon = (String)arg1.get("becomeMon");
            String compDate = (String)arg1.get("compDate");
            reurnMap = this.submitEffectDD(arg0, id, becomeMon, compDate, zwjb, yybmbm, groupClass, reimLevel, yybm, bpcbmfldm, groupClassDetails, fyfl1, fyfl2, bmfl3, groupClassDetails);
        }

        return reurnMap;
    }

    public Map<Object, Object> submitEffectRZ(Context ctx, String id, String becomeMon, String compDate, String probation, String yearBonus, String becomeDate, String bizDate, String zwjb, String yybmbm, String groupClass, String reimLevel, String yybm, String bpcbmfldm, String groupClassDetails, String fyfl1, String fyfl2, String bmfl3, String bmfl4) {
        Map reurnMap = new HashMap();
        String msg = null;
        String fid = null;
        String fnumber = null;
        StringBuffer reason = new StringBuffer();
        String msgType = null;
        String personNumber = null;
        String txStr = null;
        new EmpEnrollBizBillCollection();
        EmpEnrollBizBillInfo enInfo = new EmpEnrollBizBillInfo();
        PersonInfo peInfo = new PersonInfo();

        try {
            EmpEnrollBizBillCollection enCol = EmpEnrollBizBillFactory.getLocalInstance(ctx).getEmpEnrollBizBillCollection(" where number='" + id + "'");
            if (enCol.size() > 0) {
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                enInfo = enCol.get(0);
                EmpEnrollBizBillEntryCollection enInfoCON = enInfo.getEntrys();

                for(int i = 0; i < enInfoCON.size(); ++i) {
                    EmpEnrollBizBillEntryInfo enInfoEny = enInfoCON.get(i);
                    enInfoEny.put("yybmbm", yybmbm);
                    enInfoEny.put("groupClass", groupClass);
                    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) {
                            enInfoEny.put("zwjb", coreBaseColl.get(0));
                        }
                    }

                    enInfoEny.put("reimLevel", reimLevel);
                    enInfoEny.put("yybm", yybm);
                    enInfoEny.put("bpcbmfldm", bpcbmfldm);
                    enInfoEny.put("groupClassDetails", groupClassDetails);
                    enInfoEny.put("fyfl1", fyfl1);
                    enInfoEny.put("fyfl2", fyfl2);
                    enInfoEny.put("bmfl3", bmfl3);
                    enInfoEny.put("bmfl4", bmfl4);
                }

                EmpEnrollBizBillFactory.getLocalInstance(ctx).save(enInfo);
                if (probation != null && probation != "") {
                    enInfo.getEntrys().get(0).setProbation(Integer.valueOf(probation));
                }

                if (bizDate != null && !"".equals(bizDate)) {
                    enInfo.getEntrys().get(0).setBizDate(format1.parse(bizDate));
                }

                enInfo.getEntrys().get(0).setPlanFormalDate(format1.parse(becomeDate));
                EmpEnrollBizBillFactory.getLocalInstance(ctx).submitEffect(enInfo);
                String peSql = "SELECT FNUMBER FROM T_BD_PERSON WHERE FID = (SELECT FPERSONID FROM T_HR_EmpEnrollBizBillEntry where FBILLID = (select FID from T_HR_EmpEnrollBizBill where FNUMBER = '" + id + "'))";
                IRowSet peSet = DbUtil.executeQuery(ctx, peSql.toString());
                if (peSet.next()) {
                    personNumber = peSet.getString("FNUMBER");
                    peInfo = PersonFactory.getLocalInstance(ctx).getPersonCollection(" where number='" + personNumber + "'").get(0);
                }
            } else {
                reason.append("单据编码为：" + id + " 入职单不存在，请检查相关信息！");
            }
        } catch (BOSException e) {
            e.printStackTrace();
            reason.append(e.getMessage());
        } catch (EASBizException e) {
            e.printStackTrace();
            reason.append(e.getMessage());
        } catch (SQLException e) {
            e.printStackTrace();
            reason.append(e.getMessage());
        } catch (ParseException e) {
            e.printStackTrace();
            reason.append(e.getMessage());
        }

        if (reason.length() > 0) {
            msg = "失败";
            msgType = "0";
        } else {
            msg = "成功";
            msgType = "1";
            fnumber = id;
            fid = enInfo.getId().toString();
            EmpEnrollBizBillEntryInfo ebEnInfo = enInfo.getEntrys().get(0);
            if (org.apache.commons.lang.StringUtils.isNotBlank(becomeMon)) {
                String rzStr = "UPDATE T_HR_EmpEnrollBizBillEntry SET CFGz='" + becomeMon + "',FZDY11='" + compDate + "'  WHERE  FBILLID = '" + fid + "'";
                String nzStr = "UPDATE T_HR_EmpEnrollBizBillEntry SET CFNzjfaID=(SELECT FID FROM CT_MP_Nzjfa where fname_l2 = '" + yearBonus + "')  WHERE FBILLID = '" + fid + "'";

                try {
                    DbUtil.execute(ctx, rzStr);
                    DbUtil.execute(ctx, nzStr);
                } catch (BOSException e) {
                    e.printStackTrace();
                }
            }

            this.setPersonEx(ctx, ebEnInfo, peInfo.getNumber());
        }

        if (txStr != null) {
            reurnMap.put("reason", reason.toString() + "," + txStr);
        } else {
            reurnMap.put("reason", reason.toString());
        }

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

    public Map<Object, Object> submitEffectLZ(Context ctx, String id, String bizDate) {
        Map reurnMap = new HashMap();
        String msg = null;
        String fid = null;
        String fnumber = null;
        StringBuffer reason = new StringBuffer();
        String msgType = null;
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        ResignBizBillInfo rbInfo = new ResignBizBillInfo();
        new ResignBizBillCollection();

        try {
            ResignBizBillCollection rbCol = ResignBizBillFactory.getLocalInstance(ctx).getResignBizBillCollection(" where number = '" + id + "'");
            if (rbCol.size() > 0) {
                rbInfo = rbCol.get(0);
                if (org.apache.commons.lang.StringUtils.isNotBlank(bizDate)) {
                    rbInfo.getEntrys().get(0).setBizDate(format1.parse(bizDate));
                }

                ResignBizBillFactory.getLocalInstance(ctx).submitEffect(rbInfo);
            } else {
                reason.append("单据编码为：" + id + " 离职单不存在，请检查相关信息！");
            }
        } catch (EASBizException e) {
            e.printStackTrace();
            reason.append(e.getMessage());
        } catch (BOSException e) {
            e.printStackTrace();
            reason.append(e.getMessage());
        } catch (ParseException e) {
            e.printStackTrace();
            reason.append("离职生效日期格式转化异常_yyyy-MM-dd，" + e.getMessage());
        }

        if (reason.length() > 0) {
            msg = "失败";
            msgType = "0";
        } else {
            msg = "成功";
            msgType = "1";
            fnumber = id;
            fid = rbInfo.getId().toString();
        }

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

    public Map<Object, Object> submitEffectZZ(Context ctx, String id, String becomeMon, String compDate) {
        Map reurnMap = new HashMap();
        String msg = null;
        String fid = null;
        String fnumber = null;
        StringBuffer reason = new StringBuffer();
        String msgType = null;
        String txStr = null;
        EmpHireBizBillInfo ehInfo = new EmpHireBizBillInfo();
        new EmpHireBizBillCollection();

        try {
            EmpHireBizBillCollection ehCol = EmpHireBizBillFactory.getLocalInstance(ctx).getEmpHireBizBillCollection(" where number = '" + id + "'");
            if (ehCol.size() > 0) {
                ehInfo = ehCol.get(0);
                if (ehInfo.getBillState() == null) {
                    ehInfo.setBillState(HRBillStateEnum.SAVED);
                }

                ehInfo.setIsMultiEntry(Boolean.TRUE);
                ehInfo.setApproveType(ApproveTypeEnum.DIRECT);
                EmpHireBizBillFactory.getLocalInstance(ctx).submitEffect(ehInfo);
            } else {
                reason.append("单据编码为：" + id + " 转正单不存在，请检查相关信息！");
            }
        } catch (BOSException e) {
            e.printStackTrace();
            reason.append(e.getMessage());
        } catch (EASBizException e) {
            e.printStackTrace();
            reason.append(e.getMessage());
        }

        if (reason.length() > 0) {
            msg = "失败";
            msgType = "0";
        } else {
            msg = "成功";
            msgType = "1";
            fnumber = id;
            fid = ehInfo.getId().toString();
            EmpHireBizBillEntryInfo ehEnInfo = ehInfo.getEntrys().get(0);
            if (org.apache.commons.lang.StringUtils.isNotBlank(becomeMon)) {
                String zzStr = "UPDATE T_HR_EmpHireBizBillEntry SET CFZzhxz='" + becomeMon + "',FZDY11='" + compDate + "' WHERE  FBillID = '" + fid + "'";

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

        if (txStr != null) {
            reurnMap.put("reason", reason.toString() + "," + txStr);
        } else {
            reurnMap.put("reason", reason.toString());
        }

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

    public Map<Object, Object> submitEffectDD(Context ctx, String id, String becomeMon, String compDate, String zwjb, String yybmbm, String groupClass, String reimLevel, String yybm, String bpcbmfldm, String groupClassDetails, String fyfl1, String fyfl2, String bmfl3, String bmfl4) {
        Map reurnMap = new HashMap();
        String fids = null;
        String msg = null;
        String fid = null;
        String fnumber = null;
        String personNum = null;
        StringBuffer reason = new StringBuffer();
        String msgType = null;
        String txStr = null;
        FluctuationBizBillInfo fuInfo = new FluctuationBizBillInfo();
        new FluctuationBizBillCollection();

        try {
            FluctuationBizBillCollection fuCol = FluctuationBizBillFactory.getLocalInstance(ctx).getFluctuationBizBillCollection(" where number= '" + id + "'");
            if (fuCol.size() > 0) {
                fuInfo = fuCol.get(0);
                FluctuationBizBillEntryCollection enInfoCON = fuInfo.getEntrys();

                for(int i = 0; i < enInfoCON.size(); ++i) {
                    FluctuationBizBillEntryInfo enInfoEny = enInfoCON.get(i);
                    enInfoEny.put("yybmbm", yybmbm);
                    enInfoEny.put("groupClass", groupClass);
                    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) {
                            enInfoEny.put("zwjb", coreBaseColl.get(0));
                        }
                    }

                    enInfoEny.put("reimLevel", reimLevel);
                    enInfoEny.put("yybm", yybm);
                    enInfoEny.put("bpcbmfldm", bpcbmfldm);
                    enInfoEny.put("groupClassDetails", groupClassDetails);
                    enInfoEny.put("fyfl1", fyfl1);
                    enInfoEny.put("fyfl2", fyfl2);
                    enInfoEny.put("bmfl3", bmfl3);
                    enInfoEny.put("bmfl4", bmfl4);
                }

                FluctuationBizBillFactory.getLocalInstance(ctx).save(fuInfo);
                IObjectPK pks = FluctuationBizBillFactory.getLocalInstance(ctx).submitEffect(fuInfo);
                fids = pks.toString();
            } else {
                reason.append("单据编码为：" + id + " 调动单不存在，请检查相关信息！");
            }
        } catch (EASBizException e) {
            e.printStackTrace();
            reason.append(e.getMessage());
        } catch (BOSException e) {
            e.printStackTrace();
            reason.append(e.getMessage());
        }

        if (reason.length() > 0) {
            msg = "失败";
            msgType = "0";
        } else {
            msg = "成功";
            msgType = "1";
            fnumber = id;
            fid = fuInfo.getId().toString();
            FluctuationBizBillEntryInfo fuEnInfo = fuInfo.getEntrys().get(0);

            try {
                PersonInfo peInfo = PersonFactory.getLocalInstance(ctx).getPersonInfo(new ObjectUuidPK(fuEnInfo.getPerson().getId()));
                personNum = peInfo.getNumber();
                if (org.apache.commons.lang.StringUtils.isNotBlank(becomeMon)) {
                    String ddStr = "UPDATE T_HR_FluctuationBizBillEntry SET CFDdhgz2='" + becomeMon + "',FZDY11='" + compDate + "'  WHERE FBillID = '" + fid + "'";
                    DbUtil.execute(ctx, ddStr);
                }
            } catch (EASBizException e) {
                e.printStackTrace();
            } catch (BOSException e) {
                e.printStackTrace();
            }

            this.setPersonDDEx(ctx, fids, personNum);
        }

        if (txStr != null) {
            reurnMap.put("reason", reason.toString() + "," + txStr);
        } else {
            reurnMap.put("reason", reason.toString());
        }

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

    public void setPersonDDEx(Context ctx, String ids, String personNum) {
        String xjbmSql = "select CFXjbmID from T_HR_FluctuationBizBillEntry WHERE FBillID = '" + ids + "'";

        try {
            IRowSet xjbmSet = DbUtil.executeQuery(ctx, xjbmSql);
            if (xjbmSet.next() && org.apache.commons.lang.StringUtils.isNotBlank(xjbmSet.getString("CFXjbmID"))) {
                String xjbmPeSql = "UPDATE T_BD_PERSON SET CFXjbmID='" + xjbmSet.getString("CFXjbmID") + "' WHERE FNUMBER = '" + personNum + "'";
                DbUtil.execute(ctx, xjbmPeSql);
            }
        } catch (BOSException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public void setPersonEx(Context ctx, EmpEnrollBizBillEntryInfo ebEnInfo, String number) {
        String seSql = "select FStandingID,CFXjbmID,CFNzjfaID from T_HR_EmpEnrollBizBillEntry where FID= '" + ebEnInfo.getId().toString() + "'";

        try {
            IRowSet seSet = DbUtil.executeQuery(ctx, seSql.toString());
            if (seSet.next()) {
                if (org.apache.commons.lang.StringUtils.isNotBlank(seSet.getString("CFXjbmID"))) {
                    String xjbmSql = "UPDATE T_BD_PERSON SET CFXjbmID='" + seSet.getString("CFXjbmID") + "' WHERE FNUMBER = '" + number + "'";
                    DbUtil.execute(ctx, xjbmSql);
                }

                if (org.apache.commons.lang.StringUtils.isNotBlank(seSet.getString("FStandingID"))) {
                    String rankSql = "UPDATE T_BD_PERSON SET FSTANDINGID='" + seSet.getString("FStandingID") + "'  WHERE FNUMBER = '" + number + "'";
                    DbUtil.execute(ctx, rankSql);
                }

                if (org.apache.commons.lang.StringUtils.isNotBlank(seSet.getString("CFNzjfaID"))) {
                    String nzSql = "UPDATE T_BD_PERSON SET CFNzjfaID='" + seSet.getString("CFNzjfaID") + "'  WHERE FNUMBER = '" + number + "'";
                    DbUtil.execute(ctx, nzSql);
                }
            }
        } catch (BOSException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

    }
}
