123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- namespace app\admin\service\notify;
- use app\model\Goods;
- use app\model\MemberAccount;
- use app\model\MemberQuota;
- use app\model\MemberRole;
- use app\model\Order;
- use app\model\OrderSheet;
- use app\model\PayDetail;
- use app\model\RuleAdded;
- use app\model\RuleAddedComponent;
- use Monolog\Handler\IFTTTHandler;
- use support\exception\BusinessException;
- class RechargeService
- {
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/5/28 16:50
- *
- * @param $orderId
- * @return mixed
- * @throws BusinessException
- */
- public static function disposeOrder($orderId)
- {
- $orderSheet = OrderSheet::where('join_sheet_order_id', $orderId)->first();
- if (!$orderSheet) {
- throw new BusinessException('订单数据异常');
- }
- $goods = Goods::find($orderSheet->join_sheet_goods_id);
- // 自定义充值产品
- if($goods->join_goods_category_id == 210){
- return 0;
- }
- if (empty($goods->goods_attribute_json)) {
- throw new BusinessException('产品数据异常');
- }
- $attributeJson = json_decode($goods->goods_attribute_json, true);
- if (!isset($attributeJson['added'])) {
- throw new BusinessException('产品数据异常');
- }
- return $attributeJson['added']['nbr'];
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/5/29 16:25
- *
- * @param $memberId
- * @param $payAmount
- * @return false|mixed
- * @throws BusinessException
- */
- public static function disposeRole($memberId, $payAmount)
- {
- $memberAccount = MemberAccount::where('member_account_classify', 'CASH')
- ->where('join_account_member_id', $memberId)
- ->first();
- if (!$memberAccount) {
- throw new BusinessException('账户异常');
- }
- // $income = floatval($memberAccount->member_account_income);
- // $expend = floatval($memberAccount->member_account_expend);
- // $money = max($income, $expend);
- $recharge = PayDetail::where('join_pay_member_id',$memberId)
- ->where('pay_category','RECHARGE')
- ->where('pay_status','SUCCESS')
- ->where(function($query){
- $query->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY','OFFLINE_WXPAY','OFFLINE_ALIPAY','MONEY'])
- ->orWhere('pay_prepayid', 'like', '%CASH%');
- })
- ->sum('pay_amount');
- // $expend = PayDetail::where('join_pay_member_id',$memberId)
- // ->whereIn('pay_category',['GOODS','SERVICE','CHNMED','CHNNCD','PACKAGE','MEALS'])
- // ->where('pay_status','SUCCESS')
- // ->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY','OFFLINE_WXPAY','OFFLINE_ALIPAY','MONEY'])
- // ->sum('pay_amount');
- $paySuccessDetail = PayDetail::where('join_pay_member_id',$memberId)
- ->where('pay_status','SUCCESS')
- ->whereIn('pay_category',['GOODS','SERVICE','CHNMED','CHNNCD','PACKAGE','MEALS'])
- ->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY','OFFLINE_WXPAY','OFFLINE_ALIPAY','MONEY']);
- $payAmount = $paySuccessDetail->sum('pay_amount');
- $paySuccessDetail = $paySuccessDetail->select('pay_id','join_pay_order_id','pay_amount')
- ->get()
- ->toArray();
- $orderIds = array_column($paySuccessDetail,'join_pay_order_id');
- $refundAmount = PayDetail::whereIn('join_pay_order_id',$orderIds)
- ->where('pay_category','REFUND')
- ->where('pay_status','SUCCESS')
- ->sum('pay_amount');
- $amount = $payAmount - $refundAmount;
- $money = max($recharge, $amount);
- $memberRole = MemberRole::where('member_role_range_begin' ,'<=',$money)
- ->where('member_role_range_end','>',$money)
- ->first();
- if (!empty($memberRole->member_role_id)){
- return $memberRole->member_role_id;
- }
- return false;
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/5/28 18:03
- *
- * @param $roleId
- * @param $memberId
- * @return void
- * @throws BusinessException
- */
- public static function disposeAdded($roleId, $memberId)
- {
- $role = MemberRole::find($roleId);
- if (empty($role->member_role_rule_json)) {
- throw new BusinessException('增值套包数据异常');
- }
- $ruleJson = json_decode($role->member_role_rule_json, true);
- if (!isset($ruleJson['added'])) {
- throw new BusinessException('增值套包数据异常');
- }
- $addedId = $ruleJson['added'];
- $added = RuleAdded::find($addedId);
- if (!$added) {
- throw new BusinessException('增值套包数据异常');
- }
- $diff = $added->rule_added_goods_diff;
- $components = RuleAddedComponent::where('join_component_rule_added_id', $addedId)->get()->toArray();
- switch ($diff) {
- case "SUM":
- self::diffSum($memberId, $components);
- break;
- case "REPLACE":
- self::diffReplace($memberId, $components);
- break;
- case "NONE":
- self::diffNone($memberId, $components);
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/5/28 18:03
- *
- * @param $memberId
- * @param $components
- * @return void
- */
- public static function diffSum($memberId, $components)
- {
- foreach ($components as $component) {
- self::saveMemberQuota($memberId, $component);
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/5/28 18:02
- *
- * @param $memberId
- * @param $components
- * @return void
- */
- public static function diffReplace($memberId, $components)
- {
- foreach ($components as $component) {
- if (MemberQuota::where('join_quota_member_id', $memberId)->where('join_member_rule_added_component_id', $component['rule_added_component_id'])->exists()) {
- MemberQuota::where('join_quota_member_id', $memberId)
- ->where('join_member_rule_added_component_id', $component['rule_added_component_id'])
- ->where('member_quota_status', 'PENDING')
- ->delete();
- }
- self::saveMemberQuota($memberId, $component);
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/5/28 18:02
- *
- * @param $memberId
- * @param $components
- * @return void
- */
- public static function diffNone($memberId, $components)
- {
- foreach ($components as $component) {
- if (MemberQuota::where('join_quota_member_id', $memberId)->where('join_member_rule_added_component_id', $component['rule_added_component_id'])->exists()) {
- continue;
- }
- self::saveMemberQuota($memberId, $component);
- }
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/5/28 18:02
- *
- * @param $memberId
- * @param $component
- * @return void
- */
- private static function saveMemberQuota($memberId, $component)
- {
- if ($component['rule_added_component_classify'] == 'MONTH') {
- $total = 12 * intval($component['rule_added_component_goods_nbr']);
- for ($i = 0; $i < $total; $i++) {
- self::doSaveMemberQuota($memberId, $component, 1);
- }
- } elseif ($component['rule_added_component_classify'] == 'YEAR') {
- if ($component['rule_added_component_goods_nbr'] == "-99.00") {
- self::doSaveMemberQuota($memberId, $component, -99);
- } else {
- for ($i = 0; $i < intval($component['rule_added_component_goods_nbr']); $i++) {
- self::doSaveMemberQuota($memberId, $component, 1);
- }
- }
- } else {
- if ($component['rule_added_component_goods_nbr'] == "-99.00") {
- self::doSaveMemberQuota($memberId, $component, -99);
- } else {
- for ($i = 0; $i < intval($component['rule_added_component_goods_nbr']); $i++) {
- self::doSaveMemberQuota($memberId, $component, 1);
- }
- }
- }
- }
- private static function doSaveMemberQuota($memberId, $component, $nbr)
- {
- $model = new MemberQuota();
- $model->member_quota_id = 'MQ' . date('ymdH') . random_string(6, 'up');
- $model->join_quota_member_id = $memberId;
- $model->join_member_rule_added_component_id = $component['rule_added_component_id'];
- $model->member_quota_status = 'PENDING';
- $model->member_quota_category = $component['rule_added_component_category'];
- $model->member_quota_name = $component['rule_added_component_name'];
- $model->member_quota_nbr = $nbr;
- $model->member_quota_json = $component['rule_added_component_goods_json'];
- $model->member_quota_addtimes = time();
- $model->save();
- }
- }
|