新版订单消耗系统

SalerStatementsDetail.php 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2021/1/26
  6. * Time: 15:13
  7. */
  8. namespace App\Models;
  9. use Illuminate\Database\Eloquent\Model;
  10. class SalerStatementsDetail extends Model
  11. {
  12. public $timestamps = false;
  13. protected $table = 'saler_statements_detail';
  14. protected static $unguarded = true;
  15. public static function chargeSalerAmount($orderData)
  16. {
  17. $orderIds = array_column($orderData, 'order_id');
  18. $orderList = Order::select('final_amount','id')->whereIn('id', $orderIds)
  19. ->where('enable', 1)->get()->keyBy('id')->toArray();
  20. $statementsDetailsList = SalerStatementsDetail::selectRaw('order_id, sum(total_amount) as total_amount')
  21. ->whereIn('order_id', $orderIds)->groupBy('order_id')
  22. ->get()->keyBy('order_id')->toArray();
  23. foreach ($orderData as $order) {
  24. $orderInfo = isset($orderList[$order['order_id']]) ? $orderList[$order['order_id']] : null;
  25. $statementsDetailInfo = isset($statementsDetailsList[$order['order_id']]) ? $statementsDetailsList[$order['order_id']] : 0;
  26. if (empty($orderInfo) || ($orderInfo['final_amount'] < ($order['total_amount'] + $statementsDetailInfo['total_amount']))) {
  27. return false;
  28. }
  29. }
  30. return true;
  31. }
  32. public static function chargeSalerAmountSecond($orderData, $statemtentsId)
  33. {
  34. $orderIds = array_column($orderData, 'order_id');
  35. $orderList = Order::select('final_amount','id')->whereIn('id', $orderIds)
  36. ->where('enable', 1)->get()->keyBy('id')->toArray();
  37. $statementsDetailsList = SalerStatementsDetail::selectRaw('order_id, sum(total_amount) as total_amount')
  38. ->whereIn('order_id', $orderIds)->where('statements_id', '!=', $statemtentsId)
  39. ->groupBy('order_id')->get()->keyBy('order_id')->toArray();
  40. foreach ($orderData as $order) {
  41. $orderInfo = isset($orderList[$order['order_id']]) ? $orderList[$order['order_id']] : null;
  42. $statementsDetailInfo = isset($statementsDetailsList[$order['order_id']]) ? $statementsDetailsList[$order['order_id']] : 0;
  43. if (empty($orderInfo) || ($orderInfo['final_amount'] < ($order['total_amount'] + $statementsDetailInfo['total_amount']))) {
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49. }