validate()) { $query = Share::find()->alias('s') ->where(['s.is_delete' => 0, 's.store_id' => get_store_id()]) ->leftJoin('{{%user}} u', 'u.id=s.user_id') ->andWhere(['u.is_delete' => 0]) ->andWhere(['in', 's.status', [0, 1]]); if ($this->keyword) { $query->andWhere([ 'or', ['like', 's.name', $this->keyword], ['like', 'u.nickname', $this->keyword], ]); } if ($this->status == 0 && $this->status != '') { $query->andWhere(['s.status' => 0]); } if ($this->status == 1) { $query->andWhere(['s.status' => 1]); } if (isset($this->platform) && in_array($this->platform, [1, 0])) { $query->andWhere(['u.platform' => $this->platform]); } $query->orderBy('s.status ASC,s.created_at DESC') ->select([ 's.*', 'u.nickname', 'u.avatar_url', 'u.platform', 'u.time', 'u.price', 'u.total_price', 'u.id user_id', 'u.parent_id', 'parent_nickname' => User::find()->alias('parent')->where('parent.id = u.parent_id')->select('nickname') ])->orderBy(['status' => SORT_ASC, 'total_price' => SORT_DESC])->asArray()->all(); $data = pagination_make($query); foreach ($data['list'] as $index => &$value) { $first = $this->getTeam1($value['user_id'], 1); $value['first'] = count($first['data']); $second = $this->getTeam1($value['user_id'], 2); $value['second'] = count($second['data']); $third = $this->getTeam1($value['user_id'], 3); $value['orderCount'] = Order::find()->where([ 'store_id' => get_store_id(), 'is_delete' => 0 ])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['user_id' => $value['user_id']])->count(); $value['third'] = count($third['data']); $value['all']=$value['first'] + $value['second'] + $value['third']; $value['yiti']=$value['total_price'] - $value['price']; $value['o_count']=$value['order_count']+ $value['ms_order_count']+$value['pt_order_count']+$value['yy_order_count']; } $data['data'] = $data['list']; unset($data['list']); return [ 'code' => 0, 'msg' => 'success', 'data' => $data ]; } else { // 验证失败:$errors 是一个包含错误信息的数组 return [ 'code' => 1, "msg" => $this->getErrorSummary(false)[0] ]; } } /** * * @Author LGL 24963@qq.com * @DateTime 2021-01-21 * @desc: 获取分销商下级 * @param integer $user_id * @param integer $level * @return array */ public function getTeam1($user_id, $level) { $firstQuery = User::find()->alias('f')->select('f.*') ->where(['f.is_delete' => 0, 'f.parent_id' => $user_id, 'f.store_id' => get_store_id()]); $query = $firstQuery; if ($level > 1) { $secondQuery = User::find()->alias('s')->where(['s.is_delete' => 0, 's.store_id' => get_store_id()]) ->innerJoin(['f' => $firstQuery], 'f.id=s.parent_id'); $query = $secondQuery; if ($level > 2) { $thirdQuery = User::find()->alias('t')->where(['t.is_delete' => 0, 't.store_id' => get_store_id()]) ->innerJoin(['s' => $secondQuery], 's.id=t.parent_id'); $query = $thirdQuery; } } $list = $query->asArray()->all(); return [ 'code' => 0, 'msg' => 'success', 'data' => $list ]; } /** * Undocumented function * * @Author LGL 24963@qq.com * @DateTime 2021-01-21 * @desc: 销售统计 * @return array */ public function OrderSearch() { if (!$this->validate()) { return [ 'code' => 1, "msg" => $this->getErrorSummary(false)[0] ]; } $query = $this->where(); $nowtime = date('Y-m-d 00:00:00'); $date_query = $this->date_where(); $all_query = clone $date_query; $now_query = clone $date_query; //时间查询 if ($this->date_start) { $this->date_start=substr($this->date_start, 0, 10); $all_query->andWhere(['>=', 'o.created_at', $this->date_start . ' 00:00:00']); } if ($this->date_end) { $this->date_end=substr($this->date_end, 0, 10); $all_query->andWhere(['<=', 'o.created_at', $this->date_end . ' 23:59:59']); } if ($this->sign == 'advance') { $all = $all_query->select("COUNT(DISTINCT `o`.`user_id`) AS `user_num`, COUNT(`o`.`id`) AS `order_num`,COALESCE(SUM(`o`.`pay_price`+(`ao`.`deposit`*`ao`.`goods_num`)),0) AS `pay_price`,COALESCE(sum(`d`.`num`),0) as `goods_num`") ->asArray() ->one(); $now = $now_query->select("COUNT(DISTINCT `o`.`user_id`) AS `user_num`, COUNT(`o`.`id`) AS `order_num`,COALESCE(SUM(`o`.`pay_price`+(`ao`.`deposit`*`ao`.`goods_num`)),0) AS `pay_price`,COALESCE(sum(`d`.`num`),0) as `goods_num`") ->andWhere(['>=', 'o.created_at', strtotime($nowtime)]) ->asArray() ->one(); $query->select("DATE_FORMAT(`o`.`created_at`, '%Y-%m-%d') AS `time`,COUNT(DISTINCT `o`.`user_id`) AS `user_num`, COUNT(`o`.`id`) AS `order_num`,COALESCE(SUM(`o`.`pay_price`+(`ao`.`deposit`*`ao`.`goods_num`)),0) AS `pay_price`,COALESCE(sum(`d`.`num`),0) as `goods_num`") ->groupBy('time') ->orderBy('time DESC'); } else { $all = $all_query->select("COUNT(DISTINCT `o`.`user_id`) AS `user_num`, COUNT(`o`.`id`) AS `order_num`,COALESCE(SUM(`o`.`pay_price`),0) AS `pay_price`,COALESCE(sum(`d`.`num`),0) as `goods_num`") ->asArray() ->one(); $now = $now_query->select("COUNT(DISTINCT `o`.`user_id`) AS `user_num`, COUNT(`o`.`id`) AS `order_num`,COALESCE(SUM(`o`.`pay_price`),0) AS `pay_price`,COALESCE(sum(`d`.`num`),0) as `goods_num`") ->andWhere(['>=', 'o.created_at', strtotime($nowtime)]) ->asArray() ->one(); $query->select("FROM_UNIXTIME(`o`.`created_at`, '%Y-%m-%d') AS `time`,COUNT(DISTINCT `o`.`user_id`) AS `user_num`, COUNT(`o`.`id`) AS `order_num`,COALESCE(SUM(`o`.`pay_price`),0) AS `pay_price`,COALESCE(sum(`d`.`num`),0) as `goods_num`") ->groupBy('time') ->orderBy('time DESC'); } //门店列表 $store_query = $this->store_where(); $store_list = $store_query->select('id,name') ->asArray() ->all(); if ($this->flag == Export::EXPORT) { $new_query = clone $query; $this->export($new_query); return false; } $this->limit=20; $count = $query->count(); $commandQuery = clone $query; $data = pagination_make($query); $data['data'] = $data['list']; unset($data['list']); return [ 'code' => 0, 'msg' => 'success', 'data' => $data, 'store_list' => $store_list, 'all' => $all, 'now' => $now, 'row_count' => $count, 'd' => $this->attributes ]; } protected function store_where() { $query = Shop::find()->where(['is_delete' => 0, 'store_id' => get_store_id()])->orderBy('name'); return $query; } protected function where() { $orderQuery = OrderDetail::find()->alias('od')->where(['is_delete' => 0]) ->select(['od.order_id', 'COALESCE(SUM(`od`.`num`),0) AS `num`'])->groupBy('od.order_id'); $query = Order::find()->alias('o')->where(['o.is_delete' => 0, 'o.store_id' => get_store_id()]) ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]]) ->rightJoin(['d' => $orderQuery], 'd.order_id = o.id') ->leftJoin(['i' => User::tableName()], 'i.id = o.user_id'); //时间查询 if ($this->date_start) { $this->date_start=substr($this->date_start, 0, 10); $query->andWhere(['>=', 'o.created_at', strtotime($this->date_start . ' 00:00:00')]); } if ($this->date_end) { $this->date_end=substr($this->date_end, 0, 10); $query->andWhere(['<=', 'o.created_at', strtotime($this->date_end . ' 23:59:59')]); } //多商户 if ($this->mch_id) { $query->andWhere(['o.mch_id' => $this->mch_id]); } //门店 if (get_store_id()) { $query->andWhere(['o.store_id' => get_store_id()]); } //平台标识查询 if ($this->platform) { $query->andWhere(['i.platform' => $this->platform]); } return $query; } protected function date_where() { $orderQuery = OrderDetail::find()->alias('od')->where(['is_delete' => 0]) ->select(['od.order_id', 'COALESCE(SUM(`od`.`num`),0) AS `num`'])->groupBy('od.order_id'); $query = Order::find()->alias('o')->where(['o.is_delete' => 0, 'o.store_id' => get_store_id()]) ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]]) ->rightJoin(['d' => $orderQuery], 'd.order_id = o.id') ->leftJoin(['i' => User::tableName()], 'i.id = o.user_id'); //平台标识查询 if ($this->platform) { $query->andWhere(['i.platform' => $this->platform]); } return $query; } /** * Undocumented function * * @Author LGL 24963@qq.com * @DateTime 2021-01-22 * @desc: 发放查询 */ public function GrantSearch() { if (!$this->validate()) { return [ 'code' => 1, "msg" => $this->getErrorSummary(false)[0] ]; } $query = $this->coupon_where(); if ($this->type == 'card') { $query = $this->card_where(); } else { $query = $this->coupon_where(); } if ($this->flag == Export::EXPORT) { $new_query = clone $query; $this->export($new_query); return false; } $all_data = $query->asArray()->one(); $all_data = [ 'all_num' => !empty($all_data['all_num']) ? $all_data['all_num'] : 0, 'unuse_num' => !empty($all_data['unuse_num']) ? $all_data['unuse_num'] : 0, 'use_num' => !empty($all_data['use_num']) ? $all_data['use_num'] : 0, 'end_num' => !empty($all_data['end_num']) ? $all_data['end_num'] : 0, ]; $query->groupBy('`date`,name') ->orderBy('`date` desc'); $data = pagination_make($query); $data['data'] = $data['list']; unset($data['list']); return [ 'all_data' => $all_data, 'data' => $data, 'code' => 0, 'd' => $query->createCommand()->getRawSql() ]; } /** * + * * @Author LGL 24963@qq.com * @DateTime 2021-01-22 * @desc: 优惠券modal * @return Query */ protected function coupon_where() { $query = UserCoupon::find()->alias('uc') ->select("FROM_UNIXTIME(uc.`created_at`,'%Y-%m-%d') AS `date`,c.`name`, COUNT(uc.`created_at`) AS all_num, SUM(CASE uc.`is_use` WHEN 0 THEN 1 ELSE 0 END) AS `unuse_num`, SUM(CASE uc.`is_use` WHEN 1 THEN 1 ELSE 0 END) AS `use_num`, SUM(CASE WHEN uc.`end_time`< NOW() AND uc.`is_use` = 0 THEN 1 ELSE 0 END) AS `end_num`") ->leftJoin(['c' => Coupon::tableName()], 'c.id = uc.coupon_id') ->leftJoin(['i' => User::tableName()], 'i.id = uc.user_id') ->where(['uc.store_id' => $this->store_id, 'uc.is_delete' => 0]); if ($this->name) { $query->andWhere(['like', 'c.name', $this->name]); } //时间查询 if ($this->date_start) { $this->date_start=substr($this->date_start, 0, 10); $query->andWhere(['>=', 'uc.created_at', strtotime($this->date_start . ' 00:00:00')]); } if ($this->date_end) { $this->date_end=substr($this->date_end, 0, 10); $query->andWhere(['<=', 'uc.created_at', strtotime($this->date_end . ' 23:59:59')]); } //平台标识查询 if ($this->platform && $this->platform >= 0) { $query->andWhere(['i.platform' => $this->platform]); } $query->orderBy(!empty($this->order) ? $this->order : '`date` desc,uc.`created_at` desc'); return $query; } /** * Undocumented function * * @Author LGL 24963@qq.com * @DateTime 2021-01-22 * @desc: 卡券model * @return Query */ protected function card_where() { $query = UserCard::find()->alias('uc') ->select("FROM_UNIXTIME(uc.`created_at`,'%Y-%m-%d') AS `date`,c.`name`, COUNT(uc.`created_at`) AS all_num, SUM(CASE uc.`is_use` WHEN 0 THEN 1 ELSE 0 END) AS `unuse_num`, SUM(CASE uc.`is_use` WHEN 1 THEN 1 ELSE 0 END) AS `use_num`, SUM(CASE WHEN uc.`created_at`< NOW() AND uc.`is_use` = 0 THEN 1 ELSE 0 END) AS `end_num`") ->leftJoin(['i' => User::tableName()], 'i.id = uc.user_id') ->leftJoin(['c' => Card::tableName()], 'c.id = uc.card_id') ->where(['uc.store_id' => $this->store_id, 'uc.is_delete' => 0]); if ($this->name) { $query->andWhere(['like', 'c.name', $this->name]); } //时间查询 if ($this->date_start) { $this->date_start=substr($this->date_start, 0, 10); $query->andWhere(['>=', 'uc.created_at', strtotime($this->date_start . ' 00:00:00')]); } if ($this->date_end) { $this->date_end=substr($this->date_end, 0, 10); $query->andWhere(['<=', 'uc.created_at', strtotime($this->date_end . ' 23:59:59')]); } //平台标识查询 if ($this->platform && $this->platform >= 0) { $query->andWhere(['i.platform' => $this->platform]); } $query->orderBy(!empty($this->order) ? $this->order : '`date` desc, uc.`created_at` desc'); return $query; } /** * Undocumented function * * @Author LGL 24963@qq.com * @DateTime 2021-01-22 * @desc: 商品销售记录 */ public function GoodsSearch() { if (!$this->validate()) { return [ 'code' => 1, "msg" => $this->getErrorSummary(false)[0] ]; } $query = Goods::find()->where(['is_delete' => 0, 'store_id' => $this->store_id]); // $query->leftJoin(['uv' => UserViews::tableName()], 'g.id = uv.goods_id'); if($this->gids){ $query->andWhere(['id' => $this->gids]); } if ($this->keyword) { $query->andWhere(['LIKE', 'name', $this->keyword]); } $list = pagination_make($query); $listArray = $list['list']; foreach ($listArray as $k => $v) { //访客数 $fangke = UserViews::find()->where(['goods_id' => $v['id']]); if ($this->date_start) { $fangke->andWhere(['>=', 'updated_at', strtotime($this->date_start)]); } if ($this->date_end) { $fangke->andWhere(['<=', 'updated_at', strtotime($this->date_end)]); } $fangke = $fangke->SUM('visitors'); if (!$fangke) { $fangke = 0; } //浏览量 $views = UserViews::find()->where(['goods_id' => $v['id']]); if ($this->date_start) { $views->andWhere(['>=', 'updated_at', strtotime($this->date_start)]); } if ($this->date_end) { $views->andWhere(['<=', 'updated_at', strtotime($this->date_end)]); } $views = $views->SUM('views'); if (!$views) { $views = 0; } //加购数 $cart = Cart::find()->where(['goods_id' => $v['id']]); if ($this->date_start) { $cart->andWhere(['>=', 'created_at', strtotime($this->date_start)]); } if ($this->date_end) { $cart->andWhere(['<=', 'created_at',strtotime($this->date_end)]); } $cart = $cart->count(); if (!$cart) { $cart = 0; } // 付款人数 $paynum = Order::find()->alias('o')->where(['o.is_pay' => 1])->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL]); $paynum->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id'); $paynum->andWhere(['od.goods_id'=>$v['id']]); if ($this->date_start) { $paynum->andWhere(['>=', 'o.created_at', strtotime($this->date_start)]); } if ($this->date_end) { $paynum->andWhere(['<=', 'o.created_at',strtotime($this->date_end)]); } $paynum = $paynum->count(); if (!$paynum) { $paynum = 0; } // 付款金额 $jine = Order::find()->alias('o')->where(['o.is_pay' => 1])->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL]); $jine->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id'); $jine->andWhere(['od.goods_id'=>$v['id']]); if ($this->date_start) { $jine->andWhere(['>=', 'o.created_at', strtotime($this->date_start)]); } if ($this->date_end) { $jine->andWhere(['<=', 'o.created_at',strtotime($this->date_end)]); } $jine = $jine->SUM('od.total_price'); if (!$jine) { $jine = '0.00'; } $listArray[$k]['fangke'] = $fangke; $listArray[$k]['views'] = $views; $listArray[$k]['cart'] = $cart; $listArray[$k]['paynum'] = $paynum; $listArray[$k]['jine'] = $jine; } $this->gids && array_multisort(array_column($listArray, 'paynum'), SORT_DESC, $listArray); $list['data'] = $listArray; unset($list['list']); return [ 'code' => 0, 'msg' => 'success', 'data' => $list ]; } /** * Undocumented function * * @Author LGL 24963@qq.com * @DateTime 2021-01-22 * @desc: 商户统计报表 */ public function MchCountSearch() { if (!$this->validate()) { return [ 'code' => 1, "msg" => $this->getErrorSummary(false)[0] ]; } $query = Mch::find()->alias('m'); if ($this->keyword) { $query->andWhere(['LIKE', 'm.name', $this->keyword]); } $query->orderBy('m.created_at DESC'); $list = pagination_make($query); $listArray = $list['list']; foreach ($listArray as $k =>$v) { $order_num = Order::find()->where(['mch_id' => $v['id']])->count(); if (!$order_num) { $order_num = 0; } $listArray[$k]['order_num'] = $order_num; $yongjin = MchAccountLog::find()->where(['mch_id' => $v['id']])->SUM('price'); if (!$yongjin) { $yongjin = 0; } $listArray[$k]['yongjin'] = $yongjin; } $list['data'] = $listArray; unset($list['list']); return [ 'code' => 0, 'msg' => 'success', 'data' => $list ]; } /** * Undocumented function * * @Author LGL 24963@qq.com * @DateTime 2021-01-22 * @desc: 商户订单报表 */ public function MchOrderSearch() { if (!$this->validate()) { return [ 'code' => 1, "msg" => $this->getErrorSummary(false)[0] ]; } $query = Order::find()->alias('o')->leftJoin(Mch::tableName() . ' m', 'o.mch_id=m.id'); $query->andWhere(['>', 'o.mch_id', 0]); if (isset($this->platform) && $this->platform >= 0) { $query->andWhere(['o.platform' => $this->platform]); } if ($this->date_start) { $query->andWhere(['>=', 'o.created_at', strtotime($this->date_start)]); } if ($this->date_end) { $query->andWhere(['<=', 'o.created_at',strtotime($this->date_end)]); } if ($this->status == 1) { $query->andWhere(['=', 'o.is_pay',0]); } if ($this->status == 2) { $query->andWhere(['=', 'o.trade_status', Order::ORDER_FLOW_NO_SEND]); } if ($this->status == 3) { $query->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CONFIRM]); } if ($this->status == 4) { $query->andWhere(['=', 'o.trade_status', Order::ORDER_FLOW_CONFIRM]); } $query->orderBy('o.created_at DESC')->select('o.id,o.order_no,o.pay_price,o.is_pay,o.trade_status,m.name'); $list = pagination_make($query); $listArray = $list['list']; foreach ($listArray as $k =>$v) { $yongjin = MchAccountLog::find()->where(['LIKE', 'desc', $v['order_no']])->asArray()->all(); if ($yongjin) { $listArray[$k]['yongjin'] = $yongjin[0]['price']; } else { $listArray[$k]['yongjin'] = '暂未结算'; } } $list['data'] = $listArray; unset($list['list']); return [ 'code' => 0, 'msg' => 'success', 'data' => $list ]; } /** * Undocumented function * * @Author LGL 24963@qq.com * @DateTime 2021-01-23 * @desc: 积分统计 */ public function IntegralSearch() { if (!$this->validate()) { return [ 'code' => 1, "msg" => $this->getErrorSummary(false)[0] ]; } $query = $this->IntWhere(); $query->select(["FROM_UNIXTIME(`il`.`created_at`, '%Y-%m-%d') AS `date`, COALESCE(SUM(CASE WHEN `il`.`log_type`=1 THEN `il`.`amount` ELSE 0 END), 0) AS in_integral, COALESCE(SUM(CASE WHEN `il`.`log_type`=2 THEN `il`.`amount` ELSE 0 END), 0) AS out_integral"]); $all_query = clone $query; $query->groupBy('`date`') ->orderBy('`date` desc'); $all_data = $all_query->asArray()->one(); $count = $query->count(); $list = pagination_make($query); unset($all_data[0]['date']); $all_data['all_num'] = $count; $list['data'] = $list['list']; unset($list['list']); return [ 'code' => 0, 'msg' => 'success', 'data' => $list, 'all_data' => $all_data ]; } /** * Undocumented function * * @Author LGL 24963@qq.com * @DateTime 2021-01-23 * @desc: 积分model * @return Query */ protected function IntWhere() { $query = AccountLog::find()->alias('il') ->where(['il.store_id' => $this->store_id]) ->leftJoin(['i' => User::tableName()], 'i.id = il.user_id') ->where(['il.type' => 1]); //时间查询 if ($this->date_start) { $this->date_start=substr($this->date_start, 0, 10); $query->andWhere(['>=', 'il.created_at', strtotime($this->date_start . ' 00:00:00')]); } if ($this->date_end) { $this->date_end=substr($this->date_end, 0, 10); $query->andWhere(['<=', 'il.created_at', strtotime($this->date_end . ' 23:59:59')]); } //平台标识查询 if ($this->platform && in_array($this->platform, [0,1])) { $query->andWhere(['i.platform' => $this->platform]); } return $query; } public function orderSaleGoodsList() { $order_type_arr = [ 0 => '全部', 1 => '到店自提', 2 => '快递', 3 => '同城配送' ]; $trade_status = Order::TRADE_STATUS_TEXT; try { $status = $this->status; $type = $this->type; $date_start = $this->date_start; $date_end = $this->date_end; $store_id = $this->store_id; if (!isset($status) && !isset($type) && !isset($date_start) && !isset($date_end)) { throw new \Exception('参数为空'); } $query = OrderDetail::find()->alias('od')->leftJoin(['o' => Order::tableName()], 'od.order_id = o.id') ->where(['o.store_id' => $store_id, 'o.is_delete' => 0]); if (isset($status)) { $query->andWhere(['o.trade_status' => $status]); } if (isset($type)) { switch ($type) { case 1: $query->andWhere(['o.is_offline' => 1]); break; case 2: $query->andWhere(['AND', ['o.is_offline' => 0], ['o.is_delivery' => 0]]); break; case 3: $query->andWhere(['o.is_delivery' => 1]); break; } } if (isset($date_start)) { $query->andWhere(['>=', 'o.created_at', strtotime($date_start)]); } if (isset($date_end)) { $query->andWhere(['<=', 'o.created_at', strtotime($date_end)]); } $query->select('od.id, od.goods_id, od.goods_name, od.attr, SUM(od.num) num, COUNT(o.id) order_count, GROUP_CONCAT(o.id) order_no') ->orderBy('od.goods_id DESC')->groupBy('od.goods_id, od.attr'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $goods = Goods::findOne($item['goods_id']); $item['goods_name'] = $goods->name . "(商品ID:{$goods->id})"; $item['attr'] = json_decode($item['attr'], true); $attr_name = ''; array_map(function ($item) use (&$attr_name) { $attr_name = $item['attr_group_name'] . ':' . $item['attr_name'] . ';'; }, $item['attr']); $item['attr_name'] = $attr_name; } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list['list'], 'pageNo' => $list['pageNo'], 'totalCount' => $list['totalCount'], 'trade_status' => $trade_status, 'order_type' => $order_type_arr ] ]; } catch (\Exception $e) { return [ 'code' => 0, 'msg' => $e->getMessage(), 'data' => [ 'data' => [], 'pageNo' => 1, 'totalCount' => 0, 'trade_status' => $trade_status, 'order_type' => $order_type_arr ] ]; } } }