1], [['limit',], 'default', 'value' => 10], [['status'], 'in', 'range' => [1, 2, 3], 'on' => 'TEAM'], [['status'], 'in', 'range' => [-1, 0, 1, 2, 3], 'on' => 'ORDER'], [['store_id', 'yinbao'],'integer'] ]; } /** * 团队信息 * @return array|mixed */ public function getList() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } $exit = Share::find()->andWhere(['user_id' => $this->user_id, 'is_delete' => 0])->exists(); $user = User::findOne(['id' => $this->user_id]); // $setting = Setting::findOne(['store_id' => $this->store_id]); $setting = Option::get('share_basic_setting', $this->store_id); $setting = $setting ? Json::decode($setting['value']) : []; if ($setting['level']['value'] == 0) { return [ 'code' => 1, 'msg' => '网络异常', 'data' => [] ]; } if (!$exit || $user->is_distributor != 1) { return [ 'code' => 1, 'msg' => '网络异常', 'data' => [] ]; } $team = self::team($this->store_id, $this->user_id); $user_list = $team[1]; $data = $team[0]; if ($setting['level']['value'] > 0 && $this->status == 1) { $data['list'] = $user_list['f_c']; } if ($this->status == 2 && $setting['level']['value'] > 1) { $data['list'] = $user_list['s_c']; } if ($this->status == 3 && $setting['level']['value'] > 2) { $data['list'] = $user_list['t_c']; } foreach ($data['list'] as $index => $value) { $data['list'][$index]['time'] = date('Y-m-d', $value['created_at']); $child_count = User::find()->where(['old_parent_id' => $value['id'], 'is_delete' => 0])->count(); $data['list'][$index]['child_count'] = $child_count ? $child_count : 0; } return [ 'code' => 0, 'msg' => '', 'data' => $data ]; } /** * 获取团队信息 * @param $store_id * @param $user_id * @return array */ public static function team($store_id, $user_id) { // $share_setting = Setting::findOne(['store_id' => $store_id]); $share_setting = Option::get('share_basic_setting', $store_id); $share_setting = $share_setting ? Json::decode($share_setting['value']) : []; $list = User::find()->alias('u') ->where(['and', ['u.is_delete' => 0, 'u.store_id' => $store_id], ['>', 'u.old_parent_id', 0]]) ->leftJoin(Order::tableName() . ' o', "o.is_price=1 and o.user_id=u.id and o.parent_id = u.parent_id") ->andWhere([ 'or', ['o.is_delete' => 0, ['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL]], 'isnull(o.id)' ]) ->select([ "sum(case when isnull(o.id) then 0 else o.pay_price end) price", 'count(o.id) count', 'u.nickname', 'u.created_at', 'u.parent_id', 'u.old_parent_id', 'u.id', 'u.avatar_url' ]) ->groupBy('u.id') ->asArray()->all(); $user_list = array(); $data = []; $data['first'] = 0; $data['second'] = 0; $data['third'] = 0; $data['list'] = []; $user_list['f_c'] = []; $user_list['s_c'] = []; $user_list['t_c'] = []; //获取用户下线的数量及订单情况 foreach ($list as $index => $value) { if ($value['old_parent_id'] == $user_id) { $data['first']++; $user_list['f_c'][] = $value; if ($share_setting['level']['value'] > 1) { foreach ($list as $i => $v) { if ($v['old_parent_id'] == $value['id']) { $data['second']++; $user_list['s_c'][] = $v; if ($share_setting['level']['value'] > 2) { foreach ($list as $j => $item) { if ($item['old_parent_id'] == $v['id']) { $data['third']++; $user_list['t_c'][] = $item; } } } } } } } } return [$data, $user_list]; } /** * 获取分销团队总人数 * @return array */ public function getTeamCount() { $share_basic_setting = Option::get('share_basic_setting', $this->store_id, 'share', '[]'); $share_basic_setting = json_decode($share_basic_setting['value'], true); $share_money_setting = Option::get('share_money_setting', $this->store_id, 'share', '[]'); $share_money_setting = json_decode($share_money_setting['value'], true); $this->share_setting = array_merge($share_basic_setting, $share_money_setting); $team = [ 'f_c' => [], 's_c' => [], 't_c' => [] ]; // if (!$this->share_setting || $this->share_setting['level']['value'] == 0) { // return [ // 'team_count' => 0, // 'team' => $team // ]; // } // if ($this->share_setting['level']['value'] == 4) { // return [ // 'team_count' => 0, // 'team' => $team // ]; // } $first = User::find()->select(['id']) ->where(['store_id' => $this->store_id, 'old_parent_id' => $this->user_id, 'is_delete' => 0, 'type' => 1])->column(); $count = count($first); $team['f_c'] = $first; $second = User::find()->select(['id']) ->where(['store_id' => $this->store_id, 'old_parent_id' => $first, 'is_delete' => 0, 'type' => 1])->column(); $count += count($second); $team['s_c'] = $second; $third = User::find()->select(['id']) ->where(['store_id' => $this->store_id, 'old_parent_id' => $second, 'is_delete' => 0, 'type' => 1])->column(); $count += count($third); $team['t_c'] = $third; return [ 'team_count' => $count, 'team' => $team ]; } /** * 获取订单数量 * @return array * @throws \yii\db\Exception */ public function getOrderCount() { $arr = $this->getTeamCount(); $team_arr = $arr['team']; $sql = $this->getSql(); $order_count = 0; $andWhere_1 = " WHERE al.user_id != {$this->user_id} AND al.parent_id_1 = {$this->user_id} AND al.first_price > 0"; $order_money = 0; $order_money_un = 0; $select_1 = "SELECT al.first_price,al.is_price, al.id"; $first_price = \Yii::$app->db->createCommand($select_1 . $sql . $andWhere_1)->queryAll(); if ($first_price) { foreach ($first_price as $index => $value) { $order_count++; $order_money += doubleval($value['first_price']); if ($value['is_price'] == 0) { $order_money_un += doubleval($value['first_price']); } } } $team_f = '(' . implode(',', $team_arr['f_c']) . ')'; $team_s = '(' . implode(',', $team_arr['s_c']) . ')'; $team_t = '(' . implode(',', $team_arr['t_c']) . ')'; if (!empty($team_arr['s_c'])) { $select_2 = "SELECT al.second_price,al.is_price, al.id"; $andWhere_2 = " WHERE al.user_id != {$this->user_id} AND al.second_price > 0 AND ((al.user_id IN {$team_s} AND al.parent_id_1 IN {$team_f} AND al.parent_id_2 = 0) OR (al.parent_id_2 = {$this->user_id}))"; $second_price = \Yii::$app->db->createCommand($select_2 . $sql . $andWhere_2)->queryAll(); if ($second_price) { foreach ($second_price as $index => $value) { $order_count++; $order_money += doubleval($value['second_price']); if ($value['is_price'] == 0) { $order_money_un += doubleval($value['second_price']); } } } } if (!empty($team_arr['t_c'])) { $select_3 = "SELECT al.third_price,al.is_price, al.id"; $andWhere_3 = " WHERE al.user_id != {$this->user_id} AND al.third_price > 0 AND ((al.user_id IN {$team_t} AND al.parent_id_1 IN {$team_s} AND al.parent_id_2 = 0) OR (al.parent_id_3 = {$this->user_id}))"; $third_price = \Yii::$app->db->createCommand($select_3 . $sql . $andWhere_3)->queryAll(); if ($third_price) { foreach ($third_price as $index => $value) { $order_count++; $order_money += doubleval($value['third_price']); if ($value['is_price'] == 0) { $order_money_un += doubleval($value['third_price']); } } } } $select = "SELECT al.rebate,al.is_price,al.id {$sql} WHERE al.rebate > 0 AND al.user_id = {$this->user_id}"; $rebate = \Yii::$app->db->createCommand($select)->queryAll(); if ($rebate) { foreach ($rebate as $index => $value) { $order_count++; $order_money += doubleval($value['rebate']); if ($value['is_price'] == 0) { $order_money_un += doubleval($value['rebate']); } } } // 因为存在佣金,积分,余额多种发放模式,目前分销中心展示订单数量 $arr['order_count'] = $order_count; $arr['share_order_count'] = UserShareMoney::find()->where(['store_id' => get_store_id(), 'user_id' => $this->user_id, 'source' => 5, 'type' => 0])->count(); $arr['order_money'] = doubleval(sprintf('%.2f', $order_money)); $arr['order_money_un'] = doubleval(sprintf('%.2f', $order_money_un)); return $arr; } /** * 获取分销订单详情 * @return array * @throws \yii\db\Exception */ public function getOrder() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } $arr = $this->getTeamCount(); $team_arr = $arr['team']; $share_setting = Option::get('share_basic_setting', $this->store_id); $share_setting = $share_setting ? Json::decode($share_setting['value']) : []; $first = '(' . implode(',', $team_arr['f_c']) . ')'; $second = '(' . implode(',', $team_arr['s_c']) . ')'; $third = '(' . implode(',', $team_arr['t_c']) . ')'; $all = '(' . implode(',', array_merge($team_arr['f_c'], $team_arr['s_c'], $team_arr['t_c'])) . ')'; $user_table = User::tableName(); $saas_user_table = SaasUser::tableName(); $sql = $this->getSql(); $select = "SELECT al.*,su.name as nickname,su.avatar as avatar_url "; $where = " WHERE ((al.user_id = {$this->user_id} AND al.rebate > 0)"; if ($share_setting['level']['value'] != 4) { $where .= "OR (al.user_id != {$this->user_id} "; if ($share_setting['level']['value'] >= 1) { $where .= " AND (al.parent_id_1 = {$this->user_id} AND al.first_price > 0) "; } if ($share_setting['level']['value'] >= 2) { $where .= " OR (al.parent_id_2 = {$this->user_id} AND al.second_price > 0) "; } if ($share_setting['level']['value'] == 3) { $where .= " OR (al.parent_id_3 = {$this->user_id} AND al.third_price > 0) "; } if (!empty($team_arr['f_c']) && !empty($team_arr['s_c'])) { $where .= " OR (al.parent_id_2 = 0 AND al.user_id IN {$second} AND al.parent_id_1 IN {$first} AND al.second_price > 0) "; } if (!empty($team_arr['s_c']) && !empty($team_arr['t_c'])) { $where .= " OR (al.parent_id_2 = 0 AND al.user_id IN {$third} AND al.parent_id_1 IN {$second} AND al.third_price > 0)"; } $where .= ")"; } $where .= ")"; $sql = $sql . " LEFT JOIN {$user_table} AS u ON u.id=al.user_id LEFT JOIN {$saas_user_table} AS su ON su.mobile=u.binding {$where}"; if(!$this->yinbao){ if ($this->status == 0) { $sql = $sql . " AND al.trade_status = -1 "; } elseif ($this->status == 1) { $sql = $sql . " AND al.is_pay = 1 AND al.trade_status IN (0,2,3) and al.is_price = 0"; } elseif ($this->status == 2) { $sql = $sql . " AND al.trade_status = 2"; } elseif ($this->status == 3) { $sql = $sql . " AND al.trade_status = 3 and is_price = 1"; } } if ($this->child_user_id) { $sql = $sql . " AND al.user_id = " . $this->child_user_id; } $sql = $sql . " GROUP BY al.id ,al.order_type "; $count = \Yii::$app->db->createCommand("select count(*)".$sql)->execute(); $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]); $list = \Yii::$app->db->createCommand($select . $sql . "ORDER BY al.created_at DESC LIMIT {$pagination->limit} OFFSET {$pagination->offset}")->queryAll(); $new_list = []; $setting = Option::get('share_money_setting', $this->store_id); $setting = $setting ? Json::decode($setting['value']) : []; foreach ($list as $index => $value) { $new_list[$index]['status'] = intval($value['trade_status']); $new_list[$index]['share_send_type'] = $value['share_send_type']; $new_list[$index]['order_type'] = $value['order_type']; $new_list[$index]['order_no'] = $value['order_no']; $new_list[$index]['nickname'] = $value['nickname']; $new_list[$index]['avatar_url'] = $value['avatar_url']; $new_list[$index]['is_price'] = $value['is_price']; $new_list[$index]['status_text'] = Order::getStatusText($value['trade_status']);// "待付款"; // if ($value['is_pay'] == 0) { // $new_list[$index]['status'] = "待付款"; // } elseif ($value['is_pay'] == 1 && $value['is_price'] == 0) { // $new_list[$index]['status'] = "已付款"; // } elseif ($value['is_price'] == 1) { // $new_list[$index]['status'] = "已完成"; // } if ($value['parent_id_2'] == 0) { if ($this->user_id == $value['parent_id_1']) { $new_list[$index]['share_status'] = $setting['level_one']['text'] ? $setting['level_one']['text'] : "一级"; $new_list[$index]['share_money'] = $value['first_price']; } elseif (in_array($value['user_id'], $team_arr['s_c']) && in_array($value['parent_id_1'], $team_arr['f_c'])) { $new_list[$index]['share_status'] = $setting['level_two']['text'] ? $setting['level_two']['text'] : "二级"; $new_list[$index]['share_money'] = $value['second_price']; } elseif (in_array($value['user_id'], $team_arr['t_c']) && in_array($value['parent_id_1'], $team_arr['s_c'])) { $new_list[$index]['share_status'] = $setting['level_three']['text'] ? $setting['level_three']['text'] : "三级"; $new_list[$index]['share_money'] = $value['third_price']; } } else { if ($value['parent_id_1'] == $this->user_id) { $new_list[$index]['share_status'] = $setting['level_one']['text'] ? $setting['level_one']['text'] : "一级"; $new_list[$index]['share_money'] = $value['first_price']; } elseif ($value['parent_id_2'] == $this->user_id) { $new_list[$index]['share_status'] = $setting['level_two']['text'] ? $setting['level_two']['text'] : "二级"; $new_list[$index]['share_money'] = $value['second_price']; } elseif ($value['parent_id_3'] == $this->user_id) { $new_list[$index]['share_status'] = $setting['level_three']['text'] ? $setting['level_three']['text'] : "三级"; $new_list[$index]['share_money'] = $value['third_price']; } } if ($value['rebate'] > 0 && $value['user_id'] == $this->user_id) { $new_list[$index]['share_status'] = "自购返现"; $new_list[$index]['share_money'] = $value['rebate']; } // 订单商品详情 $new_list[$index]['orderDetail'] = $this->getOrderDetail($value['id']); $refund = OrderRefund::findOne(['order_id' => $value['id'], 'is_delete' => 0, 'store_id' => $this->store_id, 'type' => 1]); if ($refund) { if ($refund['status'] == 1) { $new_list[$index]['status_text'] = "已退款"; } elseif ($refund['status'] == 0) { $new_list[$index]['status_text'] = '售后申请中'; } } } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'row_count' => $count, 'page_count' => $pagination->pageCount, 'list' => $new_list, ] ]; } /** * 获取分销订单详情2 */ public function getOrder2() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } $arr = $this->getTeamCount(); $team_arr = $arr['team']; $share_setting = Option::get('share_basic_setting', $this->store_id); $share_setting = $share_setting ? Json::decode($share_setting['value']) : []; $first = '(' . implode(',', $team_arr['f_c']) . ')'; $second = '(' . implode(',', $team_arr['s_c']) . ')'; $third = '(' . implode(',', $team_arr['t_c']) . ')'; $user_table = User::tableName(); $saas_user_table = SaasUser::tableName(); $sql = $this->getSql2(); $select = "SELECT al.*,su.name as nickname,su.avatar as avatar_url "; $where = " WHERE ((al.user_id = {$this->user_id} AND al.rebate > 0)"; if ($share_setting['level']['value'] != 4) { $where .= "OR (al.user_id != {$this->user_id} "; if ($share_setting['level']['value'] >= 1) { $where .= " AND ((al.parent_id_1 = {$this->user_id}) "; } if ($share_setting['level']['value'] >= 2) { $where .= " OR (al.parent_id_2 = {$this->user_id}) "; } if ($share_setting['level']['value'] == 3) { $where .= " OR (al.parent_id_3 = {$this->user_id}) "; } if (!empty($team_arr['f_c']) && !empty($team_arr['s_c'])) { $where .= " OR (al.parent_id_2 = 0 AND al.user_id IN {$second} AND al.parent_id_1 IN {$first}) "; } if (!empty($team_arr['s_c']) && !empty($team_arr['t_c'])) { $where .= " OR (al.parent_id_2 = 0 AND al.user_id IN {$third} AND al.parent_id_1 IN {$second})"; } $where .= "))"; } $where .= ")"; $sql = $sql . " LEFT JOIN {$user_table} AS u ON u.id=al.user_id LEFT JOIN {$saas_user_table} AS su ON su.mobile=u.binding {$where}"; if(!$this->yinbao){ if ($this->status == 0) { $sql = $sql . " AND al.is_pay = 0 "; } elseif ($this->status == 1) { $sql = $sql . " AND al.is_pay = 1"; } elseif ($this->status == 2) { $sql = $sql . " AND al.is_price = 1 "; } } $sql = $sql . " GROUP BY al.id ,al.order_type "; $count = \Yii::$app->db->createCommand("select count(*)".$sql)->execute(); $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]); $list = \Yii::$app->db->createCommand($select . $sql . "ORDER BY al.created_at DESC LIMIT {$pagination->limit} OFFSET {$pagination->offset}")->queryAll(); $new_list = []; $setting = Option::get('share_money_setting', $this->store_id); $setting = $setting ? Json::decode($setting['value']) : []; foreach ($list as $index => $value) { $new_list[$index]['id'] = $value['id']; $new_list[$index]['md_id'] = $value['md_id']; $new_list[$index]['share_send_type'] = $value['share_send_type']; $new_list[$index]['order_type'] = $value['order_type']; $new_list[$index]['order_no'] = $value['order_no']; $new_list[$index]['nickname'] = $value['nickname']; $new_list[$index]['avatar_url'] = $value['avatar_url']; $new_list[$index]['is_price'] = $value['is_price']; $new_list[$index]['is_offline'] = $value['is_offline']; $new_list[$index]['status'] = "待付款"; if ($value['is_pay'] == 0) { $new_list[$index]['status'] = "待付款"; } elseif ($value['is_pay'] == 1 && $value['is_price'] == 0) { $new_list[$index]['status'] = "已付款"; } elseif ($value['is_price'] == 1) { $new_list[$index]['status'] = "已完成"; } if ($value['parent_id_2'] == 0) { if ($this->user_id == $value['parent_id_1']) { $new_list[$index]['share_status'] = $setting['level_one']['text'] ? $setting['level_one']['text'] : "一级"; $new_list[$index]['share_money'] = $value['first_price']; } elseif (in_array($value['user_id'], $team_arr['s_c']) && in_array($value['parent_id_1'], $team_arr['f_c'])) { $new_list[$index]['share_status'] = $setting['level_two']['text'] ? $setting['level_two']['text'] : "二级"; $new_list[$index]['share_money'] = $value['second_price']; } elseif (in_array($value['user_id'], $team_arr['t_c']) && in_array($value['parent_id_1'], $team_arr['s_c'])) { $new_list[$index]['share_status'] = $setting['level_three']['text'] ? $setting['level_three']['text'] : "三级"; $new_list[$index]['share_money'] = $value['third_price']; } } else { if ($value['parent_id_1'] == $this->user_id) { $new_list[$index]['share_status'] = $setting['level_one']['text'] ? $setting['level_one']['text'] : "一级"; $new_list[$index]['share_money'] = $value['first_price']; } elseif ($value['parent_id_2'] == $this->user_id) { $new_list[$index]['share_status'] = $setting['level_two']['text'] ? $setting['level_two']['text'] : "二级"; $new_list[$index]['share_money'] = $value['second_price']; } elseif ($value['parent_id_3'] == $this->user_id) { $new_list[$index]['share_status'] = $setting['level_three']['text'] ? $setting['level_three']['text'] : "三级"; $new_list[$index]['share_money'] = $value['third_price']; } } if ($value['rebate'] > 0 && $value['user_id'] == $this->user_id) { $new_list[$index]['share_status'] = "自购返现"; $new_list[$index]['share_money'] = $value['rebate']; } // 订单商品详情 $new_list[$index]['orderDetail'] = $this->getOrderDetail($value['id']); $new_list[$index]['md_info'] = null; if ($value['md_id']) { $new_list[$index]['md_info'] = Md::find()->where(['id' => $value['md_id']]) ->select('id, name, address, cover_url, start_time, end_time, mobile, open_status, shop_time_type, is_single')->one(); } } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'row_count' => $count, 'page_count' => $pagination->pageCount, 'list' => $new_list, ] ]; } // 订单详情--商城 public function getOrderDetail($id) { $list = OrderDetail::find()->alias('od')->where(['od.order_id' => $id]) ->leftJoin(['g' => Goods::tableName()], 'g.id=od.goods_id') ->select(['od.id', 'od.num', 'od.pic goods_pic','od.total_price', 'od.attr', 'g.name', 'g.cover_pic']) ->asArray()->all(); foreach ($list as $index => $value) { if (!$value['goods_pic']) { $list[$index]['goods_pic'] = $value['cover_pic']; } } return $list; } // 订单详情--秒杀 private function getMsOrderDetail($id) { $one = MsOrder::find()->alias('o') ->leftJoin(['g' => MsGoods::tableName()], 'g.id=o.goods_id') ->where(['o.store_id' => $this->store_id, 'o.id' => $id]) ->select(['o.id', 'o.num', 'o.pic goods_pic', 'g.name'])->asArray()->one(); $list[] = $one; return $list; } // 订单详情--拼团 public function getPtOrderDetail($id) { $list = PtOrderDetail::find()->alias('od') ->leftJoin(['g' => PtGoods::tableName()], 'od.goods_id=g.id') ->where(['od.order_id' => $id, 'od.is_delete' => 0]) ->select(['od.id', 'od.num', 'od.pic goods_pic', 'g.name'])->asArray()->all(); return $list; } // 订单详情--预约 public function getYyOrderDetail($id) { $list = YyOrder::find()->alias('o') ->leftJoin(['g' => YyGoods::tableName()], 'o.goods_id=g.id') ->where(['o.id' => $id, 'o.store_id' => $this->store_id]) ->select(['o.id', 'g.cover_pic goods_pic', 'g.name'])->asArray()->all(); foreach ($list as $index => $value) { $list[$index]['num'] = 1; } return $list; } //获取团队详情 public function getTeam() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } $arr = $this->getTeamCount(); $team_arr = $arr['team']; $data = []; $data['first'] = count($team_arr['f_c']); $data['second'] = count($team_arr['s_c']); $data['third'] = count($team_arr['t_c']); if (empty($team_arr['f_c'])) { $data['list'] = []; return [ 'code' => 0, 'msg' => '', 'data' => $data ]; } $first = '(' . implode(',', $team_arr['f_c']) . ')'; $second = '(' . implode(',', $team_arr['s_c']) . ')'; $third = '(' . implode(',', $team_arr['t_c']) . ')'; $user_table = User::tableName(); $sql = $this->getSql(); $select = "( SELECT al.* "; $sql = $select . $sql; $query = "SELECT u.nickname, u.created_at, u.parent_id,u.old_parent_id, u.id, u.avatar_url,count(al.id) count, u.binding, sum(case when isnull(al.id) then 0 else al.pay_price end) price, u.remark_name, u.level FROM {$user_table} AS u "; if ($this->status == 1) { $sql .= " WHERE al.parent_id_1 = {$this->user_id} AND al.is_price = 1 )"; $query .= " LEFT JOIN {$sql} as al ON al.user_id =u.id WHERE u.store_id = {$this->store_id} AND u.id IN {$first} "; } elseif ($this->status == 2) { if (empty($team_arr['s_c'])) { $data['list'] = []; return [ 'code' => 0, 'msg' => '', 'data' => $data ]; } $sql .= " WHERE ( (al.user_id IN {$second} AND al.parent_id_1 IN {$first} AND al.parent_id_2 = 0) OR (al.parent_id_2 = {$this->user_id}) )AND al.is_price = 1)"; $query .= " LEFT JOIN {$sql} as al ON al.user_id =u.id WHERE u.store_id = {$this->store_id} AND u.id IN {$second}"; } else { if (empty($team_arr['t_c']) || empty($team_arr['s_c'])) { $data['list'] = []; return [ 'code' => 0, 'msg' => '', 'data' => $data ]; } $sql .= " WHERE ( ( al.user_id IN {$third} AND al.parent_id_2 IN {$second} AND al.parent_id_2 = 0) OR (al.parent_id_3 = {$this->user_id}) ) AND al.is_price = 1)"; $query .= " LEFT JOIN {$sql} as al ON al.user_id =u.id WHERE u.store_id = {$this->store_id} AND u.id IN {$third}"; } $count = \Yii::$app->db->createCommand($query)->query()->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]); $list = \Yii::$app->db->createCommand($query . " GROUP BY u.id ORDER BY u.created_at DESC LIMIT {$pagination->limit} OFFSET {$pagination->offset}")->queryAll(); foreach ($list as $index => $value) { $saas_user = SaasUser::findOne(['mobile' => $value['binding']]); $list[$index]['nickname'] = $saas_user->name ?: $value['nickname']; $list[$index]['avatar_url'] = $saas_user->avatar ?: $value['avatar_url']; $list[$index]['time'] = date('Y-m-d', $value['created_at']); $child_count = User::find()->where(['old_parent_id' => $value['id'], 'is_delete' => 0])->count(); $list[$index]['child_count'] = $child_count ? $child_count : 0; $level = Level::findOne(['store_id' => $this->store_id, 'level' => $value['level'], 'is_delete' => 0]); $list[$index]['level'] = $level->name ?? '普通用户'; $share = Share::findOne(['user_id' => $value['id'], 'is_delete' => 0]); $list[$index]['share_level'] = $share ? '默认等级' : "普通用户"; if ($share && $share->level > 0) { $shareLevel = ShareLevel::findOne(['level' => $share->level, 'store_id' => $this->store_id, 'is_delete' => 0]); $list[$index]['share_level'] = $shareLevel->name ?? '默认等级'; } } $data['list'] = $list; return [ 'code' => 0, 'msg' => '', 'data' => $data ]; } private function getSql() { $s_table = Order::tableName(); if($this->yinbao){ $s_table = OrderPospal::tableName(); } // $ms_table = MsOrder::tableName(); // $pt_table = PtOrder::tableName(); // $yy_table = YyOrder::tableName(); $order_id = OrderRefund::find()->where(['store_id' => $this->store_id, 'type' => 1, 'status' => 1, 'is_agree' => 1, 'is_delete' => 0])->select('order_id')->column(); $order_id = implode(',', $order_id); $sql_s = "( SELECT 's' AS `order_type`, `id`, `order_no`, `is_pay`, `pay_price`, `is_price`, `user_id`, `apply_delete`, `trade_status`, `share_send_type`, `created_at` AS `created_at`, `old_parent_id` AS `parent_id_1`, `old_parent_id_1` AS `parent_id_2`, `old_parent_id_2` AS `parent_id_3`, `first_price`, `second_price`, `third_price`, `rebate`, `mch_id`, `store_id`, `is_show` FROM {$s_table} WHERE (`trade_status` != 1) AND (`is_delete` = 0) AND (`old_parent_id` > 0 OR `rebate` != 0 OR `limit_id` != 0) AND (store_id = {$this->store_id})"; if ($order_id) { $sql_s .= " AND id NOT IN ({$order_id})"; } $sql_s .= ")"; $sql = " FROM ( SELECT * FROM ( {$sql_s} ) AS `l` WHERE `l`.`store_id` = {$this->store_id} AND `l`.`is_show` = 1 ) AS `al` "; return $sql; } private function getSql2() { $s_table = Order::tableName(); if($this->yinbao){ $s_table = OrderPospal::tableName(); } $sql_s = "( SELECT 's' AS `order_type`, `id`, `order_no`, `is_pay`, `pay_price`, `is_price`, `user_id`, `apply_delete`, `trade_status`, `share_send_type`, `created_at` AS `created_at`, `old_parent_id` AS `parent_id_1`, `old_parent_id_1` AS `parent_id_2`, `old_parent_id_2` AS `parent_id_3`, `first_price`, `second_price`, `third_price`, `rebate`, `mch_id`, `store_id`, `is_offline`, `md_id`, `is_show` FROM {$s_table} WHERE (`trade_status` != 1) AND (`is_delete` = 0) AND (`is_offline` = 1) AND (`old_parent_id` > 0 OR `rebate` != 0 OR `limit_id` != 0) AND (`mch_id` = 0 OR (`mch_id` > 0 )) AND (store_id = {$this->store_id}) )"; $sql = " FROM ( SELECT * FROM ( {$sql_s} ) AS `l` WHERE `l`.`store_id` = {$this->store_id} AND `l`.`is_show` = 1 ) AS `al` "; return $sql; } }