package com.yem.wm.im.intauditbusa.op;

import com.yem.wm.im.intauditbusa.validator.IntAuditBuSaUnAuditValidator;
import com.yem.wm.utils.FunctionalCommon;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xwudd
 * @Description
 * @date 9:36
 **/
public class IntAuditBuSaUnAuditOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("yem_sourcebillid");
        fieldKeys.add("yem_sourcebillno");
//        fieldKeys.add("yem_intauditbusaid");
//        fieldKeys.add("billno");
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.addValidator(new IntAuditBuSaUnAuditValidator());
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntitie : dataEntities) {
            dataEntitie = BusinessDataServiceHelper.loadSingle(dataEntitie.getPkValue(), dataEntitie.getDynamicObjectType().getName());
            FunctionalCommon.removeAtt(dataEntitie);
//            List<String> attNameList = new ArrayList();
//            DynamicObject billType = dataEntitie.getDynamicObject("yem_billtype");
//            String billTypeNumber = YEM.isNotEmpty(billType)?billType.getString("number"):"";
//            if("yem_im_intauditbusa_PJWX".equals(billTypeNumber)){
//                attNameList.add(FunctionalCommon.setAttName("PJ",dataEntitie));
//            }
//            if("yem_im_intauditbusa_ZJWX".equals(billTypeNumber)) {
//                attNameList.add(FunctionalCommon.setAttName("NSZJ",dataEntitie));
//                attNameList.add(FunctionalCommon.setAttName("HSZJ",dataEntitie));
//            }
//            List<Map<String, Object>> attachments = AttachmentServiceHelper.getAttachments(dataEntitie.getString("yem_sourcebilltype"), dataEntitie.getLong("yem_sourcebillid"), "attachmentpanel");
//            for(Map<String, Object> attachment : attachments){
//                String name = (String)attachment.get("name");
//                if(attNameList.contains(name)){
//                    AttachmentServiceHelper.remove(dataEntitie.getString("dataEntitie"), dataEntitie.getLong("yem_sourcebillid"),attachment.get("uid"));
//                }
//            }
        }
    }
}
