新版订单消耗系统

Statements.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2021/1/26
  6. * Time: 15:14
  7. */
  8. namespace App\Models;
  9. use Illuminate\Database\Eloquent\Model;
  10. class Statements extends Model
  11. {
  12. protected $table = 'statements';
  13. public $timestamps = false;
  14. protected static $unguarded = true;
  15. public static function chargeSalerAmount($orderData, $isDiscount, $type)
  16. {
  17. //有问题,判断逻辑需排除本次提交
  18. return false;
  19. $orderIds = array_column($orderData, 'order_id');
  20. $orderList = Order::select('final_amount','id')->whereIn('id', $orderIds)
  21. ->where('enable', 1)->get()->keyBy('id')->toArray();
  22. $ECommerceColmus = 'actual_consumption';
  23. if(1 == $isDiscount) {
  24. $ECommerceColmus = 'discount_amount';
  25. }
  26. switch($type){
  27. case 3:
  28. $colums = $ECommerceColmus;
  29. break;
  30. default:
  31. $colums = 'total_amount';
  32. break;
  33. }
  34. $salerStatementsDetailsList = SalerStatementsDetail::selectRaw('order_id, sum(total_amount) as total_amount')
  35. ->whereIn('order_id', $orderIds)->where('enable', 1)->groupBy('order_id')
  36. ->get()->keyBy('order_id')->toArray();
  37. $ECommerceStatementsDetailsList = ECommerceStatementsDetail::selectRaw('order_id, sum('.$ECommerceColmus.') as total_amount')
  38. ->whereIn('order_id', $orderIds)->where('enable', 1)->groupBy('order_id')
  39. ->get()->keyBy('order_id')->toArray();
  40. $GameStatementsDetailsList = GameStatementsDetail::selectRaw('order_id, sum(total_amount) as total_amount')
  41. ->whereIn('order_id', $orderIds)->where('enable', 1)->groupBy('order_id')
  42. ->get()->keyBy('order_id')->toArray();
  43. $CPAStatementsDetailsList = CPAStatementsDetail::selectRaw('order_id, sum(total_amount) as total_amount')
  44. ->whereIn('order_id', $orderIds)->where('enable', 1)->groupBy('order_id')
  45. ->get()->keyBy('order_id')->toArray();
  46. foreach ($orderData as $order) {
  47. $orderInfo = isset($orderList[$order['order_id']]) ? $orderList[$order['order_id']] : null;
  48. $salerStatementsDetailInfo = isset($salerStatementsDetailsList[$order['order_id']]) ? $salerStatementsDetailsList[$order['order_id']] : 0;
  49. $ECommerceStatementsDetailInfo = isset($ECommerceStatementsDetailsList[$order['order_id']]) ? $ECommerceStatementsDetailsList[$order['order_id']] : 0;
  50. $CPAStatementsDetailInfo = isset($CPAStatementsDetailsList[$order['order_id']]) ? $CPAStatementsDetailsList[$order['order_id']] : 0;
  51. $GameStatementsDetailInfo = isset($GameStatementsDetailsList[$order['order_id']]) ? $GameStatementsDetailsList[$order['order_id']] : 0;
  52. if (empty($orderInfo) || ($orderInfo['final_amount'] < (
  53. $order[$colums] +
  54. $salerStatementsDetailInfo['total_amount'] +
  55. $ECommerceStatementsDetailInfo['total_amount'] +
  56. $CPAStatementsDetailInfo['total_amount'] +
  57. $GameStatementsDetailInfo['total_amount']
  58. ))) {
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. /*
  65. * 状态机
  66. */
  67. public static function getStatementsEnableStatus($statementsData, $user)
  68. {
  69. if ($user->role_id == 15) {
  70. return [2,3];
  71. } else if (in_array($user->role_id,[10, 17, 21])) {
  72. return [4,5];
  73. } else {
  74. return [];
  75. }
  76. }
  77. public static function chargeSalerAmountSecond($orderData, $isDiscount, $type)
  78. {
  79. $orderIds = array_column($orderData, 'order_id');
  80. $orderList = Order::query()
  81. ->select('final_amount','id')
  82. ->whereIn('id', $orderIds)
  83. ->where('enable', 1)
  84. ->get()
  85. ->keyBy('id')
  86. ->toArray();
  87. foreach ($orderData as $order) {
  88. $orderInfo = isset($orderList[$order['order_id']]) ? $orderList[$order['order_id']] : null;
  89. $orderInfo['final_amount'] = $orderInfo['final_amount'] === null ? 0 : $orderInfo['final_amount'];
  90. if (empty($orderInfo) || ($orderInfo['final_amount'] != $order['total_amount'] )) {
  91. return false;
  92. }
  93. }
  94. return true;
  95. }
  96. public static function chargeSalerAmountThird($orderData, $statementsId)
  97. {
  98. $orderIds = array_column($orderData, 'order_id');
  99. $orderList = Order::query()
  100. ->select('final_amount','id')
  101. ->whereIn('id', $orderIds)
  102. ->where('enable', 1)
  103. ->get();
  104. if (empty($orderList)) {
  105. $orderList = [];
  106. } else {
  107. $orderList = $orderList->toArray();
  108. $orderList = array_column($orderList, null, 'id');
  109. }
  110. if (0 != $statementsId) {
  111. $pddStatements = PddStatementsDetail::query()
  112. ->selectRaw('order_id, sum(total_amount) as total_amount')
  113. ->whereIn('order_id', $orderIds)
  114. ->where('statements_id', '!=', $statementsId)
  115. ->where('enable', 1)
  116. ->groupBy('order_id')
  117. ->get();
  118. if (empty($pddStatements)) {
  119. $pddStatements = [];
  120. } else {
  121. $pddStatements = $pddStatements->toArray();
  122. $pddStatements = array_column($pddStatements, null, 'order_id');
  123. }
  124. } else {
  125. $pddStatements = [];
  126. }
  127. foreach ($orderData as $order) {
  128. $orderInfo = isset($orderList[$order['order_id']]) ? $orderList[$order['order_id']] : null;
  129. $pddStatementsInfo = isset($pddStatements[$order['order_id']]) ? $pddStatements[$order['order_id']] : null;
  130. $orderInfo['final_amount'] = $orderInfo['final_amount'] === null ? 0 : $orderInfo['final_amount'];
  131. if (empty($orderInfo) || ($orderInfo['final_amount'] < ($order['total_amount'] + $pddStatementsInfo['total_amount']))) {
  132. return false;
  133. }
  134. }
  135. return true;
  136. }
  137. public static function getStatementsInfo($statementsId, $columns) {
  138. $statementsModel = self::query()->where('id', $statementsId);
  139. if(!empty($columns)) {
  140. $statementsModel->select($columns);
  141. }
  142. return $statementsModel->first();
  143. }
  144. public static function getListBySearch($search) {
  145. $statementsModel = self::query()->where('enable');
  146. if(isset($serach['statements_list']) && !empty($search['statements_list'])) $statementsModel->whereIn('id', $search['statements_list']);
  147. return $statementsModel->get();
  148. }
  149. }