package com.kingdee.shr.compensation.app.integrate;


import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.csinterface.biz.util.DBUtils;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.base.HRBillStateEnum;
import com.kingdee.eas.hr.base.IRewardPunishType;
import com.kingdee.eas.hr.base.RewardPunishTypeFactory;
import com.kingdee.eas.hr.base.RewardPunishTypeInfo;
import com.kingdee.eas.hr.emp.IPersonRewardPunish;
import com.kingdee.eas.hr.emp.PersonRewardPunishFactory;
import com.kingdee.eas.hr.emp.PersonRewardPunishInfo;
import com.kingdee.eas.hr.state.IRewardPunishLevel;
import com.kingdee.eas.hr.state.RewardPunishLevelFactory;
import com.kingdee.eas.hr.state.RewardPunishLevelInfo;
import com.kingdee.jdbc.rowset.IRowSet;
import org.apache.log4j.Logger;

import java.sql.SQLException;

/**
 * @Title: BatchSubmitShemeBillControllerBeanEx
 * @Author 青梧
 * @Package com.kingdee.shr.compensation.app.integrate
 * @Date 2026/1/8 13:50
 * @description: 审批通过后同步奖惩多行表
 */
public class BatchSubmitShemeBillControllerBeanEx extends BatchSubmitShemeBillControllerBean {
    private static Logger logger = Logger.getLogger(BatchSubmitShemeBillControllerBeanEx.class);

