Просмотр исходного кода

分红-增加限制分配条件,并增加记录日志

sunxbiao месяцев назад: 6
Родитель
Сommit
d5d28c25c9

+ 15 - 0
app/common/dao/dividend/DividendLogDao.php

@@ -0,0 +1,15 @@
1
+<?php
2
+
3
+namespace app\common\dao\dividend;
4
+
5
+use app\common\dao\BaseDao;
6
+use app\common\model\dividend\DividendLog;
7
+
8
+class DividendLogDao extends BaseDao
9
+{
10
+
11
+    protected function getModel(): string
12
+    {
13
+        return DividendLog::class;
14
+    }
15
+}

+ 16 - 0
app/common/enum/dividend/DividendLogEnum.php

@@ -0,0 +1,16 @@
1
+<?php
2
+
3
+namespace app\common\enum\dividend;
4
+
5
+use app\common\enum\CommonEnum;
6
+
7
+class DividendLogEnum extends CommonEnum
8
+{
9
+    // 1-合伙人分红;2-联创分红;3-股权分红;4-佣金补贴
10
+    const TYPE = [
11
+        'PartnerProfit' => ['code' => 1, 'name' => '合伙人分红'],
12
+        'CoFounderProfit' => ['code' => 2, 'name' => '联创分红'],
13
+        'EquityDividend' => ['code' => 3, 'name' => '股权分红'],
14
+        'CommissionSubsidy' => ['code' => 4, 'name' => '佣金补贴']
15
+    ];
16
+}

+ 19 - 0
app/common/model/dividend/DividendLog.php

@@ -0,0 +1,19 @@
1
+<?php
2
+
3
+namespace app\common\model\dividend;
4
+
5
+use app\common\model\BaseModel;
6
+
7
+class DividendLog extends BaseModel
8
+{
9
+
10
+    public static function tablePk(): string
11
+    {
12
+        return 'id';
13
+    }
14
+
15
+    public static function tableName(): string
16
+    {
17
+        return 'dividend_logs';
18
+    }
19
+}

+ 39 - 0
app/common/repositories/dividend/DividendLogRepository.php

@@ -0,0 +1,39 @@
1
+<?php
2
+
3
+namespace app\common\repositories\dividend;
4
+
5
+use app\common\dao\dividend\DividendLogDao;
6
+use app\common\repositories\BaseRepository;
7
+use app\entity\data\dividend\DividendLogEntity;
8
+
9
+class DividendLogRepository extends BaseRepository
10
+{
11
+
12
+    protected $dao;
13
+
14
+    /**
15
+     * DividendLogRepository constructor.
16
+     * @param DividendLogDao $dao
17
+     */
18
+    public function __construct(DividendLogDao $dao)
19
+    {
20
+        $this->dao = $dao;
21
+    }
22
+
23
+    public function create($data)
24
+    {
25
+        return $this->dao->create($data);
26
+    }
27
+
28
+    /**
29
+     * @param DividendLogEntity $dividendLogEntity
30
+     * @return DividendLogEntity
31
+     */
32
+    public function createByEntity(DividendLogEntity $dividendLogEntity): DividendLogEntity
33
+    {
34
+        $result = $this->create($dividendLogEntity->toUnderlineArray())->toArray();
35
+        /** @var DividendLogEntity $entity */
36
+        $entity = DividendLogEntity::newInstance($result);
37
+        return $entity;
38
+    }
39
+}

+ 139 - 2
app/controller/api/store/merchant/TaskPartnerProfits.php

@@ -4,7 +4,15 @@
4 4
 namespace app\controller\api\store\merchant;
5 5
 
6 6
 
7
+use app\common\enum\CommonEnum;
8
+use app\common\enum\dividend\DividendLogEnum;
9
+use app\common\repositories\dividend\DividendLogRepository;
10
+use app\entity\data\dividend\DividendLogEntity;
11
+use think\db\exception\DataNotFoundException;
12
+use think\db\exception\DbException;
13
+use think\db\exception\ModelNotFoundException;
7 14
 use think\facade\Db;
15
+use think\facade\Log;
8 16
 use think\Request;
9 17
 
10 18
 class TaskPartnerProfits
@@ -77,6 +85,95 @@ class TaskPartnerProfits
77 85
         return $totalPv;
78 86
     }
79 87
 
