request->isAjax) { if (\Yii::$app->request->isGet) { $get = \Yii::$app->request->get(); $where[] = ['=', 'mall_id', $this->mall_id]; // $where[] = ['=', 'is_wait', AchievementEnum::IS_WAIT_YES]; $where[] = ['=', 'status', StatusEnum::ENABLED]; if (!empty($get['date_start']) && !empty($get['date_end'])) { $where[] = ['between', 'settlement_time', strtotime($get['date_start']), strtotime($get['date_end'])]; } $maxMoney = $get['max_money'] > $get['min_money'] ? $get['max_money'] : $get['min_money']; $minMoney = $get['max_money'] > $get['min_money'] ? $get['min_money'] : $get['max_money']; if (!empty($get['max_money']) && !empty($get['min_money'])) { $where[] = ['between', 'award_money', $minMoney, $maxMoney]; } elseif (!empty($this->max_money)) { $where[] = ['<=', 'award_money', $get['max_money']]; } elseif (!empty($this->min_money)) { $where[] = ['>=', 'award_money', $get['min_money']]; } $params['order'] = 'id desc'; $params['where'] = $where; $params['limit'] = 10; $list = AchievementGrantLog::listPage($params, false); if(!empty($list['list'])){ foreach ($list['list'] as &$item){ $item['award_proportion'] = json_decode($item['award_proportion'],true); $item['award_money'] = $item['award_money'] + $item['equal_money']; } } $sum = AchievementGrantLog::getOne($where, $is_array = true, $with = [], $order = false, $select = 'sum(award_money) as money,sum(user_num) as award_user', $index = ''); $returnData['sum'] = [ 'award_money' => $sum['money'], 'award_user' => $sum['award_user'], 'grant_num' => $list['count'] ]; $returnData["list"] = $list; return $this->success('success', $returnData); } } return $this->render('index', []); } public function actionList() { if (\Yii::$app->request->isAjax) { if (\Yii::$app->request->isGet) { $get = \Yii::$app->request->get(); $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['=', 'status', StatusEnum::ENABLED]; if (!empty($get['date_start']) && !empty($get['date_end'])) { $where[] = ['between', 'created_at', strtotime($get['date_start']), strtotime($get['date_end'])]; } if (!empty($get['grant_id'])) { $where[] = ['=', 'grant_id', $get['grant_id']]; } $is_search = 0; $user_id_arr = []; if (!empty($get['keyword'])) { $is_search = 1; $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [ 'or', ['=', 'id', $get['keyword']], ['like', 'nickname', $get['keyword']], ['like', 'mobile', $get['keyword']] ]], 'select' => 'id']); $user_ids = array_column($user_list, 'id'); if (!empty($user_id_arr)) { $user_id_arr = array_intersect($user_id_arr, $user_ids); } else { $user_id_arr = $user_ids; } } if ($is_search) $where[] = ['in', 'user_id', $user_id_arr]; // if (!empty($get['keyword'])) { // $where[] = ['=', 'user_id', $get['keyword']]; // } $params['order'] = 'id desc'; $params['where'] = $where; $params['with'] = ['grantLog' => function ($query) { // $query->where(['is_wait' => AchievementEnum::IS_WAIT_YES]); }, 'user', 'updateLog']; $params['limit'] = 10; $list = AchievementAwardLog::listPage($params, false); foreach($list['list'] as &$item){ $item['award_money'] = $item['award_money'] + $item['equal_money']; $item['award_sum'] = $item['award_sum'] + $item['equal_money']; $item['edit_award_money'] = $item['award_money']; if ($item['updateLog']) { $item['edit_award_money'] = $item['updateLog']['before_award_money']; } } return $this->success('操作成功', $list); } if (Yii::$app->request->post('flag') === 'EXPORT') { $post = Yii::$app->request->post(); $post['mall_id'] = $this->mall_id; $filename = '业绩奖励明细-' . date('YmdHis') . '_' . rand(10000, 99999); $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_ACHIEVEMENT_AWARD_LOG, AchievementAwardLog::class, 'exportHandle', $post); if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError()); return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!'); } } return $this->render('list', []); } public function actionUpdateGrantAward() { try { if (\Yii::$app->request->isAjax) { if (\Yii::$app->request->isPost) { $params = \Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['id', 'award_money'], 'required'], [['id'], 'integer'], [['award_money'], 'number'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $params['mall_id'] = $this->mall_id; if ($params['award_money'] < 0) { return $this->error('奖励金额要大于0'); } $award = AchievementAwardLog::find() ->where([ 'is_end' => 0, 'is_settlement' => 0, 'status' => StatusEnum::ENABLED, 'id' => $params['id'], 'mall_id' => $this->mall_id, ]) ->with(['updateLog']) ->one(); if (!$award) { return $this->error('该条奖励数据不可修改'); } if ($award['updateLog']) { return $this->error('该条奖励数据已修改过奖励金额'); } $log_info = [ 'mall_id' => $this->mall_id, 'grant_id' => $award->grant_id, 'award_id' => $award->id, 'before_award_money' => $award->award_money, 'after_award_money' => $params['award_money'], ]; $t = \Yii::$app->db->beginTransaction(); //更新奖励明细 $award->award_money = $params['award_money']; if (!$award->save()) throw new \Exception($award->getErrorMessage()); //更新奖励明细更改日志 AchievementAwardLogUpdateLog::setData($log_info); //更新周期统计 $grant = AchievementGrantLog::findOne(['mall_id' => $this->mall_id, 'id' => $award->grant_id, 'status' => 1, 'is_settlement' => 0]); AchievementOrderLogic::updateGrant($grant); $t->commit(); return $this->success('操作成功'); } } } catch (\Exception $exception) { $t->rollBack(); return $this->error($exception->getMessage()); } } public function actionOrder() { if (\Yii::$app->request->isAjax) { if (\Yii::$app->request->isGet) { $get = \Yii::$app->request->get(); $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['=', 'status', StatusEnum::ENABLED]; if (!empty($get['user_id'])) { $where[] = ['=', 'user_id', $get['user_id']]; } if (!empty($get['goods_id'])) { $where[] = ['=', 'goods_id', $get['goods_id']]; } if (!empty($get['order_no'])) { $where[] = ['=', 'order_no', $get['order_no']]; } if (!empty($get['grant_id'])) { $where[] = ['=', 'grant_id', $get['grant_id']]; } $params['order'] = 'id desc'; $params['where'] = $where; $params['with'] = ['goods' => function ($query) { $query->select('id, goods_name')->where(['status' => StatusEnum::ENABLED]); }, 'user' => function ($query) { $query->select('id, nickname')->where(['status' => StatusEnum::ENABLED]); }]; $params['limit'] = 10; $list = AchievementOrderLog::listPage($params, false); //dd($list); $install_store = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE); $store_goods_ids = $store_goods_list = []; foreach ($list['list'] as $k) { if(strpos($k['order_no'],'st') === 0){ $store_goods_ids[] = $k['goods_id']; } } if($install_store && $store_goods_ids){ $store_goods_list = StoreGoods::find()->select('id,goods_name')->where(['id' => $store_goods_ids])->asArray()->all(); $store_goods_list = array_column($store_goods_list,'goods_name','id'); } foreach ($list['list'] as &$k) { if(strpos($k['order_no'],'st') === 0 ){ //门店订单号 unset($k['goods']); if($install_store && $store_goods_list){ $k['goods'] = ['goods_name' => $store_goods_list[$k['goods_id']].'【门店商品】']; }else{ $k['goods'] = ['goods_name' => '【门店商品】']; } } $k['is_cloud_text'] = $k['is_cloud'] == 1 ? "云库存补货" : '前端会员购买'; } return $this->success('操作成功', $list); } } return $this->render('order', []); } public function actionSetting() { $list = []; // 获取权重升级的条件 $upgradeCondition = \Yii::$app->services->setting->addons->get($this->mall_id, AchievementEnum::ADDONS_NAME, 'upgrade_condition.achievement_upgrade_condition', true); if (!empty($upgradeCondition)) { $ids = json_decode($upgradeCondition, true); if(!empty($ids)) { $list = UpgradeCondition::getData($ids); } } $upgrade_condition_data = UpgradeCondition::getUpgradeConditionData($this, $this->mall_id, $list); $group = \Yii::$app->request->get('group'); $group = $group ?: 'basic'; //此处因为历史脏数据 先不读取缓存数据 $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_ACHIEVEMENT, $group, true); if (!$configs) { $params = [ 'upgrade_condition' => [ 'achievement_upgrade_condition' => ["8", "10", "11", "12", "40"], ], 'form' => [ 'status' => 0, 'condition_type' => 0, 'goods_type' => 0, 'buy_goods_type' => 0, 'upgrade_type_goods' => 0, 'upgrade_type_condition' => 0, 'statistical_methods' => 0, 'settlement_cycle' => 0, 'delay_time' => 0, 'is_notice' => 0, 'enable_rules' => 0, 'settlement_mode' => 0, 'is_auto_upgrade' => 0, 'checked_condition_keys' => [], 'checked_condition_values' => [ [ 'key' => '10', 'value' => [ 'direct_user_num' => '', 'direct_user_buy_goods_id' => '', 'direct_user_buy_goods_id_num' => '', ] ], [ 'key' => '11', 'value' => [ 'personal_achievement' => '', 'subordinates_achievement' => '', ] ], [ 'key' => '12', 'value' => [ 'indirect_user_num' => '', 'indirect_user_order_price' => '', ] ], [ 'key' => '40', 'value' => [ 'direct_user_consume' => '', 'direct_user_consume_num' => '', 'team_user_consume' => '', 'team_user_consume_num' => '' ] ], [ 'key' => '95', 'value' => [ 'checked_level' => [], ] ] ], 'award_proportion' => [ 0 => [ 'money' => '0.00', 'proportion' => '0.00', ] ], 'settlement_time' => '', 'goods_ids' => '', 'goods_list' => '', 'rule_rights' => null, 'is_equal' => 0, 'equal_type' => 0, 'equal_percent' => 0, 'equal_price_first' => 0, 'equal_price_second'=> 0, 'team_achievement' => 0, ] ]; \Yii::$app->services->setting->addons->set($this->mall_id, AchievementEnum::ADDONS_NAME, ['basic' => $params['form'], 'upgrade_condition' => $params['upgrade_condition']]); $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_ACHIEVEMENT, $group); } if (\Yii::$app->request->isAjax) { if (\Yii::$app->request->isGet) { if (!empty($configs['award_proportion'])) { $configs['award_proportion'] = json_decode($configs['award_proportion'], true); } else { $configs['award_proportion'] = [[ 'money' => 0, 'proportion' => 0, ]]; } if (!empty($configs['checked_condition_keys'])) { $configs['checked_condition_keys'] = json_decode($configs['checked_condition_keys'], true); } if (!empty($configs['checked_condition_values'])) { $configs['checked_condition_values'] = json_decode($configs['checked_condition_values'], true); $ids = UpgradeCondition::$achievement_condition_ids; $default_list = UpgradeCondition::getData($ids); $checked_condition_values = array_column($configs['checked_condition_values'], null, 'key'); foreach ($default_list as $item) { if (!isset($checked_condition_values[$item['id']]) && $item['id'] != 8) { $value = []; foreach ($item['setting'] as $val) { $value[$val['name']] = ''; } $configs['checked_condition_values'][] = [ 'key' => $item['id'], 'value' => $value, ]; } } } $keyArr = array_column(json_decode($upgrade_condition_data['key_arr']),'key'); $configs['checked_condition_values'] = isset($configs['checked_condition_values']) && is_array($configs['checked_condition_values']) ? $configs['checked_condition_values'] : []; foreach($configs['checked_condition_values'] as $k => $v){ if(!in_array($v['key'],$keyArr)){ unset($configs['checked_condition_values'][$k]); } } usort($configs['checked_condition_values'], function($a, $b) { return intval($a['key']) - intval($b['key']); }); $targetKey = "95"; $index = null; foreach ($configs['checked_condition_values'] as $i => $item) { if (isset($item['key']) && strval($item['key']) === $targetKey) { $index = $i; break; } } if ($index !== null) { if(!isset($configs['checked_condition_values'][$index]['value'])){ $configs['checked_condition_values'][$index]['value'] = ['checked_level' => []]; } } $configs['checked_condition_values'] = array_values($configs['checked_condition_values']); if (!empty($configs['goods_list'])) { $configs['goods_list'] = json_decode($configs['goods_list'], true); } if (!empty($configs['goods_ids'])) { $configs['goods_ids'] = json_decode($configs['goods_ids'], true); } if (!isset($configs['is_show_at_center'])) $configs['is_show_at_center'] = 1; $configs['install_store'] = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE) ? 1:0; $configs['install_mch'] = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_MCH) ? 1:0; if (!isset($configs['first_order'])) $configs['first_order'] = 1; return $this->success('操作成功', $configs); } if (\Yii::$app->request->isPost) { $post = \Yii::$app->request->post(); $rules = [ [['status', 'condition_type', 'goods_type', 'buy_goods_type', 'upgrade_type_goods', 'upgrade_type_condition', 'statistical_methods', 'settlement_cycle', 'delay_time', 'is_notice', 'is_notice', 'enable_rules', 'settlement_mode', 'is_auto_upgrade', 'is_show_at_center', 'is_equal', 'equal_percent', 'equal_type', 'is_leader_first_month_no_award', 'is_award_need_confirm'], 'integer'], [['checked_condition_keys', 'checked_condition_values', 'award_proportion', 'settlement_time', 'goods_ids', 'goods_list'], 'trim'], [['rule_rights'], 'string'], [['status','first_order'], 'in', 'range' => [0, 1]], [['condition_type'], 'in', 'range' => [0, 1, 2]], [['goods_type'], 'in', 'range' => [0, 1, 2]], [['store_order_award','mch_order_award'], 'integer'], [['equal_price_first','equal_price_second', 'team_achievement'], 'number'], ]; $res = \Yii::$app->services->validate->validate($post['form'], $rules); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); if (isset($configs['status'])) { $status = $configs['status']; } else { $status = 0; } \Yii::$app->services->setting->addons->set($this->mall_id, AchievementEnum::ADDONS_NAME, ['basic' => $post['form']]); $logic = new AchievementLogic(); if ($post['form']['status'] == StatusEnum::ENABLED) { $result = AchievementLogic::addGrantLog($this->mall_id, 1); if (!$result) return $this->error('操作失败'); } \Yii::$app->custom->logs('post_form_status:' . $post['form']['status']); if ($post['form']['status'] == StatusEnum::DISABLED) { \Yii::$app->custom->logs('setLogStatus'); $result = $logic->setLogStatus($this->mall_id); if (!$result) return $this->error('操作失败'); } return $this->success('操作成功'); } } return $this->render('setting', $upgrade_condition_data); } /** * @desc:设置升级条件 * @Author: hua * @Date: 2021/10/23 * @Time: 16:27 * @Copyright: copyright (c) 2021 广东七件事集团 * @return mixed|string|void */ public function actionConditionSetting() { if (\Yii::$app->request->isAjax) { try { $request = \Yii::$app->request; if ($request->isAjax) { if ($request->isGet) { $upgrade_condition = UpgradeCondition::getData(UpgradeCondition::$achievement_condition_ids); $data['upgrade_condition'] = $upgrade_condition; $upgrade_condition = \Yii::$app->services->setting->addons->get($this->mall_id, AchievementEnum::ADDONS_NAME, 'upgrade_condition.achievement_upgrade_condition'); $data['checked_condition_keys'] = $upgrade_condition ? json_decode($upgrade_condition, true) : []; return $this->success('操作成功', $data); } elseif ($request->isPost) { $params = \Yii::$app->request->post(); $data['upgrade_condition'] = ['achievement_upgrade_condition' => $params['checked_condition_keys'] ?? []]; \Yii::$app->services->setting->addons->set($this->mall_id, AchievementEnum::ADDONS_NAME, $data); return $this->success('操作成功'); } } } catch (\Exception $e) { return $this->error($e->getMessage(), []); } } else { return $this->render('condition-setting'); } } public function actionUserList() { if (\Yii::$app->request->isAjax) { if (\Yii::$app->request->isGet) { $get = \Yii::$app->request->get(); $get['mall_id'] = $this->mall_id; $params = []; $user_id_arr = []; $is_search = 0; if (!empty($get['user_id'])) { $is_search = 1; $user_id_arr[] = $get['user_id']; } if (!empty($get['keyword'])) { $is_search = 1; $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [ 'or', ['like', 'username', $get['keyword']], ['like', 'nickname', $get['keyword']], ['like', 'mobile', $get['keyword']] ]], 'select' => 'id']); $user_ids = array_column($user_list, 'id'); if (!empty($user_id_arr)) { $user_id_arr = array_intersect($user_id_arr, $user_ids); } else { $user_id_arr = $user_ids; } } if ($is_search) $params['where'][] = ['in', 'user_id', $user_id_arr]; $params['where'][] = ['=', 'mall_id', $this->mall_id]; $params['where'][] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]]; $params['limit'] = 10; $params['order'] = 'id DESC'; $pageData = AchievementUser::listPage($params, false); if ($pageData === false) return $this->error(AchievementUser::getStaticError()); if (!empty($pageData['list'])) { $user_id_arr = array_column($pageData['list'], 'user_id'); $user_list = $user_list = User::lists(['where' => [['id' => $user_id_arr]], 'select' => 'id,nickname,avatar_url,mobile,parent_id']); $user_arr = $parent_arr = []; $parent_id_arr = []; foreach ($user_list as $item) { $parent_id_arr[] = $item['parent_id']; $user_arr[$item['id']] = $item; } $user_parent_list = $user_list = User::lists(['where' => [['id' => $parent_id_arr]], 'select' => 'id,nickname,avatar_url,mobile']); foreach ($user_parent_list as $item) { $parent_arr[$item['id']] = $item; } foreach ($pageData['list'] as &$v) { $v['nickname'] = $v['avatar_url'] = $v['parent_id'] = $v['parent_name'] = $v['mobile'] = ''; if (isset($user_arr[$v['user_id']])) { $v['mobile'] = $user_arr[$v['user_id']]['mobile']; $v['nickname'] = $user_arr[$v['user_id']]['nickname']; $v['avatar_url'] = $user_arr[$v['user_id']]['avatar_url']; $v['parent_id'] = $user_arr[$v['user_id']]['parent_id']; } } } return $this->success('操作成功', $pageData); } } return $this->render('user-list'); } public function actionEdit() { try { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isPost) { $params = Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['user_id'], 'required'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $params['mall_id'] = $this->mall_id; $res = AchievementUser::getOne([['mall_id' => $this->mall_id], ['user_id' => $params['user_id']], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]]); if ($res) return $this->error('该会员已是团长'); $params['is_manual'] = 1; $res = AchievementUser::setData($params); if ($res === false) return $this->error(AchievementUser::getStaticError()); $data = [ 'mall_id' => $this->mall_id, 'user_id' => $params['user_id'], ]; \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE, $data, AchievementLogic::class, 'fillParentTeam'); return $this->success('操作成功'); } } } catch (\Exception $e) { return $this->error($e->getMessage(), []); } return $this->render('edit'); } //不用验证权限 public function actionSearchUser() { if (\Yii::$app->request->isAjax) { $keyword = \Yii::$app->request->get('keyword', ''); $list['list'] = User::searchUser(['keyword' => $keyword, 'mall_id' => $this->mall_id]); return $this->success('操作成功', $list); } return $this->error(); } public function actionDelete() { if (\Yii::$app->request->isAjax) { if (\Yii::$app->request->isPost) { $params = \Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['id'], 'required'], [['id'], 'integer'] ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $params['mall_id'] = $this->mall_id; $params['status'] = StatusEnum::DELETE; $res = AchievementUser::setData($params); if ($res === false) return $this->error(AchievementUser::getStaticError()); $data = []; $event = new AchievementUserDelEvent($this->mall_id); $data['id'] = $params['id']; \Yii::$app->services->events->dispatch($event, $data, $event->listeners); return $this->success('操作成功'); } } return $this->error(); } /** * 获取订单列表 * @DateTime: 2023/04/12 15:53 * @Copyright: copyright (c) 2021 xuan-e * @return mixed|void */ public function actionOrderList() { if (Yii::$app->request->isAjax && Yii::$app->request->isGet) { $params = Yii::$app->request->get(); $params['where'][] = ['mall_id' => $this->mall_id]; $params['where'][] = ['status' => [StatusEnum::ENABLED]]; $params['where'][] = ['grant_id' => $params['grant_id']]; $params['where'][] = ['is_alone_bonus' => [StatusEnum::ENABLED]]; $params['order'] = 'id desc'; $list = AchievementOrderLog::listPage($params); return $this->success('操作成功', $list); } } public function actionTest() { $data = [ 'id' => 65749, ]; $res = (new OrderPayRewardListener())->handleSync($data); print_r($res);exit; } public function actionToGrantAchievement() { try { if (\Yii::$app->request->isAjax) { if (\Yii::$app->request->isPost) { $lock_key = RedisKeyEnum::suffix('lock_grant_achievement'); $identification = uniqid(); if (!LockHelper::lock($lock_key, $identification, 60, 10)) throw new \Exception('结算业绩处理阶段,请勿重复提交'); $params = \Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['id'], 'required'], [['id'], 'integer'] ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $params['mall_id'] = $this->mall_id; $now = time(); $grant = AchievementGrantLog::find() ->where([ 'is_wait' => 1, 'is_settlement' => 0, 'status' => StatusEnum::ENABLED, 'id' => $params['id'], 'mall_id' => $this->mall_id, ]) ->andWhere(['<=', 'settlement_time', $now]) ->asArray() ->one(); if (!$grant) { return $this->error('周期数据不存在'); } //结算业绩 // AchievementLogic::grantAchievement($grant['id']); \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE, $grant, AchievementLogic::class, 'grantAchievement'); //添加审核记录 AchievementGrantConfirmLog::setData([ 'mall_id' => $this->mall_id, 'grant_id' => $grant['id'], 'admin_id' => $this->admin['id'] ]); LockHelper::uLock($lock_key, $identification); return $this->success('操作成功'); } } } catch (\Exception $exception) { return $this->error($exception->getMessage()); } } }