package com.kingdee.shr.compensation.service;

import com.kingdee.bos.Context;

import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.compensation.CalSchemeInfo;
import com.kingdee.shr.compensation.app.formula.SalaryCalFunction;
import com.kingdee.shr.compensation.app.formula.data.Tools;
import com.kingdee.util.StringUtils;
import org.apache.log4j.Logger;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.*;


/**
 * 薪酬回溯
 * description: RetMoneyFunctionService <br>
 * date: 2025/4/25 18:02 <br>
 * author: lhbj <br>
 * version: 1.0 <br>
 */
public class RetMoneyFunctionV2Service extends SalaryCalFunction {
    private static Logger logger = Logger.getLogger(RetMoneyFunctionV2Service.class);

    public RetMoneyFunctionV2Service()
    {
    }

    public Context getCtx() {
        return Tools.getInstance().getCtx();
    }
    public double getRetMoneyData(Map<String, Object> paramMap, CalSchemeInfo calScheme, String mainTableRowFilter) {
        try {
            String fixPayMapKey = "getItemSalary_RetMoney_";
            String fid = (String)paramMap.get("T_HR_SCMPCALTABLE_FID");
            String personid = (String)paramMap.get("T_HR_SCMPCALTABLE_FPERSONID");
            if ( fid != null && personid != null) {
                String calschemeid = calScheme.getId().toString();
                int calTime = calScheme.getCalTime();
                int periodYear = calScheme.getPeriodYear();
                int periodMonth = calScheme.getPeriodMonth();
                String hrOrgId = calScheme.getHrOrgUnit().getId().toString();
                StringBuilder personSql = new StringBuilder("select CALTABLE.FPersonID,to_char(CALTABLE.fbegindate,'yyyy-MM-dd') fbegindate,to_char(CALTABLE.fenddate,'yyyy-MM-dd') fenddate " +
                        " from T_HR_SCmpCalTable CALTABLE where CALTABLE.FcalSchemeID = '" + calschemeid
                        + "' and CALTABLE.FcalTime = " + calTime + " and CALTABLE.FperiodYear = " + periodYear + " and CALTABLE.FperiodMonth = " + periodMonth);
                if (!StringUtils.isEmpty(mainTableRowFilter)) {
                    String replace = mainTableRowFilter.replace("t_hr_scmpcaltable.fid", "CALTABLE.fid").replace("t_hr_scmpcaltable.FCalState", "CALTABLE.fcalState");
                    personSql.append(" AND ").append(replace);
                }
                personSql.append(" AND CALTABLE.fid ='").append(fid).append("'");
                logger.error("getRetMoneyData:"+personSql);
                IRowSet rs = DbUtil.executeQuery(this.getCtx(), personSql.toString());
                if (rs.next()){
                    String fbegindate = rs.getString("fbegindate");
                    String fenddate = rs.getString("fenddate");
                    StringBuilder ppSql = new StringBuilder();
                    ppSql.append("select cfretmoney from CT_CUS_PersonPayForm where cfpersonid =? and to_char(cfmonth,'yyyy-MM-dd') >=? and to_char(cfmonth,'yyyy-MM-dd') <=? ");
                    logger.error("ppSql:"+ppSql);
                    IRowSet ppRs = DbUtil.executeQuery(this.getCtx(), ppSql.toString(),new String[]{personid,fbegindate,fenddate});
                    if (ppRs.next()){
                        BigDecimal cfretmoney = ppRs.getBigDecimal("cfretmoney");
                        cfretmoney=cfretmoney.setScale(BigDecimal.ROUND_HALF_UP,BigDecimal.ROUND_HALF_UP);
                        return cfretmoney.doubleValue();
                    }
                }
            }
            return 0.0D;
        } catch (Exception var15) {
            var15.printStackTrace();
            logger.error("ppSql:"+var15.toString());
            return 0.0D;
        }
    }

}
