package nckd.jimin.jyyy.hr.swc.print;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.print.core.data.DataRowSet;
import kd.bos.print.core.data.field.DateField;
import kd.bos.print.core.data.field.TextField;
import kd.bos.print.core.plugin.AbstractPrintPlugin;
import kd.bos.print.core.plugin.event.BeforeLoadDataEvent;
import kd.bos.print.core.plugin.event.CustomDataLoadEvent;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.workflow.WorkflowServiceHelper;
import kd.bos.workflow.component.approvalrecord.IApprovalRecordGroup;
import kd.bos.workflow.component.approvalrecord.IApprovalRecordItem;
import kd.sdk.plugin.Plugin;
import nckd.jimin.jyyy.hr.task.util.SyncOAUtil;

import java.util.Date;
import java.util.List;

/**
 * 出纳代发单打印插件 Tyx 2025-07-23
 * 获取关联的薪资审批单的审批记录
 */
public class AgentPayBillPrintPlugin extends AbstractPrintPlugin implements Plugin {

    private static final Log log = LogFactory.getLog(AgentPayBillPrintPlugin.class);
    private static String SWCAGENTBILL_ENTITY = "hsas_agencypaybill";
    private static String PAYDETAIL_ENTITY = "hsas_paydetail";
    private static String APPROVEBILL_ENTITY = "hsas_approvebill";

    @Override
    public void beforeLoadData(BeforeLoadDataEvent evt) {
        super.beforeLoadData(evt);
    }

    @Override
    public void loadCustomData(CustomDataLoadEvent evt) {
        super.loadCustomData(evt);
        String queryName = evt.getDataSource().getDsName();
        if("nckd_approvebill".equals(queryName)) {
            //获取出纳代发单ID
            Object pkId = evt.getDataSource().getPkId();
            String entityId = evt.getDataSource().getFormId();
            DynamicObject agentPayBillDyn = BusinessDataServiceHelper.loadSingle(pkId, entityId);
            //取第一条分录的源单ID/源单分录ID
            DynamicObjectCollection entryCols = agentPayBillDyn.getDynamicObjectCollection("entry");
            if(entryCols.size() > 0) {
                DynamicObject entry = entryCols.get(0);
                long sourceAgentBillId = entry.getLong("sourcebillid");
                long sourceAgentEntryId = entry.getLong("sourceentryid");
                //查询薪资代发单分录-发放明细ID
                QFilter filter = new QFilter("id", QCP.equals, sourceAgentBillId);
                filter.and("entryentity.id", QCP.equals, sourceAgentEntryId);
                DynamicObject sourceAgentBill = QueryServiceHelper.queryOne(SWCAGENTBILL_ENTITY, "entryentity.paydetail", new QFilter[]{filter});
                if(!ObjectUtils.isEmpty(sourceAgentBill)) {
                    //再获取发放明细-薪酬核算大表ID
                    String selectFields = "payrolldate,caltableid,caltask.period.number";
                    DynamicObject payDetailDyn = BusinessDataServiceHelper.loadSingle(sourceAgentBill.getLong("entryentity.paydetail"), PAYDETAIL_ENTITY, selectFields);
                    if(!ObjectUtils.isEmpty(payDetailDyn)) {
                        //薪资所属年月
                        String period = payDetailDyn.getString("caltask.period.number");
                        long calTableId = payDetailDyn.getLong("caltableid");
                        //再获取薪资审批单ID
                        QFilter filter1 = new QFilter("calentryentity.caltableid",QCP.equals,calTableId);
                        DynamicObject approveBillDyn = QueryServiceHelper.queryOne(APPROVEBILL_ENTITY, "id", new QFilter[]{filter1});
                        if(!ObjectUtils.isEmpty(approveBillDyn)) {
                            //获取薪资审批单的审批记录
                            List<IApprovalRecordGroup> appRecord = WorkflowServiceHelper.getAllApprovalRecord(String.valueOf(approveBillDyn.getLong("id")));
                            evt.getCustomDataRows().clear();
                            for(IApprovalRecordGroup record : appRecord) {
                                List<IApprovalRecordItem> childs = record.getChildren();
                                for (IApprovalRecordItem child : childs) {
                                    DataRowSet dataRowSet = new DataRowSet();
                                    String a = child.getActivityName();
                                    String b = child.getAssignee();
                                    String c = child.getTime();
                                    String d = child.getMessage();
                                    String e = child.getResult();
                                    dataRowSet.put("a", new TextField(a));
                                    dataRowSet.put("b", new TextField(b));
                                    dataRowSet.put("c", new TextField(c));
                                    dataRowSet.put("d", new TextField(d));
                                    dataRowSet.put("e", new TextField(e));
                                    dataRowSet.put("f", new TextField(period));
                                    evt.getCustomDataRows().add(dataRowSet);
                                }

                            }
                            log.info("1");
                        }
                    }
                }
            }
        }
    }
}