getJsonData(); // 针对layui table自动分页请求参数,优先GET值. $queryPage = request()->has('page') ? request()->input('page', 1) : null; $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null; if (!is_null($queryPage)) { $inputData['page'] = $queryPage; } if (!is_null($queryPageSize)) { $inputData['page_size'] = $queryPageSize; } $rules = [ 'page' => 'nullable|integer', 'page_size' => 'nullable|integer', 'deduction_type' => 'required|string', 'app_uid' => 'nullable|string', ]; $validator = $validatorFactory->make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $orders = [ ['created_at', 'DESC'], ]; if (!isset($inputData['page_size']) || $inputData['page_size'] > 500) { $pageSize = CommonConstant::PAGE_SIZE; } else { $pageSize = $inputData['page_size']; } $page = format_page($inputData['page'] ?? 0); $res = $DedRepo->pushCriteria(new AppDeductionRuleCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page); return new AppDeductionRuleResponse($res); } /** * 新增扣量规则 * @param Validator $validatorFactory * @return \Illuminate\Http\JsonResponse|ConfigSuccessResponse */ public function create() { $inputData = $this->getJsonData(); $rules = [ 'deduction_type' => 'required|string', 'deduction_rule' => 'nullable|integer', 'deduction_method' => 'nullable|integer', 'app_uid' => 'required|string', 'deduction_price' => 'nullable|string', 'deduction_pro' => 'nullable|string', 'jdata' => 'nullable|string', ]; $validator = Validator::make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } /** * @var AppDeductionRuleRepository $configRepository */ $DedRepo = make(AppDeductionRuleRepository::class); $setarr = [ 'deduction_type' => $inputData['deduction_type'], 'deduction_rule' => $inputData['deduction_rule'] ?? 0, 'app_uid' => $inputData['app_uid'], 'deduction_price' => $inputData['deduction_price'] ?? '', 'deduction_method' => $inputData['deduction_method'] ?? '', 'deduction_pro' => $inputData['deduction_pro'], 'jdata' => $inputData['jdata'], ]; $result = $DedRepo->create($setarr); if (!$result) { return json_fail('新增失败'); } else { $logSubject = '新增一个扣量规则,:' . $result->uid; $logSvalue = $result->uid; $logDbData = [ 'after_items' => [ $setarr, ], 'before_items' => [ ], ]; $this->log($logSubject, $logSvalue, $logDbData); return new ConfigSuccessResponse(); } } /** * 修改规则 * * @return \Illuminate\Http\JsonResponse|ConfigSuccessResponse */ public function modify() { $inputData = $this->getJsonData(); $rules = [ 'deduction_rule' => 'nullable|integer', 'app_uid' => 'required|string', 'uid' => 'required|string', 'deduction_price' => 'nullable|string', 'deduction_pro' => 'nullable|string', 'deduction_method' => 'nullable|integer', 'jdata' => 'nullable|string', ]; $validator = Validator::make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } /** * @var AppDeductionRuleRepository $managerRepo */ $DedRepo = app(AppDeductionRuleRepository::class); $deqInfo = $DedRepo->findBy('uid', $inputData['uid']); if (empty($deqInfo)) { return json_fail(trans('common.err_resource_not_found')); } $olddeqInfo = clone $deqInfo; if (isset($inputData['deduction_type'])) { $deqInfo->deduction_type = $inputData['deduction_type']; } if (isset($inputData['deduction_rule'])) { $deqInfo->deduction_rule = $inputData['deduction_rule']; } if (isset($inputData['app_uid'])) { $deqInfo->app_uid = $inputData['app_uid']; } if (isset($inputData['deduction_price'])) { $deqInfo->deduction_price = $inputData['deduction_price']; } if (isset($inputData['deduction_pro'])) { $deqInfo->deduction_pro = $inputData['deduction_pro']; } if (isset($inputData['deduction_method'])) { $deqInfo->deduction_method = $inputData['deduction_method']; } if (isset($inputData['jdata'])) { $deqInfo->jdata = $inputData['jdata']; } $result = $deqInfo->save(); if (!$result) { return json_fail('修改失败'); } else { $logSubject = '修改一个扣量规则, uid是:' . $deqInfo->uid; $logSvalue = $deqInfo->uid; $logDbData = [ 'after_items' => [ $deqInfo->toArray(), ], 'before_items' => [ $olddeqInfo->toArray(), ], ]; $this->log($logSubject, $logSvalue, $logDbData); //删除缓存 $DedRepo->delRuleCache($deqInfo->deduction_type); return new ConfigSuccessResponse(); } } public function delete() { $inputData = $this->getJsonData(); $rules = [ 'uid' => 'required|string', ]; $validator = Validator::make($inputData, $rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } /** * @var AppDeductionRuleRepository $managerRepo */ $DedRepo = app(AppDeductionRuleRepository::class); $deqInfo = $DedRepo->findBy('uid', $inputData['uid']); $olddeqInfo = clone $deqInfo; $result = $deqInfo->delete(); if (!$result) { return json_fail('删除规则失败'); } else { $logSubject = '删除一个扣量规则, uid是:' . $deqInfo->uid; $logSvalue = $deqInfo->uid; $logDbData = [ 'after_items' => [ $deqInfo->toArray(), ], 'before_items' => [ $olddeqInfo->toArray(), ], ]; $this->log($logSubject, $logSvalue, $logDbData); //删除缓存 $DedRepo->delRuleCache($deqInfo->deduction_type); return new CommonSuccessUidResponse($deqInfo->uid); } } /** * 规则详情 * * @param Validator $validatorFactory * @return \Illuminate\Http\JsonResponse|AppDeductionRuleResponse */ public function detail(Validator $validatorFactory) { $inputData = $this->getJsonData(); /** * @var AppDeductionRuleRepository $managerRepo */ $DedRepo = app(AppDeductionRuleRepository::class); $rules = [ 'uid' => 'required|string', ]; $validator = Validator::make($inputData,$rules); if ($validator->fails()) { // 如果有错误(可能是多个信息),这里只取第一个错误信息返回. return json_fail($validator->errors()->first()); } $res = $DedRepo->findBy('uid', $inputData['uid']); return new AppDeductionRuleResponse($res); } }