88
+    /**
89
+     * 获取周期内 所有 有效的下单人ID列表
90
+     * @param $startTime
91
+     * @param $endTime
92
+     * @param array $removeUserIdList
93
+     * @return array
94
+     */
95
+    public function getOrderUidListByTime($startTime, $endTime, array $removeUserIdList = []): array
96
+    {
97
+        $uidList = [];
98
+        try {
99
+            // 本地订单
100
+            $storeOrder = Db::name("store_order")
101
+                ->where('mer_id', '<>', CommonEnum::DESIGN_MERCHANT_ID['Repurchase']['code'])
102
+                ->whereNotIn('uid', $removeUserIdList)
103
+                ->whereIn('status', [0, 1, 2, 3])
104
+                ->whereBetween("pay_time", [$startTime, $endTime])
105
+                ->whereNotNull("pay_time") // 排除 pay_time 为 NULL 的订单
106
+                ->field('mer_id, status, pay_time, total_price, uid')
107
+                ->select()
108
+                ->toArray();
109
+            $storeOrderUidList = array_column($storeOrder, 'uid');
110
+            $uidList = array_merge($uidList, $storeOrderUidList);
111
+
112
+            // 话费订单 order_huafei
113
+            $orderHuafei = Db::name("order_huafei")
114
+                ->whereNotIn('uid', $removeUserIdList)
115
+                ->where('status', '<>', 0) // 只要不是待支付的 即算是有效活跃度
116
+                ->whereBetween("finish_time", [$startTime, $endTime])
117
+                ->field('order_huafei_id, uid, status, finish_time, amount_price')
118
+                ->select()
119
+                ->toArray();
120
+            $orderHuafeiUidList = array_column($orderHuafei, 'uid');
121
+            $uidList = array_merge($uidList, $orderHuafeiUidList);
122
+
123
+            // 三方订单
124
+            // 京东
125
+            $orderJd = Db::name("order_jd")
126
+                ->whereNotIn('uid', $removeUserIdList)
127
+                ->where('status', '<>', 15) // 只要不是待支付的 即算是有效活跃度
128
+                ->whereBetween("create_time", [$startTime, $endTime])
129
+                ->field('order_jd_id, uid, status, create_time, price')
130
+                ->select()
131
+                ->toArray();
132
+            $orderJdUidList = array_column($orderJd, 'uid');
133
+            $uidList = array_merge($uidList, $orderJdUidList);
134
+            // 淘宝
135
+            $orderTb = Db::name("order_tb")
136
+                ->whereNotIn('uid', $removeUserIdList)
137
+                // ->where('status', '<>', 15) // 只要不是待支付的 即算是有效活跃度 不能明确状态信息
138
+                ->whereBetween("create_time", [$startTime, $endTime])
139
+                ->field('order_tb_id, uid, tk_status, create_time, pay_price')
140
+                ->select()
141
+                ->toArray();
142
+            $orderTbUidList = array_column($orderTb, 'uid');
143
+            $uidList = array_merge($uidList, $orderTbUidList);
144
+            // 拼多多
145
+            $orderPdd = Db::name("order_pdd")
146
+                ->whereNotIn('uid', $removeUserIdList)
147
+                ->where('pin_order_status', '<>', -1) // 只要不是待支付的 即算是有效活跃度 不能明确状态信息
148
+                ->whereBetween("group_time", [strtotime($startTime), strtotime($endTime)])
149
+                ->field('order_pdd_id, uid, pin_order_status, create_time, pay_money')
150
+                ->select()
151
+                ->toArray();
152
+            $orderPddUidList = array_column($orderPdd, 'uid');
153
+            $uidList = array_merge($uidList, $orderPddUidList);
154
+            // 唯品会
155
+            $orderVip = Db::name("order_vip")
156
+                ->whereNotIn('uid', $removeUserIdList)
157
+                ->where('status', '<>', 0) // 只要不是待支付的 即算是有效活跃度 不能明确状态信息
158
+                ->whereBetween("order_time", [strtotime($startTime), strtotime($endTime)])
159
+                ->field('order_vip_id, uid, status, order_time, pay_money')
160
+                ->select()
161
+                ->toArray();
162
+            $orderVipUidList = array_column($orderVip, 'uid');
163
+            $uidList = array_merge($uidList, $orderVipUidList);
164
+        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
165
+            Log::error('分红-获取下单用户列表失败:' . json_encode([
166
+                    'error_message' => $e->getMessage(),
167
+                    'error_file' => $e->getFile(),
168
+                    'error_line' => $e->getLine(),
169
+                    'error_code' => $e->getCode(),
170
+                    'exception_class' => get_class($e),
171
+                    'trace' => $e->getTraceAsString()
172
+                ], JSON_UNESCAPED_UNICODE));
173
+        }
174
+        return $uidList;
175
+    }
176
+
80 177
     public function getRedEnvelopeByTime($startTime, $endTime, $removeUserIdList = [])
