BaseClass.php 8.0 KB

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