BaseBusiness.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace crmeb\basic;
  3. use think\Model;
  4. /**
  5. * Class BaseBusiness
  6. * @package crmeb\basic
  7. */
  8. abstract class BaseBusiness implements \ArrayAccess
  9. {
  10. /**
  11. * @var string
  12. */
  13. protected $pk = 'id';
  14. /**
  15. * 模型class
  16. * @var string
  17. */
  18. protected $modelClassName;
  19. /**
  20. * 模型实例
  21. * @var []
  22. */
  23. protected $modeHandle;
  24. /**
  25. * 数据列表
  26. * @var []
  27. */
  28. protected $list;
  29. /**
  30. * @var Model
  31. */
  32. protected $model;
  33. protected $where = [];
  34. /**
  35. * 获取模型
  36. * @param string|null $model
  37. * @return Model
  38. */
  39. public function getModel(string $model = null)
  40. {
  41. if (is_null($model)) {
  42. $model = $this->modelClassName;
  43. }
  44. if (isset($this->modeHandle[$model])) {
  45. return $this->modeHandle[$model];
  46. }
  47. return $this->modeHandle[$model] = new $model();
  48. }
  49. /**
  50. * 获取主键
  51. * @param string|null $pk
  52. * @return $this
  53. */
  54. protected function getPk(string $pk = null)
  55. {
  56. $pk = $pk ?: $this->pk;
  57. if (!$pk) {
  58. $pk = $this->getModel()->getPk();
  59. }
  60. return $pk;
  61. }
  62. /**
  63. * 状态开关
  64. * @param int $id
  65. * @param int $status
  66. * @return mixed
  67. */
  68. public function statusWitch(int $id, int $status, string $pk = null)
  69. {
  70. return $this->getModel()->where($this->getPk($pk), $id)->update(compact('status'));
  71. }
  72. /**
  73. * 删除开关
  74. * @param int $id
  75. * @param int $status
  76. * @return mixed
  77. */
  78. public function deleteWitch(int $id, int $is_del, string $pk = null)
  79. {
  80. return $this->getModel()->where($this->getPk($pk), $id)->update(compact('is_del'));
  81. }
  82. /**
  83. * 修改某个字段
  84. * @param int $id
  85. * @param $data
  86. * @param string|null $value
  87. * @param string $pk
  88. * @return bool
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. */
  93. public function updateField(int $id, $data, string $value = null, string $pk = null)
  94. {
  95. $resource = $this->getModel()->where($this->getPk($pk), $id)->find();
  96. if (!$resource) {
  97. return false;
  98. }
  99. if (is_array($data)) {
  100. foreach ($data as $field => $val) {
  101. $resource->{$field} = $val;
  102. }
  103. } elseif ($data && $value) {
  104. $resource->{$data} = $value;
  105. }
  106. return $resource->save();
  107. }
  108. /**
  109. * @param array $where
  110. * @return $this
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\DbException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. */
  115. public function getModelList(array $where = [])
  116. {
  117. if (!empty($where) && empty($this->where)) {
  118. $this->where = $where;
  119. }
  120. $count = $this->model->count($this->getPk($this->pk));
  121. $this->page();
  122. $this->list = $this->model->select();
  123. $this->model = null;
  124. return $this;
  125. }
  126. /**
  127. * @param int $id
  128. * @param array $where
  129. * @return $this
  130. */
  131. public function getFind(int $id, array $where = [])
  132. {
  133. $this->init()->beforeModel();
  134. $this->list = $this->model->get($id);
  135. $this->model = null;
  136. return $this;
  137. }
  138. /**
  139. * @return mixed
  140. */
  141. abstract protected function beforeModel();
  142. /**
  143. * @return mixed
  144. */
  145. abstract protected function afterModel();
  146. /**
  147. * 初始化model
  148. * @return $this
  149. */
  150. protected function init()
  151. {
  152. if (!$this->model) {
  153. $this->model = $this->getModel();
  154. }
  155. return $this;
  156. }
  157. /**
  158. * @param callable $callableModel
  159. * @return $this
  160. */
  161. public function callableWhere(callable $callableModel, array $where = [])
  162. {
  163. if (!empty($where) && empty($this->where)) {
  164. $this->where = $where;
  165. }
  166. $this->init()->beforeModel();
  167. $this->model = $callableModel($this->model, $this->where);
  168. return $this;
  169. }
  170. /**
  171. * @return $this
  172. */
  173. public function page()
  174. {
  175. if (isset($this->where['page']) && isset($this->where['limit'])) {
  176. $this->model = $this->model->page((int)$this->where['page'], (int)$this->where['limit']);
  177. }
  178. return $this;
  179. }
  180. /**
  181. * @param callable $each
  182. * @return mixed
  183. */
  184. public function each(callable $each = null)
  185. {
  186. $list = $this->list;
  187. if ($list) {
  188. if ($each) {
  189. $list = $list->each($each);
  190. }
  191. $list = $list->toArray();
  192. } else {
  193. $list = [];
  194. }
  195. $this->list = [];
  196. return $list;
  197. }
  198. protected function setAttr($name, $value)
  199. {
  200. $this->list[$name] = $value;
  201. return $this;
  202. }
  203. protected function getAttr($name)
  204. {
  205. return $this->list[$name];
  206. }
  207. /**
  208. * @param $name
  209. * @return mixed
  210. */
  211. public function __get($name)
  212. {
  213. return $this->list;
  214. }
  215. /**
  216. * @param $name
  217. * @param $arguments
  218. * @return mixed
  219. */
  220. public function __call($name, $arguments)
  221. {
  222. return $this->list->{$name}();
  223. }
  224. // ArrayAccess
  225. public function offsetSet($name, $value)
  226. {
  227. $this->setAttr($name, $value);
  228. }
  229. public function offsetGet($name)
  230. {
  231. return $this->getAttr($name);
  232. }
  233. public function offsetExists($offset)
  234. {
  235. // TODO: Implement offsetExists() method.
  236. }
  237. public function offsetUnset($offset)
  238. {
  239. // TODO: Implement offsetUnset() method.
  240. }
  241. }