81 178
     {
82 179
         // 查询指定时间区间内的所有订单,并排除 pay_time 为 NULL 的订单
@@ -121,6 +218,7 @@ class TaskPartnerProfits
121 218
 
122 219
         $startTime = $request->param('startTime', null);
123 220
         $endTime = $request->param('endTime', null);
221
+        $remark = $request->param('remark', '');
124 222
         $isTest = $request->param('isTest', 1);
125 223
         $maxAllocationAmount = $request->param('maxAllocationAmount', null);
126 224
 
@@ -184,6 +282,18 @@ class TaskPartnerProfits
184 282
             ->select()
185 283
             ->toArray();//星级等级
186 284
 
285
+        // 组装日志信息
286
+        /** @var DividendLogEntity $dividendLogEntity */
287
+        $dividendLogEntity = DividendLogEntity::newInstance();
288
+        $dividendLogEntity->setType(DividendLogEnum::TYPE['PartnerProfit']['code'])
289
+            ->setStartTime($startTime)
290
+            ->setEndTime($endTime)
291
+            ->setPerformance($performance)
292
+            ->setAbatement(json_encode([['code' => 'redEnvelope', 'name' => '红包', 'reason' => '发放的红包金额,需要从赚取的业绩中扣除', 'number' => $redEnvelope]]))
293
+            ->setAllocated($weekRecord)
294
+            ->setRule(json_encode($starList))
295
+            ->setRemark($remark);
296
+
187 297
         // 2、循环星级信息 并分组归类
188 298
         $salesArr = array_column($starList, null, 'title');
189 299
 
@@ -251,6 +361,10 @@ class TaskPartnerProfits
251 361
             //周总业绩(8400) * 合伙人级别的分佣比例(15%) = 可分红的佣金(1260)
252 362
             //            $salesArr[$key]['fs_money_nums'] = round($weekRecord * $value['profit_rate'], 2);
253 363
             $salesArr[$key]['fs_money_nums'] = floor(($weekRecord * ($value['profit_rate'] / 100)) * 100) / 100;
364
+
365
+            // 日志表 记录应分配金额
366
+            $dividendLogEntity->setExpected(bcadd($dividendLogEntity->getExpected() ?: '0.00', $salesArr[$key]['fs_money_nums'], 2));
367
+
254 368
             //可分红的业绩(分为两半,各50%,一部分是分给双联盟比例,一部分是小业绩比例)
255 369
             //把本周的业绩(8400) * 合伙人级别的分佣比例(15%) * 0.5 = 630(可分红佣金的 50%)
256 370
             //            $salesArr[$key]['fs_money'] = round($salesArr[$key]['fs_money_nums'] / 2, 2);
@@ -298,6 +412,11 @@ class TaskPartnerProfits
298 412
             }
299 413
         }
300 414
 
415
+        // 6、记录日志
416
+        // 获取周期内所有符合条件的订单
417
+        $feeUids = $this->getOrderUidListByTime($startTime, $endTime, $removeUserIdList);
418
+
419
+
301 420
         // 7、执行分红
302 421
         $testRecord = [];
303 422
         foreach ($salesArr as $key => $value) {
@@ -350,15 +469,27 @@ class TaskPartnerProfits
350 469
                 //                }
351 470
 
352 471
                 if ($isTest == 0) {
472
+
473
+                    if (!in_array($v, $feeUids)) {
474
+                        $feeWaiverUids = $dividendLogEntity->getFeeWaiverUids() ?: [];
475
+                        $feeWaiverUids[] = [
476
+                            'uid' => $v,
477
+                            'remark' => '周期内未完成订单任务'
478
+                        ];
479
+                        $dividendLogEntity->setFeeWaiverUids($feeWaiverUids);
480
+                        continue;
481
+                    }
482
+                    $dividendLogEntity->setArrivalPoint(bcadd($dividendLogEntity->getArrivalPoint() ?: '0.00', $allAward, 2));
483
+
353 484
                     $userdatassss = db::name("old_user_jbp")->where("id", $v)->find();
354 485
                     $datas_log = [
355 486
                         'note' => "名称:" . $userdatassss['nick_name'] . " 用户:" . $v . " 双联盟总业绩:" . $maxValueAll . " 我的双区业绩:" . $myBill . " 小区总业绩:" . $userPvSmallNum . " 我的业绩小的:" . $myMixMoney . "  可分红佣金:" . $amount . "  可分红总佣金:" . $fsMoneyNums . "  小区业绩可分红:" . $myMixMoney . " 双区业绩可分红:" . $myWeightMoney . " 实际分得:" . $yj_90,
356 487
                         'create_time' => date('Y-m-d H:i:s')
357 488
                     ];
358
-                    $con_data = Db::name('test_log')->insert($datas_log);   //数据存到2.0数据表中
489
+                    Db::name('test_log')->insert($datas_log);   //数据存到2.0数据表中
359 490
 
360 491
                     # code...
361
-                    $res = Db::name("user")->where("uid", $v)->inc("total_amount_old", $yj_90)->save();
492
+                    Db::name("user")->where("uid", $v)->inc("total_amount_old", $yj_90)->save();
362 493
 
363 494
                     Db::name("user")->where("uid", $v)->inc("amount_old", $yj_90)->save();//不用冻结金额和提现额度来限制佣金发放
364 495
                     Db::name("user")->where("uid", $v)->inc("brokerage_price", $yj_90)->save();
@@ -411,6 +542,12 @@ class TaskPartnerProfits
411 542
             }
412 543
             $salesArr['cs'] = $testRecord;
413 544
         }
