RechargeService.php 7.2 KB

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