AppDeductionRuleController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace Modules\ManageApi\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Repositories\AppDeductionRuleRepository;
  5. use App\Repositories\Criterias\AppDeductionRuleCriteria;
  6. use App\Repositories\Criterias\OrderLimitCriteria;
  7. use Illuminate\Support\Facades\Validator;
  8. use Modules\ManageApi\Responses\AppDeductionRuleResponse;
  9. use Modules\ManageApi\Responses\CommonSuccessUidResponse;
  10. use Modules\ManageApi\Responses\ConfigSuccessResponse;
  11. class AppDeductionRuleController extends BaseController
  12. {
  13. /**
  14. * 扣量规则列表
  15. * @return mixed
  16. */
  17. public function lists()
  18. {
  19. /**
  20. * @var Validator $validatorFactory
  21. */
  22. $validatorFactory = app('validator');
  23. /**
  24. * @var AppDeductionRuleRepository $configRepository
  25. */
  26. $DedRepo = make(AppDeductionRuleRepository::class);
  27. $inputData = $this->getJsonData();
  28. // 针对layui table自动分页请求参数,优先GET值.
  29. $queryPage = request()->has('page') ? request()->input('page', 1) : null;
  30. $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
  31. if (!is_null($queryPage)) {
  32. $inputData['page'] = $queryPage;
  33. }
  34. if (!is_null($queryPageSize)) {
  35. $inputData['page_size'] = $queryPageSize;
  36. }
  37. $rules = [
  38. 'page' => 'nullable|integer',
  39. 'page_size' => 'nullable|integer',
  40. 'deduction_type' => 'required|string',
  41. 'app_uid' => 'nullable|string',
  42. ];
  43. $validator = $validatorFactory->make($inputData, $rules);
  44. if ($validator->fails()) {
  45. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  46. return json_fail($validator->errors()->first());
  47. }
  48. $orders = [
  49. ['created_at', 'DESC'],
  50. ];
  51. if (!isset($inputData['page_size']) || $inputData['page_size'] > 500) {
  52. $pageSize = CommonConstant::PAGE_SIZE;
  53. } else {
  54. $pageSize = $inputData['page_size'];
  55. }
  56. $page = format_page($inputData['page'] ?? 0);
  57. $res = $DedRepo->pushCriteria(new AppDeductionRuleCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
  58. return new AppDeductionRuleResponse($res);
  59. }
  60. /**
  61. * 新增扣量规则
  62. * @param Validator $validatorFactory
  63. * @return \Illuminate\Http\JsonResponse|ConfigSuccessResponse
  64. */
  65. public function create()
  66. {
  67. $inputData = $this->getJsonData();
  68. $rules = [
  69. 'deduction_type' => 'required|string',
  70. 'deduction_rule' => 'nullable|integer',
  71. 'deduction_method' => 'nullable|integer',
  72. 'app_uid' => 'required|string',
  73. 'deduction_price' => 'nullable|string',
  74. 'deduction_pro' => 'nullable|string',
  75. 'jdata' => 'nullable|string',
  76. ];
  77. $validator = Validator::make($inputData, $rules);
  78. if ($validator->fails()) {
  79. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  80. return json_fail($validator->errors()->first());
  81. }
  82. /**
  83. * @var AppDeductionRuleRepository $configRepository
  84. */
  85. $DedRepo = make(AppDeductionRuleRepository::class);
  86. $setarr = [
  87. 'deduction_type' => $inputData['deduction_type'],
  88. 'deduction_rule' => $inputData['deduction_rule'] ?? 0,
  89. 'app_uid' => $inputData['app_uid'],
  90. 'deduction_price' => $inputData['deduction_price'] ?? '',
  91. 'deduction_method' => $inputData['deduction_method'] ?? '',
  92. 'deduction_pro' => $inputData['deduction_pro'],
  93. 'jdata' => $inputData['jdata'],
  94. ];
  95. $result = $DedRepo->create($setarr);
  96. if (!$result) {
  97. return json_fail('新增失败');
  98. } else {
  99. $logSubject = '新增一个扣量规则,:' . $result->uid;
  100. $logSvalue = $result->uid;
  101. $logDbData = [
  102. 'after_items' => [
  103. $setarr,
  104. ],
  105. 'before_items' => [
  106. ],
  107. ];
  108. $this->log($logSubject, $logSvalue, $logDbData);
  109. return new ConfigSuccessResponse();
  110. }
  111. }
  112. /**
  113. * 修改规则
  114. *
  115. * @return \Illuminate\Http\JsonResponse|ConfigSuccessResponse
  116. */
  117. public function modify()
  118. {
  119. $inputData = $this->getJsonData();
  120. $rules = [
  121. 'deduction_rule' => 'nullable|integer',
  122. 'app_uid' => 'required|string',
  123. 'uid' => 'required|string',
  124. 'deduction_price' => 'nullable|string',
  125. 'deduction_pro' => 'nullable|string',
  126. 'deduction_method' => 'nullable|integer',
  127. 'jdata' => 'nullable|string',
  128. ];
  129. $validator = Validator::make($inputData, $rules);
  130. if ($validator->fails()) {
  131. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  132. return json_fail($validator->errors()->first());
  133. }
  134. /**
  135. * @var AppDeductionRuleRepository $managerRepo
  136. */
  137. $DedRepo = app(AppDeductionRuleRepository::class);
  138. $deqInfo = $DedRepo->findBy('uid', $inputData['uid']);
  139. if (empty($deqInfo)) {
  140. return json_fail(trans('common.err_resource_not_found'));
  141. }
  142. $olddeqInfo = clone $deqInfo;
  143. if (isset($inputData['deduction_type'])) {
  144. $deqInfo->deduction_type = $inputData['deduction_type'];
  145. }
  146. if (isset($inputData['deduction_rule'])) {
  147. $deqInfo->deduction_rule = $inputData['deduction_rule'];
  148. }
  149. if (isset($inputData['app_uid'])) {
  150. $deqInfo->app_uid = $inputData['app_uid'];
  151. }
  152. if (isset($inputData['deduction_price'])) {
  153. $deqInfo->deduction_price = $inputData['deduction_price'];
  154. }
  155. if (isset($inputData['deduction_pro'])) {
  156. $deqInfo->deduction_pro = $inputData['deduction_pro'];
  157. }
  158. if (isset($inputData['deduction_method'])) {
  159. $deqInfo->deduction_method = $inputData['deduction_method'];
  160. }
  161. if (isset($inputData['jdata'])) {
  162. $deqInfo->jdata = $inputData['jdata'];
  163. }
  164. $result = $deqInfo->save();
  165. if (!$result) {
  166. return json_fail('修改失败');
  167. } else {
  168. $logSubject = '修改一个扣量规则, uid是:' . $deqInfo->uid;
  169. $logSvalue = $deqInfo->uid;
  170. $logDbData = [
  171. 'after_items' => [
  172. $deqInfo->toArray(),
  173. ],
  174. 'before_items' => [
  175. $olddeqInfo->toArray(),
  176. ],
  177. ];
  178. $this->log($logSubject, $logSvalue, $logDbData);
  179. //删除缓存
  180. $DedRepo->delRuleCache($deqInfo->deduction_type);
  181. return new ConfigSuccessResponse();
  182. }
  183. }
  184. public function delete()
  185. {
  186. $inputData = $this->getJsonData();
  187. $rules = [
  188. 'uid' => 'required|string',
  189. ];
  190. $validator = Validator::make($inputData, $rules);
  191. if ($validator->fails()) {
  192. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  193. return json_fail($validator->errors()->first());
  194. }
  195. /**
  196. * @var AppDeductionRuleRepository $managerRepo
  197. */
  198. $DedRepo = app(AppDeductionRuleRepository::class);
  199. $deqInfo = $DedRepo->findBy('uid', $inputData['uid']);
  200. $olddeqInfo = clone $deqInfo;
  201. $result = $deqInfo->delete();
  202. if (!$result) {
  203. return json_fail('删除规则失败');
  204. } else {
  205. $logSubject = '删除一个扣量规则, uid是:' . $deqInfo->uid;
  206. $logSvalue = $deqInfo->uid;
  207. $logDbData = [
  208. 'after_items' => [
  209. $deqInfo->toArray(),
  210. ],
  211. 'before_items' => [
  212. $olddeqInfo->toArray(),
  213. ],
  214. ];
  215. $this->log($logSubject, $logSvalue, $logDbData);
  216. //删除缓存
  217. $DedRepo->delRuleCache($deqInfo->deduction_type);
  218. return new CommonSuccessUidResponse($deqInfo->uid);
  219. }
  220. }
  221. /**
  222. * 规则详情
  223. *
  224. * @param Validator $validatorFactory
  225. * @return \Illuminate\Http\JsonResponse|AppDeductionRuleResponse
  226. */
  227. public function detail(Validator $validatorFactory)
  228. {
  229. $inputData = $this->getJsonData();
  230. /**
  231. * @var AppDeductionRuleRepository $managerRepo
  232. */
  233. $DedRepo = app(AppDeductionRuleRepository::class);
  234. $rules = [
  235. 'uid' => 'required|string',
  236. ];
  237. $validator = Validator::make($inputData,$rules);
  238. if ($validator->fails()) {
  239. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  240. return json_fail($validator->errors()->first());
  241. }
  242. $res = $DedRepo->findBy('uid', $inputData['uid']);
  243. return new AppDeductionRuleResponse($res);
  244. }
  245. }