CategoryController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * 通用分类控制器.
  4. *
  5. * @author jim <jim458734573@gmail.com>
  6. * @date 2020-01-29 10:15:03
  7. */
  8. declare(strict_types=1);
  9. namespace Modules\Manage\Controllers;
  10. use App\Constants\CommonConstant;
  11. use App\Model\CategoryModel;
  12. use App\Repositories\CategoryRepository;
  13. use App\Repositories\Criterias\CategoryPagesCriteria;
  14. use App\Repositories\Criterias\OrderLimitCriteria;
  15. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  16. use Modules\Manage\Responses\CommonPagesResponse;
  17. class CategoryController extends BaseController
  18. {
  19. public function lists()
  20. {
  21. $validatorFactory = make(ValidatorFactoryInterface::class);
  22. $inputData = $this->getJsonData();
  23. $rules = [
  24. 'page' => 'nullable|integer',
  25. 'page_size' => 'nullable|integer',
  26. 'id' => 'nullable|string',
  27. 'name' => 'nullable|string',
  28. 'alias_name' => 'nullable|string',
  29. 'position' => 'nullable|string',
  30. ];
  31. $validator = $validatorFactory->make($inputData, $rules);
  32. if ($validator->fails()) {
  33. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  34. return json_fail($validator->errors()->first());
  35. }
  36. $orders = [
  37. ['created_at', 'DESC'],
  38. ];
  39. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  40. if ($pageSize > 500) {
  41. $pageSize = CommonConstant::PAGE_SIZE;
  42. }
  43. $page = format_page($inputData['page']);
  44. $lists = make(CategoryRepository::class)->pushCriteria(new CategoryPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  45. return new CommonPagesResponse($lists);
  46. }
  47. public function create()
  48. {
  49. $validatorFactory = make(ValidatorFactoryInterface::class);
  50. $inputData = $this->getJsonData();
  51. $rules = [
  52. 'name' => 'required|string|max:120',
  53. 'alias_name' => 'required|string|max:120',
  54. 'position' => 'nullable|string',
  55. 'squad' => 'nullable|int',
  56. 'parent_id' => 'nullable|int',
  57. 'cover_pic' => 'nullable|string',
  58. 't_color' => 'nullable|string',
  59. 'show_num' => 'nullable|int',
  60. ];
  61. $validator = $validatorFactory->make($inputData, $rules);
  62. if ($validator->fails()) {
  63. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  64. return json_fail($validator->errors()->first());
  65. }
  66. $checkExists = CategoryModel::query()->where(['alias_name' => $inputData['alias_name']])->exists();
  67. if ($checkExists) {
  68. return json_fail('别名重复');
  69. }
  70. $obj = make(CategoryRepository::class)->createOne($inputData);
  71. if ($obj) {
  72. return json_success(['id' => $obj->id]);
  73. } else {
  74. return json_fail('添加通用分类失败');
  75. }
  76. }
  77. public function modify(ValidatorFactoryInterface $validatorFactory)
  78. {
  79. $inputData = $this->getJsonData();
  80. $rules = [
  81. 'id' => 'required',
  82. 'name' => 'required|string|max:120',
  83. 'alias_name' => 'required|string|max:120',
  84. 'position' => 'nullable|string',
  85. 'squad' => 'nullable|int',
  86. 'parent_id' => 'nullable|int',
  87. 'cover_pic' => 'nullable|string',
  88. 't_color' => 'nullable|string',
  89. 'show_num' => 'nullable|int',
  90. ];
  91. $validator = $validatorFactory->make($inputData, $rules);
  92. if ($validator->fails()) {
  93. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  94. return json_fail($validator->errors()->first());
  95. }
  96. $checkExists = CategoryModel::query()->where('id', '!=', $inputData['id'])->where(['alias_name' => $inputData['alias_name']])->exists();
  97. if ($checkExists) {
  98. return json_fail('别名重复');
  99. }
  100. $resultInfo = make(CategoryRepository::class)->updateOne($inputData['id'], $inputData);
  101. if ($resultInfo['is_ok']) {
  102. $logSubject = '编辑一个通用分类:'. $resultInfo['new']->name;
  103. $logSvalue = $inputData['id'];
  104. $logDbData = [
  105. 'after_items' => [
  106. $resultInfo['new']->toArray(),
  107. ],
  108. 'before_items' => [
  109. $resultInfo['old']->toArray(),
  110. ],
  111. ];
  112. $this->log($logSubject, $logSvalue, $logDbData);
  113. return json_success(['id' => $inputData['id']]);
  114. } else {
  115. return json_fail('编辑通用分类失败');
  116. }
  117. }
  118. public function delete()
  119. {
  120. $validatorFactory = make(ValidatorFactoryInterface::class);
  121. $inputData = $this->getJsonData();
  122. $rules = [
  123. 'id' => 'required',
  124. ];
  125. $validator = $validatorFactory->make($inputData, $rules);
  126. if ($validator->fails()) {
  127. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  128. return json_fail($validator->errors()->first());
  129. }
  130. $id = data_get($inputData, 'id', '');
  131. $resultInfo = make(CategoryRepository::class)->deleteOne($id);
  132. if ($resultInfo['is_ok']) {
  133. $logSubject = '删除一个通用分类:'. $resultInfo['old']->name;
  134. $logSvalue = $id;
  135. $logDbData = [
  136. 'after_items' => [
  137. ],
  138. 'before_items' => [
  139. ],
  140. ];
  141. $this->log($logSubject, $logSvalue, $logDbData);
  142. return json_success(['id' => $id]);
  143. } else {
  144. return json_fail('删除通用分类失败');
  145. }
  146. }
  147. public function batch()
  148. {
  149. $inputData = $this->getJsonData();
  150. $operateName = $inputData['operate_name'] ?? '';
  151. switch ($operateName) {
  152. case 'delete':
  153. $itemPks = $inputData['item_pks'] ?? [];
  154. if (!empty($itemPks)) {
  155. $resultInfo = make(CategoryRepository::class)->deleteMulti($itemPks);
  156. if ($resultInfo['success_ids']) {
  157. $strPks = implode(',', $resultInfo['success_ids']);
  158. $logSubject = '批量删除多个通用分类';
  159. $logSvalue = $strPks;
  160. $logDbData = [
  161. 'after_items' => [
  162. ],
  163. 'before_items' => [],
  164. ];
  165. $this->log($logSubject, $logSvalue, $logDbData);
  166. }
  167. }
  168. $data = [
  169. 'result' => 'success',
  170. 'pks' => $itemPks,
  171. ];
  172. return json_success($data);
  173. break;
  174. case 'push_perfect':
  175. $aliasName = $inputData['alias_name'] ?? '';
  176. $itemPks = $inputData['item_pks'] ?? [];
  177. if (!empty($itemPks) && !empty($aliasName)) {
  178. $resultInfo = make(CategoryRepository::class)->pushPerfectVideo($aliasName, $itemPks);
  179. if ($resultInfo['success_ids']) {
  180. $strPks = implode(',', $resultInfo['success_ids']);
  181. $logSubject = '批量推送视频到精选桶:'. $aliasName;
  182. $logSvalue = $strPks;
  183. $logDbData = [
  184. 'after_items' => [
  185. ],
  186. 'before_items' => [],
  187. ];
  188. $this->log($logSubject, $logSvalue, $logDbData);
  189. }
  190. }
  191. $data = [
  192. 'result' => 'success',
  193. 'pks' => $itemPks,
  194. ];
  195. return json_success($data);
  196. break;
  197. case 'pop_perfect':
  198. $itemPks = $inputData['item_pks'] ?? [];
  199. if (!empty($itemPks)) {
  200. $resultInfo = make(CategoryRepository::class)->popPerfectVideo($itemPks);
  201. if ($resultInfo['success_ids']) {
  202. $strPks = implode(',', $resultInfo['success_ids']);
  203. $logSubject = '批量从桶视频里剔除视频';
  204. $logSvalue = $strPks;
  205. $logDbData = [
  206. 'after_items' => [
  207. ],
  208. 'before_items' => [],
  209. ];
  210. $this->log($logSubject, $logSvalue, $logDbData);
  211. }
  212. }
  213. $data = [
  214. 'result' => 'success',
  215. 'pks' => $itemPks,
  216. ];
  217. return json_success($data);
  218. break;
  219. default:
  220. break;
  221. }
  222. return json_fail('未知操作');
  223. }
  224. /**
  225. * @return CommonPagesResponse
  226. */
  227. public function all()
  228. {
  229. $validatorFactory = make(ValidatorFactoryInterface::class);
  230. $orders = [
  231. ['created_at', 'DESC'],
  232. ];
  233. $lists = CategoryModel::query()->orderByDesc('squad')->orderByDesc('id')->get();
  234. return json_success($lists);
  235. }
  236. }