CoinsController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace Modules\Manage\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Model\CoinsLogModel;
  5. use App\Model\CoinsModel;
  6. use App\Repositories\CoinsRepository;
  7. use Carbon\Carbon;
  8. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  9. use Modules\Manage\Responses\CommonSuccessPkResponse;
  10. class CoinsController extends BaseController
  11. {
  12. /**
  13. * 分页列表
  14. * @param ValidatorFactoryInterface $validatorFactory
  15. * @return mixed
  16. */
  17. public function lists(ValidatorFactoryInterface $validatorFactory)
  18. {
  19. $inputData = $this->getJsonData();
  20. $rules = [
  21. 'page' => 'nullable|numeric',
  22. 'page_size' => 'nullable|numeric',
  23. ];
  24. $validator = $validatorFactory->make($inputData, $rules);
  25. if ($validator->fails()) {
  26. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  27. return json_fail($validator->errors()->first());
  28. }
  29. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  30. if ($pageSize > 500) {
  31. $pageSize = CommonConstant::PAGE_SIZE;
  32. }
  33. $page = isset($inputData['page']) ? intval($inputData['page']) : 1;
  34. $page = format_page($page);
  35. $arrData = [];
  36. $lists = CoinsModel::query()->orderBy('sort', 'DESC')->paginate($pageSize, ['*'], 'page', $page);
  37. if ($lists->isNotEmpty()) {
  38. $arrData = $lists->toArray();
  39. }
  40. return json_success($arrData);
  41. }
  42. /**
  43. * 详情
  44. * @param ValidatorFactoryInterface $validatorFactory
  45. * @return mixed
  46. */
  47. public function detail(ValidatorFactoryInterface $validatorFactory)
  48. {
  49. $inputData = $this->getJsonData();
  50. $rules = [
  51. 'id' => 'required',
  52. ];
  53. $validator = $validatorFactory->make($inputData, $rules);
  54. if ($validator->fails()) {
  55. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  56. return json_fail($validator->errors()->first());
  57. }
  58. $id = data_get($inputData, 'id', '');
  59. $arrData = [];
  60. $item = CoinsModel::query()->findOrFail($id);
  61. if (!empty($item)) {
  62. $arrData = $item->toArray();
  63. }
  64. return json_success($arrData);
  65. }
  66. /**
  67. * 创建或者更新.
  68. * @param ValidatorFactoryInterface $validatorFactory
  69. * @return mixed
  70. */
  71. public function update(ValidatorFactoryInterface $validatorFactory)
  72. {
  73. $inputData = $this->getJsonData();
  74. $rules = [
  75. 'id' => 'nullable|int',
  76. 'coins' => 'required|int',
  77. 'send_type' => 'required|int',
  78. 'send_num' => 'required|int',
  79. 'title' => 'required|string|max:255',
  80. 'price' => 'required|numeric',
  81. 'real_price' => 'required|numeric',
  82. 'title_desc' => 'nullable|string|max:255',
  83. 'price_desc' => 'nullable|string|max:255',
  84. 'sort' => 'required|int',
  85. 'type' => 'required|int',
  86. 'recommend_desc' => 'nullable|string|max:255',
  87. 'status' => 'nullable|int',
  88. ];
  89. $validator = $validatorFactory->make($inputData, $rules);
  90. if ($validator->fails()) {
  91. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  92. return json_fail($validator->errors()->first());
  93. }
  94. $id = data_get($inputData, 'id', '');
  95. $fields = $rules;
  96. unset($fields['id']);
  97. foreach ($fields as $fieldName => $rule) {
  98. if (in_array($fieldName, ['coins', 'send_type', 'send_num', 'sort', 'type', 'recommend_desc'])) {
  99. $setarr[$fieldName] = intval(data_get($inputData, $fieldName, 0));
  100. } else {
  101. $setarr[$fieldName] = data_get($inputData, $fieldName, '');
  102. }
  103. }
  104. unset($setarr['status']);
  105. if (isset($inputData['status']) && !empty($inputData['status'])) {
  106. $setarr['status'] = intval($inputData['status']);
  107. }
  108. if ($id) {
  109. $item = CoinsModel::query()->findOrFail($id);
  110. $result = $item->update($setarr);
  111. } else {
  112. $result = CoinsModel::query()->create($setarr);
  113. if ($result) {
  114. $id = $result->id;
  115. }
  116. }
  117. if (empty($result)) {
  118. return json_fail('保存失败');
  119. } else {
  120. make(CoinsRepository::class)->delUserCoinsCache();
  121. return json_success(['result' => 'success', 'id' => $id]);
  122. }
  123. }
  124. /**
  125. * 删除
  126. * @param ValidatorFactoryInterface $validatorFactory
  127. * @return mixed|CommonSuccessPkResponse
  128. * @throws \Exception
  129. */
  130. public function delete(ValidatorFactoryInterface $validatorFactory)
  131. {
  132. $inputData = $this->getJsonData();
  133. $rules = [
  134. 'id' => 'required|int',
  135. ];
  136. $validator = $validatorFactory->make($inputData, $rules);
  137. if ($validator->fails()) {
  138. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  139. return json_fail($validator->errors()->first());
  140. }
  141. $item = CoinsModel::query()->findOrFail($inputData['id']);
  142. $result = $item->delete();
  143. if ($result) {
  144. make(CoinsRepository::class)->delUserCoinsCache();
  145. return new CommonSuccessPkResponse($inputData['id']);
  146. } else {
  147. return json_fail('保存失败');
  148. }
  149. }
  150. /**
  151. * 修改状态
  152. * @param ValidatorFactoryInterface $validatorFactory
  153. * @return mixed|CommonSuccessPkResponse
  154. */
  155. public function switchStatus(ValidatorFactoryInterface $validatorFactory)
  156. {
  157. $inputData = $this->getJsonData();
  158. $rules = [
  159. 'id' => 'required|int',
  160. 'status' => 'required|int',
  161. ];
  162. $validator = $validatorFactory->make($inputData, $rules);
  163. if ($validator->fails()) {
  164. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  165. return json_fail($validator->errors()->first());
  166. }
  167. $operatorName = 'opened';
  168. $item = CoinsModel::query()->findOrFail($inputData['id']);
  169. if ($item->status == $inputData['status']) {
  170. return new CommonSuccessPkResponse($item->id);
  171. }
  172. if ($inputData['status'] == 2) {
  173. $operatorName = 'closed';
  174. }
  175. $item->status = $inputData['status'];
  176. $result = $item->save();
  177. if ($result) {
  178. make(CoinsRepository::class)->delUserCoinsCache();
  179. return json_success(['result' => 'success', 'id' => $inputData['id'], 'operator_name' => $operatorName]);
  180. } else {
  181. return json_fail('保存失败');
  182. }
  183. }
  184. /**
  185. * 金币明细
  186. */
  187. public function coinsLogs(ValidatorFactoryInterface $validatorFactory)
  188. {
  189. $inputData = $this->getJsonData();
  190. $rules = [
  191. 'page' => 'required|int',
  192. 'page_size' => 'required|int',
  193. 'user_code' => 'nullable|string',
  194. 'coin_type' => 'nullable|int',
  195. 'stime' => 'nullable|date',
  196. 'otime' => 'nullable|date',
  197. ];
  198. $validator = $validatorFactory->make($inputData, $rules);
  199. if ($validator->fails()) {
  200. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  201. return json_fail($validator->errors()->first());
  202. }
  203. $userCode = $inputData['user_code'];
  204. $coinType = $inputData['coin_type'];
  205. $stime = $inputData['stime'];
  206. $otime = $inputData['otime'];
  207. $result = CoinsLogModel::query()
  208. ->where(function ($q) use ($userCode,$coinType,$stime,$otime) {
  209. if ($userCode){
  210. $q->where('user_code',$userCode);
  211. }
  212. if ($coinType){
  213. $q->where('coin_type',$coinType);
  214. }
  215. if ($stime && $otime){
  216. $q->whereBetween(
  217. 'created_at',
  218. [
  219. Carbon::parse($stime)->toDateString(),
  220. Carbon::parse($otime)->toDateString()
  221. ]
  222. );
  223. }
  224. })
  225. ->orderByDesc('id')
  226. ->paginate((int)$inputData['page_size'], ['*'], 'page', (int)$inputData['page']);
  227. return json_success($result);
  228. }
  229. }