BaseClass.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace common\base\classes;
  3. use Yii;
  4. use \common\base\models\Base;
  5. class BaseClass
  6. {
  7. public static $baseFile; // 模型类名的路径字符串
  8. /**
  9. * 获取模型对象
  10. * @return Base|\yii\db\ActiveRecord
  11. * @throws \yii\base\InvalidConfigException
  12. */
  13. protected static function getModel()
  14. {
  15. if (isset(static::$baseFile) && !empty(static::$baseFile)) {
  16. if (is_string(static::$baseFile)) {
  17. return Yii::createObject(['class' => static::$baseFile]);
  18. } else {
  19. return new static::$baseFile;
  20. }
  21. }
  22. // 未设置 $baseFile,配置不完整
  23. throw new \yii\base\InvalidConfigException(
  24. 'Missing model binding for ' . static::class . ': set static $baseFile.'
  25. );
  26. }
  27. /**
  28. * 获取模型对象 (ActiveRecord)
  29. * @return \yii\db\ActiveRecord
  30. */
  31. public static function getActiveRecord()
  32. {
  33. return self::getModel();
  34. }
  35. /**
  36. * 查询全部列表 (不分页)
  37. * @param string|array $select 查询字段
  38. * @param array $where 查询条件
  39. * @param string|array $order 排序
  40. * @param string|array $with 关联查询
  41. * @return array
  42. * @throws \Exception
  43. */
  44. public static function getAllList($select, $where, $order = '', $with = '')
  45. {
  46. $model = self::getModel();
  47. $return = $model->getAllList($select, $where, $order, $with);
  48. return $return;
  49. }
  50. /**
  51. * 分页查询列表 (自动获取请求中的page和pageSize)
  52. * @param string|array $select 查询字段
  53. * @param array $where 查询条件
  54. * @param string|array $order 排序
  55. * @param string|array $with 关联查询
  56. * @return array ['totalNum' => int, 'totalPage' => int, 'moreData' => int, 'list' => array]
  57. * @throws \Exception
  58. */
  59. public static function getList($select, $where, $order = '', $with = '')
  60. {
  61. $get = Yii::$app->request->get();
  62. $page = isset($get['page']) ? $get['page'] : 1;
  63. Yii::$app->params['page'] = $page;
  64. $pageSize = isset($get['pageSize']) && !empty($get['pageSize']) ? $get['pageSize'] : Yii::$app->params['pageSize'];
  65. $model = self::getModel();
  66. $return = $model->getList($select, $where, $page, $pageSize, $order, $with);
  67. return $return;
  68. }
  69. /**
  70. * 查询指定条数的列表
  71. * @param string|array $select 查询字段
  72. * @param array $where 查询条件
  73. * @param int $limit 限制条数
  74. * @param string|array $order 排序
  75. * @param string|array $with 关联查询
  76. * @return array
  77. * @throws \Exception
  78. */
  79. public static function getLimitList($select, $where, $limit = 10, $order = '', $with = '')
  80. {
  81. $model = self::getModel();
  82. $return = $model->getLimitList($select, $where, $limit, $order, $with);
  83. return $return;
  84. }
  85. /**
  86. * 添加数据
  87. * @param array $data 数据数组
  88. * @param bool $returnObject 是否返回对象
  89. * @return array|Base
  90. * @throws \Exception
  91. */
  92. public static function add($data, $returnObject = false)
  93. {
  94. $model = self::getModel();
  95. return $model->add($data, $returnObject);
  96. }
  97. /**
  98. * 批量添加
  99. * @param array $data 二维数组
  100. * @return void
  101. * @throws \Exception
  102. */
  103. public static function batchAdd($data)
  104. {
  105. $model = self::getModel();
  106. $model->batchAdd($data);
  107. }
  108. /**
  109. * 根据主键ID删除一条记录
  110. * @param int|string $id 主键ID
  111. * @return int|false 删除的行数,失败返回false
  112. * @throws \Throwable
  113. * @throws \yii\db\StaleObjectException
  114. */
  115. public static function deleteById($id)
  116. {
  117. $model = self::getModel();
  118. return $model->deleteById($id);
  119. }
  120. /**
  121. * 根据条件删除一条或多条记录
  122. * @param array $condition 查询条件
  123. * @return int 删除的行数
  124. * @throws \Exception
  125. */
  126. public static function deleteByCondition($condition)
  127. {
  128. $model = self::getModel();
  129. $count = $model->deleteByCondition($condition);
  130. return $count;
  131. }
  132. /**
  133. * 根据多个主键ID批量删除
  134. * @param array $ids 主键ID数组
  135. * @return int 删除的行数
  136. * @throws \Exception
  137. */
  138. public static function deleteByIds($ids)
  139. {
  140. $model = self::getModel();
  141. return $model->deleteByIds($ids);
  142. }
  143. /**
  144. * 根据主键ID更新一条记录
  145. * @param int|string $id 主键ID
  146. * @param array $data 更新的数据
  147. * @return array ['status' => bool, 'data' => array]
  148. */
  149. public static function updateById($id, $data)
  150. {
  151. $model = self::getModel();
  152. return $model->updateById($id, $data);
  153. }
  154. /**
  155. * 根据多个主键ID批量更新
  156. * @param array $ids 主键ID数组
  157. * @param array $data 更新的数据
  158. * @return int 更新的行数
  159. */
  160. public static function updateByIds($ids, $data)
  161. {
  162. $model = self::getModel();
  163. return $model->updateByIds($ids, $data);
  164. }
  165. /**
  166. * 根据条件更新一条或多条记录
  167. * @param array $condition 查询条件
  168. * @param array $data 更新的数据
  169. * @return int 更新的行数
  170. */
  171. public static function updateByCondition($condition, $data)
  172. {
  173. $model = self::getModel();
  174. $count = $model->updateByCondition($condition, $data);
  175. return $count;
  176. }
  177. /**
  178. * 根据主键ID查出一条记录
  179. * @param int|string $id 主键ID
  180. * @param bool $returnObject 是否返回对象
  181. * @param string|array $field 查询字段
  182. * @return array|\yii\db\ActiveRecord|null
  183. */
  184. public static function getById($id, $returnObject = false, $field = '*')
  185. {
  186. $model = self::getModel();
  187. return $model->getById($id, $returnObject, $field);
  188. }
  189. /**
  190. * 获取第一条记录
  191. * @param bool $returnObject 是否返回对象
  192. * @param string|array|bool $order 排序
  193. * @param string|array $field 查询字段
  194. * @return array|\yii\db\ActiveRecord|null
  195. */
  196. public static function getOne($returnObject = false, $order = false, $field = '*')
  197. {
  198. $model = self::getModel();
  199. return $model->getOne($returnObject, $order, $field);
  200. }
  201. /**
  202. * 根据条件查出一条记录
  203. * @param array $condition 查询条件
  204. * @param bool $returnObject 是否返回对象
  205. * @param string|array|bool $order 排序
  206. * @param string|array $field 查询字段
  207. * @return array|\yii\db\ActiveRecord|null
  208. */
  209. public static function getByCondition($condition, $returnObject = false, $order = false, $field = '*')
  210. {
  211. $model = self::getModel();
  212. return $model->getByCondition($condition, $returnObject, $order, $field);
  213. }
  214. /**
  215. * 判断数据是否存在
  216. * @param array $condition 查询条件
  217. * @return bool
  218. * @throws \yii\base\InvalidConfigException
  219. */
  220. public static function exists($condition)
  221. {
  222. $model = self::getModel();
  223. return $model->exists($condition);
  224. }
  225. /**
  226. * 根据条件查出多条记录
  227. * @param array $condition 查询条件
  228. * @param string|array|null $order 排序
  229. * @param string|array $field 查询字段
  230. * @param string|callable|null $indexBy 索引字段
  231. * @param bool $returnObject 是否返回对象
  232. * @return array|\yii\db\ActiveRecord[]
  233. */
  234. public static function getAllByCondition($condition, $order = null, $field = '*', $indexBy = null, $returnObject = false)
  235. {
  236. $model = self::getModel();
  237. return $model->getAllByCondition($condition, $order, $field, $indexBy, $returnObject);
  238. }
  239. /**
  240. * 根据多个主键ID查询多条记录
  241. * @param array $ids 主键ID数组
  242. * @param string|array|null $order 排序
  243. * @param string|callable|null $indexBy 索引字段
  244. * @param string|array $field 查询字段
  245. * @return array
  246. */
  247. public static function getByIds($ids, $order = null, $indexBy = null, $field = '*')
  248. {
  249. $model = self::getModel();
  250. return $model->getByIds($ids, $order, $indexBy, $field);
  251. }
  252. /**
  253. * 根据条件获取记录数量
  254. * @param array $condition 查询条件
  255. * @return int|string
  256. */
  257. public static function getCount($condition)
  258. {
  259. $model = self::getModel();
  260. return $model->getCount($condition);
  261. }
  262. /**
  263. * 更新计数器 (原子操作)
  264. * @param array $counters 更新的计数器数组
  265. * @param array $condition 更新条件
  266. * @param array $params 绑定参数
  267. * @return int 更新行数
  268. */
  269. public static function counters($counters, $condition, $params = [])
  270. {
  271. $model = self::getModel();
  272. return $model->counters($counters, $condition, $params);
  273. }
  274. /**
  275. * 根据主键获取记录并锁定 (SELECT FOR UPDATE)
  276. * @param int|string $id 主键ID
  277. * @param string|array $field 查询字段
  278. * @return array|\yii\db\ActiveRecord|null
  279. */
  280. public static function getLockById($id, $field = '*')
  281. {
  282. $model = self::getModel();
  283. return $model->getLockById($id, $field);
  284. }
  285. /**
  286. * 求和
  287. * @param array $condition 查询条件
  288. * @param string $field 求和字段
  289. * @return mixed
  290. */
  291. public static function sum($condition, $field)
  292. {
  293. $model = self::getModel();
  294. return $model->sum($condition, $field);
  295. }
  296. }