'天', self::EXPIRES_TYPE_YEAR => '年' ]; /** * {@inheritdoc} */ public static function tableName() { return '{{%team_grades_level}}'; } /** * @inheritdoc */ public function rules() { return [ [['id', 'level', 'is_open_apply', 'expires_type', 'expires_in', 'is_delete', 'created_at', 'updated_at', 'store_id', 'status'], 'integer'], [['level_name', 'level_reward_setting', 'level_conditions', 'desc'], 'string'], [['tiered_bonus'], 'number'] ]; } public function behaviors() { return [ [ 'class' => TimestampBehavior::class ] ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => '', 'store_id' => '商城ID', 'level' => '团队等级', 'level_name' => '等级名称', 'is_open_apply' => '是否需要申请', 'level_reward_setting' => '业绩奖励设置', 'expires_type' => '等级有效期类型0天 1年', 'expires_in' => '等级有效期', 'level_conditions' => '升级条件', 'desc' => '特权说明', 'status' => '状态', 'is_delete' => '', 'created_at' => '', 'updated_at' => '', 'tiered_bonus' => '级差奖励' ]; } public static function getLevelInfo($level, $store_id, $field = null) { $model = self::findOne(['level' => $level, 'store_id' => $store_id, 'is_delete' => 0]); if ($field) { return $model->$field; } return $model; } public static function getLevelList($store_id) { return self::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'status' => self::STATUS_TRUE])->select('level, level_name, is_open_apply')->orderBy('level ASC')->asArray()->all(); } /** * 检测是否可以升级 * @param $user_id * @param $store_id * @return bool */ public static function getIsApply($next_level_info, $user_id, $store_id) { try { $is_apply = true; $team_grades_level_info_conditions = json_decode($next_level_info['level_conditions'], true); if (!empty($team_grades_level_info_conditions)) { foreach ($team_grades_level_info_conditions as $level_conditions_index => &$level_conditions_item) { $level_conditions_item['is_open'] = intval($level_conditions_item['is_open']); if (intval($level_conditions_item['is_open'])) { //获取团队人数 $man_data = OldUserTreePath::find()->where([ 'parent_id' => $user_id ])->andWhere([ '!=', 'child_id', $user_id ])->asArray()->all(); $child_id = array_column($man_data, 'child_id'); //判断团队人数以及消费金额 if ($level_conditions_index === 'man') { foreach ($level_conditions_item['value'] as $man_item) { // 团队人数是否达到条件 if (count($child_id) < $man_item['count']) { //未达到条件 返回false $is_apply = false; continue; } // 团队人数消费金额是否达到条件 $pay_price = Order::find()->where( [ 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $child_id ]) ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price'); if ($pay_price < $man_item['money']) { $is_apply = false; continue; } } } //判断团队消费金额 if ($level_conditions_index === 'order') { if (is_array($level_conditions_item['value'])) { foreach ($level_conditions_item['value'] as $order_item) { $order_money = Order::find()->where( [ 'store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM ]) ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]]) ->andWhere(['in', 'user_id', $child_id])->sum('pay_price'); if ($order_money < $order_item['count']) { $is_apply = false; continue; } } } else { $order_money = Order::find()->where( [ 'store_id' => $store_id, 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM ]) ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]]) ->andWhere(['in', 'user_id', $child_id])->sum('pay_price'); if ($order_money < $level_conditions_item['value']) { $is_apply = false; continue; } } } //判断充值金额 if ($level_conditions_index === 'recharge') { $recharge_money = ReOrder::find()->where(['user_id' => $user_id, 'is_pay' => 1])->sum('pay_price'); if ($recharge_money < $level_conditions_item['value']) { $is_apply = false; continue; } } //判断自身消费金额 if ($level_conditions_index === 'self') { if (intval($level_conditions_item['value']['order_number'])) { // 订单单笔金额 $order = Order::find()->where( [ 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $user_id ]) ->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]]) ->andWhere(['>=' , 'pay_price', $level_conditions_item['value']['price']])->one(); if (empty($order)) { $is_apply = false; continue; } } else { // 订单累计金额 $order_money = Order::find()->where( [ 'is_delete' => 0, 'trade_status' => Order::ORDER_FLOW_CONFIRM, 'user_id' => $user_id ])->andWhere(['or', ['is_pay' => 1], ['pay_type' => 2]])->sum('pay_price'); if ($order_money < $level_conditions_item['value']['price']) { $is_apply = false; continue; } } } //判断是否购买指定商品 if ($level_conditions_index === 'goods') { $level_conditions_item['value_finished'] = empty($level_conditions_item['value']); if (!$level_conditions_item['value_finished']) { // 查找该用户是否买过此商品 $goods_model = Order::find()->alias('o') ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id') ->where( [ 'o.user_id' => $user_id, 'o.is_delete' => 0, 'o.trade_status' => Order::ORDER_FLOW_CONFIRM ]) ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]]) ->andWhere(['od.goods_id' => $level_conditions_item['value']['id']])->one(); if (empty($goods_model)) { $is_apply = false; } } } } } } return $is_apply; } catch (\Exception $e) { return false; } } //自动升级 public static function auto_upgrade($user_id, $store_id) { $transaction = \Yii::$app->db->beginTransaction(); try { $userTeamGrades = TeamGrades::getUserTeamGrades($user_id); //获取下一等级信息 $next_level_info = TeamGradesLevel::find()->where([ 'store_id' => $store_id, 'status' => TeamGradesLevel::STATUS_TRUE, 'is_delete' => 0 ])->andWhere(['>', 'level', $userTeamGrades['team_grades_level']])->select('id, level, level_name, level_conditions, is_open_apply') ->orderBy('level ASC')->asArray()->one(); if (empty($next_level_info)) { throw new \Exception('下一等级未查询到'); } if (intval($next_level_info['is_open_apply'])) { throw new \Exception('当前等级未开启自动升级'); } if (!self::getIsApply($next_level_info, $user_id, $store_id)) { throw new \Exception('未达到升级条件'); } $teamGrades = TeamGrades::findOne($userTeamGrades['id']); if (!$teamGrades) { $user = User::findOne($user_id); if ($user) { $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]); $teamGrades = new TeamGrades(); $teamGrades->user_id = $user_id; $teamGrades->store_id = $store_id; $teamGrades->name = $saas_user->name; $teamGrades->mobile = $saas_user->mobile; $teamGrades->team_grades_level = $next_level_info['level']; $teamGrades->province_id = 0; $teamGrades->city_id = 0; $teamGrades->district_id = 0; if (!$teamGrades->save()) { throw new \Exception(json_encode($teamGrades->errors, JSON_UNESCAPED_UNICODE)); } } } else { $teamGrades->team_grades_level = $next_level_info['level']; if (!$teamGrades->save()) { throw new \Exception(json_encode($teamGrades, JSON_UNESCAPED_UNICODE)); }; } $transaction->commit(); return true; } catch (\Exception $e) { debug_log(['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()],'team_grades_auto_upgrade_error.log'); $transaction->rollback(); return false; } } }