package com.yem.wm.syn.eas.api.service.impl;

import com.grapecity.documents.excel.Q;
import com.yem.wm.syn.eas.api.service.ICommonAuditService;
import com.yem.wm.syn.eas.api.util.OperationServicUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.exception.KDBizException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

public class CommonAuditServiceImpl implements ICommonAuditService {
    @Override
    public void addNew(String number, String entityname,String auditor,Boolean status) {

        DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(entityname, new QFilter[]{new QFilter("billno", QCP.equals, number)});
        if (dynamicObject==null){
            throw new KDBizException("未找到编码为:"+number+"的单据!");
        }
        //如果审批不通过 调用单据撤销
        if (!status){
            dynamicObject.set("yem_isyzjreject",true);
            String s = OperationServicUtil.executeOperates("unsubmit", new DynamicObject[]{dynamicObject}, "撤销失败");
            if (s!=null){
                throw new KDBizException("编码为:"+number+s);
            }
            return;
        }

        //审核人
        DynamicObject auditorBill = BusinessDataServiceHelper.loadSingle("bos_user", new QFilter[]{new QFilter("number", QCP.equals, auditor)});
        if (auditorBill==null) {
            //DeleteServiceHelper.delete(dynamicObject.getDataEntityType(),new Object[]{dynamicObject.getPkValue()});
            throw new KDBizException("未找到编码为:"+auditor+"的审核人!");
        }
        String s = OperationServicUtil.executeOperates("audit", new DynamicObject[]{dynamicObject}, "审核失败");
        if (s!=null){
            throw new KDBizException("编码为:"+number+s);
        }


        DynamicObject dynamic = BusinessDataServiceHelper.loadSingle(entityname, new QFilter[]{new QFilter("billno", QCP.equals, number)});
        dynamic.set("auditor",auditorBill);
        dynamic.set("modifier",auditorBill);
        SaveServiceHelper.update(dynamic);
    }
}
