RechargeService.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace app\admin\service\notify;
  3. use app\model\Goods;
  4. use app\model\MemberAccount;
  5. use app\model\MemberQuota;
  6. use app\model\MemberRole;
  7. use app\model\Order;
  8. use app\model\OrderSheet;
  9. use app\model\PayDetail;
  10. use app\model\RuleAdded;
  11. use app\model\RuleAddedComponent;
  12. use Monolog\Handler\IFTTTHandler;
  13. use support\exception\BusinessException;
  14. class RechargeService
  15. {
  16. /**
  17. * @Desc
  18. * @Author Gorden
  19. * @Date 2024/5/28 16:50
  20. *
  21. * @param $orderId
  22. * @return mixed
  23. * @throws BusinessException
  24. */
  25. public static function disposeOrder($orderId)
  26. {
  27. $orderSheet = OrderSheet::where('join_sheet_order_id', $orderId)->first();
  28. if (!$orderSheet) {
  29. throw new BusinessException('订单数据异常');
  30. }
  31. $goods = Goods::find($orderSheet->join_sheet_goods_id);
  32. // 自定义充值产品
  33. if($goods->join_goods_category_id == 210){
  34. return 0;
  35. }
  36. if (empty($goods->goods_attribute_json)) {
  37. throw new BusinessException('产品数据异常');
  38. }
  39. $attributeJson = json_decode($goods->goods_attribute_json, true);
  40. if (!isset($attributeJson['added'])) {
  41. throw new BusinessException('产品数据异常');
  42. }
  43. return $attributeJson['added']['nbr'];
  44. }
  45. /**
  46. * @Desc
  47. * @Author Gorden
  48. * @Date 2024/5/29 16:25
  49. *
  50. * @param $memberId
  51. * @param $payAmount
  52. * @return false|mixed
  53. * @throws BusinessException
  54. */
  55. public static function disposeRole($memberId, $payAmount)
  56. {
  57. $memberAccount = MemberAccount::where('member_account_classify', 'CASH')
  58. ->where('join_account_member_id', $memberId)
  59. ->first();
  60. if (!$memberAccount) {
  61. throw new BusinessException('账户异常');
  62. }
  63. // $income = floatval($memberAccount->member_account_income);
  64. // $expend = floatval($memberAccount->member_account_expend);
  65. // $money = max($income, $expend);
  66. $recharge = PayDetail::where('join_pay_member_id',$memberId)
  67. ->where('pay_category','RECHARGE')
  68. ->where('pay_status','SUCCESS')
  69. ->where(function($query){
  70. $query->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY','OFFLINE_WXPAY','OFFLINE_ALIPAY','MONEY'])
  71. ->orWhere('pay_prepayid', 'like', '%CASH%');
  72. })
  73. ->sum('pay_amount');
  74. // $expend = PayDetail::where('join_pay_member_id',$memberId)
  75. // ->whereIn('pay_category',['GOODS','SERVICE','CHNMED','CHNNCD','PACKAGE','MEALS'])
  76. // ->where('pay_status','SUCCESS')
  77. // ->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY','OFFLINE_WXPAY','OFFLINE_ALIPAY','MONEY'])
  78. // ->sum('pay_amount');
  79. $paySuccessDetail = PayDetail::where('join_pay_member_id',$memberId)
  80. ->where('pay_status','SUCCESS')
  81. ->whereIn('pay_category',['GOODS','SERVICE','CHNMED','CHNNCD','PACKAGE','MEALS'])
  82. ->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY','OFFLINE_WXPAY','OFFLINE_ALIPAY','MONEY']);
  83. $payAmount = $paySuccessDetail->sum('pay_amount');
  84. $paySuccessDetail = $paySuccessDetail->select('pay_id','join_pay_order_id','pay_amount')
  85. ->get()
  86. ->toArray();
  87. $orderIds = array_column($paySuccessDetail,'join_pay_order_id');
  88. $refundAmount = PayDetail::whereIn('join_pay_order_id',$orderIds)
  89. ->where('pay_category','REFUND')
  90. ->where('pay_status','SUCCESS')
  91. ->sum('pay_amount');
  92. $amount = $payAmount - $refundAmount;
  93. $money = max($recharge, $amount);
  94. $memberRole = MemberRole::where('member_role_range_begin' ,'<=',$money)
  95. ->where('member_role_range_end','>',$money)
  96. ->first();
  97. if (!empty($memberRole->member_role_id)){
  98. return $memberRole->member_role_id;
  99. }
  100. return false;
  101. }
  102. /**
  103. * @Desc
  104. * @Author Gorden
  105. * @Date 2024/5/28 18:03
  106. *
  107. * @param $roleId
  108. * @param $memberId
  109. * @return void
  110. * @throws BusinessException
  111. */
  112. public static function disposeAdded($roleId, $memberId)
  113. {
  114. $role = MemberRole::find($roleId);
  115. if (empty($role->member_role_rule_json)) {
  116. throw new BusinessException('增值套包数据异常');
  117. }
  118. $ruleJson = json_decode($role->member_role_rule_json, true);
  119. if (!isset($ruleJson['added'])) {
  120. throw new BusinessException('增值套包数据异常');
  121. }
  122. $addedId = $ruleJson['added'];
  123. $added = RuleAdded::find($addedId);
  124. if (!$added) {
  125. throw new BusinessException('增值套包数据异常');
  126. }
  127. $diff = $added->rule_added_goods_diff;
  128. $components = RuleAddedComponent::where('join_component_rule_added_id', $addedId)->get()->toArray();
  129. switch ($diff) {
  130. case "SUM":
  131. self::diffSum($memberId, $components);
  132. break;
  133. case "REPLACE":
  134. self::diffReplace($memberId, $components);
  135. break;
  136. case "NONE":
  137. self::diffNone($memberId, $components);
  138. }
  139. }
  140. /**
  141. * @Desc
  142. * @Author Gorden
  143. * @Date 2024/5/28 18:03
  144. *
  145. * @param $memberId
  146. * @param $components
  147. * @return void
  148. */
  149. public static function diffSum($memberId, $components)
  150. {
  151. foreach ($components as $component) {
  152. self::saveMemberQuota($memberId, $component);
  153. }
  154. }
  155. /**
  156. * @Desc
  157. * @Author Gorden
  158. * @Date 2024/5/28 18:02
  159. *
  160. * @param $memberId
  161. * @param $components
  162. * @return void
  163. */
  164. public static function diffReplace($memberId, $components)
  165. {
  166. foreach ($components as $component) {
  167. if (MemberQuota::where('join_quota_member_id', $memberId)->where('join_member_rule_added_component_id', $component['rule_added_component_id'])->exists()) {
  168. MemberQuota::where('join_quota_member_id', $memberId)
  169. ->where('join_member_rule_added_component_id', $component['rule_added_component_id'])
  170. ->where('member_quota_status', 'PENDING')
  171. ->delete();
  172. }
  173. self::saveMemberQuota($memberId, $component);
  174. }
  175. }
  176. /**
  177. * @Desc
  178. * @Author Gorden
  179. * @Date 2024/5/28 18:02
  180. *
  181. * @param $memberId
  182. * @param $components
  183. * @return void
  184. */
  185. public static function diffNone($memberId, $components)
  186. {
  187. foreach ($components as $component) {
  188. if (MemberQuota::where('join_quota_member_id', $memberId)->where('join_member_rule_added_component_id', $component['rule_added_component_id'])->exists()) {
  189. continue;
  190. }
  191. self::saveMemberQuota($memberId, $component);
  192. }
  193. }
  194. /**
  195. * @Desc
  196. * @Author Gorden
  197. * @Date 2024/5/28 18:02
  198. *
  199. * @param $memberId
  200. * @param $component
  201. * @return void
  202. */
  203. private static function saveMemberQuota($memberId, $component)
  204. {
  205. if ($component['rule_added_component_classify'] == 'MONTH') {
  206. $total = 12 * intval($component['rule_added_component_goods_nbr']);
  207. for ($i = 0; $i < $total; $i++) {
  208. self::doSaveMemberQuota($memberId, $component, 1);
  209. }
  210. } elseif ($component['rule_added_component_classify'] == 'YEAR') {
  211. if ($component['rule_added_component_goods_nbr'] == "-99.00") {
  212. self::doSaveMemberQuota($memberId, $component, -99);
  213. } else {
  214. for ($i = 0; $i < intval($component['rule_added_component_goods_nbr']); $i++) {
  215. self::doSaveMemberQuota($memberId, $component, 1);
  216. }
  217. }
  218. } else {
  219. if ($component['rule_added_component_goods_nbr'] == "-99.00") {
  220. self::doSaveMemberQuota($memberId, $component, -99);
  221. } else {
  222. for ($i = 0; $i < intval($component['rule_added_component_goods_nbr']); $i++) {
  223. self::doSaveMemberQuota($memberId, $component, 1);
  224. }
  225. }
  226. }
  227. }
  228. private static function doSaveMemberQuota($memberId, $component, $nbr)
  229. {
  230. $model = new MemberQuota();
  231. $model->member_quota_id = 'MQ' . date('ymdH') . random_string(6, 'up');
  232. $model->join_quota_member_id = $memberId;
  233. $model->join_member_rule_added_component_id = $component['rule_added_component_id'];
  234. $model->member_quota_status = 'PENDING';
  235. $model->member_quota_category = $component['rule_added_component_category'];
  236. $model->member_quota_name = $component['rule_added_component_name'];
  237. $model->member_quota_nbr = $nbr;
  238. $model->member_quota_json = $component['rule_added_component_goods_json'];
  239. $model->member_quota_addtimes = time();
  240. $model->save();
  241. }
  242. }