services->setting->addons->get($mall_id, DoubleCopyEnum::ADDONS_NAME, 'basic'); if (!empty($config['is_open_pool']) && $config['is_open_pool'] == StatusEnum::ENABLED) { $continue = false; // 获取结算周期,0=天,1=周,2=月 $cycle_type = $config['cycle_type']; switch ($cycle_type) { case 1: if (date('w') != 1) $continue = true;// 非周一直接退出 break; case 2: if (date('d') != 1) $continue = true;// 非当月1号直接退出 break; } if ($continue) continue; $res = $this->send($mall_id, $cycle_type); if($res === false) throw new Exception($this->getError()); //return true; } } } return true; } catch (\Exception $e) { $this->setError($e->getMessage()); return false; } } /** * 分红发放处理 * @Author: lun * @DateTime: 2022/9/19 0019 17:26 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $mall_id * @param $cycle_type * @return bool */ public function send($mall_id, $cycle_type, $is_test = false) { $trans = Yii::$app->db->beginTransaction(); try { $time = time(); $date = date('Ymd', $time); $config = Yii::$app->services->setting->addons->get($mall_id, DoubleCopyEnum::ADDONS_NAME, 'basic'); $reward_currency = $config['reward_currency'] ?? 'commission'; // 获取等级 if($reward_currency == 'commission'){ $level_list = DoubleCopyPoolLevel::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->andWhere(['>', 'current_money' , 0])->asArray()->all(); }else{ $level_list = DoubleCopyPoolLevel::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])->andWhere(['>', 'current_score' , 0])->asArray()->all(); } foreach ($level_list as $item) { // 检查今天是否已发放过 $is_send = DoubleCopyPoolSend::find()->where(['mall_id' => $mall_id, 'level' => $item['level'], 'date' => $date, 'status' => StatusEnum::ENABLED])->asArray()->one(); if ($is_test == false && $is_send) continue; // 统计当前等级的总人数 $user_arr = DoubleCopyPoolUser::find()->select('id,user_id,level')->where(['mall_id' => $mall_id, 'level' => $item['level'], 'status' => StatusEnum::ENABLED])->asArray()->all(); $t_user_arr = []; if (!empty($user_arr)) { foreach ($user_arr as $t_item) { $t_user_arr[$t_item['user_id']] = $t_item; } } foreach ($level_list as $tl_item) { if (!empty($tl_item['is_enable_extra_pool']) && !empty($tl_item['extra_pool_level'])) { $extra_pool_level = Json::decode($tl_item['extra_pool_level']); if (!empty($extra_pool_level) && in_array($item['level'], $extra_pool_level)) { $tt_user_arr = DoubleCopyPoolUser::find()->select('id,user_id,level') ->where(['mall_id' => $mall_id, 'level' => $tl_item['level'], 'status' => StatusEnum::ENABLED])->asArray()->all(); if (!empty($tt_user_arr)) { foreach ($tt_user_arr as $tt_item) { // 执行去重 if (empty($t_user_arr[$tt_item['user_id']])) $t_user_arr[$tt_item['user_id']] = $tt_item; } } } } } $user_arr = $t_user_arr; $bonus_num = count($user_arr); if ($bonus_num <= 0) continue; $current_money = $reward_currency == 'commission' ? $item['current_money'] : $item['current_score']; // 计算该等级每人可得分红金额,金额不得小于一分钱 $bonus = sprintf("%.2f",substr(sprintf("%.3f", ($current_money / $bonus_num)), 0, -1));// 不四舍五入保留两位小数 if ($bonus < 0.01) continue; $send_total_money = $bonus * $bonus_num;// 已分红金额 // 修改该等级分红池剩余金额 if($reward_currency == 'commission'){ $res = DoubleCopyPoolLevel::updateAll([ 'current_money' => new Expression('`current_money`-'.$send_total_money), 'updated_at' => $time ], ['id' => $item['id']]); }else{ $res = DoubleCopyPoolLevel::updateAll([ 'current_score' => new Expression('`current_score`-'.$send_total_money), 'updated_at' => $time ], ['id' => $item['id']]); } if (!$res) throw new Exception('修改分红池当前金额数据失败'); // 生成该等级发送记录 $send = [ 'mall_id' => $mall_id, 'level' => $item['level'], 'total_bonus_money' => $reward_currency == 'commission' ? $item['current_money'] : $item['current_score'], 'bonus_num' => $bonus_num, 'bonus_money' => $bonus, 'cycle_type' => $cycle_type, 'date' => $date, 'reward_currency' => $reward_currency ]; $resp = DoubleCopyPoolSend::create($send); if ($resp === false) throw new \Exception(DoubleCopyPoolSend::getStaticError()); // 生成用户明细 foreach ($user_arr as $user) { // 修改用户分红池收入 if($reward_currency == 'commission'){ $res = DoubleCopyPoolUser::updateAll([ 'pool_income' => new Expression('`pool_income`+'.$bonus), 'updated_at' => $time ], ['id' => $user['id']]); }else{ $res = DoubleCopyPoolUser::updateAll([ 'pool_income_score' => new Expression('`pool_income_score`+'.$bonus), 'updated_at' => $time ], ['id' => $user['id']]); } if (!$res) throw new Exception('修改用户分红池收入数据失败'); $insert_wallet[] = [ 'mall_id' => $mall_id, 'user_id' => $user['user_id'], 'is_frozen' => false, 'wallet_type' => $reward_currency, 'money' => $bonus, 'desc' => '分红池发放', 'source_table' => 'addons_double_copy_pool_send', 'source_table_id' => $resp['id'], 'from_type' => DoubleCopyEnum::ADDONS_NAME, 'capital_type' => UserWalletEnum::BOUNS, ]; $rows[] = [ 'send_id' => $resp['id'], 'mall_id' => $mall_id, 'user_id' => $user['user_id'], 'level' => $item['level'], 'total_bonus_money' => $reward_currency == 'commission' ? $item['current_money'] : $item['current_score'], 'bonus_num' => $bonus_num, 'bonus_money' => $bonus, 'created_at' => $time, 'updated_at' => $time, 'reward_currency' => $reward_currency ]; } } // 插入个人分红记录 if (!empty($rows)) { $field = ['send_id','mall_id','user_id','level','total_bonus_money','bonus_num','bonus_money','created_at','updated_at','reward_currency']; $res = Yii::$app->db->createCommand()->batchInsert(DoubleCopyPoolLog::tableName(), $field, $rows)->execute(); if ($res == false) throw new Exception("批量插入奖励出错"); } $trans->commit(); if (!empty($insert_wallet)) { $res = UserWalletService::batchHandleByQueue($insert_wallet); if (empty($res)) throw new \Exception('插入钱包队列:' . UserWalletService::getStaticError()); } return true; } catch (\Exception $e) { $trans->rollBack(); $exception = FormatHelper::exception($e, "{$mall_id} 二二复制分红池发放"); Yii::error($exception); return false; } } }