BaseClass.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * @return Base
  10. */
  11. protected static function getModel()
  12. {
  13. return Yii::createObject(['class' => static::$baseFile]);
  14. }
  15. //查询全部 ssh 2019.11.27
  16. public static function getAllList($select, $where, $order = '', $with = '')
  17. {
  18. $model = self::getModel();
  19. $return = $model->getAllList($select, $where, $order, $with);
  20. return $return;
  21. }
  22. //分页查询 ssh 2019.9.21
  23. public static function getList($select, $where, $order = '', $with = '')
  24. {
  25. $get = Yii::$app->request->get();
  26. $page = isset($get['page']) ? $get['page'] : 1;
  27. Yii::$app->params['page'] = $page;
  28. $pageSize = isset($get['pageSize']) && !empty($get['pageSize']) ? $get['pageSize'] : Yii::$app->params['pageSize'];
  29. $model = self::getModel();
  30. $return = $model->getList($select, $where, $page, $pageSize, $order, $with);
  31. return $return;
  32. }
  33. //查询指定条数 ssh 2019.11.30
  34. public static function getLimitList($select, $where, $limit = 10, $order = '', $with = '')
  35. {
  36. $model = self::getModel();
  37. $return = $model->getLimitList($select, $where, $limit, $order, $with);
  38. return $return;
  39. }
  40. public static function add($data, $returnObject = false)
  41. {
  42. $model = self::getModel();
  43. return $model->add($data, $returnObject);
  44. }
  45. /**
  46. * 批量添加
  47. */
  48. public static function batchAdd($data)
  49. {
  50. $model = self::getModel();
  51. $model->batchAdd($data);
  52. }
  53. /**
  54. * 根据主键ID删除一条记录
  55. * @param $id
  56. * @return false|int the number of rows deleted, or `false` if the deletion is unsuccessful for some reason.
  57. * @throws \Throwable
  58. * @throws \yii\db\StaleObjectException
  59. */
  60. public static function deleteById($id)
  61. {
  62. $model = self::getModel();
  63. return $model->deleteById($id);
  64. }
  65. /**
  66. *根据条件删除一条或多条记录
  67. */
  68. public static function deleteByCondition($condition)
  69. {
  70. $model = self::getModel();
  71. $count = $model->deleteByCondition($condition);
  72. return $count;
  73. }
  74. //根据id批量删 ssh 2019.12.7
  75. public static function deleteByIds($ids)
  76. {
  77. $model = self::getModel();
  78. return $model->deleteByIds($ids);
  79. }
  80. /**
  81. * 根据主键ID更新一条记录
  82. */
  83. public static function updateById($id, $data)
  84. {
  85. $model = self::getModel();
  86. return $model->updateById($id, $data);
  87. }
  88. //根据多个主键id更新 ssh 2019.9.3
  89. public static function updateByIds($ids, $data)
  90. {
  91. $model = self::getModel();
  92. return $model->updateByIds($ids, $data);
  93. }
  94. /**
  95. * 根据条件更新一条或多条记录
  96. */
  97. public static function updateByCondition($condition, $data)
  98. {
  99. $model = self::getModel();
  100. $count = $model->updateByCondition($condition, $data);
  101. return $count;
  102. }
  103. /**
  104. * 根据主键ID查出一条记录
  105. * @param $id
  106. * @param bool $returnObject
  107. * @param string $field
  108. * @return array|\yii\db\ActiveRecord|null
  109. */
  110. public static function getById($id, $returnObject = false, $field = '*')
  111. {
  112. $model = self::getModel();
  113. return $model->getById($id, $returnObject, $field);
  114. }
  115. //随机取一个 ssh 2020.2.10
  116. public static function getOne($returnObject = false, $order = false, $field = '*')
  117. {
  118. $model = self::getModel();
  119. return $model->getOne($returnObject, $order, $field);
  120. }
  121. /**
  122. *根据条件查出一条记录
  123. */
  124. public static function getByCondition($condition, $returnObject = false, $order = false, $field = '*')
  125. {
  126. $model = self::getModel();
  127. return $model->getByCondition($condition, $returnObject, $order, $field);
  128. }
  129. //是否存在 ssh 2019.8.30
  130. public static function exists($condition)
  131. {
  132. $model = self::getModel();
  133. return $model->exists($condition);
  134. }
  135. /**
  136. *根据条件查出多条记录
  137. */
  138. public static function getAllByCondition($condition, $order = null, $field = '*', $indexBy = null, $returnObject = false)
  139. {
  140. $model = self::getModel();
  141. return $model->getAllByCondition($condition, $order, $field, $indexBy, $returnObject);
  142. }
  143. /**
  144. * 根据多个主键ID查询多条记录
  145. */
  146. public static function getByIds($ids, $order = null, $indexBy = null, $field = '*')
  147. {
  148. $model = self::getModel();
  149. return $model->getByIds($ids, $order, $indexBy, $field);
  150. }
  151. /**
  152. *根据条件查出数量
  153. */
  154. public static function getCount($condition)
  155. {
  156. $model = self::getModel();
  157. return $model->getCount($condition);
  158. }
  159. //计数器+1-1 shizq 2019-12-05
  160. public static function counters($counters, $condition, $params = [])
  161. {
  162. $model = self::getModel();
  163. return $model->counters($counters, $condition, $params);
  164. }
  165. //根据主键获取被销的信息 ssh 2021.5.18
  166. public static function getLockById($id, $field = '*')
  167. {
  168. $model = self::getModel();
  169. return $model->getLockById($id, $field);
  170. }
  171. //求和
  172. public static function sum($condition, $field)
  173. {
  174. $model = self::getModel();
  175. return $model->sum($condition, $field);
  176. }
  177. }