package com.kingdee.shr.compensation.web.handler.integrate;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemCollection;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.csinterface.biz.util.DBUtils;
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.PersonRewardPunishCollection;
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.eas.util.app.ContextUtil;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import com.kingdee.shr.compensation.app.integrate.*;
import com.kingdee.shr.compensation.app.integrate.multiinspire.StandingBookFacadeFactory;
import com.kingdee.shr.compensation.util.integrate.CmpIntegrateEntryExtHelper;
import com.kingdee.shr.compensation.util.integrate.CmpIntegrateUtils;
import com.kingdee.shr.compensation.web.handler.integrate.helper.CmpIntegrateWebHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.sql.Timestamp;

/**
 * @Title: BatchSubmitShemeBillListHandlerEx
 * @Author 青梧
 * @Package com.kingdee.shr.compensation.web.handler.integrate
 * @Date 2026/1/12 13:59
 * @description: 反审批撤回奖惩信息
 */
public class BatchSubmitShemeBillListHandlerEx extends BatchSubmitShemeBillListHandler {

    public void antiApprovalAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
        String billId = this.getBillId(request);
        if (!StringUtils.isEmpty(billId)) {
            Context ctx = SHRContext.getInstance().getContext();
            BatchSubmitShemeBillInfo info = CmpIntegrateWebHelper.getBatchSubmitShemeBillInfo(ctx, "where id = '" + billId + "'");
            info.setLastUpdateUser(ContextUtil.getCurrentUserInfo(ctx));
            String submitSchemeId = info.getSubmitScheme().getId().toString();
            String calShemeTableName = CmpIntegrateWebHelper.getEntryTableName(ctx, submitSchemeId);
            this.checkBillIsLegalForDelete(ctx, calShemeTableName, billId);
            if (this.isCycleScheme(ctx, submitSchemeId)) {
                this.antiApprovalControl(ctx, submitSchemeId, calShemeTableName, billId);
            }

            try {
                BatchSubmitShemeBillInfo billInfo = new BatchSubmitShemeBillInfo();
                billInfo.setId(BOSUuid.read(billId));
                this.callDeductOfSubmitScheme(ctx, billInfo);
                CmpIntegrateUtils.updateEntryBillState(ctx, billId, HRBillStateEnum.SAVED, calShemeTableName);
                String updateBillSql = "update T_HR_SBatchSubmitShemeBill set FAUDITORID='',FBillState=?,FLastUpdateTime=? where fid=?";
                Object[] param = new Object[]{HRBillStateEnum.SAVED.getValue(), new Timestamp(System.currentTimeMillis()), billId};
                DbUtil.execute(ctx, updateBillSql, param);
                CmpIntegrateEntryExtHelper.convertExchangeRateAndSaveEntryExtInSpireBill(ctx, billId, HRBillStateEnum.SAVED);
                StandingBookFacadeFactory.getLocalInstance(ctx).statistics(billId);
                this.reverseJob(ctx, billId);
                this.deleteCjjbInfo(ctx, info);
            } catch (Exception e) {
                throw new ShrWebBizException(e.getMessage(), e);
            }
            JSONUtils.SUCCESS(billId);
        }
    }

    /**
     * 删除奖惩记录
     *
     * @param info
     */
    public void deleteCjjbInfo(Context ctx, BatchSubmitShemeBillInfo info) throws BOSException, EASBizException, SQLException {

        BatchSubmitShemeBillEntryCollection entry = info.getEntry();
        IPersonRewardPunish iPersonRewardPunish = PersonRewardPunishFactory.getLocalInstance(ctx);
        //奖惩类型
        IRewardPunishType iRewardPunishType = RewardPunishTypeFactory.getLocalInstance(ctx);
        //奖惩级别
        IRewardPunishLevel iRewardPunishLevel = RewardPunishLevelFactory.getLocalInstance(ctx);
        //提报方案
        CalSubmitSchemeInfo submitScheme = info.getSubmitScheme();
        ICalSubmitScheme iCalSubmitScheme = CalSubmitSchemeFactory.getLocalInstance(ctx);
        CalSubmitSchemeInfo calSubmitSchemeInfo = iCalSubmitScheme.getCalSubmitSchemeInfo(new ObjectUuidPK(submitScheme.getId()));
        for (int i = 0; i < entry.size(); i++) {
            BatchSubmitShemeBillEntryInfo batchSubmitShemeBillEntryInfo = entry.get(i);
            FilterInfo filterInfo = new FilterInfo();
            FilterItemCollection filterItems = filterInfo.getFilterItems();
            filterItems.add(new FilterItemInfo("person", batchSubmitShemeBillEntryInfo.getPerson().getId()));
            //奖惩类型
            if (calSubmitSchemeInfo.getNumber().equals("scy20")) {
                RewardPunishTypeInfo rewardPunishTypeInfo = iRewardPunishType.getRewardPunishTypeInfo("where number = '01'");
                filterItems.add(new FilterItemInfo("type.id", rewardPunishTypeInfo.getId()));
                //奖惩级别
                String jlId = info.getObjectValue("jl").get("id").toString();
                String jlSql = "select fnumber as number from CT_MP_Jljb where fid ='" + jlId + "'";
                IRowSet iRowSet = DBUtils.executeQuery(ctx, jlSql);
                if (iRowSet.next()) {
                    String number = iRowSet.getString("number");
                    RewardPunishLevelInfo rewardPunishLevelInfo = iRewardPunishLevel.getRewardPunishLevelInfo("where number = '" + number + "'");
                    filterItems.add(new FilterItemInfo("rPLevel", rewardPunishLevelInfo.getId()));
                }
            }
            if (calSubmitSchemeInfo.getNumber().equals("scy09")) {
                RewardPunishTypeInfo rewardPunishTypeInfo = iRewardPunishType.getRewardPunishTypeInfo("where number = '02'");
                filterItems.add(new FilterItemInfo("type.id", rewardPunishTypeInfo.getId()));
                //奖惩级别
                String cjId = info.getObjectValue("cj").get("id").toString();
                String cjSql = "select fnumber as number from CT_MP_Cjjb where fid ='" + cjId + "'";
                IRowSet iRowSet = DBUtils.executeQuery(ctx, cjSql);
                if (iRowSet.next()) {
                    String number = iRowSet.getString("number");
                    RewardPunishLevelInfo rewardPunishLevelInfo = iRewardPunishLevel.getRewardPunishLevelInfo("where number = '" + number + "'");
                    filterItems.add(new FilterItemInfo("rPLevel", rewardPunishLevelInfo.getId()));
                }
            }
            //奖罚时间
            filterItems.add(new FilterItemInfo("occurDate", batchSubmitShemeBillEntryInfo.getEffectDate()));
            EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, null, null);
            PersonRewardPunishCollection personRewardPunishCollection = iPersonRewardPunish.getPersonRewardPunishCollection(entityViewInfo);
            for (int j = 0; j < personRewardPunishCollection.size(); j++) {
                PersonRewardPunishInfo personRewardPunishInfo = personRewardPunishCollection.get(j);
                iPersonRewardPunish.delete(new ObjectUuidPK(personRewardPunishInfo.getId().toString()));
            }
        }
    }

}
