//
// 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.eas.base.permission.UserFactory;
import com.kingdee.eas.base.permission.UserInfo;
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.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.framework.ObjectBaseInfo;
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.base.AffairActionReasonFactory;
import com.kingdee.eas.hr.base.AffairActionReasonInfo;
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.emp.PersonPositionFactory;
import com.kingdee.eas.hr.emp.PersonPositionInfo;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
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 SaveFluctuationBizBillService implements IHRMsfService {
    private static Logger logger = Logger.getLogger("com.kingdee.eas.custom.service.SaveFluctuationBizBillService");

    public Object process(Context arg0, Map arg1) throws EASBizException, BOSException {
        System.out.println("开始执行调动单接口...");
        new HashMap();
        String number = (String)arg1.get("number");
        String personNumbe = (String)arg1.get("personNumber");
        String bizDate = (String)arg1.get("bizDate");
        String position = (String)arg1.get("position");
        String remarks = (String)arg1.get("remarks");
        String oaNumber = (String)arg1.get("oaNumber");
        String applier = (String)arg1.get("applier");
        String applyDate = (String)arg1.get("applyDate");
        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];
            }

        }

        boolean isZyb = (String)arg1.get("isZyb") != "1";
        String lowerDep = (String)arg1.get("lowerDep");
        System.out.println("获取的参数值：number=" + number + ",personNumbe=" + personNumbe + ",bizDate=" + bizDate + ",position=" + position + ",isZyb=" + isZyb + ",remarks=" + remarks + ",oaNumber=" + oaNumber + ",applier=" + applier + ",applyDate=" + applyDate + ",lowerDep=" + lowerDep);
        Map<Object, Object> resultMap = this.createFluctuationBizBill(arg0, number, personNumbe, bizDate, position, isZyb, remarks, oaNumber, applier, applyDate, lowerDep, zwjb, yybmbm, groupClass, reimLevel, yybm, bpcbmfldm, groupClassDetails, fyfl1, fyfl2, bmfl3, groupClassDetails);
        System.out.println("执行调动单接口结束...");
        return resultMap;
    }

    public Map<Object, Object> createFluctuationBizBill(Context ctx, String number, String personNumbe, String bizDate, String position, boolean isZyb, String remarks, String oaNumber, String applier, String applyDate, String lowerDep, String zwjb, String yybmbm, String groupClass, String reimLevel, String yybm, String bpcbmfldm, String groupClassDetails, String fyfl1, String fyfl2, String bmfl3, String bmfl4) {
        Map<Object, Object> returnMap = new HashMap();
        FluctuationBizBillInfo info = new FluctuationBizBillInfo();
        FluctuationBizBillEntryInfo enInfo = new FluctuationBizBillEntryInfo();
        String msg = null;
        String fid = null;
        String fnumber = null;
        StringBuffer reason = new StringBuffer();
        String msgType = null;

        try {
            if (number != null && number != "") {
                info.setNumber(number);
            } else {
                info.setNumber("DD" + System.currentTimeMillis());
            }

            enInfo.put("yybmbm", yybmbm);
            enInfo.put("groupClass", groupClass);
            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);
            HROrgUnitInfo hrInfo = HROrgUnitFactory.getLocalInstance(ctx).getHROrgUnitInfo(new ObjectUuidPK("00000000-0000-0000-0000-000000000000CCE7AED4"));
            info.setHrOrgUnit(hrInfo);
            info.setBillState(HRBillStateEnum.SAVED);
            if (applyDate != null && applyDate != "") {
                DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                info.setApplyDate(format1.parse(applyDate));
            } else {
                info.setApplyDate(new Date());
            }

            new PersonInfo();
            if (applier != null && applier != "") {
                Map ap = this.getPerson(ctx, applier);
                if (ap.get("pe") != null) {
                    PersonInfo ueInfo = (PersonInfo)ap.get("pe");
                    info.setApplier(ueInfo);
                } else {
                    reason.append("制单人编码有误，请检查传入参数applier");
                }
            } else {
                reason.append("制单人编码为空，请检查传入参数applier");
            }

            new PersonInfo();
            new PositionInfo();
            new AdminOrgUnitInfo();
            new AdminOrgUnitInfo();
            if (personNumbe != null && personNumbe != "") {
                Map pe = this.getPerson(ctx, personNumbe);
                if (pe.get("pe") != null) {
                    PersonInfo peInfo = (PersonInfo)pe.get("pe");
                    enInfo.setPerson(peInfo);
                    EmployeeTypeInfo emsInfo = EmployeeTypeFactory.getLocalInstance(ctx).getEmployeeTypeInfo(new ObjectUuidPK(peInfo.getEmployeeType().getId()));
                    enInfo.setOldEmpType(emsInfo);
                    enInfo.setEmpType(emsInfo);
                } else {
                    reason.append("调动员工编码获取员工信息有误，请检查传入参数personNumbe");
                }

                Map ap = this.getPersonAP(ctx, personNumbe);
                if (ap.get("po") != null) {
                    PositionInfo oldPoInfo = (PositionInfo)ap.get("po");
                    enInfo.setOldPosition(oldPoInfo);
                } else {
                    reason.append("调动员工职位信息有误，请检查相关员工信息");
                }

                if (ap.get("ad") != null) {
                    AdminOrgUnitInfo oldAdInfo = (AdminOrgUnitInfo)ap.get("ad");
                    enInfo.setOldAdminOrg(oldAdInfo);
                    AdminOrgUnitInfo oldCompanyInfo = AdminOrgUnitFactory.getLocalInstance(ctx).getAdminOrgUnitInfo(new ObjectUuidPK(oldAdInfo.getCompany().getId()));
                    enInfo.setOldCompany(oldCompanyInfo);
                } else {
                    reason.append("调动员工组织信息有误，请检查相关员工信息");
                }
            } else {
                reason.append("调动员工编码为空，请检查传入参数personNumbe");
            }

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

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

            enInfo.setDescription(remarks);
            HRBizDefineInfo hrdInfo = HRBizDefineFactory.getLocalInstance(ctx).getHRBizDefineInfo(new ObjectUuidPK("DawAAAApVj/maL7Z"));
            enInfo.setHrBizDefine(hrdInfo);
            AffairActionReasonInfo affInfo = AffairActionReasonFactory.getLocalInstance(ctx).getAffairActionReasonInfo(new ObjectUuidPK("7ta1EwaZTZmts8L4z/J0LJYRae4="));
            enInfo.setAffairActionReason(affInfo);
            VariationReasonInfo varInfo = VariationReasonFactory.getLocalInstance(ctx).getVariationReasonInfo(new ObjectUuidPK("EIQNQWoyQP+Db8K7csPBNuas36w="));
            enInfo.setVariationReason(varInfo);
            enInfo.setString("ddqxz", this.getPeronGz(ctx, personNumbe));
            enInfo.setBoolean("sfzyb", isZyb);
            enInfo.setString("oaNumber", oaNumber);
            info.getEntrys().add(enInfo);
        } catch (ParseException e) {
            e.printStackTrace();
            reason.append("日期格式转化异常，请检查传入格式yyyy-MM-dd");
        } catch (BOSException e) {
            e.printStackTrace();
            reason.append(e.getMessage());
        } catch (EASBizException e) {
            e.printStackTrace();
            reason.append(e.getMessage());
        }

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

            if (reason.length() > 0) {
                msgType = "0";
                msg = "失败";
            } else {
                msg = "成功";
                msgType = "1";
                fnumber = info.getNumber();
                if (org.apache.commons.lang.StringUtils.isBlank(lowerDep)) {
                    this.setOtherParam(ctx, lowerDep, fid, enInfo.getPerson().getId().toString());
                }
            }
        }

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

    private void setOtherParam(Context ctx, String lowerDep, String id, String personid) {
        String xjbmSql = "UPDATE T_HR_FluctuationBizBillEntry SET CFXjbmID = (SELECT FID FROM CT_CUS_Xjbm where FNumber = '" + lowerDep + "')  WHERE FBillID = '" + id + "' ";
        String sql = "INSERT INTO t_person_Xjbm(fid, FXjbmID, sourceID, personID,fnumber) VALUES ('" + System.currentTimeMillis() + "', (SELECT FID FROM CT_CUS_Xjbm where FNumber = '" + lowerDep + "'), '" + id + "', '" + personid + "','" + lowerDep + "');";

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

    }

    public Map<String, ObjectBaseInfo> getPersonAP(Context ctx, String number) {
        Map<String, ObjectBaseInfo> a = new HashMap();
        PersonPositionInfo psoInfo = null;
        AdminOrgUnitInfo adInfo = null;
        PositionInfo poInfo = null;
        String sql = "SELECT PSO.FID PSOID,PO.FID POID,AD.FID ADID FROM T_BD_PERSON PE INNER JOIN T_HR_PERSONPOSITION PSO ON PE.FID = PSO.FPERSONID LEFT OUTER JOIN T_ORG_POSITION PO ON PO.FID = PSO.FPRIMARYPOSITIONID LEFT OUTER JOIN T_ORG_ADMIN AD ON AD.FID = PO.FADMINORGUNITID WHERE PE.FNUMBER = '" + number + "'";

        try {
            IRowSet asSet = DbUtil.executeQuery(ctx, sql.toString());
            if (asSet.next()) {
                if (asSet.getString("PSOID") != null) {
                    psoInfo = PersonPositionFactory.getLocalInstance(ctx).getPersonPositionInfo(new ObjectUuidPK(asSet.getString("PSOID")));
                }

                if (asSet.getString("POID") != null) {
                    poInfo = PositionFactory.getLocalInstance(ctx).getPositionInfo(new ObjectUuidPK(asSet.getString("POID")));
                }

                if (asSet.getString("ADID") != null) {
                    adInfo = AdminOrgUnitFactory.getLocalInstance(ctx).getAdminOrgUnitInfo(new ObjectUuidPK(asSet.getString("ADID")));
                }
            }
        } catch (BOSException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (EASBizException e) {
            e.printStackTrace();
        }

        a.put("pso", psoInfo);
        a.put("po", poInfo);
        a.put("ad", adInfo);
        System.out.println("getPersonAP:" + a);
        return a;
    }

    public Map<String, ObjectBaseInfo> getPerson(Context ctx, String number) {
        Map<String, ObjectBaseInfo> a = new HashMap();
        PersonInfo peInfo = null;
        UserInfo ueInfo = null;
        String sql = "SELECT pe.FID PEID , UE.FID  UEID FROM T_BD_PERSON PE LEFT OUTER JOIN T_PM_USER UE ON UE.FPersonId = PE.FID WHERE PE.FNUMBER = '" + number + "'";

        try {
            IRowSet asSet = DbUtil.executeQuery(ctx, sql.toString());
            if (asSet.next()) {
                if (asSet.getString("PEID") != null) {
                    peInfo = PersonFactory.getLocalInstance(ctx).getPersonInfo(new ObjectUuidPK(asSet.getString("PEID")));
                }

                if (asSet.getString("UEID") != null) {
                    ueInfo = UserFactory.getLocalInstance(ctx).getUserInfo(new ObjectUuidPK(asSet.getString("UEID")));
                }
            }
        } catch (BOSException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (EASBizException e) {
            e.printStackTrace();
        }

        a.put("pe", peInfo);
        a.put("ue", ueInfo);
        return a;
    }

    public String getPeronGz(Context ctx, String personNumber) {
        Properties prop = new Properties();
        String retu = "";
        if (personNumber != null && !"".equals(personNumber)) {
            try {
                prop.load(new FileInputStream(System.getProperty("EAS_HOME") + "/server/properties/lgl/personServiceParam.properties"));
                new PersonInfo();
                PersonCollection peCol = PersonFactory.getLocalInstance(ctx).getPersonCollection(" where number = '" + personNumber + "'");
                if (peCol.size() > 0) {
                    PersonInfo peInfo = peCol.get(0);
                    String gzNumber = prop.getProperty("gz");
                    String xmSql = "SELECT FieldSn FROM T_HR_SCmpItem where FNUMBER = '" + gzNumber + "'";
                    IRowSet xmRs = DbUtil.executeQuery(ctx, xmSql);
                    if (xmRs.next()) {
                        String xh = "S" + xmRs.getString("FieldSn");
                        String xxSql = "SELECT TOP 1 " + xh + " A FROM T_HR_SCmpCalTable where FCALSTATE = '20' and FPersonID = '" + peInfo.getId().toString() + "' order by FEndDate desc";
                        IRowSet xxRs = DbUtil.executeQuery(ctx, xxSql);
                        if (xxRs.next()) {
                            new HashMap();
                            if (xxRs.getBigDecimal("A") != null) {
                                if (xxRs.getBigDecimal("A").compareTo(new BigDecimal(0)) > 0) {
                                    retu = String.valueOf(xxRs.getDouble("A"));
                                } else {
                                    retu = "0";
                                }
                            } else {
                                retu = "0";
                            }
                        }
                    }
                }
            } catch (BOSException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return retu;
    }
}
