123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
-
- namespace app\controller\store\project;
-
- use app\common\logic\DataLogic;
- use app\model\store\project\ProjectModel;
- use app\model\store\project\ProjectTypeModel;
- use app\model\store\project\SnapUpModel;
- use app\controller\store\AuthController;
-
- class Snapup extends AuthController
- {
-
- protected $name = '抢购';
-
- public function index()
- {
- $abbreviation = $this->request->param('abbreviation');
- $project_type_id = $this->request->param('project_type_id/d');
- $bg_take_count = $this->request->param('bg_take_count/d');
- $end_take_count = $this->request->param('end_take_count/d');
- $status = $this->request->param('status/d', 1);
- $is_flash_sale = $this->request->param('is_flash_sale/d', 0);
- if (!in_array($status, [1, -1])) $status = 1;
- $where = [
- ['is_delete', '=', 0],
- ['status', '=', $status]
- ];
-
- if (!empty($bg_take_count)) {
- if (!empty($end_take_count)) {
- $where[] = ['take_count', 'between', [$bg_take_count, $end_take_count]];
- } else {
- $where[] = ['take_count', '>=', $bg_take_count];
- }
- } else {
- if (!empty($end_take_count)) {
- $where[] = ['take_count', '<=', $end_take_count];
- }
- }
-
- $today = date('Y-m-d', $this->request->time());
-
- if (!empty($project_type_id)) $where[] = ['project_type_id', '=', $project_type_id];
-
- $project_where = [
- ['is_delete', '=', 0],
- ];
- if (!empty($abbreviation)) $project_where[] = ['abbreviation', '=', $abbreviation];
-
- $project_ids = ProjectModel::where($project_where)->where([
- ['is_delete', '=', 0]
- ])->column('project_id') ?? [];
- $where[] = ['project_id', 'in', $project_ids];
- $snap_up_model = new SnapUpModel();
-
- if ($is_flash_sale == 1) {
- $where[] = ['is_flash_sale', '=', 1];
- $where[] = ['start_time', '<=', $today];
- $where[] = ['end_time', '>=', $today];
- $where[] = ['flash_num', '>', 0];
- } else {
- $snap_up_model = $snap_up_model->whereRaw("(start_time > '{$today}' or end_time < '{$today}' or flash_num <= 0) OR is_flash_sale = 0");
- }
-
- $snap_up_model = $snap_up_model->with(['type', 'project']);
- $order = ['sort' => 'desc', 'add_time' => 'desc'];
- $field = '*';
- $append = ['add_time_format', 'is_flash_sale_expire'];
- //???
- $return = DataLogic::getDataList($snap_up_model, $where, $order, $field, $append);
- if (DataLogic::$page == 1) {
- $type_list = ProjectTypeModel::where([
- ['is_delete', '=', 0]
- ])->field('project_type_id,name')->select()->toArray();
- $return['type_list'] = $type_list;
- }
- foreach ($return['list'] as $k => $v) {
- $return['list'][$k]['is_flash_sale'] = (string)$v['is_flash_sale'];
- }
- return $this->success($return);
- }
-
- public function show()
- {
- $snap_up_id = $this->request->param('snap_up_id/d', 0);
- if ($snap_up_id == 0) return $this->success([]);
-
- if (null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->fail($this->name . '不存在');
- if ($snap_up->is_delete != 0) return $this->fail($this->name . '不存在');
-
- if (null === $project = ProjectModel::find($snap_up->project_id)) return $this->fail($this->name . '不存在');
- if ($project->is_delete != 0) return $this->fail($this->name . '不存在');
-
- $detail = [
- 'project' => [
- 'project_id' => $project->project_id,
- 'abbreviation' => $project->abbreviation,
- 'market_price' => $project->market_price,
- 'cover_img' => $project->cover_img,
- ],
- 'vip_price' => $snap_up->vip_price,
- 'is_flash_sale' => $snap_up->is_flash_sale,
- 'flash_price' => $snap_up->flash_price,
- 'flash_num' => $snap_up->flash_num,
- 'flash_sale_time' => $snap_up->flash_sale_time,
- 'valid_time' => $snap_up->valid_time,
- 'valid_unit' => $snap_up->valid_unit,
- 'fictitious_take_count' => $snap_up->fictitious_take_count,
- 'rotation' => $snap_up->rotation,
- 'snap_up_details' => $snap_up->snap_up_details,
- 'purchase_information' => $snap_up->purchase_information,
- 'sort' => $snap_up->sort,
- ];
-
- return $this->success(['detail' => $detail]);
- }
-
- //选择项目
- public function chooseProject()
- {
- $name = $this->request->param('name');
- $where = [
- ['is_delete', '=', 0],
- ];
- if (!empty($name)) $where[] = ['abbreviation', '=', $name];
-
- //判断项目的唯一性
- $project_ids = SnapUpModel::where([
- ['is_delete', '=', 0],
- ])->column('project_id') ?? [];
- // $where[] = ['project_id', 'not in', $project_ids];
- $project_model = new ProjectModel();
- $order = ['add_time' => 'desc'];
- $field = '*';
- $append = ['add_time_format'];
- $return = DataLogic::getDataList($project_model, $where, $order, $field, $append);
- return $this->success($return);
- }
-
- public function save()
- {
- $snap_up_id = $this->request->param('snap_up_id/d', 0);
- $project_id = $this->request->param('project_id/d', 0);
- $vip_price = $this->request->param('vip_price/f', 0);
- $is_flash_sale = $this->request->param('is_flash_sale/d', 0);
- $flash_price = $this->request->param('flash_price/f', 0);
- $flash_num = $this->request->param('flash_num/d', 0);
- $flash_sale_time = $this->request->param('flash_sale_time/a', []);
- $valid_time = $this->request->param('valid_time/d', 0);
- $valid_unit = $this->request->param('valid_unit');
- $fictitious_take_count = $this->request->param('fictitious_take_count/d', 0);
- $rotation = $this->request->param('rotation/a', []);
- $snap_up_details = $this->request->param('snap_up_details', '');//, 'security_json_or_array'
- $purchase_information = $this->request->param('purchase_information');
- $sort = $this->request->param('sort/d', 0);
- //判断传来的参数
- if (null === $project = ProjectModel::find($project_id)) return $this->fail('项目不存在');
- if ($project->is_delete != 0) return $this->fail('项目不存在');
- //判断项目的唯一性 (后期商榷在添加)
- if (empty($vip_price) || $vip_price <= 0) return $this->fail('请输入会员价,且不能小于0');
- if ($vip_price > $project->market_price) return $this->fail('会员价不能高于项目市场价');
- if (!in_array($is_flash_sale, [0, 1])) return $this->fail('参数错误');
- if ($is_flash_sale == 1) {
- if (empty($flash_price) || $flash_price <= 0) return $this->fail('请输入秒杀价,且不能小于0');
- if ($flash_price > $vip_price) return $this->fail('秒杀价不能大于会员价');
- if (empty($flash_num) || $flash_num <= 0) return $this->fail('秒杀数量不能小于0');
- if (empty($flash_sale_time) || count($flash_sale_time) != 2) return $this->fail('请选择秒杀时间');
- $start_time = $flash_sale_time[0];
- $end_time = $flash_sale_time[1];
- if (false === $start_time_unix = strtotime($start_time)) return $this->fail('请选择正确的秒杀开始时间');
- if (false === $end_time_unix = strtotime($end_time)) return $this->fail('请选择正确的秒杀结束时间');
- if ($end_time_unix < $start_time_unix) return $this->fail('秒杀开始时间不能大于结束时间');
- }
- if (empty($valid_time) || $valid_time <= 0) return $this->fail('请输入有效期的值,且不能小于0');
- if (empty($valid_unit) || !in_array($valid_unit, ['day', 'month', 'year'])) return $this->fail('请选择有效期单位');
- if (empty($fictitious_take_count) || $fictitious_take_count <= 0) return $this->fail('请输入需要展示的销量');
- if (empty($rotation) || !is_array($rotation) || count($rotation) > 5) return $this->fail('请上传轮播图,且数量不能多余5个');
- if (empty($snap_up_details)) return $this->fail('请填写秒杀详情');
- if (empty($purchase_information)) return $this->fail('请输入购买须知');
- if ($sort < 0) return $this->fail('请输入排序,且不能小于0');
-
- //操作
- if ($snap_up_id != 0) {
- if (null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->fail($this->name . '不存在');
- if ($snap_up->is_delete != 0) return $this->fail($this->name . '不存在');
- $pre_rotation = $snap_up->rotation;
- $pre_snap_up_details = $snap_up->snap_up_details;
- } else {
- $snap_up = new SnapUpModel();
- $pre_rotation = [];
- $pre_snap_up_details = [];
- }
-
- $snap_up->project_id = $project_id;
- $snap_up->project_type_id = $project->project_type_id;
- $snap_up->vip_price = $vip_price;
- $snap_up->is_flash_sale = $is_flash_sale;
- if ($is_flash_sale == 1) {
- $snap_up->flash_price = $flash_price;
- $snap_up->flash_num = $flash_num;
- $snap_up->start_time = $start_time;
- $snap_up->end_time = $end_time;
- }
- $snap_up->valid_time = $valid_time;
- $snap_up->valid_unit = $valid_unit;
- $snap_up->fictitious_take_count = $fictitious_take_count;
- $snap_up->rotation = $rotation;
- $snap_up->snap_up_details = $snap_up_details;
- $snap_up->purchase_information = $purchase_information;
- $snap_up->sort = $sort;
- $snap_up->status = 1;
- $snap_up->add_time = time();
- $snap_up->save();
- return $this->success('保存成功');
- }
-
- public function undercarriage()
- {
- $snap_up_id = $this->request->param('snap_up_id/d', 0);
- if (null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->fail($this->name . '不存在');
- if ($snap_up->is_delete != 0) return $this->fail($this->name . '不存在');
- if ($snap_up->status != 1) return $this->fail($this->name . '已下架,请勿重复操作');
-
- $snap_up->status = -1;
- $snap_up->save();
- return $this->success('下架成功');
- }
-
- public function grounding()
- {
- $snap_up_id = $this->request->param('snap_up_id/d', 0);
- if (null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->fail($this->name . '不存在');
- if ($snap_up->is_delete != 0) return $this->fail($this->name . '不存在');
- if ($snap_up->status != -1) return $this->fail($this->name . '已上架,请勿重复操作');
-
- $snap_up->status = 1;
- $snap_up->save();
- return $this->success('上架成功');
- }
-
- public function del()
- {
- $snap_up_id = $this->request->param('snap_up_id/d', 0);
- if (null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->fail($this->name . '不存在');
- if ($snap_up->is_delete != 0) return $this->fail($this->name . '不存在');
- if ($snap_up->status != -1) return $this->fail($this->name . '还未下架,请先下架在删除');
-
- $snap_up->is_delete = 1;
- $snap_up->save();
- return $this->success('删除成功');
- }
-
- }
|