BaseClass.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. /**
  41. * @param $data
  42. * @param bool $returnObject
  43. * @return array|Base
  44. * @throws \Exception
  45. */
  46. public static function add($data, $returnObject = false)
  47. {
  48. $model = self::getModel();
  49. return $model->add($data, $returnObject);
  50. }
  51. /**
  52. * 批量添加
  53. */
  54. public static function batchAdd($data)
  55. {
  56. $model = self::getModel();
  57. $model->batchAdd($data);
  58. }
  59. /**
  60. * 根据主键ID删除一条记录
  61. * @param $id
  62. * @return false|int the number of rows deleted, or `false` if the deletion is unsuccessful for some reason.
  63. * @throws \Throwable
  64. * @throws \yii\db\StaleObjectException
  65. */
  66. public static function deleteById($id)
  67. {
  68. $model = self::getModel();
  69. return $model->deleteById($id);
  70. }
  71. /**
  72. *根据条件删除一条或多条记录
  73. */
  74. public static function deleteByCondition($condition)
  75. {
  76. $model = self::getModel();
  77. $count = $model->deleteByCondition($condition);
  78. return $count;
  79. }
  80. //根据id批量删 ssh 2019.12.7
  81. public static function deleteByIds($ids)
  82. {
  83. $model = self::getModel();
  84. return $model->deleteByIds($ids);
  85. }
  86. /**
  87. * 根据主键ID更新一条记录
  88. */
  89. public static function updateById($id, $data)
  90. {
  91. $model = self::getModel();
  92. return $model->updateById($id, $data);
  93. }
  94. //根据多个主键id更新 ssh 2019.9.3
  95. public static function updateByIds($ids, $data)
  96. {
  97. $model = self::getModel();
  98. return $model->updateByIds($ids, $data);
  99. }
  100. /**
  101. * 根据条件更新一条或多条记录
  102. */
  103. public static function updateByCondition($condition, $data)
  104. {
  105. $model = self::getModel();
  106. $count = $model->updateByCondition($condition, $data);
  107. return $count;
  108. }
  109. /**
  110. * 根据主键ID查出一条记录
  111. * @param $id
  112. * @param bool $returnObject
  113. * @param string $field
  114. * @return array|\yii\db\ActiveRecord|null
  115. */
  116. public static function getById($id, $returnObject = false, $field = '*')
  117. {
  118. $model = self::getModel();
  119. return $model->getById($id, $returnObject, $field);
  120. }
  121. //随机取一个 ssh 2020.2.10
  122. public static function getOne($returnObject = false, $order = false, $field = '*')
  123. {
  124. $model = self::getModel();
  125. return $model->getOne($returnObject, $order, $field);
  126. }
  127. /**
  128. * 根据条件查出一条记录
  129. * @param $condition
  130. * @param bool $returnObject
  131. * @param bool $order
  132. * @param string $field
  133. * @return array|\yii\db\ActiveRecord|null
  134. */
  135. public static function getByCondition($condition, $returnObject = false, $order = false, $field = '*')
  136. {
  137. $model = self::getModel();
  138. return $model->getByCondition($condition, $returnObject, $order, $field);
  139. }
  140. //是否存在 ssh 2019.8.30
  141. public static function exists($condition)
  142. {
  143. $model = self::getModel();
  144. return $model->exists($condition);
  145. }
  146. /**
  147. *根据条件查出多条记录
  148. */
  149. public static function getAllByCondition($condition, $order = null, $field = '*', $indexBy = null, $returnObject = false)
  150. {
  151. $model = self::getModel();
  152. return $model->getAllByCondition($condition, $order, $field, $indexBy, $returnObject);
  153. }
  154. /**
  155. * 根据多个主键ID查询多条记录
  156. */
  157. public static function getByIds($ids, $order = null, $indexBy = null, $field = '*')
  158. {
  159. $model = self::getModel();
  160. return $model->getByIds($ids, $order, $indexBy, $field);
  161. }
  162. /**
  163. *根据条件查出数量
  164. */
  165. public static function getCount($condition)
  166. {
  167. $model = self::getModel();
  168. return $model->getCount($condition);
  169. }
  170. //计数器+1-1 shizq 2019-12-05
  171. public static function counters($counters, $condition, $params = [])
  172. {
  173. $model = self::getModel();
  174. return $model->counters($counters, $condition, $params);
  175. }
  176. //根据主键获取被销的信息 ssh 2021.5.18
  177. public static function getLockById($id, $field = '*')
  178. {
  179. $model = self::getModel();
  180. return $model->getLockById($id, $field);
  181. }
  182. //求和
  183. public static function sum($condition, $field)
  184. {
  185. $model = self::getModel();
  186. return $model->sum($condition, $field);
  187. }
  188. }