545
+        $dividendLogEntity->setResult(json_encode($salesArr, JSON_UNESCAPED_UNICODE));
546
+
547
+        /** @var DividendLogRepository $dividendLogRepository */
548
+        $dividendLogRepository = app()->make(DividendLogRepository::class);
549
+        $dividendLogRepository->createByEntity($dividendLogEntity);
550
+
414 551
         return json($salesArr);
415 552
     }
416 553
 

+ 281 - 0
app/entity/data/dividend/DividendLogEntity.php

@@ -0,0 +1,281 @@
1
+<?php
2
+
3
+namespace app\entity\data\dividend;
4
+
5
+use app\entity\data\DataEntity;
6
+
7
+class DividendLogEntity extends DataEntity
8
+{
9
+    public $id = null;  // 主键
10
+    public $type = null;  // 分润类型:1合伙人分红;2联创分红;3股权分红;4佣金补贴
11
+    public $startTime = null;  // 分润周期开始时间
12
+    public $endTime = null;  // 分润周期结束时间
13
+    public $performance = null;  // 业绩
14
+    public $abatement = null;  // 扣减项(原因)
15
+    public $allocated = null;  // 实际分配业绩
16
+    public $rule = null;  // 分配规则
17
+    public $expected = null;
18
+    public $feeWaiverUids = null;  // 免分配人员ID集合(可能由于业绩不达标)
19
+    public $arrivalPoint = null;  // 到账金额
20
+    public $result = null;  // 实际分配结果
21
+    public $remark = null;  // 备注
22
+    public $createTime = null;  // 创建时间
23
+
24
+    /**
25
+     * @return mixed
26
+     */
27
+    public function getId()
28
+    {
29
+        return $this->id;
30
+    }
31
+
32
+    /**
33
+     * @param mixed $id
34
+     * @return DividendLogEntity
35
+     */
36
+    public function setId($id): DividendLogEntity
37
+    {
38
+        $this->id = $id;
39
+        return $this;
40
+    }
41
+
42
+    /**
43
+     * @return mixed
44
+     */
45
+    public function getType()
46
+    {
47
+        return $this->type;
48
+    }
49
+
50
+    /**
51
+     * @param mixed $type
52
+     * @return DividendLogEntity
53
+     */
54
+    public function setType($type): DividendLogEntity
55
+    {
56
+        $this->type = $type;
57
+        return $this;
58
+    }
59
+
60
+    /**
61
+     * @return mixed
62
+     */
63
+    public function getStartTime()
64
+    {
65
+        return $this->startTime;
66
+    }
67
+
68
+    /**
69
+     * @param mixed $startTime
70
+     * @return DividendLogEntity
71
+     */
72
+    public function setStartTime($startTime): DividendLogEntity
73
+    {
74
+        $this->startTime = $startTime;
75
+        return $this;
76
+    }
77
+
78
+    /**
79
+     * @return mixed
80
+     */
81
+    public function getEndTime()
82
+    {
83
+        return $this->endTime;
84
+    }
85
+
86
+    /**
87
+     * @param mixed $endTime
88
+     * @return DividendLogEntity
89
+     */
90
+    public function setEndTime($endTime): DividendLogEntity
91
+    {
92
+        $this->endTime = $endTime;
93
+        return $this;
94
+    }
95
+
96
+    /**
97
+     * @return mixed
98
+     */
99
+    public function getPerformance()
100
+    {
101
+        return $this->performance;
102
+    }
103
+
104
+    /**
105
+     * @param mixed $performance
106
+     * @return DividendLogEntity
107
+     */
108
+    public function setPerformance($performance): DividendLogEntity
109
+    {
110
+        $this->performance = $performance;
111
+        return $this;
112
+    }
113
+
114
+    /**
115
+     * @return mixed
116
+     */
117
+    public function getAbatement()
118
+    {
119
+        return $this->abatement;
120
+    }
121
+
122
+    /**
123
+     * @param mixed $abatement
124
+     * @return DividendLogEntity
125
+     */
126
+    public function setAbatement($abatement): DividendLogEntity
127
+    {
128
+        $this->abatement = $abatement;
129
+        return $this;
130
+    }
131
+
132
+    /**
133
+     * @return mixed
134
+     */
135
+    public function getAllocated()
136
+    {
137
+        return $this->allocated;
138
+    }
139
+
140
+    /**
141
+     * @param mixed $allocated
142
+     * @return DividendLogEntity
143
+     */
144
+    public function setAllocated($allocated): DividendLogEntity
145
+    {
146
+        $this->allocated = $allocated;
147
+        return $this;
148
+    }
149
+
150
+    /**
151
+     * @return mixed
152
+     */
153
+    public function getRule()
154
+    {
155
+        return $this->rule;
156
+    }
157
+
158
+    /**
159
+     * @param mixed $rule
160
+     * @return DividendLogEntity
161
+     */
162
+    public function setRule($rule): DividendLogEntity
163
+    {
164
+        $this->rule = $rule;
165
+        return $this;
166
+    }
167
+
168
+    /**
169
+     * @return mixed
170
+     */
171
+    public function getExpected()
172
+    {
173
+        return $this->expected;
174
+    }
175
+
176
+    /**
177
+     * @param mixed $expected
178
+     * @return DividendLogEntity
179
+     */
180
+    public function setExpected($expected): DividendLogEntity
181
+    {
182
+        $this->expected = $expected;
183
+        return $this;
184
+    }
185
+
186
+    /**
187
+     * @return mixed
188
+     */
189
+    public function getFeeWaiverUids()
190
+    {
191
+        if (is_string($this->feeWaiverUids)) {
192
+            return json_decode($this->feeWaiverUids, true);
193
+        }
194
+        return $this->feeWaiverUids;
195
+    }
196
+
197
+    /**
198
+     * @param mixed $feeWaiverUids
199
+     * @return DividendLogEntity
200
+     */
201
+    public function setFeeWaiverUids($feeWaiverUids): DividendLogEntity
202
+    {
203
+        if (is_array($feeWaiverUids)) {
204
+            $feeWaiverUids = json_encode($feeWaiverUids);
205
+        }
206
+        $this->feeWaiverUids = $feeWaiverUids;
207
+        return $this;
208
+    }
209
+
210
+    /**
211
+     * @return mixed
212
+     */
213
+    public function getArrivalPoint()
214
+    {
215
+        return $this->arrivalPoint;
216
+    }
217
+
218
+    /**
219
+     * @param mixed $arrivalPoint
220
+     * @return DividendLogEntity
221
+     */
222
+    public function setArrivalPoint($arrivalPoint): DividendLogEntity
223
+    {
224
+        $this->arrivalPoint = $arrivalPoint;
225
+        return $this;
226
+    }
227
+
228
+    /**
229
+     * @return mixed
230
+     */
231
+    public function getResult()
232
+    {
233
+        return $this->result;
234
+    }
235
+
236
+    /**
237
+     * @param mixed $result
238
+     * @return DividendLogEntity
239
+     */
240
+    public function setResult($result): DividendLogEntity
241
+    {
242
+        $this->result = $result;
243
+        return $this;
244
+    }
245
+
246
+    /**
247
+     * @return mixed
248
+     */
249
+    public function getRemark()
250
+    {
251
+        return $this->remark;
252
+    }
253
+
254
+    /**
255
+     * @param mixed $remark
256
+     * @return DividendLogEntity
257
+     */
258
+    public function setRemark($remark): DividendLogEntity
259
+    {
260
+        $this->remark = $remark;
261
+        return $this;
262
+    }
263
+
264
+    /**
265
+     * @return mixed
266
+     */
267
+    public function getCreateTime()
268
+    {
269
+        return $this->createTime;
270
+    }
271
+
272
+    /**
273
+     * @param mixed $createTime
274
+     * @return DividendLogEntity
275
+     */
276
+    public function setCreateTime($createTime): DividendLogEntity
277
+    {
278
+        $this->createTime = $createTime;
279
+        return $this;
280
+    }
281
+}