package fi.cas.task;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.exception.KDException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;

import java.util.Map;

public class AgentPayUpadteStatusTask extends AbstractTask {
    private static final Log log = LogFactory.getLog(AgentPayUpadteStatusTask.class);
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        //获取要查询的数据。状态为已审批，支付状态为支付成功。
        QFilter filter = new QFilter("billstatus", QCP.equals, "C");
        filter.and("nckd_paystatus", QCP.equals, "3");
        filter.and("nckd_fkcs", QCP.large_equals, 0);
        DynamicObject[] payinfos = BusinessDataServiceHelper.load("cas_agentpaybill","billno", new QFilter[] {filter});
        if(payinfos.length>0){
            for (int i = 0; i <payinfos.length ; i++) {
                DynamicObject payinfo = BusinessDataServiceHelper.loadSingle(payinfos[i].getPkValue(),"cas_agentpaybill");
                try {
                    OperationServiceHelper.executeOperate("pay","cas_agentpaybill",new DynamicObject[]{payinfo}, OperateOption.create());
                }catch (Exception e){
                    log.info("代发单变更状态报错："+payinfo.get("billno"));
                }
            }


        }

        //获取要查询的数据。状态为已审批，支付状态为支付成功。
        QFilter filter_bfcg = new QFilter("billstatus", QCP.equals, "C");
        filter_bfcg.and("nckd_paystatus", QCP.equals, "7");
        filter_bfcg.and("nckd_fkcs", QCP.large_equals, 0);
        DynamicObject[] payinfos_bfcg = BusinessDataServiceHelper.load("cas_agentpaybill","billno", new QFilter[] {filter_bfcg});
        if(payinfos_bfcg.length>0){
            for (int i = 0; i <payinfos_bfcg.length ; i++) {
                DynamicObject payinfo_bfcg = BusinessDataServiceHelper.loadSingle(payinfos_bfcg[i].getPkValue(),"cas_agentpaybill");
                try {
                    OperationServiceHelper.executeOperate("paynew","cas_agentpaybill",new DynamicObject[]{payinfo_bfcg}, OperateOption.create());
                }catch (Exception e){
                    log.info("代发单变更状态报错："+payinfo_bfcg.get("billno"));
                }
            }


        }

    }
}
