package beans.order; import beans.accountbalance.Accountbalance; import beans.accountbalance.CashbillEntity; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import common.BaseClass; import common.Controller; import common.YosException; import common.data.*; import beans.parameter.Parameter; import common.data.db.SQLList; import java.math.BigDecimal; import java.util.ArrayList; public class Order extends BaseClass { /** * 查询订单金额汇总信息 * * @param controller * @param sa_orderids * @return * @throws YosException */ public static RowsMap getSumOrderAmount(Controller controller, ArrayList sa_orderids) throws YosException { SQLFactory sqlFactory = new SQLFactory(new Order(), "查询订单金额汇总"); sqlFactory.addParameter("siteid", controller.siteid); sqlFactory.addParameter_in("sa_orderid", sa_orderids); Rows rows = controller.dbConnect.runSqlQuery(sqlFactory); return rows.toRowsMap("sa_orderid"); } /** * 查询订单金额汇总信息 * * @param controller * @param sa_orderids * @return * @throws YosException */ public static RowsMap getSumReturnQty(Controller controller, ArrayList sa_orderids) throws YosException { SQLFactory sqlFactory = new SQLFactory(new Order(), "查询订单退货数量汇总"); sqlFactory.addParameter("siteid", controller.siteid); sqlFactory.addParameter_in("sa_orderid", sa_orderids); Rows rows = controller.dbConnect.runSqlQuery(sqlFactory); return rows.toRowsMap("sa_orderid"); } /** * 获取收货人信息 * * @param controller * @param contactsids * @return * @throws YosException */ public static RowsMap getContactsRowsMap(Controller controller, ArrayList contactsids) throws YosException { SQLFactory sqlFactory = new SQLFactory(new Order(), "订单-收货信息"); sqlFactory.addParameter_in("contactsid", contactsids); sqlFactory.addParameter("siteid", controller.siteid); Rows rows = controller.dbConnect.runSqlQuery(sqlFactory); return rows.toRowsMap("contactsid"); } /** * 获取收货人信息 * * @param controller * @param contactsid * @return * @throws YosException */ public static Row getContactsRow(Controller controller, Long contactsid) throws YosException { SQLFactory sqlFactory = new SQLFactory(new Order(), "订单-收货信息"); sqlFactory.addParameter_in("contactsid", contactsid); sqlFactory.addParameter("siteid", controller.siteid); String sql = sqlFactory.getSQL(); Rows rows = controller.dbConnect.runSqlQuery(sql); return rows.isNotEmpty() ? rows.get(0) : new Row(); } /** * 获取企业订单默认扣款账户 * * @param siteid 站点 * @param ordertype 订单类型 (如标准订单、项目订单) * @return * @throws YosException */ public static Long getDefaultAccount(String siteid, String ordertype) throws YosException { String orderdefaultaccount = Parameter.getString(siteid, "orderdefaultaccount"); try { JSONArray array = JSONArray.parseArray(orderdefaultaccount); for (Object o : array) { JSONObject object = (JSONObject) o; if (object.getString("ordertype").equals(ordertype)) { return object.getLongValue("sa_accountclassid"); } } return 0L; } catch (Exception e) { return 0L; } } /** * 获取企业订订单类型默认可重复添加设置 * * @param siteid 站点 * @param ordertype 订单类型 (如标准订单、项目订单) * @return * @throws YosException */ public static boolean getDefaultIsRepeatValue(String siteid, String ordertype) throws YosException { String orderdefaultaccount = Parameter.getString(siteid, "orderdefaultrepeat"); try { JSONArray array = JSONArray.parseArray(orderdefaultaccount); for (Object o : array) { JSONObject object = (JSONObject) o; if (object.getString("ordertype").equals(ordertype)) { return object.getBooleanValue("isrepeat"); } } return false; } catch (Exception e) { return false; } } /** * 更新订单返利金信息 * * @param controller * @param sa_orderid * @param rebate_used * @param rebate_userate * @param rebateamount * @return * @return * @throws YosException */ public static String updateRateInfo(Controller controller, Long sa_orderid, BigDecimal rebate_used, BigDecimal rebate_userate, BigDecimal rebateamount) throws YosException { SQLFactory sqlFactory = new SQLFactory(new Order(), "更新订单返利金信息"); sqlFactory.addParameter("siteid", controller.siteid); sqlFactory.addParameter("userid", controller.userid); sqlFactory.addParameter("username", controller.username); sqlFactory.addParameter("sa_orderid", sa_orderid); sqlFactory.addParameter("rebate_used", rebate_used); sqlFactory.addParameter("rebate_userate", rebate_userate); sqlFactory.addParameter("rebateamount", rebateamount); return sqlFactory.getSQL(); } /** * 设置返利金 * * @param sa_orderid * @throws YosException */ public static void setRebateAmount(Controller controller, Long sa_orderid, BigDecimal rebateamount) throws YosException { Rows orderRows = getOrderRows(controller, sa_orderid); if (orderRows.isEmpty()) { return; } Row orderRow = orderRows.get(0); boolean rebate_used = orderRow.getBoolean("rebate_used"); //不使用返利金 if (!rebate_used) { return; } //获取订单折前价 BigDecimal defaultAmount = getDefaultAmount(controller, sa_orderid); //获取订单的返利金使用额度 if (rebateamount.compareTo(BigDecimal.ZERO) <= 0) { return; } BigDecimal rebate_userate; if (defaultAmount.compareTo(BigDecimal.ZERO) <= 0) { rebate_userate = BigDecimal.ZERO; } else { rebate_userate = rebateamount.divide(defaultAmount, 6, BigDecimal.ROUND_CEILING); } SQLList sqlList = new SQLList(); //更新订单表体中的rebate_userate sqlList.add("UPDATE sa_order SET rebate_userate='" + rebate_userate + "' ,rebateamount='" + rebateamount + "' WHERE sa_orderid=" + sa_orderid + " and siteid='" + controller.siteid + "'"); //更新订单商品数据 sqlList.add("UPDATE sa_orderitems SET rebateamount = defaultamount * " + rebate_userate + ",amount = defaultamount - rebateamount ,price = amount / qty WHERE sa_orderid = " + sa_orderid + " and siteid='" + controller.siteid + "' and qty!=0"); sqlList.add("UPDATE sa_orderitems SET rebateamount =0,amount = 0 WHERE sa_orderid = " + sa_orderid + " and siteid='" + controller.siteid + "' and qty=0"); controller.dbConnect.runSqlUpdate(sqlList); } /** * 调整最后一行数据 * * @param sa_orderid * @throws YosException */ public static void adjustLastRebateAmount(Controller controller, Long sa_orderid) throws YosException { Rows orderRows = getOrderRows(controller, sa_orderid); if (orderRows.isEmpty()) { return; } BigDecimal rebateamount = orderRows.get(0).getBigDecimal("rebateamount"); //调整返利金最后一行数据 Rows orderItemRows = controller.dbConnect.runSqlQuery("SELECT sa_orderitemsid FROM sa_orderitems WHERE sa_orderid = " + sa_orderid + " and siteid='" + controller.siteid + "' ORDER BY rowno"); if (orderItemRows.size() >= 1) { Long sa_orderitemsid = orderItemRows.get(orderItemRows.size() - 1).getLong("sa_orderitemsid"); Rows rows = controller.dbConnect.runSqlQuery("SELECT SUM(rebateamount) rebateamount from sa_orderitems WHERE sa_orderid = " + sa_orderid); BigDecimal sum_rebateamount = rows.get(0).getBigDecimal("rebateamount"); BigDecimal temp = sum_rebateamount.subtract(rebateamount); if (temp.compareTo(BigDecimal.ZERO) == 1) { controller.dbConnect.runSqlUpdate(" UPDATE sa_orderitems SET rebateamount = rebateamount - " + temp + ",amount = amount + " + temp + " ,price = amount / qty WHERE sa_orderitemsid = " + sa_orderitemsid + " and siteid='" + controller.siteid + "' and qty!=0"); } if (temp.compareTo(BigDecimal.ZERO) == -1) { temp = temp.abs(); controller.dbConnect.runSqlUpdate(" UPDATE sa_orderitems SET rebateamount = rebateamount + " + temp + ",amount = amount - " + temp + " ,price = amount / qty WHERE sa_orderitemsid = " + sa_orderitemsid + " and siteid='" + controller.siteid + "' and qty!=0"); } controller.dbConnect.runSqlUpdate(" UPDATE sa_orderitems SET rebateamount = 0,amount = 0 WHERE sa_orderitemsid = " + sa_orderitemsid + " and siteid='" + controller.siteid + "' and qty=0"); } } /** * 获取订单最大可使用用返利金金额 * * @return */ public static BigDecimal getMaxUsedRebateAmount(Controller controller, Long sa_orderid) throws YosException { Rows orderRows = getOrderRows(controller, sa_orderid); Long pay_enterpriseid = orderRows.get(0).getLong("pay_enterpriseid"); //站点设置的最大返利金比例 BigDecimal order_rebate_userate = BigDecimal.valueOf(Parameter.getDouble(controller.siteid, "order_rebate_userate")); //订单的折前价格 BigDecimal defaultAmount = getDefaultAmount(controller, sa_orderid); //当前订单最大可使用返利金 BigDecimal maxAmount = defaultAmount.multiply(order_rebate_userate); //当前可使用返利金额度 BigDecimal rebateBalance = Accountbalance.getRebateBalance(controller, pay_enterpriseid); if (maxAmount.compareTo(rebateBalance) <= 0) { //System.err.println(maxAmount); return maxAmount; } else { // System.err.println(rebateBalance); return rebateBalance; } } /** * 获取订单表头的rows * * @param sa_orderid * @return * @throws YosException */ public static Rows getOrderRows(Controller controller, Long sa_orderid) throws YosException { return controller.dbConnect.runSqlQuery("SELECT * from sa_order WHERE deleted=0 and sa_orderid = " + sa_orderid + " and siteid = '" + controller.siteid + "'"); } /** * 获取订单折后价格 * * @param sa_orderid * @return * @throws YosException */ public static BigDecimal getAmount(Controller controller, Long sa_orderid) throws YosException { Rows rows = controller.dbConnect.runSqlQuery("SELECT ifnull(sum(amount),0) amount from sa_orderitems WHERE sa_orderid=" + sa_orderid + " AND siteid = '" + controller.siteid + "'"); return rows.get(0).getBigDecimal("amount"); } /** * 获取订单折前价格 * * @param controller * @param sa_orderid * @return * @throws YosException */ public static BigDecimal getDefaultAmount(Controller controller, Long sa_orderid) throws YosException { Rows rows = controller.dbConnect.runSqlQuery("SELECT ifnull(sum(defaultamount),0) defaultamount from sa_orderitems WHERE sa_orderid=" + sa_orderid + " AND siteid = '" + controller.siteid + "'"); return rows.get(0).getBigDecimal("defaultamount"); } public static SQLList createRebateBillPay(Controller controller, CashbillEntity cashbillEntity, Long sys_enterpriseid, Long sa_cashbillid_rebate) throws YosException { Long sa_cashbillid = controller.createTableID("sa_cashbill"); String billcode = controller.createBillCode("cashbill1"); Rows rows = controller.dbConnect.runSqlQuery("SELECT * from sa_cashbill WHERE sa_cashbillid = " + sa_cashbillid_rebate + " and siteid = '" + controller.siteid + "'"); Long sa_accountclassid = 0L; if (rows.isNotEmpty()) { sa_accountclassid = rows.get(0).getLong("sa_accountclassid"); } SQLList sqlList = new SQLList(); SQLFactory sqlFactory = new SQLFactory(new Order(), "返利代金券支出-新增"); sqlFactory.addParameter("billno", billcode); sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid); sqlFactory.addParameter("sa_accountclassid", sa_accountclassid); sqlFactory.addParameter("type", 0); sqlFactory.addParameter("siteid", controller.siteid); sqlFactory.addParameter("remarks", cashbillEntity.getRemarks()); sqlFactory.addParameter("amount", cashbillEntity.getAmount()); sqlFactory.addParameter("sa_cashbillid", sa_cashbillid); sqlFactory.addParameter("userid", controller.userid); sqlFactory.addParameter("username", controller.username); sqlFactory.addParameter("ownertable", cashbillEntity.getOwnertable()); sqlFactory.addParameter("ownerid", cashbillEntity.getOwnerid()); sqlFactory.addParameter("source", cashbillEntity.getSource()); sqlFactory.addParameter("sourcenote", cashbillEntity.getSourcenote()); sqlFactory.addParameter("sa_cashbillid_rebate", sa_cashbillid_rebate); //取金额的绝对值 BigDecimal amount = cashbillEntity.getAmount().abs(); BigDecimal balance = Accountbalance.getRebateBalance(controller, sys_enterpriseid).add(amount); sqlFactory.addParameter("balance", balance); sqlFactory.addParameter("status", "审核"); sqlFactory.addParameter("checkby", controller.username); sqlFactory.addParameter_SQL("checkdate", "CURRENT_TIME"); sqlList.add(sqlFactory.getSQL()); sqlList.add("UPDATE sa_cashbill SET rebate_balance= rebate_balance +" + amount + ",balance= " + balance + " WHERE sa_cashbillid = " + sa_cashbillid_rebate + " and siteid = '" + controller.siteid + "'"); sqlList.add("UPDATE sa_accountbalance SET balance=" + balance + " WHERE sys_enterpriseid = " + sys_enterpriseid + " and sa_accountclassid=" + sa_accountclassid + " and siteid = '" + controller.siteid + "'"); return sqlList; } /** * 通过版本更新订单表头数据 * * @param controller * @throws YosException */ public static void updateOrderWithVersion(Controller controller) throws YosException { if (controller.content.containsKey("sa_order_v")) { int sa_order_v = controller.content.getIntValue("sa_order_v"); Long sa_orderid = controller.content.getLong("sa_orderid"); UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, "sa_order"); updateSQL.setDataVersion(sa_order_v); updateSQL.setValue("changeby", controller.username); updateSQL.setWhere("sa_orderid", sa_orderid); updateSQL.setWhere("siteid", controller.siteid); updateSQL.update(); } } }