LottoRecordController.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace frontend\controllers;
  3. use Yii;
  4. use frontend\models\LottoRecord;
  5. use backend\helpers\DateTimeHelper;
  6. class LottoRecordController extends BaseController
  7. {
  8. /**
  9. * 中奖记录
  10. */
  11. public function actionList()
  12. {
  13. $post = Yii::$app->request->post();
  14. $order = isset($post['order']) ? strtolower($post['order']) : '';
  15. $orderBy = isset($post['orderBy']) ? strtolower($post['orderBy']) : 'desc';
  16. $search = isset($post['search']) ? $post['search'] : '';
  17. $start = isset($post['start']) ? (int) $post['start'] : 0;
  18. $length = isset($post['length']) ? (int) $post['length'] : 20;
  19. $draw = isset($post['draw']) ? $post['draw'] : 1;
  20. $activityType = isset($post['activityType']) ? $post['activityType'] : 0;
  21. // 搜索
  22. $where = ['and'];
  23. if ($search) {
  24. $where[] = ['like', 'mobile', $search];
  25. }
  26. if ($activityType) {
  27. $where[] = ['=', 'activity_type', $activityType];
  28. }
  29. // 排序
  30. $allowOrderColumn = ['id', 'mobile', 'lottery', 'activity_type', 'in_time'];
  31. if (in_array($order, $allowOrderColumn) && in_array($orderBy, ['asc', 'desc'])) {
  32. if ($orderBy == 'asc') {
  33. $orderCondition = [$order => SORT_ASC];
  34. } else {
  35. $orderCondition = [$order => SORT_DESC];
  36. }
  37. } else {
  38. $orderCondition = ['id' => SORT_DESC];
  39. }
  40. $query = LottoRecord::find();
  41. $query->where($where);
  42. $count = $query->count();
  43. $list = [];
  44. if ($count) {
  45. $list = $query->orderBy($orderCondition)->offset($start)->limit($length)->asArray()->all();
  46. }
  47. $data['data'] = $list;
  48. $data['draw'] = $draw;
  49. $data['recordsFiltered'] = $count;
  50. $data['recordsTotal'] = $count;
  51. return $this->outJson(1, $data);
  52. }
  53. /**
  54. * 删除中奖记录
  55. */
  56. public function actionDelete()
  57. {
  58. $id = (int) Yii::$app->request->post('id');
  59. $model = LottoRecord::find()->where(['id' => $id])->limit(1)->one();
  60. if (!$model) {
  61. return $this->outJson(0, [], '参数错误');
  62. }
  63. $result = $model->delete();
  64. if ($result) {
  65. return $this->outJson(1);
  66. } else {
  67. return $this->outJson(0, [], '删除失败');
  68. }
  69. }
  70. /**
  71. * 新增
  72. */
  73. public function actionAdd()
  74. {
  75. $request = Yii::$app->getRequest()->post();
  76. $model = new LottoRecord();
  77. $model->in_time = DateTimeHelper::microtime_float();
  78. $model->setAttributes($request);
  79. if ($model->save()) {
  80. return $this->outJson(1, $model->id);
  81. } else {
  82. return $this->outJson(0, [], '保存失败!');
  83. }
  84. }
  85. }