    @Override
    protected void _setPassState(Context ctx, BOSUuid billId) throws BOSException, EASBizException {
        super._setPassState(ctx, billId);
        try {
            syncPersonRewardPunish(ctx, new ObjectUuidPK(billId.toString()));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public IObjectPK _submitEffect(Context ctx, CoreBaseInfo model) throws BOSException, EASBizException {
        IObjectPK iObjectPK = super._submitEffect(ctx, model);
        try {
            syncPersonRewardPunish(ctx, iObjectPK);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return iObjectPK;
    }

    /***
     * 同步奖罚记录
     * @param ctx
     * @param iObjectPK
     */
    public void syncPersonRewardPunish(Context ctx, IObjectPK iObjectPK) throws BOSException, EASBizException, SQLException {
        logger.error("syncPersonRewardPunish---start");
        BatchSubmitShemeBillInfo batchSubmitShemeBillInfo = BatchSubmitShemeBillFactory.getLocalInstance(ctx).getBatchSubmitShemeBillInfo(iObjectPK);
        //奖惩类型
        IRewardPunishType iRewardPunishType = RewardPunishTypeFactory.getLocalInstance(ctx);
        //奖惩级别
        IRewardPunishLevel iRewardPunishLevel = RewardPunishLevelFactory.getLocalInstance(ctx);
        //提报方案
        CalSubmitSchemeInfo submitScheme = batchSubmitShemeBillInfo.getSubmitScheme();
        ICalSubmitScheme iCalSubmitScheme = CalSubmitSchemeFactory.getLocalInstance(ctx);
        CalSubmitSchemeInfo calSubmitSchemeInfo = iCalSubmitScheme.getCalSubmitSchemeInfo(new ObjectUuidPK(submitScheme.getId()));
        logger.error("billstate----" + batchSubmitShemeBillInfo.getBillState());
        logger.error("CalSubmitSchemeNumber----" + calSubmitSchemeInfo.getNumber());
        //判断是否是审批通过并且提报方案是奖励或者惩戒
        if (batchSubmitShemeBillInfo.getBillState().equals(HRBillStateEnum.AUDITED) && (calSubmitSchemeInfo.getNumber().equals("scy20") || calSubmitSchemeInfo.getNumber().equals("scy09"))) {
            BatchSubmitShemeBillEntryCollection entry = batchSubmitShemeBillInfo.getEntry();
            IPersonRewardPunish iPersonRewardPunish = PersonRewardPunishFactory.getLocalInstance(ctx);
            for (int i = 0; i < entry.size(); i++) {
                //奖惩记录表
                PersonRewardPunishInfo personRewardPunishInfo = new PersonRewardPunishInfo();
                BatchSubmitShemeBillEntryInfo batchSubmitShemeBillEntryInfo = entry.get(i);
                //人员
                personRewardPunishInfo.setPerson(batchSubmitShemeBillEntryInfo.getPerson());
                //奖惩类型
                if (calSubmitSchemeInfo.getNumber().equals("scy20")) {
                    RewardPunishTypeInfo rewardPunishTypeInfo = iRewardPunishType.getRewardPunishTypeInfo("where number = '01'");
                    personRewardPunishInfo.setType(rewardPunishTypeInfo);
                    //测试环境sql  上生产需要改成生产表  测试环境表：惩戒：T_HR_SchemeBillEntry066cmp 奖励：T_HR_SchemeBillEntry064cmp
                    //生产环境表
                    String sql = "select s77,s84,s83 from T_HR_SchemeBillEntry064cmp where fbillId ='" + batchSubmitShemeBillInfo.getId() + "'";
                    logger.error("sql--" + sql);
                    IRowSet iRowSet = DBUtils.executeQuery(ctx, sql);
                    if (iRowSet.next()) {
                        //奖惩依据
                        personRewardPunishInfo.setContent(iRowSet.getString("s83"));
                        //奖惩事由
                        personRewardPunishInfo.setDescription(iRowSet.getString("s84"));
                        //奖惩金额
                        personRewardPunishInfo.put("jcje", iRowSet.getString("s77"));
                    }
                    //奖惩级别
                    String jlId = batchSubmitShemeBillInfo.getObjectValue("jl").get("id").toString();
                    String jlSql = "select fnumber as number from CT_MP_Jljb where fid ='" + jlId + "'";
                    iRowSet = DBUtils.executeQuery(ctx, jlSql);
                    if (iRowSet.next()) {
                        String number = iRowSet.getString("number");
                        RewardPunishLevelInfo rewardPunishLevelInfo = iRewardPunishLevel.getRewardPunishLevelInfo("where number = '" + number + "'");
                        personRewardPunishInfo.setRPLevel(rewardPunishLevelInfo);
                    }
                }
                if (calSubmitSchemeInfo.getNumber().equals("scy09")) {
                    RewardPunishTypeInfo rewardPunishTypeInfo = iRewardPunishType.getRewardPunishTypeInfo("where number = '02'");
                    personRewardPunishInfo.setType(rewardPunishTypeInfo);
                    //测试环境sql  上生产需要改成生产表  测试环境表：T_HR_SchemeBillEntry066cmp  生产环境表
                    String sql = "select s69,s84,s83 from T_HR_SchemeBillEntry066cmp where fbillId ='" + batchSubmitShemeBillInfo.getId() + "'";
                    logger.error("sql--" + sql);
                    IRowSet iRowSet = DBUtils.executeQuery(ctx, sql);
                    if (iRowSet.next()) {
                        //奖惩依据
                        personRewardPunishInfo.setContent(iRowSet.getString("s83"));
                        //奖惩事由
                        personRewardPunishInfo.setDescription(iRowSet.getString("s84"));
                        //奖惩金额
                        personRewardPunishInfo.put("jcje", iRowSet.getString("s69"));
                    }
                    //奖惩级别
                    String cjId = batchSubmitShemeBillInfo.getObjectValue("cj").get("id").toString();
                    String cjSql = "select fnumber as number from CT_MP_Cjjb where fid ='" + cjId + "'";
                    iRowSet = DBUtils.executeQuery(ctx, cjSql);
                    if (iRowSet.next()) {
                        String number = iRowSet.getString("number");
                        RewardPunishLevelInfo rewardPunishLevelInfo = iRewardPunishLevel.getRewardPunishLevelInfo("where number = '" + number + "'");
                        personRewardPunishInfo.setRPLevel(rewardPunishLevelInfo);
                    }
                }

                //奖惩时间
                personRewardPunishInfo.setOccurDate(batchSubmitShemeBillEntryInfo.getEffectDate());
                //单行显示
                personRewardPunishInfo.setIsSingle(true);
                iPersonRewardPunish.save(personRewardPunishInfo);
//                iPersonRewardPunish.addnew(personRewardPunishInfo);
            }
        }
        logger.error("syncPersonRewardPunish---end");
    }


}
