BaseClass.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. * @param $condition
  92. * @return int
  93. */
  94. public static function deleteByCondition($condition)
  95. {
  96. $model = self::getModel();
  97. $count = $model->deleteByCondition($condition);
  98. return $count;
  99. }
  100. //根据id批量删 ssh 2019.12.7
  101. public static function deleteByIds($ids)
  102. {
  103. $model = self::getModel();
  104. return $model->deleteByIds($ids);
  105. }
  106. /**
  107. * 根据主键ID更新一条记录
  108. */
  109. public static function updateById($id, $data)
  110. {
  111. $model = self::getModel();
  112. return $model->updateById($id, $data);
  113. }
  114. //根据多个主键id更新 ssh 2019.9.3
  115. public static function updateByIds($ids, $data)
  116. {
  117. $model = self::getModel();
  118. return $model->updateByIds($ids, $data);
  119. }
  120. /**
  121. * 根据条件更新一条或多条记录
  122. */
  123. public static function updateByCondition($condition, $data)
  124. {
  125. $model = self::getModel();
  126. $count = $model->updateByCondition($condition, $data);
  127. return $count;
  128. }
  129. /**
  130. * 根据主键ID查出一条记录
  131. * @param $id
  132. * @param bool $returnObject
  133. * @param string $field
  134. * @return array|\yii\db\ActiveRecord|null
  135. */
  136. public static function getById($id, $returnObject = false, $field = '*')
  137. {
  138. $model = self::getModel();
  139. return $model->getById($id, $returnObject, $field);
  140. }
  141. //随机取一个 ssh 2020.2.10
  142. public static function getOne($returnObject = false, $order = false, $field = '*')
  143. {
  144. $model = self::getModel();
  145. return $model->getOne($returnObject, $order, $field);
  146. }
  147. /**
  148. * 根据条件查出一条记录
  149. * @param $condition
  150. * @param bool $returnObject
  151. * @param bool $order
  152. * @param string $field
  153. * @return array|\yii\db\ActiveRecord|null
  154. */
  155. public static function getByCondition($condition, $returnObject = false, $order = false, $field = '*')
  156. {
  157. $model = self::getModel();
  158. return $model->getByCondition($condition, $returnObject, $order, $field);
  159. }
  160. /**
  161. * 是否存在
  162. * @param $condition
  163. * @return bool
  164. * @throws \yii\base\InvalidConfigException
  165. */
  166. public static function exists($condition)
  167. {
  168. $model = self::getModel();
  169. return $model->exists($condition);
  170. }
  171. /**
  172. * 根据条件查出多条记录
  173. * @param array $condition
  174. * @param null $order
  175. * @param string $field
  176. * @param null $indexBy
  177. * @param bool $returnObject
  178. * @return array|\yii\db\ActiveRecord[]
  179. */
  180. public static function getAllByCondition($condition, $order = null, $field = '*', $indexBy = null, $returnObject = false)
  181. {
  182. $model = self::getModel();
  183. return $model->getAllByCondition($condition, $order, $field, $indexBy, $returnObject);
  184. }
  185. /**
  186. * 根据多个主键ID查询多条记录
  187. */
  188. public static function getByIds($ids, $order = null, $indexBy = null, $field = '*')
  189. {
  190. $model = self::getModel();
  191. return $model->getByIds($ids, $order, $indexBy, $field);
  192. }
  193. /**
  194. *根据条件查出数量
  195. */
  196. public static function getCount($condition)
  197. {
  198. $model = self::getModel();
  199. return $model->getCount($condition);
  200. }
  201. //计数器+1-1 shizq 2019-12-05
  202. public static function counters($counters, $condition, $params = [])
  203. {
  204. $model = self::getModel();
  205. return $model->counters($counters, $condition, $params);
  206. }
  207. //根据主键获取被销的信息 ssh 2021.5.18
  208. public static function getLockById($id, $field = '*')
  209. {
  210. $model = self::getModel();
  211. return $model->getLockById($id, $field);
  212. }
  213. //求和
  214. public static function sum($condition, $field)
  215. {
  216. $model = self::getModel();
  217. return $model->sum($condition, $field);
  218. }
  219. }