| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <?php
- namespace app\models;
- use yii\behaviors\TimestampBehavior;
- /**
- * This is the model class for table "{{%team_grades_level}}".
- *
- * @property integer $id
- * @property integer $store_id
- * @property integer $level
- * @property string $level_name
- * @property integer $is_open_apply
- * @property string $level_reward_setting
- * @property integer $expires_type
- * @property integer $expires_in
- * @property string $level_conditions
- * @property string $desc
- * @property string $status
- * @property integer $is_delete
- * @property integer $created_at
- * @property integer $updated_at
- * @property float $tiered_bonus
- */
- class TeamGradesLevel extends \yii\db\ActiveRecord
- {
- //是否开启申请等级
- const IS_OPEN_APPLY_FALSE = 0;
- const IS_OPEN_APPLY_TRUE = 1;
- //状态
- const STATUS_FALSE = 0;
- const STATUS_TRUE = 1;
- const EXPIRES_TYPE_DAY = 0;
- const EXPIRES_TYPE_YEAR = 1;
- //等级有效期类型
- public static $expires_type_list = [
- self::EXPIRES_TYPE_DAY => '天',
- 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;
- }
- }
- }
|