rabin 2 hari lalu
induk
melakukan
ce56c162a5

+ 1 - 0
src/place/benefit/app/Lib/Item.php

@@ -52,6 +52,7 @@ class Item
             */
         ];
         if ($type != 1) {
+            //$value = [];
             $value = array_merge([
                 ['id' => 'score_num', 'name' => '奖励积分'],
                 //['id' => 'exchange', 'name' => '兑换'],# 用优惠券替代

+ 2 - 0
src/place/benefit/app/Lib/Item/Promotion.php

@@ -12,6 +12,8 @@ class Promotion
             $info = [];
             foreach ($data as $v) {
                 $r = [];
+                $r['source_type'] = $v['source_type'];
+                $r['source_id'] = $v['source_id'];
                 $r['id'] = $v['id'];
                 $r['name'] = $v['name'];
                 $r['type'] = $v['type'];

+ 69 - 21
src/place/benefit/app/Lib/Item/Rebate.php

@@ -4,7 +4,7 @@ use Place;
 # 返利
 class Rebate
 {
-    # 获取返利信息
+    # 根据用户获取返利信息
     public function get($uid, $scope, $price, $rule = [])
     {
         if ($price <= 0) {
@@ -14,25 +14,74 @@ class Rebate
         if ($data) {
             $result = ['rule' => $rule, 'buy' => ['name' => '购买立返', 'desc' => []], 'share' => ['name' => '分享可赚', 'desc' => []]];
             foreach ($data as $info) {
-                $rule = Dever::db('pbenefit/rebate_rule')->select(['rebate_id' => $info['id']], ['col' => 'id,value,`condition`,level,score_id']);
-                foreach ($rule as $k => $v) {
-                    if (!$v['value']) {
-                        continue;
+                $rebate = Dever::db('pbenefit/rebate_rule')->select(['rebate_id' => $info['id']], ['col' => 'id,value,`condition`,level,score_id']);
+                $this->getResult($uid, $rebate, $info, $price, $result);
+            }
+            $result['buy']['desc'] = implode('、', $result['buy']['desc']);
+            $result['share']['desc'] = implode('、', $result['share']['desc']);
+            return $result;
+        }
+        return false;
+    }
+
+    # 支付时,获取分账信息
+    public function getData($uid, $scope, $price, $rule = [])
+    {
+        # 获取该用户的上级信息,三层
+        $parent = Dever::load(\Invite\Lib\Relation::class)->getParentAll($uid, ['<=', 3]);
+        $user = [];
+        $user[] = $uid;
+        if ($parent) {
+            foreach ($parent as $v) {
+                $user[] = $v['uid'];
+            }
+        }
+        $data = Dever::load(\Pbenefit\Lib\Item::class)->getList('rebate', $scope);
+        if ($data) {
+            $result = ['rule' => $rule];
+            foreach ($data as $info) {
+                foreach ($user as $k1 => $v1) {
+                    $check = Dever::load(\Pbenefit\Lib\User::class)->getValidInfo($v1, 'rebate', $info['id']);
+                    if ($check) {
+                        $rebate = Dever::db('pbenefit/rebate_rule')->select(['rebate_id' => $info['id'], 'level' => $k1], ['col' => 'id,value,`condition`,level,score_id']);
+                        $info['source_type'] = $check[0];
+                        $info['source_id'] = $check[1];
+                        $this->getResult($v1, $rebate, $info, $price, $result);
                     }
-                    if ($v['condition'] && !$this->check($uid, $v['condition'])) {
-                        continue;
+                }
+            }
+            return $result;
+        }
+        return false;
+    }
+
+    private function getResult($uid, $rebate, $info, $price, &$result)
+    {
+        if ($rebate) {
+            foreach ($rebate as $k => $v) {
+                if (!$v['value']) {
+                    continue;
+                }
+                if ($v['condition'] && !$this->check($uid, $v['condition'])) {
+                    continue;
+                }
+                $v['source_type'] = $info['source_type'];
+                $v['source_id'] = $info['source_id'];
+                $v['source_value'] = $v['value'];
+                $v['value'] = Dever::math('value', $price, $v['value']);
+                if ($v['value'] > 0) {
+                    $score = Dever::load(\Pscore\Lib\Info::class)->get($v['score_id']);
+                    if (isset($result['rule'][$v['id']])) {
+                        $v['value'] += $result['rule'][$v['id']]['value'];
                     }
-                    $v['source_value'] = $v['value'];
-                    $v['value'] = Dever::math('value', $price, $v['value']);
-                    if ($v['value'] > 0) {
-                        $score = Dever::load(\Pscore\Lib\Info::class)->get($v['score_id']);
-                        if (isset($result['rule'][$v['id']])) {
-                            $v['value'] += $result['rule'][$v['id']]['value'];
-                        }
-                        $v['value_text'] = Dever::load(\Pscore\Lib\Info::class)->getText($v['value'], $score);
-                        $v['rebate_id'] = $info['id'];
+                    $v['value_text'] = Dever::load(\Pscore\Lib\Info::class)->getText($v['value'], $score);
+                    $v['rebate_id'] = $info['id'];
+                    if ($v['level'] > 0) {
+                        $v['name'] = '团队' . $info['name'];
+                    } else {
                         $v['name'] = $info['name'];
-                        $result['rule'][$v['id']] = $v;
+                    }
+                    if (isset($result['buy'])) {
                         if ($v['level'] == 0) {
                             # 购买可得
                             $result['buy']['desc'][$v['id']] = $v['value_text'];
@@ -40,14 +89,13 @@ class Rebate
                             # 分享可得
                             $result['share']['desc'][$v['id']] = $v['value_text'];
                         }
+                    } else {
+                        $v['uid'] = $uid;
                     }
+                    $result['rule'][$v['id']] = $v;
                 }
             }
-            $result['buy']['desc'] = implode('、', $result['buy']['desc']);
-            $result['share']['desc'] = implode('、', $result['share']['desc']);
-            return $result;
         }
-        return false;
     }
 
     private function check($uid, $condition)

+ 12 - 0
src/place/benefit/app/Lib/Item/Score.php

@@ -12,6 +12,8 @@ class Score
             # 获取信息
             foreach ($data as $v) {
                 if ($v['score_id'] == $info['id']) {
+                    $info['source_type'] = $v['source_type'];
+                    $info['source_id'] = $v['source_id'];
                     $info['rule_id'] = $v['id'];
                     $info['rule_name'] = $v['name'];
                     $info['recharge'] = $v['recharge'];
@@ -60,6 +62,11 @@ class Score
         }
         
         $data = Dever::load(\Porder\Lib\Score\Pay::class)->get([$info], ['score' => $info, 'rebate' => []], false);
+
+        # 消耗权益
+        if ($info['source_type'] == 1) {
+            Dever::load(\Pbenefit\Lib\User::class)->use($info['source_id'], -1, 22);
+        }
         return $data;
     }
 
@@ -107,6 +114,11 @@ class Score
             $value = Dever::math('mul', $number, $exchange['value']);
             Dever::load(\Pscore\Lib\Log::class)->action('兑换', $info['id'])->add($user['uid'], '', -1*$number);
             Dever::load(\Pscore\Lib\Log::class)->action('兑换', $exchange['exchange_score_id'])->add($user['uid'], '', $value);
+
+            # 消耗权益
+            if ($info['source_type'] == 1) {
+                Dever::load(\Pbenefit\Lib\User::class)->use($info['source_id'], -1, 22);
+            }
             return 'ok';
         } else {
             Dever::error('兑换失败');

+ 88 - 3
src/place/benefit/app/Lib/User.php

@@ -2,7 +2,7 @@
 use Dever;
 class User
 {
-    # 获取用户当前有效的权益
+    # 获取用户当前有效的权益列表
     public function getValid($uid, $type, $scope = '')
     {
         $result = [];
@@ -28,6 +28,32 @@ class User
         return $result;
     }
 
+    # 获取用户当前有效的权益信息
+    public function getValidInfo($uid, $type, $type_id)
+    {
+        $result = [];
+        # 先验证用户是否有该权益
+        $user = Dever::db('pbenefit/user')->find(['uid' => $uid, 'type' => $type, 'type_id' => $type_id, 'status' => 1]);
+        if ($user && $user['num'] > 0) {
+            return [1, $user['id']];
+        }
+        # 再验证身份等级是否有该权益
+        $level = Dever::load(\Puser\Lib\Role::class)->getLevel($uid);
+        if (!$level) {
+            # 如果当前用户没有任何身份等级,需要给一个默认的
+            $user = Dever::db('puser/info')->find($uid);
+            Dever::load(\Prole\Lib\Info::class)->up($uid, $user['sales_type'], $user['sales_id']);
+            $level = Dever::load(\Puser\Lib\Role::class)->getLevel($uid);
+        }
+        if ($level) {
+            $role = Dever::db('pbenefit/role_upgrade')->find(['type' => $type, 'type_id' => $type_id,'level_id' => ['in', $level]]);
+            if ($role) {
+                return [2, $role['id']];
+            }
+        }
+        return false;
+    }
+
     # 获取用户所有持有的权益
     public function getList($uid)
     {
@@ -157,13 +183,13 @@ class User
         $log = Dever::db('pbenefit/user_log')->select($where);
         $result = '';
         if ($log) {
-            $name = '领取明细';
+            $name = '明细';
             if ($type == 'perk') {
                 $result = '<div style="color:red;text-align:center;margin-bottom:6px;">点击兑换码可复制,请到权益包里兑换</div>';
                 $name = '兑换码';
             }
             $result .= '<table style="width:100%; border-collapse:collapse; text-align:left; table-layout:auto;">';
-            $result .= '<tr><td style="border:1px solid #ddd; padding:8px;">领取日期</td><td style="border:1px solid #ddd; padding:8px;">'.$name.'</td></tr>';
+            $result .= '<tr><td style="border:1px solid #ddd; padding:8px;">时间</td><td style="border:1px solid #ddd; padding:8px;">'.$name.'</td></tr>';
             foreach ($log as $k => $v) {
                 $v['cdate'] = date('Y-m-d H:i', $v['cdate']);
                 if ($v['content']) {
@@ -180,6 +206,54 @@ class User
         return $result;
     }
 
+    # 消耗权益 促销、返利、积分规则可用
+    public function use_commit(){}
+    public function use($id, $num = 1, $source, $source_id)
+    {
+        if (!$id) {
+            return;
+        }
+        $user = Dever::db('pbenefit/user')->find($id);
+        if (!$user) {
+            Dever::error('无权益');
+        }
+        if ($num < 0) {
+            if ($user['status'] != 1) {
+                Dever::error('无权益');
+            }
+            if ($user['num'] <= 0) {
+                Dever::db('pbenefit/user')->update($id, ['status' => 2]);
+                Dever::error('无权益');
+            }
+        }
+
+        $update['num'] = $user['num'] + $num;
+        if ($update['num'] <= 0) {
+            $update['status'] = 2;
+        } else {
+            $update['status'] = 1;
+        }
+        $state = Dever::db('pbenefit/user')->update($id, $update);
+        if ($state) {
+            $log = [
+                'uid' => $user['uid'],
+                'type' => $user['type'],
+                'type_id' => $user['type_id'],
+            ];
+            $log['num'] = $num;
+            $log['user_id'] = $user['id'];
+            if ($source == 'source') {
+                $log['source'] = 20;
+            } elseif ($source == 'role') {
+                $log['source'] = 21;
+            } else {
+                $log['source'] = 22;
+            }
+            $log['source_id'] = $source_id;
+            $state = Dever::db('pbenefit/user_log')->insert($log);
+        }
+    }
+
     # 领取权益
     public function receive_commit(){}
     public function receive($uid, $info, $source = 1, $state = false)
@@ -221,9 +295,20 @@ class User
         $user = Dever::db('pbenefit/user')->find($log);
         if (!$user) {
             $log['num'] = $info['num'];
+            if ($log['num'] >= 0) {
+                $log['status'] = 1;
+            } else {
+                $log['status'] = 2;
+            }
             $user_id = Dever::db('pbenefit/user')->insert($log);
         } else {
+            $num = $user['num'] + $info['num'];
             $log['num'] = ['+', $info['num']];
+            if ($num >= 0) {
+                $log['status'] = 1;
+            } else {
+                $log['status'] = 2;
+            }
             $user_id = $user['id'];
             $log['cdate'] = time();
             Dever::db('pbenefit/user')->update($user_id, $log);

+ 14 - 2
src/place/benefit/manage/Lib/User.php

@@ -10,12 +10,24 @@ class User
 
     public function getSourceInfo($source, $source_id)
     {
-        if ($source == 2) {
+        if ($source == 1) {
+            $db = Dever::db('prole/info');
+        } elseif ($source == 2) {
             $db = Dever::db('pbenefit/perk');
+        } elseif ($source == 3) {
+            $db = Dever::db('psource/info');
         } elseif ($source == 10) {
             $db = Dever::db('sector/user');
+        } elseif ($source == 20) {
+            $db = Dever::db('porder/source')->find($source_id);
+            return $db['order_num'] ?? '-';
+        } elseif ($source == 21) {
+            $db = Dever::db('porder/role')->find($source_id);
+            return $db['order_num'] ?? '-';
+        } elseif ($source == 22) {
+            $db = Dever::db('pscore/info');
         } else {
-            $db = Dever::db('prole/info');
+            return '-';
         }
         $info = $db->find($source_id);
         return $info['name'] ?? '-';

+ 1 - 1
src/place/benefit/manage/user.php

@@ -13,7 +13,7 @@ return [
             'num' => [
                 'name' => '剩余数量',
             ],
-            'status',
+            'status' => '',
             'cdate' => ['name' => '更新时间'],
         ],
         'button' => [

+ 1 - 1
src/place/benefit/manage/user_log.php

@@ -15,7 +15,7 @@ return [
                 'show' => 'Dever::call("Pbenefit/Manage/Lib/User.getSourceInfo", [{source}, {source_id}])',
             ],
             'num' => [
-                'name' => '领取数量',
+                'name' => '数量',
             ],
             'cdate',
         ],

+ 4 - 0
src/place/benefit/table/user_log.php

@@ -33,6 +33,10 @@ return [
                 2 => '权益包',
                 3 => '购买资源',
                 10 => '后台发放',
+
+                20 => '购买资源消耗权益',
+                21 => '购买身份消耗权益',
+                22 => '积分操作消耗权益',
             ],
         ],
 

+ 1 - 0
src/place/interface/app/Api/Source.php

@@ -276,6 +276,7 @@ class Source extends Core
     # 拉起支付
     public function pay()
     {
+        Dever::input('pay', 'set', 1);
         $this->payConfirm();
         return Dever::load(\Porder\Lib\Source\Pay::class)->act(false);
     }

+ 8 - 4
src/place/order/app/Lib/Notify.php

@@ -51,13 +51,15 @@ class Notify
     # 抵扣
     private function deduct($type, $order)
     {
+        $score = Dever::load(\Pscore\Lib\Info::class)->get($order['score_id']);
+
         # 使用优惠券
         if (isset($order['user_coupon_id']) && $order['user_coupon_id'] > 0) {
-            Dever::load(\Puser\Lib\Coupon::class)->use($type, $order, $this->set['score']);
+            Dever::load(\Puser\Lib\Coupon::class)->use($type, $order, $score);
         }
         # 使用礼品卡
         if (isset($order['user_gift_id']) && $order['user_gift_id'] > 0) {
-            Dever::load(\Puser\Lib\Gift::class)->use($type, $order, $this->set['score']);
+            Dever::load(\Puser\Lib\Gift::class)->use($type, $order, $score);
         }
         # 使用钱包支付
         if (isset($order['wallet_cash']) && $order['wallet_cash'] > 0) {
@@ -72,6 +74,8 @@ class Notify
                 $status = 2;
             }
             foreach ($rebate as $k => $v) {
+                Dever::load(\Pscore\Lib\Log::class)->action('返利', $v['score_id'])->add($v['uid'], $v['name'], $v['value'], $status, $type . '_order_' . $order['id']);
+                /*
                 if ($v['level'] == 0) {
                     # 给自己返利
                     Dever::load(\Pscore\Lib\Log::class)->action('返利', $v['score_id'])->add($order['uid'], $v['name'], $v['value'], $status, $type . '_order_' . $order['id']);
@@ -81,7 +85,7 @@ class Notify
                     if ($parent) {
                         Dever::load(\Pscore\Lib\Log::class)->action('返利', $v['score_id'])->add($parent['uid'], $v['name'], $v['value'], $status, $type . '_order_' . $order['id']);
                     }
-                }
+                }*/
             }
         }
     }
@@ -151,7 +155,7 @@ class Notify
                     Dever::load(\Prole\Lib\User::class)->up($order['uid'], $v['role_id'], $v['level_id'], $order['sales_type'], $order['sales_id'], 1, $order['id'], '订单号:' . $order['order_num']);
                 }
 
-                $this->sales($order, $this->type);
+                $this->sales($order, 'role');
 
                 # 微信小程序发货
                 Dever::load(\Place\Lib\Account::class)->delivery($order, $detail[0], [], 1);

+ 34 - 1
src/place/order/app/Lib/Pay.php

@@ -10,6 +10,7 @@ class Pay
     protected $coupon = [];
     protected $gift = [];
     protected $set = [];
+    protected $pay = false;
     protected $place;
 
     public function __construct(Place $place)
@@ -26,6 +27,10 @@ class Pay
         if (empty($set['score'])) {
             Dever::error('积分规则不能为空');
         }
+        $pay = Dever::input('pay');
+        if ($pay == 1) {
+            $this->pay = true;
+        }
         $this->set = $set;
         $this->set['rebate'] = ['rule' => []];
         $this->order['name'] = '';
@@ -218,7 +223,15 @@ class Pay
 
             if ($v['pay_cash'] > 0) {
                 $v['pay_money_cash'] = Dever::math('mul', $v['pay_cash'], $this->set['score']['exp']);
-                $this->set['rebate'] = Dever::load(\Pbenefit\Lib\Item::class)->load('rebate')->get($this->order['uid'], $v['scope'], $v['pay_money_cash'], $this->set['rebate']['rule']);
+
+                # 计算方式不同
+                if ($this->pay) {
+                    # 支付时,按照明细计算
+                    $this->set['rebate'] = Dever::load(\Pbenefit\Lib\Item::class)->load('rebate')->getData($this->order['uid'], $v['scope'], $v['pay_money_cash'], $this->set['rebate']['rule']);
+                } else {
+                    # 展示时,按照uid计算
+                    $this->set['rebate'] = Dever::load(\Pbenefit\Lib\Item::class)->load('rebate')->get($this->order['uid'], $v['scope'], $v['pay_money_cash'], $this->set['rebate']['rule']);
+                }
             }
         }
 
@@ -291,6 +304,7 @@ class Pay
         Dever::db('puser/info')->update($this->order['uid'], ['num_order' => ['+', 1]]);
 
         $table = $this->type . '_detail';
+        $use_promotion = [];
         foreach ($this->detail as $v) {
             $promotion = $v['promotion'];
             unset($v['promotion']);
@@ -318,6 +332,12 @@ class Pay
                     $promotion['table_id'] = $id;
                     $promotion['promotion_id'] = $promotion['id'];
                     unset($promotion['id']);
+                    if ($promotion['source_type'] == 1) {
+                        if (empty($use_promotion[$promotion['source_id']])) {
+                            $use_promotion[$promotion['source_id']] = 1;
+                        }
+                    }
+                    
                     Dever::db('porder/promotion')->insert($promotion);
                 }
                 # 记录返利 以后增加明细返利记录
@@ -340,11 +360,24 @@ class Pay
                 $v1['table'] = $this->type;
                 $v1['table_id'] = $order_id;
                 $v1['rebate_rule_id'] = $v1['id'];
+                $v1['source_type'] = $v1['source_type'];
+                $v1['source_id'] = $v1['source_id'];
+                $v1['uid'] = $v1['uid'];
+                if ($v1['source_type'] == 1) {
+                    # 消耗用户权益数量
+                    Dever::load(\Pbenefit\Lib\User::class)->use($v1['source_id'], -1, $this->type, $order_id);
+                }
                 unset($v1['id']);
                 Dever::db('porder/rebate')->insert($v1);
             }
         }
 
+        if (isset($use_promotion) && $use_promotion) {
+            foreach ($use_promotion as $k => $v) {
+                Dever::load(\Pbenefit\Lib\User::class)->use($k, -1, $this->type, $order_id);
+            }
+        }
+
         # 设置活跃
         Dever::db('place/code')->update(['code' => $this->place->info['code']], ['status' => 1]);
     }

+ 34 - 12
src/place/order/app/Lib/Source/Refund.php

@@ -105,6 +105,33 @@ class Refund
                 if ($refund['type'] == 1) {
                     Dever::load(\Pstock\Lib\Info::class)->refundAll($order);
                 }
+                # 如果有返利
+                $rebate = Dever::db('porder/rebate')->select(['table' => 'source', 'table_id' => $order['id']]);
+                if ($rebate) {
+                    foreach ($rebate as $k => $v) {
+                        if ($v['source_type'] == 1) {
+                            # 恢复权益
+                            Dever::load(\Pbenefit\Lib\User::class)->use($v['source_id'], 1, 'source', $order['id']);
+                        }
+                    }
+                }
+
+                $promotion = Dever::db('porder/promotion')->select(['table' => 'source', 'table_id' => $order['id']]);
+                if ($promotion) {
+                    $use = [];
+                    foreach ($promotion as $k => $v) {
+                        if ($v['source_type'] == 1) {
+                            if (empty($use[$v['source_id']])) {
+                                $use[$v['source_id']] = 1;
+                            }
+                        }
+                    }
+                    if ($use) {
+                        foreach ($use as $k => $v) {
+                            Dever::load(\Pbenefit\Lib\User::class)->use($k, 1, 'source', $order['id']);
+                        }
+                    }
+                }
             } else {
                 # 只退了一部分
                 $refund_detail = Dever::db('porder/source_refund_detail')->select(['source_refund_id' => $refund['id']]);
@@ -131,7 +158,10 @@ class Refund
                                     $v['pay_cash'] -= $v['refund_cash'];
                                     if ($v['pay_cash'] > 0) {
                                         $money = Dever::math('mul', $v['pay_cash'], $score['exp']);
-                                        $rebate = Dever::load(\Pbenefit\Lib\Item::class)->load('rebate')->get($order['uid'], $v['scope'], $money, $rebate['rule']);
+
+                                        $rebate = Dever::load(\Pbenefit\Lib\Item::class)->load('rebate')->getData($order['uid'], $v['scope'], $money, $rebate['rule']);
+
+                                        //$rebate = Dever::load(\Pbenefit\Lib\Item::class)->load('rebate')->get($order['uid'], $v['scope'], $money, $rebate['rule']);
                                     }
                                 }
                             }
@@ -140,19 +170,11 @@ class Refund
                                     $v1['table'] = 'source';
                                     $v1['table_id'] = $order['id'];
                                     $v1['rebate_rule_id'] = $v1['id'];
+                                    $v1['source_type'] = $v1['source_type'];
+                                    $v1['source_id'] = $v1['source_id'];
                                     unset($v1['id']);
                                     Dever::db('porder/rebate')->insert($v1);
-
-                                    if ($v1['level'] == 0) {
-                                        # 给自己返利
-                                        Dever::load(\Pscore\Lib\Log::class)->action('返利', $v1['score_id'])->add($order['uid'], $v1['name'], $v1['value'], 2, 'source_order_' . $order['id']);
-                                    } else {
-                                        # 给上级返利
-                                        $parent = Dever::load(\Invite\Lib\Relation::class)->getParent($order['uid'], $v1['level']);
-                                        if ($parent) {
-                                            Dever::load(\Pscore\Lib\Log::class)->action('返利', $v1['score_id'])->add($parent['uid'], $v1['name'], $v1['value'], 2, 'source_order_' . $order['id']);
-                                        }
-                                    }
+                                    Dever::load(\Pscore\Lib\Log::class)->action('返利', $v1['score_id'])->add($v1['uid'], $v1['name'], $v1['value'], 2, 'source_order_' . $order['id']);
                                 }
                             }
                         }

+ 14 - 0
src/place/order/table/promotion.php

@@ -48,6 +48,20 @@ return [
             'name'      => '折扣前价格',
             'type'      => 'decimal(11,2)',
         ],
+
+        'source_type'       => [
+            'type'      => 'tinyint(1)',
+            'name'      => '权益来源类型',
+            'value'     => [
+                1 => '用户权益',
+                2 => '角色权益',
+            ],
+        ],
+
+        'source_id'       => [
+            'type'      => 'int(11)',
+            'name'      => '权益来源表id',
+        ],
     ],
     'index' => [
         'search' => '`table`,table_id,promotion_id',

+ 19 - 0
src/place/order/table/rebate.php

@@ -14,6 +14,11 @@ return [
             'name'      => '表ID',
         ],
 
+        'uid'       => [
+            'type'      => 'int(11)',
+            'name'      => '用户ID',
+        ],
+
         'rebate_id'       => [
             'type'      => 'int(11)',
             'name'      => '返利',
@@ -38,6 +43,20 @@ return [
             'name'      => '奖励层级',
             'type'      => 'int(11)',
         ],
+        'source_type'       => [
+            'type'      => 'tinyint(1)',
+            'name'      => '权益来源类型',
+            'value'     => [
+                1 => '用户权益',
+                2 => '角色权益',
+            ],
+        ],
+
+        'source_id'       => [
+            'type'      => 'int(11)',
+            'name'      => '权益来源表id',
+        ],
+
         'source_value' => [
             'name'      => '奖励数值设置',
             'type'      => 'varchar(20)',

+ 1 - 1
src/place/score/app/Lib/Log.php

@@ -76,7 +76,7 @@ class Log
             } else {
                 $type = 2;
             }
-            $state = Dever::db('pscore/user_log')->update($id, ['amount' => $amount, 'type' => $type]);
+            $state = Dever::db('pscore/user_log')->update($id, ['amount' => $amount, 'type' => $type, 'status' => 1]);
             if ($state) {
                 $state = Dever::load(User::class)->up($log['user_id'], $amount, 3);
             }

+ 9 - 2
src/place/score/app/Lib/Withdraw.php

@@ -82,12 +82,12 @@ class Withdraw
         }
         $log = Dever::load(Log::class)->action('提现', $info['id'])->add($user['uid'], '汇率'.$info['exp'].',手续费'.$info['withdraw_fee'].'%,预计到账' . $cash . '元', -1*$number);
         if ($log) {
-            $this->log($user['uid'], $info['id'], $log['id'], $day, $number, $fee, $cash, $account);
+            $this->log($user['uid'], $info['id'], $log['id'], $day, $number, $fee, $cash, $account, $info['source_type'], $info['source_id']);
         }
         return 'ok';
     }
 
-    private function log($uid, $info_id, $log_id, $day, $amount, $fee, $cash, $account)
+    private function log($uid, $info_id, $log_id, $day, $amount, $fee, $cash, $account, $source_type, $source_id)
     {
         $data['uid'] = $uid;
         $data['info_id'] = $info_id;
@@ -101,6 +101,13 @@ class Withdraw
         $data['type'] = $account['type'];
         $data['realname'] = $account['realname'];
         $data['account'] = $account['account'];
+        $data['source_type'] = $source_type;
+        $data['source_id'] = $source_id;
         Dever::db('pscore/user_withdraw')->insert($data);
+
+        # 消耗权益
+        if ($source_type == 1) {
+            Dever::load(\Pbenefit\Lib\User::class)->use($source_id, -1, 22, $info_id);
+        }
     }
 }

+ 4 - 0
src/place/score/manage/Lib/Withdraw.php

@@ -19,6 +19,10 @@ class Withdraw
             if ($data['desc']) {
                 $data['desc'] = ':' . $data['desc'];
             }
+            # 恢复消耗权益
+            if ($info['source_type'] == 1) {
+                Dever::load(\Pbenefit\Lib\User::class)->use($info['source_id'], 1, 22, $info['info_id']);
+            }
             Dever::load(\Pscore\Lib\Log::class)->action('提现', $info['info_id'])->add($info['uid'], '审核失败' . $data['desc'], $info['amount']);
         }
         Dever::db('pscore/user_withdraw')->update($data['user_withdraw_id'], $update);

+ 13 - 0
src/place/score/table/user_withdraw.php

@@ -60,6 +60,19 @@ return [
             'name'      => '到账金额',
             'type'      => 'decimal(11,2)',
         ],
+        'source_type'       => [
+            'type'      => 'tinyint(1)',
+            'name'      => '权益来源类型',
+            'value'     => [
+                1 => '用户权益',
+                2 => '角色权益',
+            ],
+        ],
+
+        'source_id'       => [
+            'type'      => 'int(11)',
+            'name'      => '权益来源表id',
+        ],
         'status' => [
             'name'      => '状态',
             'type'      => 'tinyint(1)',