| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
- namespace addons\BusinessBonus\api\modules\v1\controllers;
- use addons\BusinessBonus\common\enums\BusinessBonusEnums;
- use addons\BusinessBonus\common\models\BusinessBonusLevel;
- use addons\BusinessBonus\common\models\BusinessBonusLevelInfo;
- use addons\BusinessBonus\common\models\BusinessBonusReward;
- use addons\BusinessBonus\common\models\BusinessBonusStore;
- use addons\BusinessBonus\common\models\BusinessBonusUpgradeApply;
- use addons\BusinessBonus\common\models\BusinessBonusUpgradeCondition;
- use addons\BusinessBonus\common\models\BusinessBonusUser;
- use addons\Mch\common\models\Mch;
- use addons\Store\common\models\Store;
- use common\enums\StatusEnum;
- use Yii;
- use api\controllers\OnAuthController;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\BusinessBonus\api\modules\v1\controllers
- */
- class DefaultController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index'];
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- return 'Hello world';
- }
- /**
- * 获取招商员信息
- */
- public function actionUserInfo()
- {
- $request = Yii::$app->request;
- if ($request->isPost) {
- $user_info = BusinessBonusUser::getInfo($this->mall_id, $this->user);
- $config = Yii::$app->services->setting->addons->get($this->mall_id, BusinessBonusEnums::ADDONS_NAME, 'basic');
- // 结算周期
- $settle_circle = $config['settle_circle'] ?? BusinessBonusEnums::SETTLE_CIRCLE_DAY;
- // 今天
- $today_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('today'), strtotime('tomorrow') - 1);
- if ($settle_circle == BusinessBonusEnums::SETTLE_CIRCLE_DAY) { // 一天
- // 昨天、前天
- $current_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('yesterday'), strtotime('today') - 1);
- $pre_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('yesterday -1 day'), strtotime('yesterday') - 1);
- $bonus_count = [
- ['title' => '今天', 'value' => $today_count],
- ['title' => '昨天', 'value' => $current_count],
- ['title' => '前天', 'value' => $pre_count]
- ];
- } else if ($settle_circle == BusinessBonusEnums::SETTLE_CIRCLE_WEEK) { // 一周
- // 本周、上周
- $current_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('Monday this week'), time());
- $pre_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('Monday last week'), strtotime('Monday this week') - 1);
- $bonus_count = [
- ['title' => '今天', 'value' => $today_count],
- ['title' => '本周', 'value' => $current_count],
- ['title' => '上周', 'value' => $pre_count]
- ];
- } else { // 一个月
- // 本月、上月
- $current_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('midnight first day of this month'), time());
- $pre_count = BusinessBonusReward::getCustomStatistic($this->mall_id, $this->user_id, strtotime('midnight first day of last month'), strtotime('midnight first day of this month') - 1);
- $bonus_count = [
- ['title' => '今天', 'value' => $today_count],
- ['title' => '本月', 'value' => $current_count],
- ['title' => '上月', 'value' => $pre_count]
- ];
- }
- $user_info['bonus_count'] = $bonus_count;
- return $this->success('招商员信息', $user_info);
- }
- }
- /**
- * 获取申请页面的数据
- */
- public function actionApplyInfo()
- {
- $request = Yii::$app->request;
- if ($request->isPost) {
- // 初级招商员的福利介绍
- $business_bonus_level = BusinessBonusEnums::FIRST_BUSINESS_LEVEL;
- $benefit_intro = BusinessBonusLevelInfo::getBenefitIntro($this->mall_id, $business_bonus_level);
- $level_info = BusinessBonusLevel::getOne([
- ['=', 'mall_id', $this->mall_id],
- ['=', 'level', $business_bonus_level],
- ['>=', 'status', StatusEnum::DISABLED]
- ], true);
- $is_apply = BusinessBonusUpgradeApply::getOne([
- ['=', 'mall_id', $this->mall_id],
- ['=', 'user_id', $this->user_id],
- ['=', 'status', StatusEnum::ENABLED],
- ], true, '', 'id desc');
- $is_applying = 0;
- if ($is_apply && $is_apply['audit_status'] == BusinessBonusEnums::AUDIT_STATUS_CHECKING) {
- $is_applying = 1;
- }
- $info = [
- 'benefit_intro' => $benefit_intro,
- 'level_name' => $level_info['level_name'] ?? '',
- 'bonus_rate' => $level_info['bonus_rate'] ?? 0,
- 'user_id' => $this->user->id,
- 'nickname' => $this->user->nickname,
- 'mobile' => $this->user->mobile,
- 'is_applying' => $is_applying
- ];
- return $this->success('申请成为招商员', $info);
- }
- }
- /**
- * 获取等级说明
- */
- public function actionLevelDesc()
- {
- $request = Yii::$app->request;
- if ($request->isPost) {
- // 初级招商员的福利介绍
- $config = Yii::$app->services->setting->addons->get($this->mall_id, BusinessBonusEnums::ADDONS_NAME, 'basic');
- return $this->success('申请成为招商员', ['level_desc' => $config['level_desc'] ?? '']);
- }
- }
- /**
- * 申请成为招商员
- */
- public function actionApplyBusinessUser()
- {
- $request = Yii::$app->request;
- if ($request->isPost) {
- $res = BusinessBonusUpgradeApply::apply($this->mall_id, $this->user_id);
- if ($res == false) return $this->error(BusinessBonusUpgradeApply::getStaticError());
- return $this->success('提交成功', ['res_code' => $res]);
- }
- }
- /**
- * 购买指定商品成为招商员-等级列表
- */
- public function actionBusinessUserLevel()
- {
- $request = Yii::$app->request;
- if ($request->isPost) {
- $params = $request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['page'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [
- ['=', 'mall_id', $this->mall_id],
- ['=', 'status', StatusEnum::ENABLED],
- ];
- $order = 'level asc';
- $list = BusinessBonusLevel::listPage(compact('where', 'order'), false, true);
- $list['list'] = $list['list'] ?? [];
- $level_array = array_unique(array_filter(array_column($list['list'], 'level')));
- $info_list = BusinessBonusLevelInfo::find()
- ->select('business_bonus_level,benefit_intro')
- ->andWhere(['=', 'status', StatusEnum::ENABLED])
- ->andWhere(['in', 'business_bonus_level', $level_array])
- ->andWhere(['=', 'mall_id', $this->mall_id])
- ->asArray()
- ->all();
- $level_info_data = array_column($info_list, 'benefit_intro', 'business_bonus_level');
- $cond_list = BusinessBonusUpgradeCondition::find()
- ->select('business_bonus_level,goods_id,upgrade_type,is_audit')
- ->andWhere(['=', 'status', StatusEnum::ENABLED])
- ->andWhere(['in', 'business_bonus_level', $level_array])
- ->andWhere(['=', 'mall_id', $this->mall_id])
- ->asArray()
- ->all();
- $cond_data = array_column($cond_list, null, 'business_bonus_level');
- $business_user = BusinessBonusUser::getOne([
- ['=', 'mall_id', $this->mall_id],
- ['>=', 'status', StatusEnum::DISABLED],
- ['=', 'user_id', $this->user_id]
- ], true);
- foreach ($list['list'] as $key => $val) {
- $val['benefit_intro'] = $level_info_data[$val['level']] ?? '';
- $condition = $cond_data[$val['level']] ?? [];
- $val['goods_id'] = $condition['goods_id'] ?? 0;
- $val['goods_id'] = explode(',', $val['goods_id'])[0]; // 防止有多个,目前只有一个
- $val['upgrade_type'] = $condition['upgrade_type'] ?? 0;
- $val['current_level'] = $business_user['business_bonus_level'] ?? 0;
- $list['list'][$key] = $val;
- }
- return $this->success('招商员等级', $list);
- }
- }
- /**
- * 奖励记录
- */
- public function actionRewardList()
- {
- $request = Yii::$app->request;
- if ($request->isPost) {
- $params = $request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['page', 'bonus_status', 'store_id', 'store_type', 'add_order_goods'], 'integer'],
- [['time_value'], 'string']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [
- ['=', 'mall_id', $this->mall_id],
- ['=', 'status', StatusEnum::ENABLED],
- ['=', 'user_id', $this->user_id],
- ];
- if ($params['bonus_status'] !== '' || $params['bonus_status'] !== null) {
- $where[] = ['=', 'bonus_status', $params['bonus_status']];
- }
- if ($params['store_id']) {
- $where[] = ['=', 'store_id', $params['store_id']];
- }
- if ($params['store_type'] == BusinessBonusEnums::STORE_TYPE_STORE) {
- $where[] = ['=', 'from_type', BusinessBonusEnums::FROM_TYPE_STORE];
- } else if ($params['store_type'] == BusinessBonusEnums::STORE_TYPE_MCH) {
- $where[] = ['=', 'from_type', BusinessBonusEnums::FROM_TYPE_MCH];
- }
- if ($params['time_value'] == 'today') { // 今天
- $where[] = ['>=', 'created_at', strtotime('today')];
- $where[] = ['<=', 'created_at', time()];
- } else if ($params['time_value'] == 'one_week') { // 最近1周
- $where[] = ['>=', 'created_at', strtotime('-1 week')];
- $where[] = ['<=', 'created_at', time()];
- } else if ($params['time_value'] == 'two_week') { // 最近2周
- $where[] = ['>=', 'created_at', strtotime('-2 weeks')];
- $where[] = ['<=', 'created_at', time()];
- } else if ($params['time_value'] == 'one_month') { // 最近1个月
- $where[] = ['>=', 'created_at', strtotime('-1 month')];
- $where[] = ['<=', 'created_at', time()];
- } else if ($params['time_value'] == 'three_month') { // 最近3个月
- $where[] = ['>=', 'created_at', strtotime('-3 months')];
- $where[] = ['<=', 'created_at', time()];
- } else if ($params['time_value'] == 'six_month') { // 最近6个月
- $where[] = ['>=', 'created_at', strtotime('-6 months')];
- $where[] = ['<=', 'created_at', time()];
- } else if ($params['time_value'] == 'one_year') { // 最近1年
- $where[] = ['>=', 'created_at', strtotime('-1 year')];
- $where[] = ['<=', 'created_at', time()];
- }
- $order = 'id desc';
- $select = 'id,order_no,settle_amount,bonus_rate,bonus_amount,from_type,store_id,order_id,created_at';
- $list = BusinessBonusReward::listPage(compact('where', 'order', 'select'), false, true);
- $list['list'] = $list['list'] ?? [];
- $list['list'] = BusinessBonusReward::dealRewardList($list['list'], $this->mall_id, $params['add_order_goods']);
- if ($params['page'] == 1) $list['bonus_amount_count'] = BusinessBonusReward::getOne($where, true, '', '', 'sum(bonus_amount) as total_amount')['total_amount'] ?? 0;
- return $this->success('分红记录', $list);
- }
- }
- /**
- * 获取可选门店
- * @return mixed|void
- */
- public function actionStoreList()
- {
- $request = Yii::$app->request;
- if ($request->isPost) {
- $params = $request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['store_type'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $list = BusinessBonusStore::find()
- ->select('id,store_id,store_type')
- ->andWhere(['=', 'mall_id', $this->mall_id])
- ->andWhere(['=', 'business_user_id', $this->user_id])
- ->andWhere(['=', 'status', StatusEnum::ENABLED]);
- if ($params['store_type']) {
- $list = $list
- ->andWhere(['=', 'store_type', $params['store_type']]);
- }
- $list = $list
- ->orderBy('id desc')
- ->asArray()
- ->all();
- $store_ids = $mch_ids = [];
- foreach ($list as $item) {
- if ($item['store_type'] == BusinessBonusEnums::STORE_TYPE_STORE) {
- $store_ids[] = $item['store_id'];
- } else {
- $mch_ids[] = $item['store_id'];
- }
- }
- $store_array = $mch_array = [];
- if ($store_ids) {
- $store_list = Store::find()
- ->select('id,user_id,store_name')
- ->andWhere(['>=', 'status', StatusEnum::DISABLED])
- ->andWhere(['in', 'id', $store_ids])
- ->andWhere(['=', 'mall_id', $this->mall_id])
- ->asArray()
- ->all();
- $store_array = array_column($store_list, null, 'id');
- }
- if ($mch_ids) {
- $mch_list = Mch::find()
- ->select('id,user_id,store_name')
- ->andWhere(['>=', 'status', StatusEnum::DISABLED])
- ->andWhere(['in', 'id', $mch_ids])
- ->andWhere(['=', 'mall_id', $this->mall_id])
- ->asArray()
- ->all();
- $mch_array = array_column($mch_list, null, 'id');
- }
- foreach ($list as $index => $value) {
- $value['store_name'] = '';
- if ($value['store_type'] == BusinessBonusEnums::STORE_TYPE_STORE) {
- $value['store_name'] = $store_array[$value['store_id']]['store_name'] ?? '';
- } else {
- $value['store_name'] = $mch_array[$value['store_id']]['store_name'] ?? '';
- }
- unset($value['id']);
- $list[$index] = $value;
- }
- return $this->success('分红记录', $list);
- }
- }
- }
|