BaseClass.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. public static $baseTable; // 表名(新增属性,表名和模型类名二选一,如果同时存在,则优先使用模型类名)
  9. public static $baseDb; // 数据库组件ID
  10. /**
  11. * @return Base
  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. // 兼容:通过在 Class 上声明 static $baseTable 来动态指定表名
  23. // 可选:再通过 static $baseDb 指定使用的 DB 组件 ID(如不设置则走默认)
  24. if (property_exists(static::class, 'baseTable') && !empty(static::$baseTable)) {
  25. $tableName = static::$baseTable;
  26. $dbId = property_exists(static::class, 'baseDb') ? static::$baseDb : null;
  27. $model = new class($tableName, $dbId) extends \common\base\models\Base {
  28. private static $_tableName;
  29. private static $_dbId;
  30. public function __construct($tableName, $dbId = null)
  31. {
  32. self::$_tableName = $tableName;
  33. self::$_dbId = $dbId;
  34. parent::__construct();
  35. }
  36. public static function tableName()
  37. {
  38. return self::$_tableName;
  39. }
  40. public static function getDb()
  41. {
  42. if (!empty(self::$_dbId)) {
  43. return \Yii::$app->get(self::$_dbId);
  44. }
  45. return parent::getDb();
  46. }
  47. };
  48. return $model;
  49. }
  50. // 未设置 $baseFile / $baseTable,配置不完整
  51. throw new \yii\base\InvalidConfigException(
  52. 'Missing model binding for ' . static::class . ': set static $baseFile or static $baseTable.'
  53. );
  54. }
  55. //查询全部 ssh 2019.11.27
  56. public static function getAllList($select, $where, $order = '', $with = '')
  57. {
  58. $model = self::getModel();
  59. $return = $model->getAllList($select, $where, $order, $with);
  60. return $return;
  61. }
  62. //分页查询 ssh 2019.9.21
  63. public static function getList($select, $where, $order = '', $with = '')
  64. {
  65. $get = Yii::$app->request->get();
  66. $page = isset($get['page']) ? $get['page'] : 1;
  67. Yii::$app->params['page'] = $page;
  68. $pageSize = isset($get['pageSize']) && !empty($get['pageSize']) ? $get['pageSize'] : Yii::$app->params['pageSize'];
  69. $model = self::getModel();
  70. $return = $model->getList($select, $where, $page, $pageSize, $order, $with);
  71. return $return;
  72. }
  73. //查询指定条数 ssh 2019.11.30
  74. public static function getLimitList($select, $where, $limit = 10, $order = '', $with = '')
  75. {
  76. $model = self::getModel();
  77. $return = $model->getLimitList($select, $where, $limit, $order, $with);
  78. return $return;
  79. }
  80. /**
  81. * @param $data
  82. * @param bool $returnObject
  83. * @return array|Base
  84. * @throws \Exception
  85. */
  86. public static function add($data, $returnObject = false)
  87. {
  88. $model = self::getModel();
  89. return $model->add($data, $returnObject);
  90. }
  91. /**
  92. * 批量添加
  93. */
  94. public static function batchAdd($data)
  95. {
  96. $model = self::getModel();
  97. $model->batchAdd($data);
  98. }
  99. /**
  100. * 根据主键ID删除一条记录
  101. * @param $id
  102. * @return false|int the number of rows deleted, or `false` if the deletion is unsuccessful for some reason.
  103. * @throws \Throwable
  104. * @throws \yii\db\StaleObjectException
  105. */
  106. public static function deleteById($id)
  107. {
  108. $model = self::getModel();
  109. return $model->deleteById($id);
  110. }
  111. /**
  112. *根据条件删除一条或多条记录
  113. */
  114. public static function deleteByCondition($condition)
  115. {
  116. $model = self::getModel();
  117. $count = $model->deleteByCondition($condition);
  118. return $count;
  119. }
  120. //根据id批量删 ssh 2019.12.7
  121. public static function deleteByIds($ids)
  122. {
  123. $model = self::getModel();
  124. return $model->deleteByIds($ids);
  125. }
  126. /**
  127. * 根据主键ID更新一条记录
  128. */
  129. public static function updateById($id, $data)
  130. {
  131. $model = self::getModel();
  132. return $model->updateById($id, $data);
  133. }
  134. //根据多个主键id更新 ssh 2019.9.3
  135. public static function updateByIds($ids, $data)
  136. {
  137. $model = self::getModel();
  138. return $model->updateByIds($ids, $data);
  139. }
  140. /**
  141. * 根据条件更新一条或多条记录
  142. */
  143. public static function updateByCondition($condition, $data)
  144. {
  145. $model = self::getModel();
  146. $count = $model->updateByCondition($condition, $data);
  147. return $count;
  148. }
  149. /**
  150. * 根据主键ID查出一条记录
  151. * @param $id
  152. * @param bool $returnObject
  153. * @param string $field
  154. * @return array|\yii\db\ActiveRecord|null
  155. */
  156. public static function getById($id, $returnObject = false, $field = '*')
  157. {
  158. $model = self::getModel();
  159. return $model->getById($id, $returnObject, $field);
  160. }
  161. //随机取一个 ssh 2020.2.10
  162. public static function getOne($returnObject = false, $order = false, $field = '*')
  163. {
  164. $model = self::getModel();
  165. return $model->getOne($returnObject, $order, $field);
  166. }
  167. /**
  168. * 根据条件查出一条记录
  169. * @param $condition
  170. * @param bool $returnObject
  171. * @param bool $order
  172. * @param string $field
  173. * @return array|\yii\db\ActiveRecord|null
  174. */
  175. public static function getByCondition($condition, $returnObject = false, $order = false, $field = '*')
  176. {
  177. $model = self::getModel();
  178. return $model->getByCondition($condition, $returnObject, $order, $field);
  179. }
  180. //是否存在 ssh 2019.8.30
  181. public static function exists($condition)
  182. {
  183. $model = self::getModel();
  184. return $model->exists($condition);
  185. }
  186. /**
  187. * 根据条件查出多条记录
  188. * @param array $condition
  189. * @param null $order
  190. * @param string $field
  191. * @param null $indexBy
  192. * @param bool $returnObject
  193. * @return array|\yii\db\ActiveRecord[]
  194. */
  195. public static function getAllByCondition($condition, $order = null, $field = '*', $indexBy = null, $returnObject = false)
  196. {
  197. $model = self::getModel();
  198. return $model->getAllByCondition($condition, $order, $field, $indexBy, $returnObject);
  199. }
  200. /**
  201. * 根据多个主键ID查询多条记录
  202. */
  203. public static function getByIds($ids, $order = null, $indexBy = null, $field = '*')
  204. {
  205. $model = self::getModel();
  206. return $model->getByIds($ids, $order, $indexBy, $field);
  207. }
  208. /**
  209. *根据条件查出数量
  210. */
  211. public static function getCount($condition)
  212. {
  213. $model = self::getModel();
  214. return $model->getCount($condition);
  215. }
  216. //计数器+1-1 shizq 2019-12-05
  217. public static function counters($counters, $condition, $params = [])
  218. {
  219. $model = self::getModel();
  220. return $model->counters($counters, $condition, $params);
  221. }
  222. //根据主键获取被销的信息 ssh 2021.5.18
  223. public static function getLockById($id, $field = '*')
  224. {
  225. $model = self::getModel();
  226. return $model->getLockById($id, $field);
  227. }
  228. //求和
  229. public static function sum($condition, $field)
  230. {
  231. $model = self::getModel();
  232. return $model->sum($condition, $field);
  233. }
  234. }