Order.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package beans.order;
  2. import beans.accountbalance.Accountbalance;
  3. import beans.accountbalance.CashbillEntity;
  4. import com.alibaba.fastjson2.JSONArray;
  5. import com.alibaba.fastjson2.JSONObject;
  6. import common.BaseClass;
  7. import common.Controller;
  8. import common.YosException;
  9. import common.data.*;
  10. import beans.parameter.Parameter;
  11. import common.data.db.SQLList;
  12. import java.math.BigDecimal;
  13. import java.util.ArrayList;
  14. public class Order extends BaseClass {
  15. /**
  16. * 查询订单金额汇总信息
  17. *
  18. * @param controller
  19. * @param sa_orderids
  20. * @return
  21. * @throws YosException
  22. */
  23. public static RowsMap getSumOrderAmount(Controller controller, ArrayList<Long> sa_orderids) throws YosException {
  24. SQLFactory sqlFactory = new SQLFactory(new Order(), "查询订单金额汇总");
  25. sqlFactory.addParameter("siteid", controller.siteid);
  26. sqlFactory.addParameter_in("sa_orderid", sa_orderids);
  27. Rows rows = controller.dbConnect.runSqlQuery(sqlFactory);
  28. return rows.toRowsMap("sa_orderid");
  29. }
  30. /**
  31. * 查询订单金额汇总信息
  32. *
  33. * @param controller
  34. * @param sa_orderids
  35. * @return
  36. * @throws YosException
  37. */
  38. public static RowsMap getSumReturnQty(Controller controller, ArrayList<Long> sa_orderids) throws YosException {
  39. SQLFactory sqlFactory = new SQLFactory(new Order(), "查询订单退货数量汇总");
  40. sqlFactory.addParameter("siteid", controller.siteid);
  41. sqlFactory.addParameter_in("sa_orderid", sa_orderids);
  42. Rows rows = controller.dbConnect.runSqlQuery(sqlFactory);
  43. return rows.toRowsMap("sa_orderid");
  44. }
  45. /**
  46. * 获取收货人信息
  47. *
  48. * @param controller
  49. * @param contactsids
  50. * @return
  51. * @throws YosException
  52. */
  53. public static RowsMap getContactsRowsMap(Controller controller, ArrayList<Long> contactsids) throws YosException {
  54. SQLFactory sqlFactory = new SQLFactory(new Order(), "订单-收货信息");
  55. sqlFactory.addParameter_in("contactsid", contactsids);
  56. sqlFactory.addParameter("siteid", controller.siteid);
  57. Rows rows = controller.dbConnect.runSqlQuery(sqlFactory);
  58. return rows.toRowsMap("contactsid");
  59. }
  60. /**
  61. * 获取收货人信息
  62. *
  63. * @param controller
  64. * @param contactsid
  65. * @return
  66. * @throws YosException
  67. */
  68. public static Row getContactsRow(Controller controller, Long contactsid) throws YosException {
  69. SQLFactory sqlFactory = new SQLFactory(new Order(), "订单-收货信息");
  70. sqlFactory.addParameter_in("contactsid", contactsid);
  71. sqlFactory.addParameter("siteid", controller.siteid);
  72. String sql = sqlFactory.getSQL();
  73. Rows rows = controller.dbConnect.runSqlQuery(sql);
  74. return rows.isNotEmpty() ? rows.get(0) : new Row();
  75. }
  76. /**
  77. * 获取企业订单默认扣款账户
  78. *
  79. * @param siteid 站点
  80. * @param ordertype 订单类型 (如标准订单、项目订单)
  81. * @return
  82. * @throws YosException
  83. */
  84. public static Long getDefaultAccount(String siteid, String ordertype) throws YosException {
  85. String orderdefaultaccount = Parameter.getString(siteid, "orderdefaultaccount");
  86. try {
  87. JSONArray array = JSONArray.parseArray(orderdefaultaccount);
  88. for (Object o : array) {
  89. JSONObject object = (JSONObject) o;
  90. if (object.getString("ordertype").equals(ordertype)) {
  91. return object.getLongValue("sa_accountclassid");
  92. }
  93. }
  94. return 0L;
  95. } catch (Exception e) {
  96. return 0L;
  97. }
  98. }
  99. /**
  100. * 获取企业订订单类型默认可重复添加设置
  101. *
  102. * @param siteid 站点
  103. * @param ordertype 订单类型 (如标准订单、项目订单)
  104. * @return
  105. * @throws YosException
  106. */
  107. public static boolean getDefaultIsRepeatValue(String siteid, String ordertype) throws YosException {
  108. String orderdefaultaccount = Parameter.getString(siteid, "orderdefaultrepeat");
  109. try {
  110. JSONArray array = JSONArray.parseArray(orderdefaultaccount);
  111. for (Object o : array) {
  112. JSONObject object = (JSONObject) o;
  113. if (object.getString("ordertype").equals(ordertype)) {
  114. return object.getBooleanValue("isrepeat");
  115. }
  116. }
  117. return false;
  118. } catch (Exception e) {
  119. return false;
  120. }
  121. }
  122. /**
  123. * 更新订单返利金信息
  124. *
  125. * @param controller
  126. * @param sa_orderid
  127. * @param rebate_used
  128. * @param rebate_userate
  129. * @param rebateamount
  130. * @return
  131. * @return
  132. * @throws YosException
  133. */
  134. public static String updateRateInfo(Controller controller, Long sa_orderid, BigDecimal rebate_used, BigDecimal rebate_userate, BigDecimal rebateamount) throws YosException {
  135. SQLFactory sqlFactory = new SQLFactory(new Order(), "更新订单返利金信息");
  136. sqlFactory.addParameter("siteid", controller.siteid);
  137. sqlFactory.addParameter("userid", controller.userid);
  138. sqlFactory.addParameter("username", controller.username);
  139. sqlFactory.addParameter("sa_orderid", sa_orderid);
  140. sqlFactory.addParameter("rebate_used", rebate_used);
  141. sqlFactory.addParameter("rebate_userate", rebate_userate);
  142. sqlFactory.addParameter("rebateamount", rebateamount);
  143. return sqlFactory.getSQL();
  144. }
  145. /**
  146. * 设置返利金
  147. *
  148. * @param sa_orderid
  149. * @throws YosException
  150. */
  151. public static void setRebateAmount(Controller controller, Long sa_orderid, BigDecimal rebateamount) throws YosException {
  152. Rows orderRows = getOrderRows(controller, sa_orderid);
  153. if (orderRows.isEmpty()) {
  154. return;
  155. }
  156. Row orderRow = orderRows.get(0);
  157. boolean rebate_used = orderRow.getBoolean("rebate_used");
  158. //不使用返利金
  159. if (!rebate_used) {
  160. return;
  161. }
  162. //获取订单折前价
  163. BigDecimal defaultAmount = getDefaultAmount(controller, sa_orderid);
  164. //获取订单的返利金使用额度
  165. if (rebateamount.compareTo(BigDecimal.ZERO) <= 0) {
  166. return;
  167. }
  168. BigDecimal rebate_userate;
  169. if (defaultAmount.compareTo(BigDecimal.ZERO) <= 0) {
  170. rebate_userate = BigDecimal.ZERO;
  171. } else {
  172. rebate_userate = rebateamount.divide(defaultAmount, 6, BigDecimal.ROUND_CEILING);
  173. }
  174. SQLList sqlList = new SQLList();
  175. //更新订单表体中的rebate_userate
  176. sqlList.add("UPDATE sa_order SET rebate_userate='" + rebate_userate + "' ,rebateamount='" + rebateamount + "' WHERE sa_orderid=" + sa_orderid + " and siteid='" + controller.siteid + "'");
  177. //更新订单商品数据
  178. 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");
  179. sqlList.add("UPDATE sa_orderitems SET rebateamount =0,amount = 0 WHERE sa_orderid = " + sa_orderid + " and siteid='" + controller.siteid + "' and qty=0");
  180. controller.dbConnect.runSqlUpdate(sqlList);
  181. }
  182. /**
  183. * 调整最后一行数据
  184. *
  185. * @param sa_orderid
  186. * @throws YosException
  187. */
  188. public static void adjustLastRebateAmount(Controller controller, Long sa_orderid) throws YosException {
  189. Rows orderRows = getOrderRows(controller, sa_orderid);
  190. if (orderRows.isEmpty()) {
  191. return;
  192. }
  193. BigDecimal rebateamount = orderRows.get(0).getBigDecimal("rebateamount");
  194. //调整返利金最后一行数据
  195. Rows orderItemRows = controller.dbConnect.runSqlQuery("SELECT sa_orderitemsid FROM sa_orderitems WHERE sa_orderid = " + sa_orderid + " and siteid='" + controller.siteid + "' ORDER BY rowno");
  196. if (orderItemRows.size() >= 1) {
  197. Long sa_orderitemsid = orderItemRows.get(orderItemRows.size() - 1).getLong("sa_orderitemsid");
  198. Rows rows = controller.dbConnect.runSqlQuery("SELECT SUM(rebateamount) rebateamount from sa_orderitems WHERE sa_orderid = " + sa_orderid);
  199. BigDecimal sum_rebateamount = rows.get(0).getBigDecimal("rebateamount");
  200. BigDecimal temp = sum_rebateamount.subtract(rebateamount);
  201. if (temp.compareTo(BigDecimal.ZERO) == 1) {
  202. 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");
  203. }
  204. if (temp.compareTo(BigDecimal.ZERO) == -1) {
  205. temp = temp.abs();
  206. 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");
  207. }
  208. controller.dbConnect.runSqlUpdate(" UPDATE sa_orderitems SET rebateamount = 0,amount = 0 WHERE sa_orderitemsid = " + sa_orderitemsid + " and siteid='" + controller.siteid + "' and qty=0");
  209. }
  210. }
  211. /**
  212. * 获取订单最大可使用用返利金金额
  213. *
  214. * @return
  215. */
  216. public static BigDecimal getMaxUsedRebateAmount(Controller controller, Long sa_orderid) throws YosException {
  217. Rows orderRows = getOrderRows(controller, sa_orderid);
  218. Long pay_enterpriseid = orderRows.get(0).getLong("pay_enterpriseid");
  219. //站点设置的最大返利金比例
  220. BigDecimal order_rebate_userate = BigDecimal.valueOf(Parameter.getDouble(controller.siteid, "order_rebate_userate"));
  221. //订单的折前价格
  222. BigDecimal defaultAmount = getDefaultAmount(controller, sa_orderid);
  223. //当前订单最大可使用返利金
  224. BigDecimal maxAmount = defaultAmount.multiply(order_rebate_userate);
  225. //当前可使用返利金额度
  226. BigDecimal rebateBalance = Accountbalance.getRebateBalance(controller, pay_enterpriseid);
  227. if (maxAmount.compareTo(rebateBalance) <= 0) {
  228. //System.err.println(maxAmount);
  229. return maxAmount;
  230. } else {
  231. // System.err.println(rebateBalance);
  232. return rebateBalance;
  233. }
  234. }
  235. /**
  236. * 获取订单表头的rows
  237. *
  238. * @param sa_orderid
  239. * @return
  240. * @throws YosException
  241. */
  242. public static Rows getOrderRows(Controller controller, Long sa_orderid) throws YosException {
  243. return controller.dbConnect.runSqlQuery("SELECT * from sa_order WHERE deleted=0 and sa_orderid = " + sa_orderid + " and siteid = '" + controller.siteid + "'");
  244. }
  245. /**
  246. * 获取订单折后价格
  247. *
  248. * @param sa_orderid
  249. * @return
  250. * @throws YosException
  251. */
  252. public static BigDecimal getAmount(Controller controller, Long sa_orderid) throws YosException {
  253. Rows rows = controller.dbConnect.runSqlQuery("SELECT ifnull(sum(amount),0) amount from sa_orderitems WHERE sa_orderid=" + sa_orderid + " AND siteid = '" + controller.siteid + "'");
  254. return rows.get(0).getBigDecimal("amount");
  255. }
  256. /**
  257. * 获取订单折前价格
  258. *
  259. * @param controller
  260. * @param sa_orderid
  261. * @return
  262. * @throws YosException
  263. */
  264. public static BigDecimal getDefaultAmount(Controller controller, Long sa_orderid) throws YosException {
  265. Rows rows = controller.dbConnect.runSqlQuery("SELECT ifnull(sum(defaultamount),0) defaultamount from sa_orderitems WHERE sa_orderid=" + sa_orderid + " AND siteid = '" + controller.siteid + "'");
  266. return rows.get(0).getBigDecimal("defaultamount");
  267. }
  268. public static SQLList createRebateBillPay(Controller controller, CashbillEntity cashbillEntity, Long sys_enterpriseid, Long sa_cashbillid_rebate) throws YosException {
  269. Long sa_cashbillid = controller.createTableID("sa_cashbill");
  270. String billcode = controller.createBillCode("cashbill1");
  271. Rows rows = controller.dbConnect.runSqlQuery("SELECT * from sa_cashbill WHERE sa_cashbillid = " + sa_cashbillid_rebate + " and siteid = '" + controller.siteid + "'");
  272. Long sa_accountclassid = 0L;
  273. if (rows.isNotEmpty()) {
  274. sa_accountclassid = rows.get(0).getLong("sa_accountclassid");
  275. }
  276. SQLList sqlList = new SQLList();
  277. SQLFactory sqlFactory = new SQLFactory(new Order(), "返利代金券支出-新增");
  278. sqlFactory.addParameter("billno", billcode);
  279. sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
  280. sqlFactory.addParameter("sa_accountclassid", sa_accountclassid);
  281. sqlFactory.addParameter("type", 0);
  282. sqlFactory.addParameter("siteid", controller.siteid);
  283. sqlFactory.addParameter("remarks", cashbillEntity.getRemarks());
  284. sqlFactory.addParameter("amount", cashbillEntity.getAmount());
  285. sqlFactory.addParameter("sa_cashbillid", sa_cashbillid);
  286. sqlFactory.addParameter("userid", controller.userid);
  287. sqlFactory.addParameter("username", controller.username);
  288. sqlFactory.addParameter("ownertable", cashbillEntity.getOwnertable());
  289. sqlFactory.addParameter("ownerid", cashbillEntity.getOwnerid());
  290. sqlFactory.addParameter("source", cashbillEntity.getSource());
  291. sqlFactory.addParameter("sourcenote", cashbillEntity.getSourcenote());
  292. sqlFactory.addParameter("sa_cashbillid_rebate", sa_cashbillid_rebate);
  293. //取金额的绝对值
  294. BigDecimal amount = cashbillEntity.getAmount().abs();
  295. BigDecimal balance = Accountbalance.getRebateBalance(controller, sys_enterpriseid).add(amount);
  296. sqlFactory.addParameter("balance", balance);
  297. sqlFactory.addParameter("status", "审核");
  298. sqlFactory.addParameter("checkby", controller.username);
  299. sqlFactory.addParameter_SQL("checkdate", "CURRENT_TIME");
  300. sqlList.add(sqlFactory.getSQL());
  301. sqlList.add("UPDATE sa_cashbill SET rebate_balance= rebate_balance +" + amount + ",balance= " + balance + " WHERE sa_cashbillid = " + sa_cashbillid_rebate + " and siteid = '" + controller.siteid + "'");
  302. sqlList.add("UPDATE sa_accountbalance SET balance=" + balance + " WHERE sys_enterpriseid = " + sys_enterpriseid + " and sa_accountclassid=" + sa_accountclassid + " and siteid = '" + controller.siteid + "'");
  303. return sqlList;
  304. }
  305. /**
  306. * 通过版本更新订单表头数据
  307. *
  308. * @param controller
  309. * @throws YosException
  310. */
  311. public static void updateOrderWithVersion(Controller controller) throws YosException {
  312. if (controller.content.containsKey("sa_order_v")) {
  313. int sa_order_v = controller.content.getIntValue("sa_order_v");
  314. Long sa_orderid = controller.content.getLong("sa_orderid");
  315. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, "sa_order");
  316. updateSQL.setDataVersion(sa_order_v);
  317. updateSQL.setValue("changeby", controller.username);
  318. updateSQL.setWhere("sa_orderid", sa_orderid);
  319. updateSQL.setWhere("siteid", controller.siteid);
  320. updateSQL.update();
  321. }
  322. }
  323. }