package nckd.jimin.jyyy.bd.plugin.workflow;

import com.kingdee.util.StringUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.fileservice.extension.FileServiceExtFactory;
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.datasource.PrtDataSource;
import kd.bos.print.core.data.field.Field;
import kd.bos.print.core.data.field.ImageField;
import kd.bos.print.core.plugin.AbstractPrintPlugin;
import kd.bos.print.core.plugin.event.AfterLoadDataEvent;
import kd.bos.print.core.plugin.event.BeforeOutputGridEvent;
import kd.bos.print.core.plugin.event.BeforeOutputWidgetEvent;
import kd.bos.print.core.plugin.event.bo.PWGridBo;
import kd.bos.print.core.plugin.event.bo.PWGridCellBo;
import kd.bos.print.core.plugin.event.bo.PWGridRowBo;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import java.util.List;

/**
 *
 */
public class WFUserSignaturePrintPlugin extends AbstractPrintPlugin {
    //工作流-审批线路数据源=formId+.workflow.approveline
//    private static final String wfsDs = "";
    /**
     * 数据加载后事件，根据工作流-审批线路数据，查询对应的人员签章信息，再填充入审批线路集合
     * @param evt
     */
    @Override
    public void afterLoadData(AfterLoadDataEvent evt) {
        super.afterLoadData(evt);
        PrtDataSource dataSource = evt.getDataSource();
        if(dataSource == null){
            return;
        }
        String wfsDs = dataSource.getDsName();
        //非工作流直接返回
        if(!wfsDs.contains("workflow.approveline")){
            return;
        }
        //数据源集合
        List<DataRowSet> dataRowSets = evt.getDataRowSets();
        for (DataRowSet dataRowSet : dataRowSets) {
            //获取任务ID
            Field taskIdField = dataRowSet.getField("taskId");
            if(taskIdField==null || "".equals(taskIdField.getValue())){
                continue;
            }
            long taskId = Long.parseLong(String.valueOf(taskIdField));
            //通过任务ID查询工作流的历史参与人基础资料对应记录，其中含有参与人ID
            DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingleFromCache("wf_hiparticipant", "userid",
                    new QFilter[]{new QFilter("taskid", QFilter.equals, taskId)});
            if(dynamicObject==null){
                continue;
            }
            //获取参与人ID
            Object userid = dynamicObject.get("userid");
            //通过参与人ID查询该用户签章信息
            QFilter qFilter = new QFilter("user.id", QCP.equals, userid);
            DynamicObject signInfo = BusinessDataServiceHelper.loadSingleFromCache("bos_handwritten_sign", qFilter.toArray());
            if(signInfo==null){
                continue;
            }
            //将签章信息设置进数据集合
            String extPictureUrl = signInfo.getString("handwrittensign");
            extPictureUrl = kd.bos.fileservice.extension.FileServiceExtFactory.getImageFileServiceExt().getRealPath(extPictureUrl);
//            extPictureUrl = kd.bos.url.UrlService.getImageFullUrl(extPictureUrl);

            Field picField = new ImageField(extPictureUrl);
            dataRowSet.put("signature",picField);
        }
    }

    /**
     * 控件输出前事件-数据表格输出前-设置数据行签章单元格绑定工作流审批线路中新填充的签章字段
     * @param evt
     */
    @Override
    public void beforeOutputWidget(BeforeOutputWidgetEvent evt) {
        super.beforeOutputWidget(evt);
        if(!(evt instanceof BeforeOutputGridEvent)){
            return;
        }
        BeforeOutputGridEvent gridEvent = (BeforeOutputGridEvent) evt;
        PWGridBo pwGridBo = gridEvent.getGrid();

        String wfsDs = pwGridBo.getDataSource();
        //非工作流直接返回
        if(wfsDs == null || !wfsDs.contains("workflow.approveline")){
            return;
        }

//        for(int i = 0; i < pwGridBo.getRowCount(); i++) {
            //获取数据行
            PWGridRowBo row = pwGridBo.getRow(1);
            //获取签章单元格
            PWGridCellBo cell = row.getCell(2);
            //设置单元格绑定工作流数据源中的签章
            cell.setCellValue(wfsDs, "signature");
//        }
    }
}
