RechargeService.php 7.3 KB

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