| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- namespace addons\DoubleCopy\common\logic;
- use addons\DoubleCopy\common\enums\DoubleCopyEnum;
- use addons\DoubleCopy\common\models\DoubleCopyPoolLevel;
- use addons\DoubleCopy\common\models\DoubleCopyPoolLog;
- use addons\DoubleCopy\common\models\DoubleCopyPoolSend;
- use addons\DoubleCopy\common\models\DoubleCopyPoolUser;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\helpers\FormatHelper;
- use common\models\mall\Mall;
- use common\traits\ErrorTrait;
- use services\common\UserWalletService;
- use Yii;
- use yii\db\Exception;
- use yii\db\Expression;
- use yii\helpers\Json;
- /**
- * 分红池逻辑处理
- * @Author: lun
- * @DateTime: 2022/9/8 0008 16:10
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @package addons\DoubleCopy\common\logic
- */
- class PoolSeedLogic
- {
- use ErrorTrait;
- /**
- * 执行
- * @Author: lun
- * @DateTime: 2022/9/16 0016 14:54
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return bool
- */
- public function exec()
- {
- try {
- // 获取有效的商城
- $mall_list = Mall::getEnableMallList(true);
- if(!empty($mall_list)){
- foreach ($mall_list as $mall){
- $mall_id = $mall['id'];
- // 获取基础配置
- $config = Yii::$app->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;
- }
- }
- }
|