package com.yem.wm.es.salesorder.print;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.print.core.data.DataRowSet;
import kd.bos.print.core.data.datasource.PrtDataSource;
import kd.bos.print.core.data.datasource.WorkflowDataSource;
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.CustomDataLoadEvent;
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 kd.bos.servicehelper.workflow.WorkflowServiceHelper;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 外销整机合同 - 销售合同打印(新)
 */
public class SalesorderSignaturePlugin extends AbstractPrintPlugin {
    //费用报销单
    private static final String wfsDs = "yem_es_salesorder.workflow.activity";

    @Override
    public void afterLoadData(AfterLoadDataEvent evt) {
        // TODO Auto-generated method stub
        super.afterLoadData(evt);
        PrtDataSource dataSource = evt.getDataSource();
        if(dataSource==null) {
            return;
        }
        if(!StringUtils.equals(dataSource.getDsName(),wfsDs)){
            return;
        }
        String businessKey = ((WorkflowDataSource) dataSource).getBusinessKey();
        DynamicObject yem_es_salesorder = BusinessDataServiceHelper.loadSingle(businessKey,"yem_es_salesorder");
        if (yem_es_salesorder==null){
            return;
        }
        String status = yem_es_salesorder.getString("billstatus");
        if (!"C".equals(status)){
            return;
        }

        //非工作流直接返回
        List<DataRowSet> dataRowSets = evt.getDataRowSets();
        //1820996513912865792 1820996513912865792
        //获取数据源集合
        for (DataRowSet dataRowSet:dataRowSets) {

            Field YZSPTaskId = dataRowSet.getField("YZSP.taskId");
            //主管
            if(YZSPTaskId.toString().equals("")) {
                continue;
            }else{
                String imageUrl = signature(YZSPTaskId);
                ImageField picField = new ImageField(imageUrl);
                dataRowSet.add("zgTaskId", picField);
                //公司签章
                String companySignUrl = companySignature(yem_es_salesorder);
                ImageField companySignUrlField = new ImageField(companySignUrl);
                dataRowSet.add("companySignUrlField", companySignUrlField);
            }



        }


    }


    public String signature(Field taskIds){
        long taskId = Long.parseLong(String.valueOf(taskIds));
//			DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingleFromCache
//					("wf_hiparticipant", "userid",
//							new QFilter[]{new QFilter("taskid", QFilter.equals, taskId)});
        DynamicObjectCollection collection = WorkflowServiceHelper.getCommentsByTaskId(taskId);
        DynamicObject dynamicObject = collection.get(0);
        if(dynamicObject==null){
            return null;
        }
        //获取参与人ID
        Object userid = dynamicObject.get("userid");
        //通过参与人ID查询该用户签章信息
        DynamicObject user = BusinessDataServiceHelper.loadSingleFromCache(userid, "bos_user", "yem_signature");
        if(user==null){
            return null;
        }
        //将签章信息设置进数据集合
        String extPictureUrl = user.getString("yem_signature");
        return extPictureUrl;
    }
    public String companySignature(DynamicObject yem_es_salesorder){
        DynamicObject yem_company = yem_es_salesorder.getDynamicObject("yem_company");
        String yemCompanysign = yem_company.getString("yem_companysign");
        return yemCompanysign;
    }



    /**
     * 控件输出前事件-数据表格输出前-设置数据行签章单元格绑定工作流审批线路中新填充的签章字段
     * @param evt
     */
    @Override
    public void beforeOutputWidget(BeforeOutputWidgetEvent evt) {

        super.beforeOutputWidget(evt);
        if(!(evt instanceof BeforeOutputGridEvent)){
            return;
        }
        BeforeOutputGridEvent gridEvent = (BeforeOutputGridEvent) evt;
        //gridEvent.getGrid().grid.getKey();
        String key = evt.getWidgetKey();
        if("LayoutGridYZ".equals(key)){
            PWGridBo pwGridBo = gridEvent.getGrid();

            //非工作流直接返回
//				if(!StringUtils.equals(pwGridBo.getDataSource(),wfsDs)){
//					return;
//				}

            //获取数据行 主管
            PWGridRowBo zgTaskIdRow = pwGridBo.getRow(0);
            //获取签章单元格
            PWGridCellBo zgTaskIdRowCell = zgTaskIdRow.getCell(0);
            //设置单元格绑定工作流数据源中的签章
            zgTaskIdRowCell.setCellValue(wfsDs,"zgTaskId");



        }

        if ("LayoutGridhHTYZ".equals(key)){
            PWGridBo pwGridBo = gridEvent.getGrid();
            //获取数据行 公司
            PWGridRowBo zgTaskIdRow = pwGridBo.getRow(0);
            //获取签章单元格
            PWGridCellBo zgTaskIdRowCell = zgTaskIdRow.getCell(0);
            //设置单元格绑定工作流数据源中的签章
            zgTaskIdRowCell.setCellValue(wfsDs,"companySignUrlField");
        }
    }
}