VipCodeController.php 9.6 KB

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