getThRegionNames($data); } /** * 获取完整地址 * @return string */ public function getThFullAddressAttr($value, $data) { $regionNames = $this->getThRegionNames($data); return "{$regionNames['province']}{$regionNames['city']}{$regionNames['region']}{$data['th_address']}"; } /** * 获取省市区名称 * @param array $data * @return mixed */ private function getThRegionNames(array $data) { static $dataset = []; $id = $data[$this->getPk()]; if (!isset($dataset[$id])) { $dataset[$id] = [ 'province' => RegionModel::getNameById($data['th_province_id']), 'city' => RegionModel::getNameById($data['th_city_id']), 'region' => RegionModel::getNameById($data['th_region_id']), ]; } return $dataset[$id]; } /** * 获取全部记录 * @param array $param * @return \think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getAll($param = []) { // 检索查询条件 $filter = $this->getFilter($param); // 查询列表数据 return $this->where($filter)->order(['sort', $this->getPk()])->select(); } /** * 获取列表 * @param array $param * @return \think\Paginator * @throws \think\db\exception\DbException */ public function getList($param = []) { // 检索查询调价你 $filter = $this->getFilter($param); // 查询列表数据 return $this->where($filter)->order(['sort' => 'desc', 'provider_id' => 'desc'])->paginate(15); } /** * 检索查询条件 * @param array $param * @return array */ private function getFilter($param = []) { // 默认查询条件 $params = $this->setQueryDefaultValue($param, ['provider_name' => '','provider_contact' => '','provider_contact_tel' => '']); // 检索查询条件 $filter = []; !empty($params['provider_name']) && $filter[] = ['provider_name', 'like', "%{$params['provider_name']}%"]; !empty($params['provider_contact']) && $filter[] = ['provider_contact', 'like', "%{$params['provider_contact']}%"]; !empty($params['provider_contact_tel']) && $filter[] = ['provider_contact_tel', 'like', "%{$params['provider_contact_tel']}%"]; return $filter; } /** * 详情 * @param int $providerId * @return null|static */ public static function detail(int $providerId) { return self::get($providerId); } /** * 添加新记录 * @param $data * @return false|int */ public function add($data) { if (empty($data['th_cascader'])) { $data['th_cascader'] = [0, 0, 0]; } list($data['th_province_id'], $data['th_city_id'], $data['th_region_id']) = $data['th_cascader']; return $this->save($data); } /** * 编辑记录 * @param $data * @return mixed */ public function edit($data) { if (empty($data['th_cascader'])) { $data['th_cascader'] = [0, 0, 0]; } list($data['th_province_id'], $data['th_city_id'], $data['th_region_id']) = $data['th_cascader']; return $this->save($data) !== false; } }