Modules_Manage_Controllers_VipCodeController.proxy.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * 兑换码
  4. * @author ningque
  5. */
  6. declare (strict_types=1);
  7. namespace Modules\Manage\Controllers;
  8. use App\Model\Mongo\VipCodeExchangeHistoryModel;
  9. use App\Model\VipCodeModel as Model;
  10. use App\Repositories\VipCodeRepository;
  11. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  12. class VipCodeController extends BaseController
  13. {
  14. use \Hyperf\Di\Aop\ProxyTrait;
  15. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  16. function __construct()
  17. {
  18. if (method_exists(parent::class, '__construct')) {
  19. parent::__construct(...func_get_args());
  20. }
  21. $this->__handlePropertyHandler(__CLASS__);
  22. }
  23. /**
  24. * 列表
  25. * @param ValidatorFactoryInterface $validatorFactory
  26. * @return mixed
  27. * @example param_json_data string: {"handshake":"","data":{"page":1,"page_size":5,"search":{"title":"测试兑换码3","code":"","status":"","is_expire":""}}}
  28. */
  29. public function lists(ValidatorFactoryInterface $validatorFactory)
  30. {
  31. $inputData = $this->getJsonData();
  32. $page = isset($inputData['page']) ? (int) $inputData['page'] : 1;
  33. $page_size = isset($inputData['page_size']) ? (int) $inputData['page_size'] : 20;
  34. $select = ['*'];
  35. $model = Model::query();
  36. $search = isset($inputData['search']) ? $inputData['search'] : array();
  37. if ($search) {
  38. foreach ($search as $k => $v) {
  39. if (!strlen((string) $v)) {
  40. continue;
  41. }
  42. switch ($k) {
  43. case "title":
  44. // 标题
  45. case "code":
  46. // 兑换码
  47. $model->where($k, 'like', "{$v}%");
  48. break;
  49. case "is_expire":
  50. // 是否查询已过期的,""-不过滤 1-已过期 0-未过期
  51. $now_date = date("Y-m-d");
  52. if ($v == '1') {
  53. //select * from vip_code where expire != "" and expire <= "2020-02-27"
  54. $model->whereRaw('expire != "" and expire <= "?"', [$now_date]);
  55. } else {
  56. //select * from vip_code where ( expire is NULL OR expire > "2020-02-27")
  57. $model->whereRaw('expire is NULL OR expire > "?"', [$now_date]);
  58. }
  59. break;
  60. default:
  61. $model->where($k, $v);
  62. }
  63. }
  64. }
  65. $result = $model->select($select)->orderBy('id', 'desc')->paginate($page_size, ['*'], 'page', $page);
  66. return json_success($result);
  67. }
  68. /**
  69. * 新增
  70. * @param ValidatorFactoryInterface $validatorFactory
  71. * @return mixed
  72. * @example param_json_data string: {"handshake":"","data":{"title":"测试","remark":"备注","day":1,"type":1,"limit":9000,"expire":"2023-01-01","count":5}}
  73. */
  74. public function store(ValidatorFactoryInterface $validatorFactory)
  75. {
  76. $inputData = $this->getJsonData();
  77. $rules = ['title' => 'required|string', 'remark' => 'string', 'day' => 'required|int', 'type' => 'required|int', 'limit' => 'required|int', 'expire' => 'nullable|string', 'count' => 'required|int'];
  78. $validator = $validatorFactory->make($inputData, $rules);
  79. if ($validator->fails()) {
  80. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  81. return json_fail($validator->errors()->first());
  82. }
  83. $vip_code = make(VipCodeRepository::class)->makeCode($inputData);
  84. // 创建管理员日志
  85. $logSubject = "创建兑换码";
  86. $logSvalue = '';
  87. $logDbData = ['after_items' => [], 'before_items' => []];
  88. $this->log($logSubject, $logSvalue, $logDbData);
  89. return json_success($vip_code);
  90. }
  91. /**
  92. * 编辑
  93. * @param ValidatorFactoryInterface $validatorFactory
  94. * @return mixed
  95. * @example param_json_data string: {"handshake":"","data":{"status":1,"id":1}}
  96. */
  97. public function update(ValidatorFactoryInterface $validatorFactory)
  98. {
  99. $inputData = $this->getJsonData();
  100. $rules = ['id' => 'required|int', 'status' => 'required|int'];
  101. $validator = $validatorFactory->make($inputData, $rules);
  102. if ($validator->fails()) {
  103. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  104. return json_fail($validator->errors()->first());
  105. }
  106. $info = Model::query()->where('id', $inputData['id'])->first();
  107. if (!$info) {
  108. return json_fail('兑换码信息不存在');
  109. }
  110. $result = Model::query()->where('id', $inputData['id'])->update(['status' => $inputData['status']]);
  111. // 创建管理员日志
  112. $olditem = clone $info;
  113. $newitem = Model::query()->where('id', $inputData['id'])->first();
  114. $logSubject = "修改兑换码";
  115. $logSvalue = $olditem->id;
  116. $logDbData = ['after_items' => [$newitem->toArray()], 'before_items' => [$olditem->toArray()]];
  117. $this->log($logSubject, $logSvalue, $logDbData);
  118. return json_success($result);
  119. }
  120. /**
  121. * 删除 (真实删除数据)
  122. * @param ValidatorFactoryInterface $validatorFactory
  123. * @return mixed
  124. * @example param_json_data string: {"handshake":"","data":{"id":8}}
  125. */
  126. public function delete(ValidatorFactoryInterface $validatorFactory)
  127. {
  128. $inputData = $this->getJsonData();
  129. $rules = ['id' => 'required|int'];
  130. $validator = $validatorFactory->make($inputData, $rules);
  131. if ($validator->fails()) {
  132. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  133. return json_fail($validator->errors()->first());
  134. }
  135. $info = Model::query()->where('id', $inputData['id'])->first();
  136. if (!$info) {
  137. return json_fail('兑换码信息不存在');
  138. }
  139. $result = Model::query()->where('id', $inputData['id'])->delete();
  140. if (!$result) {
  141. return json_fail('删除失败');
  142. }
  143. // 创建管理员日志
  144. $olditem = clone $info;
  145. $logSubject = "删除兑换码";
  146. $logSvalue = $olditem->id;
  147. $logDbData = ['after_items' => [], 'before_items' => [$olditem->toArray()]];
  148. $this->log($logSubject, $logSvalue, $logDbData);
  149. return json_success($result);
  150. }
  151. /**
  152. * 兑换记录(mongodb) - ok
  153. * @param ValidatorFactoryInterface $validatorFactory
  154. * @return mixed
  155. * @author x.b
  156. * @example param_json_data string: {"handshake":"","data":{"page":1,"page_size":5,"search":{"nick":"","user_id":"","user_code":"","code":"","title":"","stime":"","otime":""}}}
  157. * @example param_json_data string: {"handshake":"","data":{"page":1,"page_size":5,"search":{"nick":"C5YU32","user_id":"32","user_code":"","code":"","title":"","stime":"2020-02-22 17:33:22","otime":"2020-02-29"}}}
  158. */
  159. public function history(ValidatorFactoryInterface $validatorFactory)
  160. {
  161. $inputData = $this->getJsonData();
  162. $page = isset($inputData['page']) ? (int) $inputData['page'] : 1;
  163. $page_size = isset($inputData['page_size']) ? (int) $inputData['page_size'] : 20;
  164. $where = [];
  165. $search = isset($inputData['search']) ? $inputData['search'] : array();
  166. if ($search) {
  167. foreach ($search as $k => $v) {
  168. if (!strlen((string) $v)) {
  169. continue;
  170. }
  171. switch ($k) {
  172. case "title":
  173. // 标题
  174. case "code":
  175. // 兑换码
  176. case "nick":
  177. // 用户名
  178. //$model->where($k, 'like', "{$v}%"); //mysql
  179. $where[$k] = ['$regex' => '/^' . $v . '/'];
  180. $where[$k] = $v;
  181. //暂时不提供like查询, - 这里是从mongodb库查询记录的
  182. break;
  183. case "user_id":
  184. // 用户id,mongodb中注意存储类型,需要为int
  185. $where[$k] = (int) $v;
  186. break;
  187. case "stime":
  188. // 自定义-开始日期
  189. //$model->where('created_at', '>=', $v);
  190. $where['created_at']['$gte'] = $v;
  191. break;
  192. case "otime":
  193. // 自定义-结束日期mong
  194. //$model->where('created_at', '<=', $v);
  195. $where['created_at']['$lte'] = $v;
  196. break;
  197. default:
  198. //$model->where($k, $v);
  199. $where[$k] = $v;
  200. }
  201. }
  202. }
  203. $skip = ($page - 1) * $page_size;
  204. $options = ['skip' => $skip, 'limit' => $page_size, 'sort' => ['created_at' => -1]];
  205. $total = make(VipCodeExchangeHistoryModel::class)->db()->countDocuments($where);
  206. $data = make(VipCodeExchangeHistoryModel::class)->db()->find($where, $options)->toArray();
  207. $result = ['data' => $data, 'total' => $total];
  208. return json_success($result);
  209. }
  210. }