DictionaryController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace frontend\modules\api\controllers;
  3. use common\components\AjaxException;
  4. use common\models\Dictionary;
  5. use common\services\DictionaryRelationshipService;
  6. use common\services\DictionaryService;
  7. use frontend\modules\api\components\BaseAdminController;
  8. use Throwable;
  9. use Yii;
  10. use yii\base\InvalidConfigException;
  11. use yii\db\Exception;
  12. use yii\web\Response;
  13. class DictionaryController extends BaseAdminController
  14. {
  15. /**
  16. * 字典对象数据
  17. * @param $is_all
  18. * @return Response
  19. */
  20. public function actionList($is_all = 0)
  21. {
  22. //返回
  23. if ($is_all == 1) {
  24. $data['data'] = DictionaryService::getDictionaryObjAllData();
  25. } else {
  26. $data['data'] = DictionaryService::getDictionaryObjShowData();
  27. }
  28. return $this->asJson($data);
  29. }
  30. /**
  31. * 字典分类列表
  32. * @return Response
  33. */
  34. public function actionListCategory($category)
  35. {
  36. $post = Yii::$app->request->post();
  37. $query = Dictionary::find()
  38. ->where(['classname' => $category]);
  39. //排序
  40. $this->initSequence($query);
  41. //主要筛选条件
  42. $query = $this->addConditionToQuery("optionname", $query, true);
  43. $data['data'] = $query->asArray()->all();
  44. return $this->asJson($data);
  45. }
  46. /**
  47. * 字典分类列表
  48. * @return Response
  49. */
  50. public function actionListCategoryName()
  51. {
  52. $info = Dictionary::find()
  53. ->select("id,classname")
  54. ->orderBy("id asc")
  55. ->asArray()
  56. ->all();
  57. //组合
  58. $arr = [];
  59. foreach ($info as $key => $value) {
  60. if (!in_array($value['classname'], $arr)) {
  61. $arr[] = $value['classname'];
  62. }
  63. }
  64. //返回
  65. $data['data'] = $arr;
  66. return $this->asJson($data);
  67. }
  68. /**
  69. * 字典列表
  70. * @return Response
  71. */
  72. public function actionListAll()
  73. {
  74. $info = Dictionary::find()
  75. ->where(['status' => 1])
  76. ->orderBy("id asc")
  77. ->asArray()
  78. ->all();
  79. //返回
  80. $data['data'] = $info;
  81. return $this->asJson($data);
  82. }
  83. /**
  84. * 字典列表
  85. * @return Response
  86. */
  87. public function actionAll()
  88. {
  89. $query = Dictionary::find();
  90. $data['data'] = $query->asArray()->all();
  91. return $this->asJson($data);
  92. }
  93. /**
  94. * 字典添加
  95. * @return Response
  96. * @throws Exception
  97. * @throws Throwable
  98. */
  99. public function actionAdd()
  100. {
  101. $post = Yii::$app->request->post();
  102. if (!isset($post["name"]) || strlen(trim($post["name"])) == 0) {
  103. return $this->asJson([], 1, "名称不可以为空!");
  104. }
  105. $parent = null;
  106. //判断父分类是否存在
  107. if ($post["pid"] != 0) {
  108. $parent = Dictionary::findOne($post["pid"]);
  109. if (!$parent) {
  110. throw new AjaxException("分类不存在!");
  111. }
  112. }
  113. $dictionary = Dictionary::find()->where(["name" => $post["name"], "pid" => $post["pid"]])->one();
  114. if ($dictionary) {
  115. return $this->asJson([], 1, "名称 {$post["name"]} 已存在!");
  116. }
  117. $param = [
  118. "has_children" => $post["has_children"],
  119. "name" => $post["name"],
  120. "pid" => $post["pid"],
  121. "status" => $post["status"],
  122. "multiple" => $post["multiple"],
  123. "sort" => $post["sort"] ?: 0,
  124. ];
  125. $model = new Dictionary($param);
  126. $model->param_name = "";
  127. Yii::$app->db->transaction(function () use ($model, $parent, $post) {
  128. if (!$model->save()) {
  129. throw new Exception($model->getErrorSummary(true)[0]);
  130. }
  131. //给父分类的has_child如果为0,那么改为1
  132. if ($post["pid"] != 0) {
  133. if ($parent->has_children == 0) {
  134. $parent->has_children = 1;
  135. $parent->save();
  136. }
  137. }
  138. //添加派生关系
  139. if ($post["derive"] != []) {
  140. DictionaryRelationshipService::addByDerive($post["derive"], $model->id);
  141. }
  142. });
  143. return $this->asJson();
  144. }
  145. /**
  146. * 字典修改
  147. * @param $id
  148. * @return Response
  149. * @throws AjaxException
  150. * @throws InvalidConfigException
  151. * @throws Throwable
  152. */
  153. public function actionUpdate($id)
  154. {
  155. $post = Yii::$app->request->post();
  156. //校验
  157. /** @var Dictionary $dictionary */
  158. $model = Dictionary::findOne(['id' => $post["id"]]);
  159. if (!$model) {
  160. return $this->asJson([], 1, "记录不存在!"); //通知前端
  161. }
  162. $parent = null;
  163. //判断父分类是否存在
  164. if ($post["pid"] != 0) {
  165. $parent = Dictionary::findOne($post["pid"]);
  166. if (!$parent) {
  167. throw new AjaxException("分类不存在!");
  168. }
  169. }
  170. //校验是否重名
  171. $otherInfo = Dictionary::find()
  172. ->where(['<>', 'id', $id])
  173. ->andWhere(["name" => $post["name"], "pid" => $post["pid"]])
  174. ->one();
  175. if ($otherInfo) {
  176. return $this->asJson([], 1, "名称 {$post["name"]} 已存在!"); //通知前端
  177. }
  178. $this->setAttributeFromGetAndPost($model);
  179. Yii::$app->db->transaction(function () use ($model, $parent, $post) {
  180. if (!$model->save()) {
  181. throw new Exception($model->getErrorSummary(true)[0]);
  182. }
  183. //给父分类的has_child如果为0,那么改为1
  184. if ($post["pid"] != 0) {
  185. if ($parent->has_children == 0) {
  186. $parent->has_children = 1;
  187. $parent->save();
  188. }
  189. }
  190. //修改派生关系
  191. if ($post["derive"] != []) {
  192. DictionaryRelationshipService::updateByDerive($post["derive"], $model->id);
  193. }
  194. });
  195. return $this->asJson();
  196. }
  197. }