StoreSeckillTime.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\v1\marketing\seckill;
  12. use app\controller\admin\AuthController;
  13. use app\services\activity\seckill\StoreSeckillTimeServices;
  14. use app\services\activity\StoreActivityServices;
  15. use think\facade\App;
  16. /**
  17. * 限时秒杀时间 控制器
  18. * Class StoreSeckillTime
  19. * @package app\controller\admin\v1\marketing\seckill
  20. */
  21. class StoreSeckillTime extends AuthController
  22. {
  23. public function __construct(App $app, StoreSeckillTimeServices $services)
  24. {
  25. parent::__construct($app);
  26. $this->services = $services;
  27. }
  28. /**
  29. * 显示资源列表
  30. *
  31. * @return \think\Response
  32. */
  33. public function index()
  34. {
  35. $where = $this->request->getMore([
  36. ['title', ''],
  37. [['status', 's'], '']
  38. ]);
  39. $where['is_del'] = 0;
  40. return $this->success($this->services->systemPage($where));
  41. }
  42. /**
  43. * 可用秒杀时间段
  44. * @return mixed
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. */
  49. public function time_list()
  50. {
  51. return $this->success($this->services->time_list());
  52. }
  53. /**
  54. * @param $id
  55. * @return mixed
  56. */
  57. public function create($id = '')
  58. {
  59. return $this->success($this->services->createForm((int)$id));
  60. }
  61. /**
  62. * 详情
  63. * @param $id
  64. * @return mixed
  65. */
  66. public function read($id)
  67. {
  68. $info = $this->services->getInfo((int)$id);
  69. return $this->success(compact('info'));
  70. }
  71. /**
  72. * 保存秒杀商品
  73. * @param int $id
  74. */
  75. public function save($id)
  76. {
  77. $data = $this->request->postMore([
  78. [['title', 's'], ''],
  79. [['describe', 's'], ''],
  80. ['time', []],
  81. ['pic', ''],
  82. [['status', 'd'], 0],
  83. ]);
  84. $this->validate($data, \app\validate\admin\marketing\StoreSeckillTimeValidate::class, 'save');
  85. $data['start_time'] = str_replace(':', '', substr($data['time'][0], 0, -3));
  86. $data['end_time'] = str_replace(':', '', substr($data['time'][1], 0, -3));
  87. unset($data['time']);
  88. $id = (int)$id;
  89. if ($data['end_time'] <= $data['start_time']) {
  90. return app('json')->fail('时间段结束时间要大于开始时间');
  91. }
  92. if (!$this->services->checkTime($data, $id))
  93. return app('json')->fail('时间段不可重叠');
  94. if ($id) {
  95. $seckill = $this->services->get($id);
  96. if (!$seckill) {
  97. return $this->fail('数据不存在');
  98. }
  99. }
  100. if ($id) {
  101. $this->services->update($id, $data);
  102. } else {
  103. $this->services->save($data);
  104. }
  105. return $this->success('保存成功');
  106. }
  107. /**
  108. * 修改状态
  109. * @param $id
  110. * @param $status
  111. * @return mixed
  112. */
  113. public function set_status($id, $status)
  114. {
  115. $this->services->update($id, ['status' => $status]);
  116. return $this->success($status == 0 ? '关闭成功' : '开启成功');
  117. }
  118. /**
  119. * 删除指定资源
  120. *
  121. * @param int $id
  122. * @return \think\Response
  123. */
  124. public function delete(StoreActivityServices $activityServices, $id)
  125. {
  126. if (!$id) return $this->fail('缺少参数');
  127. $info = $this->services->get((int)$id);
  128. if (!$info) {
  129. return $this->fail('数据不存在');
  130. }
  131. if ($activityServices->existenceActivity(['start_time' => $info->start_time, 'end_time' => $info->end_time]))
  132. return app('json')->fail('已有开启活动,不可删除');
  133. $this->services->delete($id);
  134. return $this->success('删除成功!');
  135. }
  136. }