1], ]; } /** * 数据列表 */ public function search() { if (!in_array($this->user_type, $this->type)) { return $this->apiReturnError('账号错误'); } //门店管理员 1-门店管理员 2-门店服务员 3-门店财务人员 if ($this->user_type == 2) { $storeUser = ApiHelper::findOneUserStoreId($this->user_id); if ($storeUser['user_type'] == 2) { return $this->apiReturnError('账号错误'); } if (empty($this->store_id)) { return $this->apiReturnError('门店ID不能为空'); } } //门店总数 $countStore = Store::find()->count('id'); //球车总数 $ballCartCount = BallCart::find()->andFilterWhere(['store_id' => $this->store_id])->count('id'); $orderData = Order::find()->andFilterWhere([ 'store_id' => $this->store_id ])->andWhere(['is_delete' => 0, 'is_pay' => '1']); //总租赁次数 $order_count = $orderData->count(); // $orderData = array_column($orderData, 'total_pay_price'); //总收入 $order_money = $orderData->sum('total_pay_price'); $data = [ 'share_money' => 0, 'store_num' => $countStore, //门店总数 'ball_num' => $ballCartCount, //球车总数 'money' => round($order_money, 2), //总收入 'lease_num' => $order_count //总租赁次数 ]; return [ 'code' => 0, 'msg' => 'ok', 'data' => $data, ]; } /** * 获取月份租赁次数 * @param $date /月份 */ public function getLessNum($date) { if (!$date) { return $this->apiReturnError('时间错误'); } $date = strtotime($date); $riqi = $this->getMonthBeginAndEnd($date); $start_date = $riqi['begin']; $end_date = $riqi['end']; $store_id = $this->store_id ? $this->store_id : 0; $arr = []; $arr[] = $this->countLess($start_date, $end_date, $store_id); if ($store_id > 0) { $where = ['store_id' => $this->store_id]; } $count = Order::find()->where(['between', 'created_at', $start_date, $end_date])->andFilterWhere($where)->andWhere([ 'is_delete' => 0, 'is_pay' => 1, ])->count('id'); return [ 'code' => 0, 'msg' => 'ok', 'data' => $arr, 'count' => $count ]; } /** * 获取球车租赁收入 */ public function getMoneySum($date) { if (!$date) { return $this->apiReturnError('时间错误'); } $date = strtotime($date); $riqi = $this->getMonthBeginAndEnd($date); $start_date = $riqi['begin']; $end_date = $riqi['end']; $store_id = $this->store_id ? $this->store_id : 0; $arr = []; $arr[] = $this->sumMoney($start_date, $end_date, $store_id); if ($store_id > 0) { $where = ['store_id' => $this->store_id]; } $sum = Order::find()->where(['between', 'created_at', $start_date, $end_date])->andFilterWhere($where)->andWhere([ 'is_delete' => 0, 'is_pay' => 1, ])->sum('total_pay_price'); $sum = $sum ? $sum : 0; return [ 'code' => 0, 'msg' => 'ok', 'data' => $arr, 'money' => $sum ]; } /** * * 获取指定月份的开始时间戳跟结束时间戳 * @param int $timestamp * @return array */ function getMonthBeginAndEnd($timestamp = 0) { $timestamp = $timestamp ? $timestamp : time(); $year = date('Y', $timestamp); $month = date('m', $timestamp); $d = date('t', strtotime($year . '-' . $month)); return ['begin' => strtotime($year . '-' . $month), 'end' => mktime(23, 59, 59, $month, $d, $year)]; } /** * 统计租赁次数 */ public function countLess($start_date, $end_date, $store_id) { if ($store_id != 0) { $where = " AND (`store_id` = $store_id) "; } $sql = "SELECT from_unixtime(`created_at`, '%Y-%m-%d') AS `riqi`, count(`id`) AS `count` FROM `cx_order` WHERE(`created_at` BETWEEN $start_date AND $end_date) $where AND (`is_delete` = 0) AND (`is_pay` = '1') GROUP BY from_unixtime(`created_at`, '%Y-%m-%d')"; $query = (new Order())->find(); $query->sql = $sql; $create = $query->createCommand(); $record = $create->queryAll(); return $record; } /** * 统计租赁金额 */ public function sumMoney($start_date, $end_date, $store_id) { if ($store_id != 0) { $where = " AND (`store_id` = $store_id) "; } $sql = "SELECT from_unixtime(`created_at`, '%Y-%m-%d') AS `riqi`, SUM(`total_pay_price`) AS `sum_money` FROM `cx_order` WHERE(`created_at` BETWEEN $start_date AND $end_date) $where AND (`status` = 0) AND (`is_delete` = 0) AND (`is_pay` = '1') GROUP BY from_unixtime(`created_at`, '%Y-%m-%d')"; $query = (new Order())->find(); $query->sql = $sql; $create = $query->createCommand(); $record = $create->queryAll(); return $record; } }