BaseClass.php 6.5 KB

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