Service.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2020-01-30 09:53
  7. */
  8. namespace common\services;
  9. use backend\models\search\SearchInterface;
  10. use yii\base\Exception;
  11. use yii\data\ActiveDataProvider;
  12. use yii\db\ActiveRecord;
  13. use yii\web\NotFoundHttpException;
  14. abstract class Service extends \yii\base\BaseObject implements ServiceInterface
  15. {
  16. /**
  17. * @param array $options
  18. * @return mixed
  19. */
  20. abstract public function getSearchModel(array $options=[]);
  21. /**
  22. * @param $id
  23. * @param array $options
  24. * - scenario string model scenario(you can different with model scenario, every layer has its own definition).
  25. *
  26. *
  27. * @return mixed
  28. */
  29. abstract public function getModel($id, array $options=[]);
  30. /**
  31. * @param array $options
  32. * - scenario string model scenario(you can different with model scenario, every layer has its own definition).
  33. * - loadDefaultValues bool fill model with database column default value. default is true.
  34. * @return mixed
  35. */
  36. abstract public function newModel(array $options=[]);
  37. /**
  38. * get backend list.
  39. * first will get list by your provided search model($this->>getSearchModel($options)).
  40. * if getSearchModel returns null, will fetch all records by page.
  41. *
  42. * @param array $query
  43. * @param array $options
  44. * @return array
  45. * @throws Exception
  46. */
  47. public function getList(array $query = [], array $options=[])
  48. {
  49. $searchModel = $this->getSearchModel($options);
  50. if( $searchModel === null ){
  51. /** @var ActiveRecord $model */
  52. $model = $this->newModel();
  53. $result = [
  54. 'dataProvider' => new ActiveDataProvider([
  55. 'query' => $model->find(),
  56. ]),
  57. ];
  58. }else if( $searchModel instanceof SearchInterface ) {
  59. $dataProvider = $searchModel->search($query, $options);
  60. $result = [
  61. 'dataProvider' => $dataProvider,
  62. 'searchModel' => $searchModel,
  63. ];
  64. }else{
  65. throw new Exception("getSearchModel must return null or backend\models\search\SearchInterface ");
  66. }
  67. return $result;
  68. }
  69. /**
  70. * get record detail by primary key(usually column `id`).
  71. * if record not exists, will throw NotFound exception display a 404 page.
  72. *
  73. * @param $id
  74. * @param array $options
  75. * @return mixed
  76. * @throws NotFoundHttpException
  77. */
  78. public function getDetail($id, array $options = [])
  79. {
  80. $model = $this->getModel($id, $options);
  81. if( empty($model) ){
  82. throw new NotFoundHttpException("Record " . $id . " not exists");
  83. }
  84. return $model;
  85. }
  86. /**
  87. * do create a record.
  88. * if data validate error or save data error, will return whole model. otherwise return true.
  89. *
  90. * @param array $postData
  91. * @param array $options
  92. * @return bool|ActiveRecord
  93. */
  94. public function create(array $postData, array $options=[])
  95. {
  96. /** @var ActiveRecord $model */
  97. $model = $this->newModel($options);
  98. if( $model->load($postData) && $model->save() ){
  99. return true;
  100. }
  101. return $model;
  102. }
  103. /**
  104. * do update a record.
  105. * if data validate error or update data error, will return whole model. otherwise return true.
  106. *
  107. * @param $id
  108. * @param array $postData
  109. * @param array $options
  110. * @return bool|ActiveRecord
  111. * @throws NotFoundHttpException
  112. */
  113. public function update($id, array $postData, array $options=[])
  114. {
  115. /** @var ActiveRecord $model */
  116. $model = $this->getModel($id, $options);
  117. if( empty($model) ){
  118. throw new NotFoundHttpException("Record " . $id . " not exists");
  119. }
  120. if( $model->load($postData) && $model->save() ){
  121. return true;
  122. }
  123. return $model;
  124. }
  125. /**
  126. * do delete a record.
  127. * if delete error, will return whole model. otherwise return true.
  128. *
  129. * @param $id
  130. * @param array $options
  131. * @return bool|ActiveRecord
  132. * @throws NotFoundHttpException
  133. * @throws \Throwable
  134. * @throws \yii\db\StaleObjectException
  135. */
  136. public function delete($id, array $options=[])
  137. {
  138. /** @var ActiveRecord $model */
  139. $model = $this->getModel($id, $options);
  140. if( empty($model) ){
  141. throw new NotFoundHttpException("Record " . $id . " not exists");
  142. }
  143. $result = $model->delete();
  144. if( $result ){
  145. return true;
  146. }
  147. return $model;
  148. }
  149. /**
  150. * do update a record sort.
  151. * if data validate error or update data error, will return whole model. otherwise return true.
  152. *
  153. * @param $id
  154. * @param $sort
  155. * @param array $options
  156. * - sortField, string, your database table column name. default will be `sort`
  157. * @return bool|string
  158. */
  159. public function sort($id, $sort, array $options=[])
  160. {
  161. $sortField = "sort";
  162. if( isset($options['sortField']) && !empty($options['sortField']) ){
  163. $sortField = $options['sortField'];
  164. }
  165. /** @var ActiveRecord $model */
  166. $model = $this->getModel($id, $options);
  167. if( empty($model) ){
  168. return "Id " . $id . " not exists";
  169. }
  170. $model->$sortField = $sort;
  171. $result = $model->save();
  172. if ($result){
  173. return true;
  174. }
  175. return $model;
  176. }
  177. }