* @date 2020-01-29 10:15:03 */ declare(strict_types=1); namespace Modules\Manage\Controllers; use App\Constants\CommonConstant; use App\Model\CategoryModel; use App\Repositories\CategoryRepository; use App\Repositories\Criterias\CategoryPagesCriteria; use App\Repositories\Criterias\OrderLimitCriteria; use Hyperf\Validation\Contract\ValidatorFactoryInterface; use Modules\Manage\Responses\CommonPagesResponse; class CategoryController extends BaseController { public function lists() { $validatorFactory = make(ValidatorFactoryInterface::class); $inputData = $this->getJsonData(); $rules = [ 'page' => 'nullable|integer', 'page_size' => 'nullable|integer', 'id' => 'nullable|string', 'name' => 'nullable|string', 'alias_name' => 'nullable|string', 'position' => 'nullable|string', ]; $validator = $validatorFactory->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $orders = [ ['created_at', 'DESC'], ]; $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE; if ($pageSize > 500) { $pageSize = CommonConstant::PAGE_SIZE; } $page = format_page($inputData['page']); $lists = make(CategoryRepository::class)->pushCriteria(new CategoryPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page); return new CommonPagesResponse($lists); } public function create() { $validatorFactory = make(ValidatorFactoryInterface::class); $inputData = $this->getJsonData(); $rules = [ 'name' => 'required|string|max:120', 'alias_name' => 'required|string|max:120', 'position' => 'nullable|string', 'squad' => 'nullable|int', 'parent_id' => 'nullable|int', 'cover_pic' => 'nullable|string', 't_color' => 'nullable|string', 'show_num' => 'nullable|int', ]; $validator = $validatorFactory->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $checkExists = CategoryModel::query()->where(['alias_name' => $inputData['alias_name']])->exists(); if ($checkExists) { return json_fail('别名重复'); } $obj = make(CategoryRepository::class)->createOne($inputData); if ($obj) { return json_success(['id' => $obj->id]); } else { return json_fail('添加通用分类失败'); } } public function modify(ValidatorFactoryInterface $validatorFactory) { $inputData = $this->getJsonData(); $rules = [ 'id' => 'required', 'name' => 'required|string|max:120', 'alias_name' => 'required|string|max:120', 'position' => 'nullable|string', 'squad' => 'nullable|int', 'parent_id' => 'nullable|int', 'cover_pic' => 'nullable|string', 't_color' => 'nullable|string', 'show_num' => 'nullable|int', ]; $validator = $validatorFactory->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $checkExists = CategoryModel::query()->where('id', '!=', $inputData['id'])->where(['alias_name' => $inputData['alias_name']])->exists(); if ($checkExists) { return json_fail('别名重复'); } $resultInfo = make(CategoryRepository::class)->updateOne($inputData['id'], $inputData); if ($resultInfo['is_ok']) { $logSubject = '编辑一个通用分类:'. $resultInfo['new']->name; $logSvalue = $inputData['id']; $logDbData = [ 'after_items' => [ $resultInfo['new']->toArray(), ], 'before_items' => [ $resultInfo['old']->toArray(), ], ]; $this->log($logSubject, $logSvalue, $logDbData); return json_success(['id' => $inputData['id']]); } else { return json_fail('编辑通用分类失败'); } } public function delete() { $validatorFactory = make(ValidatorFactoryInterface::class); $inputData = $this->getJsonData(); $rules = [ 'id' => 'required', ]; $validator = $validatorFactory->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $id = data_get($inputData, 'id', ''); $resultInfo = make(CategoryRepository::class)->deleteOne($id); if ($resultInfo['is_ok']) { $logSubject = '删除一个通用分类:'. $resultInfo['old']->name; $logSvalue = $id; $logDbData = [ 'after_items' => [ ], 'before_items' => [ ], ]; $this->log($logSubject, $logSvalue, $logDbData); return json_success(['id' => $id]); } else { return json_fail('删除通用分类失败'); } } public function batch() { $inputData = $this->getJsonData(); $operateName = $inputData['operate_name'] ?? ''; switch ($operateName) { case 'delete': $itemPks = $inputData['item_pks'] ?? []; if (!empty($itemPks)) { $resultInfo = make(CategoryRepository::class)->deleteMulti($itemPks); if ($resultInfo['success_ids']) { $strPks = implode(',', $resultInfo['success_ids']); $logSubject = '批量删除多个通用分类'; $logSvalue = $strPks; $logDbData = [ 'after_items' => [ ], 'before_items' => [], ]; $this->log($logSubject, $logSvalue, $logDbData); } } $data = [ 'result' => 'success', 'pks' => $itemPks, ]; return json_success($data); break; case 'push_perfect': $aliasName = $inputData['alias_name'] ?? ''; $itemPks = $inputData['item_pks'] ?? []; if (!empty($itemPks) && !empty($aliasName)) { $resultInfo = make(CategoryRepository::class)->pushPerfectVideo($aliasName, $itemPks); if ($resultInfo['success_ids']) { $strPks = implode(',', $resultInfo['success_ids']); $logSubject = '批量推送视频到精选桶:'. $aliasName; $logSvalue = $strPks; $logDbData = [ 'after_items' => [ ], 'before_items' => [], ]; $this->log($logSubject, $logSvalue, $logDbData); } } $data = [ 'result' => 'success', 'pks' => $itemPks, ]; return json_success($data); break; case 'pop_perfect': $itemPks = $inputData['item_pks'] ?? []; if (!empty($itemPks)) { $resultInfo = make(CategoryRepository::class)->popPerfectVideo($itemPks); if ($resultInfo['success_ids']) { $strPks = implode(',', $resultInfo['success_ids']); $logSubject = '批量从桶视频里剔除视频'; $logSvalue = $strPks; $logDbData = [ 'after_items' => [ ], 'before_items' => [], ]; $this->log($logSubject, $logSvalue, $logDbData); } } $data = [ 'result' => 'success', 'pks' => $itemPks, ]; return json_success($data); break; default: break; } return json_fail('未知操作'); } /** * @return CommonPagesResponse */ public function all() { $validatorFactory = make(ValidatorFactoryInterface::class); $orders = [ ['created_at', 'DESC'], ]; $lists = CategoryModel::query()->orderByDesc('squad')->orderByDesc('id')->get(); return json_success($lists); } }