RechargeService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. namespace app\admin\service\notify;
  3. use app\admin\service\coupon\CouponDetailService;
  4. use app\model\Coupon;
  5. use app\model\CouponDetail;
  6. use app\model\Goods;
  7. use app\model\Member;
  8. use app\model\MemberAccount;
  9. use app\model\MemberQuota;
  10. use app\model\MemberRole;
  11. use app\model\Order;
  12. use app\model\OrderSheet;
  13. use app\model\PayDetail;
  14. use app\model\RuleAdded;
  15. use app\model\RuleAddedComponent;
  16. use Monolog\Handler\IFTTTHandler;
  17. use support\exception\BusinessException;
  18. use support\Log;
  19. class RechargeService
  20. {
  21. /**
  22. * @Desc
  23. * @Author Gorden
  24. * @Date 2024/5/28 16:50
  25. *
  26. * @param $orderId
  27. * @return mixed
  28. * @throws BusinessException
  29. */
  30. public static function disposeOrder($orderId)
  31. {
  32. $orderSheet = OrderSheet::where('join_sheet_order_id', $orderId)->first();
  33. if (!$orderSheet) {
  34. throw new BusinessException('订单数据异常');
  35. }
  36. $goods = Goods::find($orderSheet->join_sheet_goods_id);
  37. // 自定义充值产品
  38. if ($goods->join_goods_category_id == 210) {
  39. return 0;
  40. }
  41. if (empty($goods->goods_attribute_json)) {
  42. throw new BusinessException('产品数据异常');
  43. }
  44. $attributeJson = json_decode($goods->goods_attribute_json, true);
  45. if (!isset($attributeJson['added'])) {
  46. throw new BusinessException('产品数据异常');
  47. }
  48. return $attributeJson['added']['nbr'];
  49. }
  50. public static function getGoodsAttributeJson($orderId)
  51. {
  52. try {
  53. $orderSheet = OrderSheet::where('join_sheet_order_id', $orderId)->first();
  54. if (!$orderSheet) {
  55. throw new BusinessException('订单数据异常');
  56. }
  57. $goods = Goods::find($orderSheet->join_sheet_goods_id);
  58. if (empty($goods->goods_attribute_json)) {
  59. return [];
  60. // throw new BusinessException('产品数据异常');
  61. }
  62. return json_decode($goods->goods_attribute_json, true);
  63. } catch (BusinessException $e) {
  64. throw new BusinessException($e->getMessage());
  65. } catch (\Exception $e) {
  66. throw new BusinessException('产品数据异常');
  67. }
  68. }
  69. /**
  70. * @Desc
  71. * @Author Gorden
  72. * @Date 2024/5/29 16:25
  73. *
  74. * @param $memberId
  75. * @param $payAmount
  76. * @throws BusinessException
  77. */
  78. public static function disposeRole($memberId, $payAmount)
  79. {
  80. try {
  81. $member = Member::with([
  82. 'role' => function ($query) {
  83. $query->select('member_role_id', 'member_role_sort');
  84. }
  85. ])->where('member_id', $memberId)
  86. ->first();
  87. $memberRoleConfigJson = [];
  88. if (!empty($member->member_role_config_json)) {
  89. $memberRoleConfigJson = json_decode($member->member_role_config_json, true);
  90. }
  91. $role = MemberRole::where('member_role_category', 'COMBINE')
  92. ->where('member_role_range_begin', '<=', $payAmount)
  93. ->where('member_role_range_end', '>=', $payAmount)
  94. ->first();
  95. $memberRoleId = '';
  96. $memberRoleBegintime = '';
  97. // 没有角色,或者有角色,新角色等级高
  98. if ((!empty($member->role) && !empty($role) && $role->member_role_sort > $member->role->member_role_sort) || (empty($member->role) && !empty($role))) {
  99. $memberRoleId = $role->member_role_id;
  100. $memberRoleBegintime = date('Y-m-d H:i:s');
  101. $memberRoleConfigJson['next'] = [];
  102. } elseif (!empty($member->role) && !empty($role) && $role->member_role_sort == $member->role->member_role_sort) {
  103. $memberRoleId = $role->member_role_id;
  104. $memberRoleBegintime = date('Y-m-d H:i:s');
  105. $memberRoleConfigJson['next'] = [];
  106. } elseif (!empty($member->role) && !empty($role) && $role->member_role_sort < $member->role->member_role_sort) {
  107. $memberRoleId = $member->role->member_role_id;
  108. $memberRoleBegintime = $member->member_role_begintime;
  109. $memberRoleConfigJson['next'][] = ['member_role_begintime' => date('Y-m-d H:i:s'), 'member_role_id' => $role->member_role_id];
  110. }
  111. $memberRoleConfigJson['deadline_datetime'] = date('Y-m-d 23:59:59',strtotime($memberRoleBegintime.' +364 days'));
  112. if(!empty($memberRoleId)){
  113. Member::where('member_id', $memberId)->update([
  114. 'join_member_role_id' => $memberRoleId,
  115. 'member_role_begintime' => $memberRoleBegintime,
  116. 'member_role_config_json' => json_encode($memberRoleConfigJson)
  117. ]);
  118. }
  119. } catch (\Exception $e) {
  120. Log::error('计算等级异常:' . $e->getMessage());
  121. throw new BusinessException("计算等级异常");
  122. }
  123. }
  124. public static function disposeRoleCoupon($goodsAttributeJson,$couponParams, $memberId)
  125. {
  126. try {
  127. if (empty($goodsAttributeJson['coupon'])) {
  128. return;
  129. }
  130. // 发券
  131. foreach ($goodsAttributeJson['coupon'] as $key => $coupon) {
  132. $couponModel = Coupon::where('coupon_id', $key)->select('coupon_id', 'coupon_validdate_day', 'coupon_validdate_end', 'coupon_is_period', 'coupon_number')->first();
  133. if (empty($couponModel)) {
  134. continue;
  135. }
  136. // 券是否过期
  137. if (!empty($couponModel->coupon_validdate_end) && strtotime($couponModel->coupon_validdate_end) < time()) {
  138. continue;
  139. }
  140. // 发周期券
  141. if ($couponModel->coupon_is_period == 'Y') {
  142. // 发券参数
  143. $couponSendParams = ['gettype' => 'ROLE', 'coupon_id' => $key, 'member_id' => $memberId,'order_id'=>$couponParams['order_id'] ?? ''];
  144. Log::info("发周期券参数", $couponSendParams);
  145. CouponDetailService::sendPeriodCoupon($couponSendParams);
  146. continue;
  147. }
  148. $couponResidue = 'all';
  149. if ($couponModel->coupon_number != 0) {
  150. $couponResidue = CouponDetail::where('join_detail_coupon_id', $key)->whereIn('coupon_detail_status', ['INIT', 'PENDING'])->count();
  151. // 超出优惠券设置的数量
  152. if ($couponResidue < $coupon['num']) {
  153. continue;
  154. }
  155. }
  156. $endDate = '';
  157. if (!empty($couponModel->coupon_validdate_end)) {
  158. $endDate = $couponModel->coupon_validdate_end;
  159. } elseif ($couponModel->coupon_validdate_day > 0) {
  160. $endDate = date('Y-m-d H:i:s', time() + ($couponModel->coupon_validdate_day * 24 * 3600) - 1);
  161. }
  162. // 发券参数
  163. $couponSendParams = [
  164. 'gettype' => 'ROLE',
  165. 'coupon_id' => $key,
  166. 'chooseCouponNbr' => $coupon['num'],
  167. 'member_id' => $memberId,
  168. 'coupon_detail_gain_datetime' => date('Y-m-d H:i:s'),
  169. 'coupon_detail_deadline_datetime' => $endDate,
  170. 'order_id'=>$couponParams['order_id'] ?? ''
  171. ];
  172. Log::info("发普通券参数", $couponSendParams);
  173. if ($couponResidue != 'all' && $couponResidue - $coupon['num'] >= 0) {
  174. CouponDetailService::customSendCouponHave($couponSendParams);
  175. } else {
  176. for ($i = 0; $i < $coupon['num']; $i++) {
  177. CouponDetailService::customSendCoupon($couponSendParams);
  178. }
  179. }
  180. }
  181. } catch (\Exception $e) {
  182. throw new BusinessException("发券异常");
  183. }
  184. }
  185. /**
  186. * @Desc
  187. * @Author Gorden
  188. * @Date 2024/5/28 18:03
  189. *
  190. * @param $roleId
  191. * @param $memberId
  192. * @return void
  193. * @throws BusinessException
  194. */
  195. public static function disposeAdded($roleId, $memberId)
  196. {
  197. $role = MemberRole::find($roleId);
  198. if (empty($role->member_role_rule_json)) {
  199. throw new BusinessException('增值套包数据异常');
  200. }
  201. $ruleJson = json_decode($role->member_role_rule_json, true);
  202. if (!isset($ruleJson['added'])) {
  203. throw new BusinessException('增值套包数据异常');
  204. }
  205. $addedId = $ruleJson['added'];
  206. $added = RuleAdded::find($addedId);
  207. if (!$added) {
  208. throw new BusinessException('增值套包数据异常');
  209. }
  210. $diff = $added->rule_added_goods_diff;
  211. $components = RuleAddedComponent::where('join_component_rule_added_id', $addedId)->get()->toArray();
  212. switch ($diff) {
  213. case "SUM":
  214. self::diffSum($memberId, $components);
  215. break;
  216. case "REPLACE":
  217. self::diffReplace($memberId, $components);
  218. break;
  219. case "NONE":
  220. self::diffNone($memberId, $components);
  221. }
  222. }
  223. /**
  224. * @Desc
  225. * @Author Gorden
  226. * @Date 2024/5/28 18:03
  227. *
  228. * @param $memberId
  229. * @param $components
  230. * @return void
  231. */
  232. public static function diffSum($memberId, $components)
  233. {
  234. foreach ($components as $component) {
  235. self::saveMemberQuota($memberId, $component);
  236. }
  237. }
  238. /**
  239. * @Desc
  240. * @Author Gorden
  241. * @Date 2024/5/28 18:02
  242. *
  243. * @param $memberId
  244. * @param $components
  245. * @return void
  246. */
  247. public static function diffReplace($memberId, $components)
  248. {
  249. foreach ($components as $component) {
  250. if (MemberQuota::where('join_quota_member_id', $memberId)->where('join_member_rule_added_component_id', $component['rule_added_component_id'])->exists()) {
  251. MemberQuota::where('join_quota_member_id', $memberId)
  252. ->where('join_member_rule_added_component_id', $component['rule_added_component_id'])
  253. ->where('member_quota_status', 'PENDING')
  254. ->delete();
  255. }
  256. self::saveMemberQuota($memberId, $component);
  257. }
  258. }
  259. /**
  260. * @Desc
  261. * @Author Gorden
  262. * @Date 2024/5/28 18:02
  263. *
  264. * @param $memberId
  265. * @param $components
  266. * @return void
  267. */
  268. public static function diffNone($memberId, $components)
  269. {
  270. foreach ($components as $component) {
  271. if (MemberQuota::where('join_quota_member_id', $memberId)->where('join_member_rule_added_component_id', $component['rule_added_component_id'])->exists()) {
  272. continue;
  273. }
  274. self::saveMemberQuota($memberId, $component);
  275. }
  276. }
  277. /**
  278. * @Desc
  279. * @Author Gorden
  280. * @Date 2024/5/28 18:02
  281. *
  282. * @param $memberId
  283. * @param $component
  284. * @return void
  285. */
  286. private static function saveMemberQuota($memberId, $component)
  287. {
  288. if ($component['rule_added_component_classify'] == 'MONTH') {
  289. $total = 12 * intval($component['rule_added_component_goods_nbr']);
  290. for ($i = 0; $i < $total; $i++) {
  291. self::doSaveMemberQuota($memberId, $component, 1);
  292. }
  293. } elseif ($component['rule_added_component_classify'] == 'YEAR') {
  294. if ($component['rule_added_component_goods_nbr'] == "-99.00") {
  295. self::doSaveMemberQuota($memberId, $component, -99);
  296. } else {
  297. for ($i = 0; $i < intval($component['rule_added_component_goods_nbr']); $i++) {
  298. self::doSaveMemberQuota($memberId, $component, 1);
  299. }
  300. }
  301. } else {
  302. if ($component['rule_added_component_goods_nbr'] == "-99.00") {
  303. self::doSaveMemberQuota($memberId, $component, -99);
  304. } else {
  305. for ($i = 0; $i < intval($component['rule_added_component_goods_nbr']); $i++) {
  306. self::doSaveMemberQuota($memberId, $component, 1);
  307. }
  308. }
  309. }
  310. }
  311. private static function doSaveMemberQuota($memberId, $component, $nbr)
  312. {
  313. $model = new MemberQuota();
  314. $model->member_quota_id = 'MQ' . date('ymdHi') . random_string(4, 'up');
  315. $model->join_quota_member_id = $memberId;
  316. $model->join_member_rule_added_component_id = $component['rule_added_component_id'];
  317. $model->member_quota_status = 'PENDING';
  318. $model->member_quota_category = $component['rule_added_component_category'];
  319. $model->member_quota_name = $component['rule_added_component_name'];
  320. $model->member_quota_nbr = $nbr;
  321. $model->member_quota_json = $component['rule_added_component_goods_json'];
  322. $model->member_quota_addtimes = time();
  323. $model->save();
  324. }
  325. /**
  326. * @Desc 充值前保存原账户数据
  327. * @Author Gorden
  328. * @Date 2024/10/14 9:18
  329. *
  330. * @param MemberAccount $memberAccount
  331. * @param $payAmount
  332. * @param $addedRate
  333. * @param $addedAmount
  334. * @return void
  335. * @throws BusinessException
  336. */
  337. public static function saveOriginData(MemberAccount $memberAccount, $payAmount, $addedRate, $addedAmount)
  338. {
  339. try {
  340. $memberAccount = $memberAccount->toArray();
  341. $memberAccount['pay_amount'] = $payAmount;
  342. $memberAccount['added_rate'] = $addedRate;
  343. $memberAccount['added_amount'] = $addedAmount;
  344. $member = Member::where('member_id', $memberAccount['join_account_member_id'])->first();
  345. $memberExtendJson = [];
  346. if (!empty($member->member_extend_json)) {
  347. $memberExtendJson = json_decode($member->member_extend_json, true);
  348. }
  349. $memberExtendJson['snap'][date('Y-m-d H:i:s')] = $memberAccount;
  350. $member->member_extend_json = json_encode($memberExtendJson);
  351. $member->save();
  352. } catch (\Exception $e) {
  353. throw new BusinessException('存储原数据失败');
  354. }
  355. }
  356. }