| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace addons\CloudStock\common\logic\reward;
- use addons\CloudStock\common\models\CloudStockBonusPoolLevel;
- use addons\CloudStock\common\models\CloudStockBonusPoolLog;
- use addons\CloudStock\common\models\CloudStockBonusPoolLogDetail;
- use addons\CloudStock\common\models\CloudStockBonusPoolUser;
- use common\enums\AddonsEnum;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\DateHelper;
- use services\common\UserWalletService;
- class PoolReward
- {
- //分红池统计,结算
- public static function index($mall_id,$time,$start=null,$end=null)
- {
- $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
- $is_upgrade_bag_give_goods_pool = $configs['is_upgrade_bag_give_goods_pool'] ?? 0;
- $is_superposition = $configs['is_superposition'] ?? 0;
- $pool_settlement_type = $configs['pool_settlement_type'] ?? 0;
- $day = date('d', $time);
- if ($day != '01') return false;
- switch ($pool_settlement_type) {
- case 0:
- if(!isset($start)||!isset($end)) {
- $res = DateHelper::lastMonth();
- $start = $res['start'];
- $end = $res['end'];
- }
- break;
- case 1:
- $month = date('m', time());
- if (!in_array($month, ['01', '04', '07', '10'])) {
- return false;
- }
- if(!isset($start)||!isset($end)) {
- $res = DateHelper::lastSeason();
- $start = $res['start'];
- $end = $res['end'];
- }
- break;
- }
- if (empty($start) || empty($end)) return false;
- $list_level = CloudStockBonusPoolLevel::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ],
- 'order' => 'level asc',
- ], false);
- if (empty($list_level)) return false;
- $time = time();
- $t = \Yii::$app->db->beginTransaction();
- try {
- $pool_log_id_arr = [];
- foreach ($list_level as $item) {
- $query = CloudStockBonusPoolUser::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
- if ($is_superposition) {
- $query->andWhere(['>=', 'level', $item['level']]);
- } else {
- $query->andWhere(['=', 'level', $item['level']]);
- }
- $query_copy = clone $query;
- $counts = $query->count();
- if (empty($counts)) continue;
- $pool_money = $item['money'];
- $average_money = $item['money'] / $counts;
- $add_data = [
- 'mall_id' => $mall_id,
- 'level' => $item['level'],
- 'start' => $start,
- 'end' => $end,
- 'money' => $pool_money,
- 'dispatch_money' => $pool_money,
- 'people' => $counts,
- 'is_upgrade_bag_give_goods_pool' => $is_upgrade_bag_give_goods_pool,
- 'is_superposition' => $is_superposition,
- 'pool_settlement_type' => $pool_settlement_type,
- ];
- $model = CloudStockBonusPoolLog::create($add_data);
- $pool_log_id_arr[] = $model['id'];
- $item->money = 0;
- $item->save();
- if ($average_money <= 0) continue;
- $field = ['mall_id', 'pool_log_id', 'user_id', 'level', 'money', 'is_settled', 'status', 'created_at', 'updated_at'];
- $i = 1;
- $page_size = 1000;
- while ($user_list = $query_copy->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
- $rows = [];
- foreach ($user_list as $user) {
- $user->pool_income += $average_money;
- $user->save();
- $rows[] = [
- $mall_id,
- $model['id'],
- $user['user_id'],
- $user['level'],
- $average_money,
- 0,
- 1,
- $time,
- $time
- ];
- }
- $res = \Yii::$app->db->createCommand()->batchInsert(CloudStockBonusPoolLogDetail::tableName(), $field, $rows)->execute();
- if ($res == false) throw new \Exception("批量插入奖励出错");
- $i++;
- var_dump($i);
- }
- }
- $t->commit();
- $data = [
- 'mall_id' => $mall_id,
- 'pool_log_id_arr' => $pool_log_id_arr,
- ];
- var_dump($data);
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $data, PoolReward::class, 'sendUserWallet');
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- var_dump("PoolReward::index" . $e->getMessage());
- return false;
- }
- }
- public static function sendUserWallet($params)
- {
- $pool_log_id_arr = $params['pool_log_id_arr'];
- $mall_id = $params['mall_id'];
- $pool_log_list = CloudStockBonusPoolLog::lists(['where' => [['id' => $pool_log_id_arr], ['mall_id' => $mall_id]], 'index' => 'id']);
- $level_arr = array_column($pool_log_list, 'level');
- $level_list = CloudStockBonusPoolLevel::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['level' => $level_arr],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ],
- 'index' => 'level'
- ]);
- $i = 1;
- $page_size = 1000;
- while ($detail_list = CloudStockBonusPoolLogDetail::find()->where(['pool_log_id' => $pool_log_id_arr])->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
- $desc = '';
- $insert_wallet = [];
- $id_arr = [];
- foreach ($detail_list as $detail) {
- $id_arr[] = $detail['id'];
- if (isset($pool_log_list[$detail['pool_log_id']])) {
- $level = $pool_log_list[$detail['pool_log_id']]['level'];
- if (isset($level_list[$level])) {
- $desc = $level_list[$level]['name'] . '分红池分红';
- }
- }
- $insert_wallet[] = [
- 'mall_id' => $detail['mall_id'],
- 'user_id' => $detail['user_id'],
- 'is_frozen' => false,
- 'wallet_type' => 'commission',
- 'money' => $detail['money'],
- 'desc' => $desc,
- 'source_table' => CloudStockBonusPoolLogDetail::tableName(),
- 'source_table_id' => $detail['id'],
- 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK,
- 'capital_type' => 'bouns',
- ];
- }
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if ($res) {
- CloudStockBonusPoolLogDetail::updateAll(['is_settled' => 1], ['id' => $id_arr]);
- }
- $i++;
- }
- }
- public static function inPool($mall_id, $goods_id, $num, $fill_unit_price)
- {
- $list = CloudStockBonusPoolLevel::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED]
- ],
- 'order' => 'level asc'
- ], false);
- if (empty($list)) return false;
- foreach ($list as $item) {
- $goods_setting = json_decode($item['goods_setting'], true);
- if(!is_array($goods_setting)) continue;
- $goods = array_column($goods_setting,null,'goods_id');
- if (!isset($goods[$goods_id])) continue;
- if ($goods[$goods_id]['is_percent'] == 0) {
- $money = bcmul($num, $goods[$goods_id]['money'], 2);
- } else {
- $money = bcdiv($fill_unit_price * $num * $goods[$goods_id]['money'], 100, 2);
- }
- $item->money += $money;
- $item->total_money += $money;
- $item->save();
- }
- }
- }
|