Unit.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\common\model;
  4. /**
  5. * 计量单位模型
  6. * Class Unit
  7. * @package app\common\model
  8. */
  9. class Unit extends BaseModel
  10. {
  11. // 定义表名
  12. protected $name = 'unit';
  13. // 定义主键
  14. protected $pk = 'id';
  15. /**
  16. * 获取全部记录
  17. * @param array $param
  18. * @return \think\Collection
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\DbException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. */
  23. public function getAll($param = [])
  24. {
  25. // 检索查询条件
  26. $filter = $this->getFilter($param);
  27. // 查询列表数据
  28. return $this->where($filter)->order(['sort', $this->getPk()])->select();
  29. }
  30. /**
  31. * 获取列表
  32. * @param array $param
  33. * @return \think\Paginator
  34. * @throws \think\db\exception\DbException
  35. */
  36. public function getList($param = [])
  37. {
  38. // 检索查询调价你
  39. $filter = $this->getFilter($param);
  40. // 查询列表数据
  41. return $this->where($filter)->order(['sort', 'id'])->paginate(15);
  42. }
  43. /**
  44. * 检索查询条件
  45. * @param array $param
  46. * @return array
  47. */
  48. private function getFilter($param = [])
  49. {
  50. // 默认查询条件
  51. $params = $this->setQueryDefaultValue($param, ['name' => '']);
  52. // 检索查询条件
  53. $filter = [];
  54. !empty($params['name']) && $filter[] = ['name', 'like', "%{$params['name']}%"];
  55. return $filter;
  56. }
  57. /**
  58. * 详情
  59. * @param int $providerId
  60. * @return null|static
  61. */
  62. public static function detail(int $providerId)
  63. {
  64. return self::get($providerId);
  65. }
  66. /**
  67. * 添加新记录
  68. * @param $data
  69. * @return false|int
  70. */
  71. public function add($data)
  72. {
  73. return $this->save($data);
  74. }
  75. /**
  76. * 编辑记录
  77. * @param $data
  78. * @return mixed
  79. */
  80. public function edit($data)
  81. {
  82. return $this->save($data) !== false;
  83. }
  84. }