Provider.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\common\model;
  4. use app\common\model\chef\ChefAreas as RegionModel;
  5. /**
  6. * 供应商模型
  7. * Class Provider
  8. * @package app\common\model
  9. */
  10. class Provider extends BaseModel
  11. {
  12. // 定义表名
  13. protected $name = 'provider';
  14. // 定义主键
  15. protected $pk = 'provider_id';
  16. /**
  17. * 追加字段
  18. * @var array
  19. */
  20. protected $append = ['th_cascader', 'th_region', 'th_full_address'];
  21. /**
  22. * 省市区id集
  23. * @param $value
  24. * @param $data
  25. * @return array
  26. */
  27. public function getThCascaderAttr($value, $data)
  28. {
  29. if (!$data['th_province_id']) {
  30. return [];
  31. }
  32. return [$data['th_province_id'], $data['th_city_id'], $data['th_region_id']];
  33. }
  34. /**
  35. * 省市区名称集
  36. * @param $value
  37. * @param $data
  38. * @return array
  39. */
  40. public function getThRegionAttr($value, $data)
  41. {
  42. return $this->getThRegionNames($data);
  43. }
  44. /**
  45. * 获取完整地址
  46. * @return string
  47. */
  48. public function getThFullAddressAttr($value, $data)
  49. {
  50. $regionNames = $this->getThRegionNames($data);
  51. return "{$regionNames['province']}{$regionNames['city']}{$regionNames['region']}{$data['th_address']}";
  52. }
  53. /**
  54. * 获取省市区名称
  55. * @param array $data
  56. * @return mixed
  57. */
  58. private function getThRegionNames(array $data)
  59. {
  60. static $dataset = [];
  61. $id = $data[$this->getPk()];
  62. if (!isset($dataset[$id])) {
  63. $dataset[$id] = [
  64. 'province' => RegionModel::getNameById($data['th_province_id']),
  65. 'city' => RegionModel::getNameById($data['th_city_id']),
  66. 'region' => RegionModel::getNameById($data['th_region_id']),
  67. ];
  68. }
  69. return $dataset[$id];
  70. }
  71. /**
  72. * 获取全部记录
  73. * @param array $param
  74. * @return \think\Collection
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function getAll($param = [])
  80. {
  81. // 检索查询条件
  82. $filter = $this->getFilter($param);
  83. // 查询列表数据
  84. return $this->where($filter)->order(['sort', $this->getPk()])->select();
  85. }
  86. /**
  87. * 获取列表
  88. * @param array $param
  89. * @return \think\Paginator
  90. * @throws \think\db\exception\DbException
  91. */
  92. public function getList($param = [])
  93. {
  94. // 检索查询调价你
  95. $filter = $this->getFilter($param);
  96. // 查询列表数据
  97. return $this->where($filter)->order(['sort' => 'desc', 'provider_id' => 'desc'])->paginate(15);
  98. }
  99. /**
  100. * 检索查询条件
  101. * @param array $param
  102. * @return array
  103. */
  104. private function getFilter($param = [])
  105. {
  106. // 默认查询条件
  107. $params = $this->setQueryDefaultValue($param, ['provider_name' => '','provider_contact' => '','provider_contact_tel' => '']);
  108. // 检索查询条件
  109. $filter = [];
  110. !empty($params['provider_name']) && $filter[] = ['provider_name', 'like', "%{$params['provider_name']}%"];
  111. !empty($params['provider_contact']) && $filter[] = ['provider_contact', 'like', "%{$params['provider_contact']}%"];
  112. !empty($params['provider_contact_tel']) && $filter[] = ['provider_contact_tel', 'like', "%{$params['provider_contact_tel']}%"];
  113. return $filter;
  114. }
  115. /**
  116. * 详情
  117. * @param int $providerId
  118. * @return null|static
  119. */
  120. public static function detail(int $providerId)
  121. {
  122. return self::get($providerId);
  123. }
  124. /**
  125. * 添加新记录
  126. * @param $data
  127. * @return false|int
  128. */
  129. public function add($data)
  130. {
  131. if (empty($data['th_cascader'])) {
  132. $data['th_cascader'] = [0, 0, 0];
  133. }
  134. list($data['th_province_id'], $data['th_city_id'], $data['th_region_id']) = $data['th_cascader'];
  135. return $this->save($data);
  136. }
  137. /**
  138. * 编辑记录
  139. * @param $data
  140. * @return mixed
  141. */
  142. public function edit($data)
  143. {
  144. if (empty($data['th_cascader'])) {
  145. $data['th_cascader'] = [0, 0, 0];
  146. }
  147. list($data['th_province_id'], $data['th_city_id'], $data['th_region_id']) = $data['th_cascader'];
  148. return $this->save($data) !== false;
  149. }
  150. }