query()->insertGetId($data); // 如果操作失败 if( !$id ) return $id; // 更新缓存 $this->getList(true); // 返回结果 return $id; } /** * 添加数据 * */ public function edit($id,$data) { // 更新时间 $data['update_time'] = time(); // 写入数据表 $result = $this->query()->where(['id'=>$id])->update($data); // 如果操作失败 if( !$result ) return $result; // 更新缓存 $this->getList(true); // 返回结果 return $result; } /** * 获取列表 * @param Bool $force 是否强制更新 * */ public function getList($force = false) { // 结果数据 $list = $force ? [] : cache('admin:lottery:order:list'); // 不存在数据 if ( !$list ) { // 从数据库获取数据 $data = $this->query()->where([['status','=',0]])->get(['id','name','logo','rule','status','start_time','end_time','tag_scope','city_ids']); // 是否有数据 $data = $data ? $data->toArray() : []; // 循环处理数据 $list = []; // 进行更新 foreach ($data as $value) { // 重组数据 $list[$value['id']] = $value; } // 存起来 cache(['admin:lottery:order:list'=>$list]); } // 返回结果 return $list; } /** * 获取配置平台对应的应用数据 * * @param Array 用户ID * @param String 指定字段 * */ public function getOne($id,$field='') { // 获取列表数据 $list = $this->getList(); // 获取数据 $one = isset($list[$id]) ? $list[$id] : []; // 返回值 return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null); } /** * 获取配置平台对应的应用数据 * * @param int $cityId 城市ID * */ public function getOneByCity($cityId){ // 获取列表数据 $list = $this->getList(true); // 列表数据不存在 if( !$list ) return []; // 全国的码 $all = []; // 城市的码 $city = []; // 循环列表 foreach ($list as $key => $value) { // 如果没有限制城市 if( empty($value['city_ids']) ) { $all[] = $value; continue; } // 如果限制了的话,转数组 $value['city_ids'] = explode(',',$value['city_ids']); // 判断用户的城市是否在内 if( in_array($cityId,$value['city_ids']) ) { $city[] = $value; continue; } // 不在范围的删除 unset($list[$key]); } // 先获取城市的,再获取全国的 $one = $city ? array_shift($city) : array_shift($all); // 判断是否存在二维码 return (array) $one; } }