search($where)->field($field)->when($page && $limit, function ($query) use ($page, $limit) { $query->page($page, $limit); })->order('id desc')->select()->toArray(); } /** * 账单记录 * @param $where * @param int $page * @param int $limit * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getRecordList($where, $page = 0, $limit = 0) { $model = $this->search($where) ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) { $timeUnix = '%Y-%m-%d'; switch ($where['type']) { case "day" : $timeUnix = "%Y-%m-%d"; break; case "week" : $timeUnix = "%Y-%u"; break; case "month" : $timeUnix = "%Y-%m"; break; } $query->field("FROM_UNIXTIME(add_time,'$timeUnix') as day,sum(if(price >= 0,price,0)) as income_price,sum(if(price < 0,price,0)) as exp_price,add_time,group_concat(id) as ids"); $query->group("FROM_UNIXTIME(add_time, '$timeUnix')"); }); $count = $model->count(); $list = $model->when($page && $limit, function ($query) use ($page, $limit) { $query->page($page, $limit); })->order('add_time desc')->select()->toArray(); return compact('list', 'count'); } }