package service; import com.alibaba.fastjson2.JSONObject; import common.Controller; import common.ServiceController; import common.api.YOSAPI; import common.data.Row; import common.data.Rows; import restcontroller.R; import java.util.ArrayList; import java.util.UUID; public class OrderAutoCheck extends ServiceController { @Override public void serviceRun() throws Exception { Rows orderRows = dbConnect.runSqlQuery("select t1.sa_orderid,t1.sa_accountclassid,t1.sys_enterpriseid,t1.sonum,sum(t2.amount)as amount from sa_order t1\n" + "inner join sa_orderitems t2 on t1.sa_orderid=t2.sa_orderid \n" + "inner join sa_agents t3 on t1.sys_enterpriseid=t3.sys_enterpriseid and t3.submitautocheck=1 \n" + "where t1.status='提交' and not exists(select * from sa_orderitems t4 where t1.sa_orderid=t4.sa_orderid and t4.remarks is not null)\n" + " and exists(select * from sa_cashbill t5 where t5.type=1 and t5.status='审核' and t5.sys_enterpriseid=t1.sys_enterpriseid and t5.billdate>=date(now()))\n" + "group by t1.sa_orderid,t1.sa_accountclassid,t1.sys_enterpriseid,t1.sonum having sum(t2.amount)>0 order by t1.sys_enterpriseid,sum(t2.amount)"); logger.info("监测到有{}个订单待审核", orderRows.size()); Controller md = getSysController("MD"); ArrayList errenterpriseid = new ArrayList<>(); for (Row orderRow : orderRows) { long enterpriseid = orderRow.getLong("sys_enterpriseid"); if (!errenterpriseid.contains(enterpriseid)) { long sa_accountclassid = orderRow.getLong("sa_accountclassid"); double amount = orderRow.getDouble("amount"); Rows accountbalanceRows = dbConnect.runSqlQuery("select ifnull(balance,0)+ifnull(creditquota,0)+ifnull(discountamount,0)-ifnull(freezamount,0) as canuseamount from sa_accountbalance where sys_enterpriseid=" + enterpriseid + " and sa_accountclassid=" + sa_accountclassid); if (accountbalanceRows.isEmpty() || accountbalanceRows.get(0).getDouble("canuseamount") < amount) { logger.info("订单{}审核失败;{}", orderRow.getString("sonum"), "账户可用余额不足"); continue; } JSONObject content = new JSONObject(); content.put("sa_orderid", orderRow.getLong("sa_orderid")); content.put("reviewtype", ""); YOSAPI.YosRequest yosRequest = new YOSAPI.YosRequest(); yosRequest.setRequestKey(UUID.randomUUID().toString()); yosRequest.setAccesstoken(md.getAccessToken().getToken()); yosRequest.setRequestsessionid("-1"); yosRequest.setContent(content); JSONObject resultObject = ((JSONObject) YOSAPI.getApi(R.ID20221108153502.v1.class).action(yosRequest)); if (resultObject.getIntValue("status") == 0) { logger.info("订单{}审核失败;{}", orderRow.getString("sonum"), resultObject.getString("msg")); errenterpriseid.add(enterpriseid); } else { dbConnect.runSqlUpdate("update sa_order set checkby='自动审核' where sa_orderid=" + orderRow.getLong("sa_orderid")); logger.info("订单{}审核成功", orderRow.getString("sonum")); } } } } @Override public ServiceParam paramSet() { return new ServiceParam("订单自动审核", 10, RunType.minute); } }