123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- declare (strict_types = 1);
- namespace app\common\model;
- use app\common\model\chef\ChefAreas as RegionModel;
- /**
- * 供应商模型
- * Class Provider
- * @package app\common\model
- */
- class Provider extends BaseModel
- {
- // 定义表名
- protected $name = 'provider';
- // 定义主键
- protected $pk = 'provider_id';
- /**
- * 追加字段
- * @var array
- */
- protected $append = ['th_cascader', 'th_region', 'th_full_address'];
- /**
- * 省市区id集
- * @param $value
- * @param $data
- * @return array
- */
- public function getThCascaderAttr($value, $data)
- {
- if (!$data['th_province_id']) {
- return [];
- }
- return [$data['th_province_id'], $data['th_city_id'], $data['th_region_id']];
- }
- /**
- * 省市区名称集
- * @param $value
- * @param $data
- * @return array
- */
- public function getThRegionAttr($value, $data)
- {
- return $this->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;
- }
- }
|