| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Goods;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\OrderRefund;
- use app\models\OrderShare;
- use app\models\SaasUser;
- use app\models\Share;
- use app\models\ShareLevel;
- use app\models\Shop;
- use app\models\User;
- use app\models\StatisticForm;
- use app\models\UserBindLog;
- use app\models\UserShareMoney;
- use app\utils\Export;
- use app\utils\ExportList;
- use yii\base\Model;
- use yii\console\widgets\Table;
- use yii\data\Pagination;
- use yii\db\Query;
- use yii\helpers\VarDumper;
- use app\models\OrderPospal;
- use app\models\OrderPospalDetail;
- class ShareOrderForm extends Model
- {
- public $yinbao = 0;
- public $store_id;
- public $user_id;
- public $keyword;
- public $status;
- public $is_offline;
- public $clerk_id;
- public $parent_id;
- public $shop_id;
- public $id;
- public $order_type;
- public $platform;//所属平台
- public $comments; //用户备注
- public $dateStart;
- public $dateEnd;
- public $type;//分销下级等级
- public $flag;
- public $fields = [
- [
- 'key' => 'order_no',
- 'value' => '订单编号',
- 'hidden' => false,
- 'selected' => 1,
- 'type' => [0, 1],
- ],
- [
- 'key' => 'nickname',
- 'value' => '下单用户',
- 'hidden' => false,
- 'selected' => 1,
- 'type' => [0, 1],
- ],
- [
- 'key' => 'total_price',
- 'value' => '总金额',
- 'hidden' => false,
- 'selected' => 1,
- 'type' => [0],
- ],
- [
- 'key' => 'pay_price',
- 'value' => '实际付款',
- 'hidden' => false,
- 'selected' => 1,
- 'type' => [0],
- ],
- [
- 'key' => 'created_at',
- 'value' => '下单时间',
- 'hidden' => false,
- 'selected' => 1,
- 'type' => [0],
- ],
- [
- 'key' => 'order_status',
- 'value' => '订单状态',
- 'hidden' => true,
- 'selected' => 1,
- 'type' => [0],
- ],
- [
- 'key' => 'rebate',
- 'value' => '自购返利',
- 'hidden' => false,
- 'selected' => 1,
- 'order_type' => [5],
- 'type' => [0, 1],
- ],
- [
- 'key' => 'share_commission_first',
- 'value' => '一级分销商',
- 'hidden' => false,
- 'selected' => 1,
- 'order_type' => [5],
- 'type' => [0, 1],
- ],
- [
- 'key' => 'share_commission_second',
- 'value' => '二级分销商',
- 'hidden' => false,
- 'selected' => 1,
- 'order_type' => [5],
- 'type' => [0, 1],
- ],
- [
- 'key' => 'share_commission_third',
- 'value' => '三级分销商',
- 'hidden' => false,
- 'selected' => 1,
- 'order_type' => [5],
- 'type' => [0, 1],
- ],
- ];
- public function rules()
- {
- return [
- [['keyword', 'order_type', 'flag'], 'trim'],
- [['fields', 'yinbao'], 'safe'],
- [['status', 'user_id', 'is_offline', 'clerk_id', 'shop_id', 'id', 'type'], 'integer'],
- [['status',], 'default', 'value' => -1],
- ['type', 'default', 'value' => 1],
- [['order_type', 'flag', 'dateStart', 'dateEnd'], 'string'],
- [['order_type'], 'in', 'range' => ['s', 'ms', 'pt', 'yy','ds','b']],
- ];
- }
- public function search()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- if($this->yinbao){
- $s_table = \app\models\OrderPospal::tableName();
- }else{
- $s_table = Order::tableName();
- }
- $order_share_table = OrderShare::tableName();
- $user_table = User::tableName();
- $sql_s = "
- SELECT
- 's' AS `order_type`,
- `id`,
- `order_no`,
- `is_pay`,
- `user_id`,
- `apply_delete`,
- `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`,
- `trade_status`,
- `is_recycle`,
- `limit_price`,
- `limit_id`,
- `future_sales_time`
- FROM
- {$s_table}
- WHERE
- (`store_id` = {$this->store_id})
- AND (`trade_status` != 1)
- AND (`is_delete` = 0)
- AND (`is_show` = 1)
- AND (first_price > 0 OR second_price > 0 OR third_price > 0 OR rebate > 0 OR limit_price > 0)
- ";
- $select = "SELECT `al`.*,u.nickname,u.platform,u.avatar_url ";
- // if($this->order_type){
- // switch($this->order_type){
- // case 's':
- // $sqlTable = '('.$sql_s . 'AND mch_id = 0)';
- // break;
- // case 'ds':
- // $sqlTable = '('.$sql_s . 'AND mch_id > 0)';
- // break;
- // case 'b':
- // $sqlTable = '('.$sql_s . 'AND type = 2)';
- // break;
- // default:
- // $sqlTable = $sql_s;
- // }
- // $sql = "FROM {$sqlTable} `al` LEFT JOIN {$user_table} `u` ON al.user_id = u.id";
- // }else{
- $sql = "FROM ( ({$sql_s})) `al` LEFT JOIN {$user_table}
- `u` ON al.user_id = u.id";
- // }
-
- $andWhere = '';
- if ($this->keyword) {
- if (!empty($andWhere)) {
- $andWhere .= " AND ";
- } else {
- $andWhere = " WHERE ";
- }
- $andWhere .= " (`al`.order_no LIKE '%{$this->keyword}%' OR u.nickname LIKE '%{$this->keyword}%') ";
- }
- if ($this->dateStart) {
- if (!empty($andWhere)) {
- $andWhere .= " AND ";
- } else {
- $andWhere = " WHERE ";
- }
- $andWhere .= ' (`al`.created_at >= ' . strtotime($this->dateStart) .' AND `al`.created_at < ' . strtotime($this->dateEnd) . ') ';
- }
-
- // 12.8新增订单状态判断
- if ($this->status) {
- if (!empty($andWhere)) {
- $andWhere .= " AND ";
- } else {
- $andWhere = " WHERE ";
- }
- switch ($this->status) {
- case '1':
- $andWhere .= "(`al`.is_pay = 0)";
- break;
- case '2':
- $andWhere .= "(`al`.trade_status = 0)";
- break;
- case '3':
- $andWhere .= "(`al`.trade_status = 2)";
- break;
- case '4':
- $andWhere .= "(`al`.trade_status = 3)";
- break;
- case '5':
- $andWhere .= "(`al`.apply_delete = 1)";
- break;
- case '6':
- $andWhere .= "(`al`.trade_status = 1)";
- break;
- case '7':
- $andWhere .= "(`al`.is_recycle = 1)";
- break;
- default:
- # code...
- break;
- }
- // $andWhere .= "(`al`.trade_status = {$this->status})";
- }
-
- if ($this->parent_id) {
- if (!empty($andWhere)) {
- $andWhere .= " AND ";
- } else {
- $andWhere = " WHERE ";
- }
- $andWhere .= " (`al`.parent_id_1 = {$this->parent_id}) ";
- }
- if (isset($this->platform)) {
- if (!empty($andWhere)) {
- $andWhere .= " AND ";
- } else {
- $andWhere = " WHERE ";
- }
- $andWhere .= "(`u`.platform = {$this->platform})";
- }
- $sql .= $andWhere;
-
- if ($this->flag == Export::EXPORT) {
- $export = new ExportList();
- $export->is_offline = $this->is_offline;
- $export->order_type = 0;
- $export->fields = $this->fields;
- $export->shareOrderExportData($sql);
- }
- $count = \Yii::$app->db->createCommand($select . $sql)->query()->count();
- $pageNo = get_params('pageNo', 1);
- $pageSize = get_params('pageSize', \Yii::$app->params['pageSize']);
- $offset = ($pageNo - 1) * $pageSize;
- $list = \Yii::$app->db->createCommand($select . $sql . " ORDER BY created_at DESC LIMIT {$pageSize}
- OFFSET {$offset}")->queryAll();
-
- $list = $this->transform($list);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => intval($pageNo),
- 'totalCount' => intval($count),
- 'sql' => $select . $sql . " ORDER BY created_at DESC LIMIT {$pageSize} OFFSET {$offset}",
- ],
- ];
- }
- /**
- * 数据格式整理
- * @param $list
- * @return mixed
- */
- public function transform($list) {
- foreach ($list as $i => $item) {
- if ($item['order_type'] == 's') {
- $list[$i]['goods_list'] = $this->getOrderGoodsList($item['id']);
- if($this->yinbao){
- $order = \app\models\OrderPospal::find()->where(['id' => $item['id']])->asArray()->one();
- }else{
- $order = Order::find()->where(['id' => $item['id']])->asArray()->one();
- }
- $order_refund = OrderRefund::findOne(['store_id' => $this->store_id, 'order_id' => $item['id'],
- 'is_delete' => 0]);
- $list[$i]['refund'] = "";
- if ($order_refund) {
- $list[$i]['refund'] = $order_refund->status;
- }
- if($order['mch_id'] > 0){
- $list[$i]['order_type'] = 'ds';
- }
- }
- $bind_share = null;
- if ($item['limit_price'] > 0) {
- $bind_share_ = UserBindLog::find()->alias('ubl')->where([
- 'ubl.id' => $item['limit_id']
- ])->leftJoin(['u' => User::tableName()], 'u.id = ubl.parent_id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
- ->select('su.name, ubl.parent_id')->asArray()->one();
- $bind_share = [
- 'money' => $item['limit_price'],
- 'name' => $bind_share_['name'] ?: '',
- 'parent_id' => $bind_share_['parent_id'],
- 'limit_id' => $item['limit_id']
- ];
- }
- $list[$i]['bind_share'] = $bind_share;
- $list[$i] = array_merge($list[$i], $order);
- if ($order['is_offline'] == 1 || $item['order_type'] == 'yy') {
- if (!empty($order['clerk_id'])) {
- $user = User::findOne(['id' => $order['clerk_id'], 'store_id' => $this->store_id]);
- $list[$i]['clerk_name'] = $user->nickname;
- } else {
- $list[$i]['clerk_name'] = '';
- }
- }
- if ($order['shop_id'] && $order['shop_id'] != 0) {
- $shop = Shop::find()->where(['store_id' => $this->store_id, 'id' => $order['shop_id']])->asArray()->one();
- $list[$i]['shop'] = $shop;
- }
- $share = null;
- if ($item['parent_id_1'] != 0 && $item['parent_id_1'] != -1) {
- $share = User::find()->alias('u')->where(['u.id' => $item['parent_id_1'], 'u.store_id' =>
- $this->store_id, 'u.is_delete' => 0])
- ->leftJoin(SaasUser::tableName() . ' s', 's.mobile = u.binding')->select([
- 'u.nickname', 'u.platform', 'u.binding'
- ])->asArray()->one();
- }
- $list[$i]['share'] = $share;
- if ($item['parent_id_2'] != 0 && $item['parent_id_1'] != -1) {
- $share_1 = User::find()->alias('u')->where(['u.id' => $item['parent_id_2'], 'u.store_id' =>
- $this->store_id, 'u.is_delete' => 0])
- ->leftJoin(SaasUser::tableName() . ' s', 's.mobile = u.binding')->select([
- 'u.nickname', 'u.platform', 'u.binding'
- ])->asArray()->one();
- $list[$i]['share_1'] = $share_1;
- }
- if ($item['parent_id_3'] != 0 && $item['parent_id_1'] != -1) {
- $share_2 = User::find()->alias('u')->where(['u.id' => $item['parent_id_3'], 'u.store_id' =>
- $this->store_id, 'u.is_delete' => 0])
- ->leftJoin(SaasUser::tableName() . ' s', 's.mobile = u.binding')->select([
- 'u.nickname', 'u.platform', 'u.binding'
- ])->asArray()->one();
- $list[$i]['share_2'] = $share_2;
- }
- $list[$i]['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- }
- return $list;
- }
- /**
- * 普通订单
- * @param $order_id
- * @return array|\yii\db\ActiveRecord[]
- */
- public function getOrderGoodsList($order_id)
- {
- if($this->yinbao){
- $find= \app\models\OrderPospalDetail::find();
- }else{
- $find = OrderDetail::find();
- }
- $order_detail_list = $find->alias('od')
- ->leftJoin(['g' => Goods::tableName()], 'od.goods_id=g.id')
- ->where([
- 'od.is_delete' => 0,
- 'od.order_id' => $order_id,
- ])->select('od.*,g.name,g.unit')->asArray()->all();
- foreach ($order_detail_list as $i => $order_detail) {
- $order_detail_list[$i]['attr_list'] = json_decode($order_detail['attr']);
- }
- return $order_detail_list;
- }
- /**
- * 分销商详情
- * @param $user_id
- * @return array
- */
- public function detail() {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
-
- if ($this->user_id) {
- $user = User::find()->alias('u')
- ->leftJoin(['s' => Share::tableName()], 'u.id = s.user_id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
- ->where([
- 'u.id' => $this->user_id,
- 'u.store_id' => $this->store_id
- ])->select('u.*,s.created_at create_time,s.id s_id,s.seller_comments,su.avatar sass_avatar,su.name sass_name')->asArray()->one();
- $user['create_time'] = date('Y-m-d H:i:s',$user['create_time']);
- // 上级id
- $user['name'] = !empty($user['sass_name']) ? $user['sass_name'] : $user['nickname'];
- $user['nickname'] = !empty($user['sass_name']) ? $user['sass_name'] : $user['nickname'];
- $user['avatar_url'] = !empty($user['sass_avatar']) ? $user['sass_avatar'] : $user['avatar_url'];
- $parentUser = User::find()->where(['id' => $user['old_parent_id']])->one();
- $user['parent_name'] = '总店';
- if ($parentUser) {
- $saasParentUser = SaasUser::findOne(['mobile' => $parentUser->binding]);
- $user['parent_name'] = $saasParentUser->name;
- }
-
- // 分销下级人数
- $level_count = count(User::findOld()->childs($this->user_id)->andWhere(['store_id' => $this->store_id])->asArray()->all()) ?: 0;
-
- // 一级人数
- $level_first = User::findOld()->childs($this->user_id, false, 1)
- ->andWhere(['store_id' => $this->store_id])
- ->select('id')->column();
-
- // 二级人数
- $level_second = User::find()
- ->where(['in','old_parent_id',$level_first,'store_id' => $this->store_id])
- ->select('id')->column();
-
- // 三级人数
- $level_third = User::find()
- ->where(['in','old_parent_id',$level_second, 'store_id' => $this->store_id])
- ->select('id')->column();
- $first = count($level_first);
- $second = count($level_second);
- $third = count($level_third);
- $user['level_info'] = [
- 'first' => $first,
- 'second' => $second,
- 'third' => $third,
- 'level_count' => $level_count
- ];
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $user
- ];
-
- } else {
- return [
- 'code' => 0,
- 'msg' => '缺少参数',
- ];
- }
-
- }
- /**
- * 分销商订单
- * @param $user_id
- * @return array
- */
- public function shareOrder() {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- if ($this->user_id) {
- if($this->yinbao){
- $s_table = \app\models\OrderPospal::tableName();
- }else{
- $s_table = Order::tableName();
- }
- $user_table = User::tableName();
- $sql_s = "
- SELECT
- 's' AS `order_type`,
- `id`,
- `order_no`,
- `is_pay`,
- `user_id`,
- `apply_delete`,
- `created_at`,
- `parent_id` AS `parent_id_1`,
- `parent_id_1` AS `parent_id_2`,
- `parent_id_2` AS `parent_id_3`,
- `first_price`,
- `second_price`,
- `third_price`,
- `rebate`,
- `trade_status`,
- `is_recycle`
- FROM
- {$s_table}
- WHERE
- (`store_id` = {$this->store_id})
- AND (`trade_status` != 1)
- AND (`is_delete` = 0)
- AND (`is_show` = 1)
- AND (`old_parent_id` > 0 OR `rebate` != 0)
- AND (mch_id = 0 OR (mch_id > 0 )) AND ((first_price > 0 OR second_price > 0 OR third_price > 0) OR `rebate` != 0)
- ";
- $select = "SELECT `al`.*,u.nickname,u.platform,u.avatar_url ";
- $sql = "FROM ( ({$sql_s})) `al` LEFT JOIN {$user_table}
- `u` ON al.user_id = u.id";
- $andWhere = '';
- if (!empty($andWhere)) {
- $andWhere .= " AND ";
- } else {
- $andWhere = " WHERE ";
- }
- $andWhere .= "(`u`.id = {$this->user_id})";
- $sql .= $andWhere;
- $count = \Yii::$app->db->createCommand("select count(*) " . $sql)->queryScalar();
- $pageNo = get_params('pageNo', 1);
- $pageSize = get_params('pageSize', \Yii::$app->params['pageSize']);
- $offset = ($pageNo - 1) * $pageSize;
- $list = \Yii::$app->db->createCommand($select . $sql . " ORDER BY created_at DESC LIMIT {$pageSize}
- OFFSET {$offset}")->queryAll();
- $list = $this->transform($list);
- return [
- 'q' => $sql,
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => intval($pageNo),
- 'totalCount' => intval($count)
- ],
- ];
- } else {
- return [
- 'code' => 0,
- 'msg' => '缺少参数',
- ];
- }
- }
- //获取分销商下级
- public function shareChildList() {
- $user_id = $this->user_id;
- $store_id = $this->store_id;
- $type = $this->type;//1一级 2二级 3三级
- // 一级人数
- $user_id_arr = User::find()->where(['old_parent_id' => $user_id, 'is_delete' => 0])
- ->andWhere(['store_id' => $store_id])
- ->select('id')->column();
- if ($type > 1) {
- // 二级人数
- $user_id_arr = User::find()
- ->where(['in', 'old_parent_id' , $user_id_arr])
- ->andWhere(['store_id' => $store_id, 'is_delete' => 0])
- ->select('id')->column();
- if (intval($type) === 3) {
- // 三级人数
- $user_id_arr = User::find()
- ->where(['in', 'old_parent_id', $user_id_arr])
- ->andWhere(['store_id' => $store_id, 'is_delete' => 0])
- ->select('id')->column();
- }
- }
- $query = User::find()->alias('u')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
- ->where(['u.is_delete' => 0, 'su.is_delete' => 0, 'u.id' => $user_id_arr])
- ->select('su.name, su.avatar, su.avatar, su.mobile, u.total_price, u.price');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $share = Share::findOne(['user_id' => $item['id'], 'is_delete' => 0]);
- if (isset($share) && $share->level > 0) {
- $item['level'] = ShareLevel::findOne(['is_delete' => 0, 'status' => 1, 'level' => $item['level']])->name ?? '无等级';
- } else {
- $item['level'] = '无等级';
- }
- }
- return [
- 'code' => 0,
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'] ?? '',
- 'totalCount' => $list['totalCount'] ?? ''
- ]
- ];
- }
- }
|