BaseModel.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Schema;
  7. use DateTimeInterface;
  8. class BaseModel extends Model
  9. {
  10. use Criteria;
  11. //use ApiSoftDeletes; laravels 下常驻内存 as Name 会出问题
  12. /**
  13. * 插入时间字段
  14. */
  15. const CREATED_AT = 'create_time';
  16. /**
  17. * 更新时间字段
  18. */
  19. const UPDATED_AT = 'update_time';
  20. /**
  21. * 状态字段
  22. */
  23. const DELETED_AT = 'status';
  24. /**
  25. * 正常状态
  26. */
  27. const STATUS_ENABLED = 0;
  28. /**
  29. * 禁用状态
  30. */
  31. const STATUS_DISABLED = 1;
  32. /**
  33. * 删除状态
  34. */
  35. const STATUS_DELETED = 2;
  36. /**
  37. * 该模型是否被自动维护时间戳
  38. *
  39. * @var bool
  40. */
  41. public $timestamps = true;
  42. /**
  43. * 别名名称
  44. * @var null
  45. */
  46. protected $aliasName = null;
  47. /**
  48. * 模型的日期字段保存格式。
  49. *
  50. * @var string
  51. */
  52. protected $dateFormat = 'Y-m-d H:i:s';
  53. /* public function __construct(array $attributes = [])
  54. {
  55. parent::__construct($attributes);
  56. //自动添加字段
  57. $this->setFillable();
  58. }*/
  59. /**
  60. * 自动获取数据库表字段
  61. */
  62. protected function setFillable()
  63. {
  64. $key = get_class($this);
  65. $cache = Cache::store('file')->get($key);
  66. if (empty($cache)) {
  67. $columns = Schema::getColumnListing($this->table);
  68. $cache = $columns;
  69. Cache::store('file')->put($key, $columns, config('cache.columns'));
  70. }
  71. $this->fillable = $cache;
  72. }
  73. /**
  74. * 过滤掉非数据库字段的数据
  75. * @param $data
  76. * @return array
  77. */
  78. public function filter($data)
  79. {
  80. if(empty($this->fillable)){
  81. $this->setFillable();
  82. }
  83. $result = [];
  84. if(empty($data) || !is_array($data)){
  85. return $result;
  86. }
  87. foreach($this->fillable as $item){
  88. if(isset($data[$item])){
  89. $result[$item] = $data[$item];
  90. }
  91. }
  92. return $result;
  93. }
  94. /**
  95. * 覆盖setCreatedAt 不添加createAt字段
  96. *
  97. * @param mixed $value
  98. * @return $this
  99. */
  100. public function setCreatedAt($value)
  101. {
  102. if (!empty(static::CREATED_AT)) {
  103. $this->{static::CREATED_AT} = $value;
  104. }
  105. if (!empty(static::UPDATED_AT)) {
  106. $this->{static::UPDATED_AT} = $value;
  107. }
  108. return $this;
  109. }
  110. /**
  111. * Set the value of the "updated at" attribute.
  112. *
  113. * @param mixed $value
  114. * @return $this
  115. */
  116. public function setUpdatedAt($value)
  117. {
  118. if (!empty(static::UPDATED_AT)) {
  119. $this->{static::UPDATED_AT} = $value;
  120. }
  121. return $this;
  122. }
  123. /**
  124. * 别名
  125. * @param $name
  126. */
  127. public function alias($name)
  128. {
  129. $this->aliasName = $name;
  130. return $this->from($this->getTable().' as '.$name);
  131. }
  132. /**
  133. * 获取别名名称
  134. * @return null
  135. */
  136. public function getAliasName(){
  137. return $this->aliasName;
  138. }
  139. /**
  140. * Set the table associated with the model.
  141. *
  142. * @param string $table
  143. * @return $this
  144. */
  145. public function setTable($table)
  146. {
  147. $table = trim($table);
  148. $this->table = $table;
  149. if(strpos($table,' as ')){
  150. list($table, $alias) = explode(' as ', $table);
  151. if(!empty($alias)){
  152. $this->aliasName = trim($alias);
  153. }
  154. }
  155. return $this;
  156. }
  157. /* @param DateTimeInterface $date
  158. *
  159. * @return string
  160. */
  161. protected function serializeDate(DateTimeInterface $date)
  162. {
  163. return $date->format('Y-m-d H:i:s');
  164. }
  165. /**
  166. * 获取分页行数
  167. * @param $data
  168. * @return int
  169. */
  170. protected function getPageSize($data)
  171. {
  172. return $data['page_size'] ?? config('app.app_rows');
  173. }
  174. /**
  175. * 获取页码
  176. * @param $data
  177. * @return int
  178. */
  179. protected function getPageNo($data)
  180. {
  181. return $data['page'] ?? config('app.app_page_no');
  182. }
  183. /**
  184. * 获取查询偏移量
  185. * @param $data
  186. * @return int
  187. */
  188. protected function getSkip($page,$pageSize)
  189. {
  190. return ($page - 1) * $pageSize;;
  191. }
  192. /**
  193. * 获取分页参数
  194. * */
  195. protected function getPaginatorParams($params){
  196. $pageSize = $this->getPageSize($params);
  197. $page = $this->getPageNo($params);
  198. $skip = $this->getSkip($page,$pageSize);
  199. return array($pageSize, $page,$skip);
  200. }
  201. /**
  202. * 构造返回页面数据
  203. * @param $data //数据
  204. * @param $formNb //开始数量
  205. * @param $page //页数
  206. * @param $perPageSize //每页数量
  207. * @param $total //总数量
  208. * @return mixed
  209. */
  210. public function buildPaginator($data = [], $formNb = 0, $page = 1, $perPageSize = 10, $total = 0)
  211. {
  212. $lastPage = ceil($total / $perPageSize);
  213. $toNb = $page * $perPageSize;
  214. if (empty($formNb)) {
  215. $formNb = $toNb - $perPageSize;
  216. }
  217. if ($toNb > $total) {
  218. $toNb = $total;
  219. }
  220. $resultDdata = array(
  221. 'current_page' => $page,
  222. 'data' => $data,
  223. 'from' => $formNb,
  224. 'last_page' => $lastPage,
  225. 'per_page' => $perPageSize,
  226. 'to' => $toNb,
  227. 'total' => $total,
  228. );
  229. return $resultDdata;
  230. }
  231. /**
  232. * 字段唯一性性验证
  233. * 返回ture 代表数据库不存在,是唯一的 false 代表数据库存在,不是唯一的
  234. * @param $field
  235. * @param $value
  236. * @param $criteria
  237. * @return bool
  238. */
  239. public function checkFieldUnique($field, $criteria)
  240. {
  241. $existData = $this->newInstance()->buildQuery($criteria)->selectRaw(DB::raw($field))->exists();
  242. if ($existData) {
  243. return false;
  244. }else{
  245. return true;
  246. }
  247. }
  248. }