| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- <?php
- namespace addons\BusinessBonus\backend\controllers;
- use addons\BusinessBonus\common\enums\BusinessBonusEnums;
- use addons\BusinessBonus\common\models\BusinessBonusLevel;
- use addons\BusinessBonus\common\models\BusinessBonusLevelInfo;
- use addons\BusinessBonus\common\models\BusinessBonusUpgradeCondition;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\user\UserLevel;
- use Yii;
- use yii\db\Exception;
- class LevelController extends BaseController
- {
- /**
- * 等级列表
- */
- public function actionIndex()
- {
- if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params,[
- [['page'], 'integer'],
- [['bonus_rate'], 'number'],
- [['level_name'], 'string'],
- [['created_at'], 'safe'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [
- ['=', 'mall_id', $this->mall_id],
- ['<>', 'status', StatusEnum::DELETE],
- ];
- if ($params['bonus_rate']) {
- $where[] = ['>=', 'bonus_rate', (float)$params['bonus_rate']];
- }
- if ($params['level_name']) {
- $where[] = ['like', 'level_name', '%' . trim($params['level_name']) . '%', false];
- }
- // 创建时间
- $start_time = strtotime($params['created_at'][0] ?? 0);
- $end_time = strtotime($params['created_at'][1] ?? 0);
- if ($start_time > 0 && $end_time > 0 && $end_time >= $start_time) {
- $where[] = ['>=', 'created_at', $start_time];
- $where[] = ['<=', 'created_at', $end_time];
- }
- $condition = ['where' => $where, 'order' => 'level desc'];
- $page_data = BusinessBonusLevel::listPage($condition, false, true);
- $page_data['list'] = is_array($page_data['list']) ? $page_data['list'] : [];
- $levels = array_column($page_data['list'], 'level');
- $list = BusinessBonusUpgradeCondition::find()
- ->andWhere(['=', 'mall_id', $this->mall_id])
- ->andWhere(['=', 'status', StatusEnum::ENABLED])
- ->andWhere(['in', 'business_bonus_level', $levels])
- ->asArray()
- ->all();
- $goods_ids = [];
- $cond_data = [];
- foreach ($list as $cond) {
- $cond_data[$cond['business_bonus_level']][] = $cond;
- if ($cond['upgrade_type'] == BusinessBonusEnums::UPGRADE_TYPE_SHOPPING) {
- $id = explode(',', $cond['goods_id']);
- if (!$id) {
- continue;
- }
- $goods_ids = array_merge($goods_ids, $id);
- }
- }
- if ($goods_ids) {
- $goods_list = Goods::find()
- ->select('id,goods_name')
- ->andWhere(['=', 'mall_id', $this->mall_id])
- ->andWhere(['in', 'id', $goods_ids])
- ->asArray()
- ->all();
- $goods_data = array_column($goods_list, null, 'id');
- } else {
- $goods_data = [];
- }
- $len = 20;
- foreach ($page_data['list'] as $index => $item) {
- $conditions = $cond_data[$item['level']] ?? [];
- $cond_text_arr = [];
- foreach ($conditions as $c) {
- // if ($c['upgrade_type'] == BusinessBonusEnums::UPGRADE_TYPE_FREE) {
- // $cond_text_arr[] = '无条件';
- // }
- // if ($c['upgrade_type'] == BusinessBonusEnums::UPGRADE_TYPE_APPLY) {
- // $cond_text_arr[] = '前端申请, ' . ($c['is_audit'] == 1 ? '需要审核' : '不需要审核');
- // }
- // if ($c['upgrade_type'] == BusinessBonusEnums::UPGRADE_TYPE_SHOPPING) {
- // $ids = explode(',', $c['goods_id']);
- // $names = [];
- // foreach ($ids as $id) {
- // $goods_name = $goods_data[$id]['goods_name'] ?? '';
- // $names[] = mb_strlen($goods_name) > $len ? mb_substr($goods_name, 0, $len) . '...' : $goods_name;
- // }
- // $cond_text_arr[] = '购买指定商品:' . implode(',', $names);
- // }
- //
- // if ($c['upgrade_type'] == BusinessBonusEnums::UPGRADE_TYPE_STORE_SHOPPING) {
- // $upgrade_store_goods_list = json_decode($c['upgrade_store_goods_list'], true);
- // $names = '任意门店商品';
- //
- // if ($upgrade_store_goods_list['store_goods_type'] == 2 && $upgrade_store_goods_list['store_goods_list']) {
- // $names = array_column($upgrade_store_goods_list['store_goods_list'], 'name');
- // $names = implode(',', $names);
- // }
- //
- // $cond_text_arr[] = '购买门店商品:' . $names;
- // }
- if ($c['is_buy_goods_upgrade']) {
- if ($c['buy_goods_upgrade_type'] == 1) {
- if ($c['buy_goods_upgrade_method'] == 1) {
- $cond_text_arr[] = '购买指定商品:任意商品';
- } else {
- $ids = explode(',', $c['goods_id']);
- $names = [];
- foreach ($ids as $id) {
- $goods_name = $goods_data[$id]['goods_name'] ?? '';
- $names[] = mb_strlen($goods_name) > $len ? mb_substr($goods_name, 0, $len) . '...' : $goods_name;
- }
- $cond_text_arr[] = '购买指定商品:' . implode(',', $names);
- }
- } else {
- $upgrade_store_goods_list = json_decode($c['upgrade_store_goods_list'], true);
- $names = '任意门店商品';
- if ($c['buy_goods_upgrade_method'] == 2 && $upgrade_store_goods_list['store_goods_list']) {
- $names = array_column($upgrade_store_goods_list['store_goods_list'], 'name');
- $names = implode(',', $names);
- }
- $cond_text_arr[] = '购买门店商品:' . $names;
- }
- }
- if ($c['is_condition_upgrade']) {
- $checked_condition_values = json_decode($c['checked_condition_values'], true) ?? [];
- $checked_condition_keys = json_decode($c['checked_condition_keys'], true) ?? [];
- $level = BusinessBonusLevel::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['>=', 'status', StatusEnum::DISABLED]
- ],
- 'index' => 'level',
- 'select' => 'id,level,level_name as name'
- ]);
- foreach ($checked_condition_values as $key => $value) {
- if(in_array($key, $checked_condition_keys)) {
- if ($key == 0 && $value['is_need_conditional'] == 0) {
- $cond_text_arr[] = '无条件';
- }
- if ($key == 1) {
- $cond_text_arr[] = '前端申请, ' . ($value['is_audit'] == 1 ? '需要审核' : '不需要审核');;
- }
- if ($key == 2) {
- $cond_text_arr[] = '直推' . $value['nums'] . '个O2O门店商家';
- }
- if ($key == 3) {
- $level_name = $level[$value['level']]['name'] ?? '';
- $cond_text_arr[] = '直推招商分红'. $level_name .'等级数量' . $value['nums'] . '人';
- }
- }
- }
- }
- }
- $item['cond_context'] = $cond_text_arr ? implode(' | ', $cond_text_arr) : '';
- $page_data['list'][$index] = $item;
- }
- $this->success('获取成功', compact('page_data'));
- } else {
- return $this->render('index');
- }
- }
- /**
- * 招商员等级-表格操作
- */
- public function actionHandle()
- {
- if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id', 'status'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $res = BusinessBonusLevel::setData($params);
- if ($res == false) return $this->error("修改失败: " . BusinessBonusLevel::getStaticError());
- return $this->success("修改成功");
- }
- }
- /**
- * 编辑、新增招商员等级
- */
- public function actionEdit()
- {
- if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id', 'level', 'status', 'created_at', 'created_at','is_buy_goods_upgrade','is_condition_upgrade', 'buy_goods_upgrade_type', 'buy_goods_upgrade_method'], 'integer'],
- [['level_name'], 'string'],
- [['bonus_rate'], 'number'],
- [['condition', 'level_info', 'upgrade_store_goods_list','checked_condition_values', 'checked_condition_keys'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if (!$params['created_at']) unset($params['created_at']);
- $params['mall_id'] = $this->mall_id;
- // 是否重复添加
- $where = [
- ['=', 'mall_id', $this->mall_id],
- ['=', 'level', $params['level']],
- ['>=', 'status', StatusEnum::DISABLED],
- ];
- if (!$params['id']) {
- unset($params['id']);
- } else {
- $where[] = ['<>', 'id', $params['id']];
- }
- $is_repeact = BusinessBonusLevel::getOne($where, true);
- if ($is_repeact) {
- return $this->error('等级权重已使用,请勿重复');
- }
- $tran = Yii::$app->db->beginTransaction();
- try {
- $condition = $params['condition'];
- $condition['checked_condition_values'] = !empty($params['checked_condition_values']) ? json_encode($params['checked_condition_values']) : '[]';
- $condition['checked_condition_keys'] = !empty($params['checked_condition_keys']) ? json_encode($params['checked_condition_keys']) : '[]';
- $condition['status'] = StatusEnum::ENABLED;
- $condition['is_buy_goods_upgrade'] = $params['is_buy_goods_upgrade'];
- $condition['is_condition_upgrade'] = $params['is_condition_upgrade'];
- $condition['buy_goods_upgrade_type'] = $params['buy_goods_upgrade_type'];
- $condition['buy_goods_upgrade_method'] = $params['buy_goods_upgrade_method'];
- $condition['mall_id'] = $this->mall_id;
- $condition['goods_buy_number'] = 1;
- $condition['business_bonus_level'] = $params['level'];
- $goods_list = $condition['goods_list'] && is_array($condition['goods_list']) ? $condition['goods_list'] : [];
- // if ($goods_list && $condition['upgrade_type'] == 3) {
- if ($goods_list && $condition['is_buy_goods_upgrade'] == 1 && $condition['buy_goods_upgrade_type'] == 1) {
- $goods_ids = array_column($goods_list, 'goods_id');
- $goods_ids = array_unique($goods_ids);
- $list = BusinessBonusUpgradeCondition::find()
- ->select('goods_id,business_bonus_level')
- ->andWhere(['=', 'mall_id', $this->mall_id])
- ->andWhere(['=', 'upgrade_type', BusinessBonusEnums::UPGRADE_TYPE_SHOPPING])
- ->andWhere(['=', 'status', StatusEnum::ENABLED])
- ->andWhere(['<>', 'business_bonus_level', $params['level']])
- ->asArray()
- ->all();
- if ($list) {
- $goods_id_array = explode(',', implode(',', array_column($list, 'goods_id')));
- $result = array_intersect($goods_ids, $goods_id_array);
- if ($result) throw new Exception('该商品已被其他等级指定!请重新选择');
- }
- $condition['goods_id'] = implode(',', $goods_ids);
- } else {
- $condition['goods_id'] = '';
- }
- // if ($condition['upgrade_type'] == 3 && !$condition['goods_id']) {
- // throw new Exception('请选择指定商品!');
- // }
- // if ($condition['upgrade_type'] == 6 || isset($params['upgrade_store_goods_list'])) {
- if($condition['buy_goods_upgrade_method'] == 1) {
- $params['upgrade_store_goods_list']['store_goods_ids'] = [];
- $params['upgrade_store_goods_list']['store_goods_list'] = [];
- }
- $condition['upgrade_store_goods_list'] = json_encode($params['upgrade_store_goods_list']);
- BusinessBonusUpgradeCondition::updateAll(['status' => StatusEnum::DISABLED], ['mall_id' => $this->mall_id, 'business_bonus_level' => $params['level']]);
- $res = BusinessBonusUpgradeCondition::setData($condition);
- if ($res == false) throw new Exception(BusinessBonusUpgradeCondition::getStaticError());
- $level_info = $params['level_info'];
- $level_info['status'] = StatusEnum::ENABLED;
- $level_info['mall_id'] = $this->mall_id;
- $level_info['business_bonus_level'] = $params['level'];
- BusinessBonusLevelInfo::updateAll(['status' => StatusEnum::DISABLED], ['mall_id' => $this->mall_id, 'business_bonus_level' => $params['level']]);
- $res = BusinessBonusLevelInfo::setData($level_info);
- if ($res == false) throw new Exception(BusinessBonusLevelInfo::getStaticError());
- unset($params['condition'], $params['level_info']);
- $res = BusinessBonusLevel::setData($params);
- if ($res == false) throw new Exception(BusinessBonusLevel::getStaticError());
- $tran->commit();
- } catch (Exception $e) {
- $tran->rollBack();
- return $this->error("保存失败: " . $e->getMessage());
- }
- return $this->success("保存成功");
- } else {
- return $this->render('edit');
- }
- }
- /**
- * 获取编辑招商员等级的信息
- */
- public function actionInfo()
- {
- if (Yii::$app->request->isAjax) {
- $params = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['inc_id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $info = BusinessBonusLevel::getOne([
- ['id' => $params['inc_id']],
- ['mall_id' => $this->mall_id]
- ], true);
- if ($info) {
- $info['condition'] = BusinessBonusUpgradeCondition::getLevelCondition($this->mall_id, $info['level']);
- if (!$info['condition']) {
- // 默认值,防止前端报错
- $info['condition'] = [
- 'upgrade_type' => null,
- 'is_audit' => 1,
- 'goods_list' => [],
- 'is_buy_goods_upgrade' => 0,
- 'is_condition_upgrade' => 0,
- 'checked_condition_values' => [
- ['key' => 0, 'value' => [], 'is_need_conditional' => 0, 'is_inviter' => 1],
- ['key' => 1, 'value' => [], "is_audit" => 0],
- ['key' => 2, 'value' => [], "nums" => 0],
- ['key' => 3, 'value' => [], "nums" => 0, 'level' => null]
- ],
- 'checked_condition_keys' => [],
- 'buy_goods_upgrade_type' => 1
- ];
- }
- $info['level_info']['benefit_intro'] = BusinessBonusLevelInfo::getBenefitIntro($this->mall_id, $info['level']);
- }
- $is_null = $info ? 0 : 1;
- $goods_ids = [];
- if(!empty($info['condition']['goods_list'])){
- $goods_ids = array_column($info['condition']['goods_list'],'goods_id');
- }
- if ($info['condition']['upgrade_store_goods_list']) {
- !empty($info['condition']['upgrade_store_goods_list']) && $info['condition']['upgrade_store_goods_list'] = json_decode($info['condition']['upgrade_store_goods_list'], true);
- } else {
- $info['condition']['upgrade_store_goods_list'] = [];
- }
- // $info['condition']['upgrade_store_goods_list']['store_goods_type'] = $info['condition']['upgrade_store_goods_list']['store_goods_type'] ?? 1;
- $info['condition']['upgrade_store_goods_list']['store_goods_ids'] = $info['condition']['upgrade_store_goods_list']['store_goods_ids'] ?? [];
- $info['condition']['upgrade_store_goods_list']['store_goods_list'] = $info['condition']['upgrade_store_goods_list']['store_goods_list'] ?? [];
- if ($info['condition']['checked_condition_values']) {
- $info['condition']['checked_condition_values'] = json_decode($info['condition']['checked_condition_values'], true);
- }
- if ($info['condition']['checked_condition_keys']) {
- $info['condition']['checked_condition_keys'] = json_decode($info['condition']['checked_condition_keys'], true);
- }
- return $this->success("保存成功", compact('info', 'is_null','goods_ids'));
- }
- }
- /**
- * 获取权重列表
- */
- public function actionWeights()
- {
- if (Yii::$app->request->isAjax) {
- $level_weights = UserLevel::getLevelList($this->mall_id);
- $level = BusinessBonusLevel::getOne([
- ['mall_id' => $this->mall_id],
- ['>=', 'status', StatusEnum::DISABLED]
- ]);
- // 等级是否为空,没有等级时,添加的第一个等级只能是权重1
- $is_level_null = $level ? 0 : 1;
- return $this->success("保存成功", compact('level_weights', 'is_level_null'));
- }
- }
- public function actionLevels()
- {
- if (Yii::$app->request->isAjax) {
- $level = BusinessBonusLevel::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['>=', 'status', StatusEnum::DISABLED]
- ],
- 'select' => 'id,level,level_name as name'
- ]);
- return $this->success("保存成功", $level);
- }
- }
- }
|