BaseClass.php 9.8 KB

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