package com.yem.em.bd.materialpicupload.op;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.url.UrlService;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;

/**
 * @author 夜子
 * @ClassName MaterialPicMatchOp
 * @description: 图片批量上传
 * @date 2023年07月12日
 * @version: 1.0
 */
public class MaterialPicMatchOp extends AbstractOperationServicePlugIn {


    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        super.beginOperationTransaction(e);

        for (DynamicObject dataEntity : e.getDataEntities()) {
            String pkValue = dataEntity.getPkValue().toString();
            dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(),dataEntity.getDynamicObjectType().getName());
            DynamicObjectCollection attachmentpanelCol =  dataEntity.getDynamicObjectCollection("yem_attachmentpanel");
            DynamicObject[] atts = BusinessDataServiceHelper.load("bos_attachment", "id,ffileid,fattachmentname",
                    new QFilter[] { new QFilter("fbilltype", QCP.equals, "yem_em_materialpicupload"),
                            new QFilter("finterid", QCP.equals, pkValue) });
            for (DynamicObject att : atts) {
                String fileName = att.getString("fattachmentname");
                String materialNum = StringUtils.substringBeforeLast(fileName, ".").trim();
                QFilter qFilter1 = new QFilter("number", QCP.equals, materialNum);
                QFilter qFilter2 = new QFilter("number", QCP.equals, materialNum+"_ZYH");
                DynamicObject[] comlefttreeCol = BusinessDataServiceHelper.load("yem_em_comlefttree", "id,number    ",
                        new QFilter[] { qFilter1.or(qFilter2)});
                for(DynamicObject comlefttree:comlefttreeCol){
                    comlefttree = BusinessDataServiceHelper.loadSingle(comlefttree.getPkValue(), comlefttree.getDynamicObjectType().getName());
                    comlefttree.set("yem_picture", UrlService.getAttachmentFullUrl(att.getString("ffileid")));
                    SaveServiceHelper.save(new DynamicObject[] { comlefttree });
                }

                DynamicObject[] etcmanualCol = BusinessDataServiceHelper.load("yem_em_equipmentlist", "id,number    ",
                        new QFilter[] { new QFilter("number", QCP.equals, materialNum) });
                for(DynamicObject etcmanual:etcmanualCol){
                    etcmanual = BusinessDataServiceHelper.loadSingle(etcmanual.getPkValue(), etcmanual.getDynamicObjectType().getName());
                    etcmanual.set("yem_picture", UrlService.getAttachmentFullUrl(att.getString("ffileid")));
                    SaveServiceHelper.save(new DynamicObject[] { etcmanual });
                }

                DynamicObject[] ecataloggroupCol = BusinessDataServiceHelper.load("yem_em_component", "id,yem_cnumber",
                        new QFilter[] { new QFilter("yem_cnumber", QCP.equals, materialNum) });
                for(DynamicObject ecataloggroup:ecataloggroupCol){
                    ecataloggroup = BusinessDataServiceHelper.loadSingle(ecataloggroup.getPkValue(), ecataloggroup.getDynamicObjectType().getName());
                    String url = UrlService.getAttachmentFullUrl(att.getString("ffileid"));
                    ecataloggroup.set("yem_picture", url);
                    String imageUrls = ecataloggroup.getString("yem_imageurl");
                    String[] urls = imageUrls.split(";");
                    boolean ifContain = Arrays.toString(urls).contains(url);
                    if(!ifContain){
                        imageUrls = imageUrls+url+";";
                    }
                    ecataloggroup.set("yem_imageurl", imageUrls);

                    SaveServiceHelper.save(new DynamicObject[] { ecataloggroup });
                }

                DynamicObject[] ecatalogCol = BusinessDataServiceHelper.load("yem_em_part", "id,yem_pnumber",
                        new QFilter[] { new QFilter("yem_pnumber", QCP.equals, materialNum) });
                for(DynamicObject ecatalog:ecatalogCol){
                    ecatalog = BusinessDataServiceHelper.loadSingle(ecatalog.getPkValue(), ecatalog.getDynamicObjectType().getName());
                    ecatalog.set("yem_picture", UrlService.getAttachmentFullUrl(att.getString("ffileid")));
                    SaveServiceHelper.save(new DynamicObject[] { ecatalog });
                }

                for (DynamicObject attachmentpanel : attachmentpanelCol) {
                    String yem_attachmentname =  attachmentpanel.getString("yem_attachmentname");
                    if(yem_attachmentname.equals(fileName)){
                        attachmentpanel.set("yem_syncstatus","B");
                    }
                }
            }

            SaveServiceHelper.save(new DynamicObject[] { dataEntity });
        }
    }

}
