| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- /**
- * 兑换码
- * @author ningque
- */
- declare(strict_types=1);
- namespace Modules\Manage\Controllers;
- use App\Model\Mongo\VipCodeExchangeHistoryModel;
- use App\Model\VipCodeModel as Model;
- use App\Repositories\VipCodeRepository;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- class VipCodeController extends BaseController
- {
- /**
- * 列表
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"page":1,"page_size":5,"search":{"title":"测试兑换码3","code":"","status":"","is_expire":""}}}
- */
- public function lists(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $page = isset($inputData['page']) ? (int)$inputData['page'] : 1;
- $page_size = isset($inputData['page_size']) ? (int)$inputData['page_size'] : 20;
- $select = ['*'];
- $model = Model::query();
- $search = isset($inputData['search']) ? $inputData['search'] : array();
- if ($search) {
- foreach ($search as $k => $v) {
- if (!strlen((string)$v)) {
- continue;
- }
- switch ($k) {
- case "title": // 标题
- case "code": // 兑换码
- $model->where($k, 'like', "{$v}%");
- break;
- case "is_expire": // 是否查询已过期的,""-不过滤 1-已过期 0-未过期
- $now_date = date("Y-m-d");
- if ($v == '1') {
- //select * from vip_code where expire != "" and expire <= "2020-02-27"
- $model->whereRaw('expire != "" and expire <= "?"', [$now_date]);
- } else {
- //select * from vip_code where ( expire is NULL OR expire > "2020-02-27")
- $model->whereRaw('expire is NULL OR expire > "?"', [$now_date]);
- }
- break;
- default:
- $model->where($k, $v);
- }
- }
- }
- $result = $model->select($select)
- ->orderBy('id', 'desc')
- ->paginate($page_size, ['*'], 'page', $page);
- return json_success($result);
- }
- /**
- * 新增
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"title":"测试","remark":"备注","day":1,"type":1,"limit":9000,"expire":"2023-01-01","count":5}}
- */
- public function store(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'title' => 'required|string',
- 'remark' => 'string',
- 'day' => 'required|int',
- 'type' => 'required|int',
- 'limit' => 'required|int',
- 'expire' => 'nullable|string',
- 'count' => 'required|int', //一次生成多少个,如6
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $vip_code = make(VipCodeRepository::class)->makeCode($inputData);
- // 创建管理员日志
- $logSubject = "创建兑换码";
- $logSvalue = '';
- $logDbData = [
- 'after_items' => [],
- 'before_items' => [],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return json_success($vip_code);
- }
- /**
- * 编辑
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"status":1,"id":1}}
- */
- public function update(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'id' => 'required|int',
- 'status' => 'required|int'
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $info = Model::query()
- ->where('id', $inputData['id'])
- ->first();
- if (!$info) {
- return json_fail('兑换码信息不存在');
- }
- $result = Model::query()
- ->where('id', $inputData['id'])
- ->update(
- [
- 'status' => $inputData['status']
- ]
- );
- // 创建管理员日志
- $olditem = clone $info;
- $newitem = Model::query()
- ->where('id', $inputData['id'])
- ->first();
- $logSubject = "修改兑换码";
- $logSvalue = $olditem->id;
- $logDbData = [
- 'after_items' => [
- $newitem->toArray(),
- ],
- 'before_items' => [
- $olditem->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return json_success($result);
- }
- /**
- * 删除 (真实删除数据)
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"id":8}}
- */
- public function delete(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'id' => 'required|int',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $info = Model::query()
- ->where('id', $inputData['id'])
- ->first();
- if (!$info) {
- return json_fail('兑换码信息不存在');
- }
- $result = Model::query()
- ->where('id', $inputData['id'])
- ->delete();
- if (!$result) {
- return json_fail('删除失败');
- }
- // 创建管理员日志
- $olditem = clone $info;
- $logSubject = "删除兑换码";
- $logSvalue = $olditem->id;
- $logDbData = [
- 'after_items' => [
- ],
- 'before_items' => [
- $olditem->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return json_success($result);
- }
- /**
- * 兑换记录(mongodb) - ok
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @author x.b
- * @example param_json_data string: {"handshake":"","data":{"page":1,"page_size":5,"search":{"nick":"","user_id":"","user_code":"","code":"","title":"","stime":"","otime":""}}}
- * @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"}}}
- */
- public function history(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $page = isset($inputData['page']) ? (int)$inputData['page'] : 1;
- $page_size = isset($inputData['page_size']) ? (int)$inputData['page_size'] : 20;
- $where = [];
- $search = isset($inputData['search']) ? $inputData['search'] : array();
- if ($search) {
- foreach ($search as $k => $v) {
- if (!strlen((string)$v)) {
- continue;
- }
- switch ($k) {
- case "title": // 标题
- case "code": // 兑换码
- case "nick": // 用户名
- //$model->where($k, 'like', "{$v}%"); //mysql
- $where[$k] = [
- '$regex' => '/^' . $v . '/'
- ];
- $where[$k] = $v; //暂时不提供like查询, - 这里是从mongodb库查询记录的
- break;
- case "user_id": // 用户id,mongodb中注意存储类型,需要为int
- $where[$k] = (int)$v;
- break;
- case "stime": // 自定义-开始日期
- //$model->where('created_at', '>=', $v);
- $where['created_at']['$gte'] = $v;
- break;
- case "otime": // 自定义-结束日期mong
- //$model->where('created_at', '<=', $v);
- $where['created_at']['$lte'] = $v;
- break;
- default:
- //$model->where($k, $v);
- $where[$k] = $v;
- }
- }
- }
- $skip = ($page -1) * $page_size;
- $options = [
- 'skip' => $skip,
- 'limit' => $page_size,
- 'sort' => ['created_at' => -1] // -1为倒叙 1为正叙 (mongodb书写格式)
- ];
- $total = make(VipCodeExchangeHistoryModel::class)->db()->countDocuments($where);
- $data = make(VipCodeExchangeHistoryModel::class)->db()->find($where, $options)->toArray();
- $result = [
- 'data' => $data,
- 'total' => $total, //与H5沟通,他仅要总数即可
- ];
- return json_success($result);
- }
- }
|