1], [['limit',], 'default', 'value' => 10], [['status'], 'in', 'range' => [1, 2, 3], 'on' => 'TEAM'], [['status'], 'in', 'range' => [-1, 0, 1, 2], 'on' => 'ORDER'], [['store_id'],'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(['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.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.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['parent_id'] == $user_id) { $data['first']++; $user_list['f_c'][] = $value; if ($share_setting['level']['value'] > 1) { foreach ($list as $i => $v) { if ($v['parent_id'] == $value['id']) { $data['second']++; $user_list['s_c'][] = $v; if ($share_setting['level']['value'] > 2) { foreach ($list as $j => $item) { if ($item['parent_id'] == $v['id']) { $data['third']++; $user_list['t_c'][] = $item; } } } } } } } } return [$data, $user_list]; } /** * 获取分销团队总人数 * @return array */ public function getTeamCount() { $team = [ 'f_c' => [], 's_c' => [], 't_c' => [] ]; $first = SaasUser::find()->select(['id'])->where(['parent_id' => $this->saas_id, 'is_delete' => 0])->column(); $first_count = count($first); $count = $first_count; $team['f_c']['list'] = $first; $team['f_c']['count'] = $count; if($first_count > 0){ $second = SaasUser::find()->select(['id'])->where(['parent_id' => $first, 'is_delete' => 0])->column(); $second_count = count($second); $count += $second_count; $team['s_c']['list'] = $second; $team['s_c']['count'] = $second_count; if($second_count > 0){ $third = SaasUser::find()->select(['id'])->where(['parent_id' => $second, 'is_delete' => 0])->column(); $third_count = count($third); $count += $third_count; $team['t_c']['list'] = $third; $team['t_c']['count'] = $third_count; }else{ $team['t_c']['list'] = []; $team['t_c']['count'] = 0; } }else{ $team['s_c']['list'] = []; $team['s_c']['count'] = 0; $team['t_c']['list'] = []; $team['t_c']['count'] = 0; } 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}"; $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.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.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}"; } 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_2 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->status == 0) { $sql = $sql . " AND al.is_pay = 0 "; } elseif ($this->status == 1) { $sql = $sql . " AND al.is_pay = 1 AND al.is_price = 0 "; } 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]['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'] = "待付款"; 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'] = "已退款"; } elseif ($refund['status'] == 0) { $new_list[$index]['status'] = '售后申请中'; } } } 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; } //获取团队详情 public function getTeam() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } $arr = $this->getTeamCount(); if($arr['team_count'] <= 0){ return [ 'code' => 0, 'msg' => '', 'data' => [ 'list'=>[], 'page'=>$this->page, 'totalPage'=>0 ] ]; } $list = []; $page_limit = 10; if($this->level == 1){ if($arr['team']['f_c']['count'] > 0){ //获取开始位置 $start = $this->getStart($arr['team']['f_c']['count'],$page_limit); $list = $this->getLevelSalesInfo(array_slice($arr['team']['f_c']['list'],$start,$page_limit)); $totalPage = ceil($arr['team']['f_c']['count']/$page_limit); } } if($this->level == 2){ if(isset($arr['team']['s_c']['count']) && $arr['team']['s_c']['count'] > 0){ //获取开始位置 $start = $this->getStart($arr['team']['s_c']['count'],$page_limit); $list = $this->getLevelSalesInfo(array_slice($arr['team']['s_c']['list'],$start,$page_limit)); $totalPage = ceil($arr['team']['s_c']['count']/$page_limit); } } if($this->level == 3){ if(isset($arr['team']['t_c']['count']) && $arr['team']['t_c']['count'] > 0){ //获取开始位置 $start = $this->getStart($arr['team']['t_c']['count'],$page_limit); $list = $this->getLevelSalesInfo(array_slice($arr['team']['t_c']['list'],$start,$page_limit)); $totalPage = ceil($arr['team']['t_c']['count']/$page_limit); } } return [ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'list'=>$list, 'page'=>$this->page, 'totalPage'=>$totalPage ] ]; } //获取位置信息 private function getStart($total,$page_limit){ $max_num = ceil($total/$page_limit); if($this->page >= $max_num) $this->page = $max_num; $start = ($this->page - 1) * $page_limit; return $start; } private function getLevelSalesInfo($level_ids){ $orderInfo = Order::find()->where(['saas_id' => $level_ids, 'is_delete' => 0])->select('id,saas_id,total_price')->asArray()->all(); $saas_user_Info = SaasUser::find()->where(['id' => $level_ids, 'is_delete'=>0])->select('id,avatar,created_at,name')->asArray()->all(); $scan_order = \app\plugins\scanCodePay\models\Order::find()->where(['saas_id' => $level_ids, 'is_delete' => 0]) ->select('id,saas_id,total_price')->asArray()->all(); $orderInfo = array_values(array_merge($orderInfo, $scan_order)); $pay_info = []; foreach($orderInfo as $key=>$val){ if(isset($pay_info[$val['saas_id']]['num']) && $pay_info[$val['saas_id']]['num'] > 0){ $pay_info[$val['saas_id']]['num'] += 1; $pay_info[$val['saas_id']]['price'] += number_format($val['total_price'], 2); }else{ $pay_info[$val['saas_id']]['num'] = 1; $pay_info[$val['saas_id']]['price'] = number_format($val['total_price'], 2); } } foreach($saas_user_Info as $key=>$val){ $saas_user_Info[$key]['child_count'] = SaasUser::find()->select(['id'])->where(['parent_id' => $val['id'], 'is_delete' => 0])->count(); $saas_user_Info[$key]['order_num'] = (isset($pay_info[$val['id']]['num']) && $pay_info[$val['id']]['num'] > 0 ) ? $pay_info[$val['id']]['num']:0; $saas_user_Info[$key]['total_price'] = (isset($pay_info[$val['id']]['price']) && $pay_info[$val['id']]['price'] > 0 ) ? number_format($pay_info[$val['id']]['price'],2):0; } return $saas_user_Info; } }