| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- namespace Modules\ManageApi\Controllers;
- use App\Constants\CommonConstant;
- use App\Repositories\AppDeductionRuleRepository;
- use App\Repositories\Criterias\AppDeductionRuleCriteria;
- use App\Repositories\Criterias\OrderLimitCriteria;
- use Illuminate\Support\Facades\Validator;
- use Modules\ManageApi\Responses\AppDeductionRuleResponse;
- use Modules\ManageApi\Responses\CommonSuccessUidResponse;
- use Modules\ManageApi\Responses\ConfigSuccessResponse;
- class AppDeductionRuleController extends BaseController
- {
- /**
- * 扣量规则列表
- * @return mixed
- */
- public function lists()
- {
- /**
- * @var Validator $validatorFactory
- */
- $validatorFactory = app('validator');
- /**
- * @var AppDeductionRuleRepository $configRepository
- */
- $DedRepo = make(AppDeductionRuleRepository::class);
- $inputData = $this->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);
- }
- }
|