You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Snapup.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace app\controller\store\project;
  3. use app\common\logic\DataLogic;
  4. use app\model\store\project\ProjectModel;
  5. use app\model\store\project\ProjectTypeModel;
  6. use app\model\store\project\SnapUpModel;
  7. use app\controller\store\AuthController;
  8. class Snapup extends AuthController
  9. {
  10. protected $name = '抢购';
  11. public function index()
  12. {
  13. $abbreviation = $this->request->param('abbreviation');
  14. $project_type_id = $this->request->param('project_type_id/d');
  15. $bg_take_count = $this->request->param('bg_take_count/d');
  16. $end_take_count = $this->request->param('end_take_count/d');
  17. $status = $this->request->param('status/d', 1);
  18. $is_flash_sale = $this->request->param('is_flash_sale/d', 0);
  19. if (!in_array($status, [1, -1])) $status = 1;
  20. $where = [
  21. ['is_delete', '=', 0],
  22. ['status', '=', $status]
  23. ];
  24. if (!empty($bg_take_count)) {
  25. if (!empty($end_take_count)) {
  26. $where[] = ['take_count', 'between', [$bg_take_count, $end_take_count]];
  27. } else {
  28. $where[] = ['take_count', '>=', $bg_take_count];
  29. }
  30. } else {
  31. if (!empty($end_take_count)) {
  32. $where[] = ['take_count', '<=', $end_take_count];
  33. }
  34. }
  35. $today = date('Y-m-d', $this->request->time());
  36. if (!empty($project_type_id)) $where[] = ['project_type_id', '=', $project_type_id];
  37. $project_where = [
  38. ['is_delete', '=', 0],
  39. ];
  40. if (!empty($abbreviation)) $project_where[] = ['abbreviation', '=', $abbreviation];
  41. $project_ids = ProjectModel::where($project_where)->where([
  42. ['is_delete', '=', 0]
  43. ])->column('project_id') ?? [];
  44. $where[] = ['project_id', 'in', $project_ids];
  45. $snap_up_model = new SnapUpModel();
  46. if ($is_flash_sale == 1) {
  47. $where[] = ['is_flash_sale', '=', 1];
  48. $where[] = ['start_time', '<=', $today];
  49. $where[] = ['end_time', '>=', $today];
  50. $where[] = ['flash_num', '>', 0];
  51. } else {
  52. $snap_up_model = $snap_up_model->whereRaw("(start_time > '{$today}' or end_time < '{$today}' or flash_num <= 0) OR is_flash_sale = 0");
  53. }
  54. $snap_up_model = $snap_up_model->with(['type', 'project']);
  55. $order = ['sort' => 'desc', 'add_time' => 'desc'];
  56. $field = '*';
  57. $append = ['add_time_format', 'is_flash_sale_expire'];
  58. //???
  59. $return = DataLogic::getDataList($snap_up_model, $where, $order, $field, $append);
  60. if (DataLogic::$page == 1) {
  61. $type_list = ProjectTypeModel::where([
  62. ['is_delete', '=', 0]
  63. ])->field('project_type_id,name')->select()->toArray();
  64. $return['type_list'] = $type_list;
  65. }
  66. foreach ($return['list'] as $k => $v) {
  67. $return['list'][$k]['is_flash_sale'] = (string)$v['is_flash_sale'];
  68. }
  69. return $this->success($return);
  70. }
  71. public function show()
  72. {
  73. $snap_up_id = $this->request->param('snap_up_id/d', 0);
  74. if ($snap_up_id == 0) return $this->success([]);
  75. if (null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->fail($this->name . '不存在');
  76. if ($snap_up->is_delete != 0) return $this->fail($this->name . '不存在');
  77. if (null === $project = ProjectModel::find($snap_up->project_id)) return $this->fail($this->name . '不存在');
  78. if ($project->is_delete != 0) return $this->fail($this->name . '不存在');
  79. $detail = [
  80. 'project' => [
  81. 'project_id' => $project->project_id,
  82. 'abbreviation' => $project->abbreviation,
  83. 'market_price' => $project->market_price,
  84. 'cover_img' => $project->cover_img,
  85. ],
  86. 'vip_price' => $snap_up->vip_price,
  87. 'is_flash_sale' => $snap_up->is_flash_sale,
  88. 'flash_price' => $snap_up->flash_price,
  89. 'flash_num' => $snap_up->flash_num,
  90. 'flash_sale_time' => $snap_up->flash_sale_time,
  91. 'valid_time' => $snap_up->valid_time,
  92. 'valid_unit' => $snap_up->valid_unit,
  93. 'fictitious_take_count' => $snap_up->fictitious_take_count,
  94. 'rotation' => $snap_up->rotation,
  95. 'snap_up_details' => $snap_up->snap_up_details,
  96. 'purchase_information' => $snap_up->purchase_information,
  97. 'sort' => $snap_up->sort,
  98. ];
  99. return $this->success(['detail' => $detail]);
  100. }
  101. //选择项目
  102. public function chooseProject()
  103. {
  104. $name = $this->request->param('name');
  105. $where = [
  106. ['is_delete', '=', 0],
  107. ];
  108. if (!empty($name)) $where[] = ['abbreviation', '=', $name];
  109. //判断项目的唯一性
  110. $project_ids = SnapUpModel::where([
  111. ['is_delete', '=', 0],
  112. ])->column('project_id') ?? [];
  113. // $where[] = ['project_id', 'not in', $project_ids];
  114. $project_model = new ProjectModel();
  115. $order = ['add_time' => 'desc'];
  116. $field = '*';
  117. $append = ['add_time_format'];
  118. $return = DataLogic::getDataList($project_model, $where, $order, $field, $append);
  119. return $this->success($return);
  120. }
  121. public function save()
  122. {
  123. $snap_up_id = $this->request->param('snap_up_id/d', 0);
  124. $project_id = $this->request->param('project_id/d', 0);
  125. $vip_price = $this->request->param('vip_price/f', 0);
  126. $is_flash_sale = $this->request->param('is_flash_sale/d', 0);
  127. $flash_price = $this->request->param('flash_price/f', 0);
  128. $flash_num = $this->request->param('flash_num/d', 0);
  129. $flash_sale_time = $this->request->param('flash_sale_time/a', []);
  130. $valid_time = $this->request->param('valid_time/d', 0);
  131. $valid_unit = $this->request->param('valid_unit');
  132. $fictitious_take_count = $this->request->param('fictitious_take_count/d', 0);
  133. $rotation = $this->request->param('rotation/a', []);
  134. $snap_up_details = $this->request->param('snap_up_details', '');//, 'security_json_or_array'
  135. $purchase_information = $this->request->param('purchase_information');
  136. $sort = $this->request->param('sort/d', 0);
  137. //判断传来的参数
  138. if (null === $project = ProjectModel::find($project_id)) return $this->fail('项目不存在');
  139. if ($project->is_delete != 0) return $this->fail('项目不存在');
  140. //判断项目的唯一性 (后期商榷在添加)
  141. if (empty($vip_price) || $vip_price <= 0) return $this->fail('请输入会员价,且不能小于0');
  142. if ($vip_price > $project->market_price) return $this->fail('会员价不能高于项目市场价');
  143. if (!in_array($is_flash_sale, [0, 1])) return $this->fail('参数错误');
  144. if ($is_flash_sale == 1) {
  145. if (empty($flash_price) || $flash_price <= 0) return $this->fail('请输入秒杀价,且不能小于0');
  146. if ($flash_price > $vip_price) return $this->fail('秒杀价不能大于会员价');
  147. if (empty($flash_num) || $flash_num <= 0) return $this->fail('秒杀数量不能小于0');
  148. if (empty($flash_sale_time) || count($flash_sale_time) != 2) return $this->fail('请选择秒杀时间');
  149. $start_time = $flash_sale_time[0];
  150. $end_time = $flash_sale_time[1];
  151. if (false === $start_time_unix = strtotime($start_time)) return $this->fail('请选择正确的秒杀开始时间');
  152. if (false === $end_time_unix = strtotime($end_time)) return $this->fail('请选择正确的秒杀结束时间');
  153. if ($end_time_unix < $start_time_unix) return $this->fail('秒杀开始时间不能大于结束时间');
  154. }
  155. if (empty($valid_time) || $valid_time <= 0) return $this->fail('请输入有效期的值,且不能小于0');
  156. if (empty($valid_unit) || !in_array($valid_unit, ['day', 'month', 'year'])) return $this->fail('请选择有效期单位');
  157. if (empty($fictitious_take_count) || $fictitious_take_count <= 0) return $this->fail('请输入需要展示的销量');
  158. if (empty($rotation) || !is_array($rotation) || count($rotation) > 5) return $this->fail('请上传轮播图,且数量不能多余5个');
  159. if (empty($snap_up_details)) return $this->fail('请填写秒杀详情');
  160. if (empty($purchase_information)) return $this->fail('请输入购买须知');
  161. if ($sort < 0) return $this->fail('请输入排序,且不能小于0');
  162. //操作
  163. if ($snap_up_id != 0) {
  164. if (null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->fail($this->name . '不存在');
  165. if ($snap_up->is_delete != 0) return $this->fail($this->name . '不存在');
  166. $pre_rotation = $snap_up->rotation;
  167. $pre_snap_up_details = $snap_up->snap_up_details;
  168. } else {
  169. $snap_up = new SnapUpModel();
  170. $pre_rotation = [];
  171. $pre_snap_up_details = [];
  172. }
  173. $snap_up->project_id = $project_id;
  174. $snap_up->project_type_id = $project->project_type_id;
  175. $snap_up->vip_price = $vip_price;
  176. $snap_up->is_flash_sale = $is_flash_sale;
  177. if ($is_flash_sale == 1) {
  178. $snap_up->flash_price = $flash_price;
  179. $snap_up->flash_num = $flash_num;
  180. $snap_up->start_time = $start_time;
  181. $snap_up->end_time = $end_time;
  182. }
  183. $snap_up->valid_time = $valid_time;
  184. $snap_up->valid_unit = $valid_unit;
  185. $snap_up->fictitious_take_count = $fictitious_take_count;
  186. $snap_up->rotation = $rotation;
  187. $snap_up->snap_up_details = $snap_up_details;
  188. $snap_up->purchase_information = $purchase_information;
  189. $snap_up->sort = $sort;
  190. $snap_up->status = 1;
  191. $snap_up->add_time = time();
  192. $snap_up->save();
  193. return $this->success('保存成功');
  194. }
  195. public function undercarriage()
  196. {
  197. $snap_up_id = $this->request->param('snap_up_id/d', 0);
  198. if (null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->fail($this->name . '不存在');
  199. if ($snap_up->is_delete != 0) return $this->fail($this->name . '不存在');
  200. if ($snap_up->status != 1) return $this->fail($this->name . '已下架,请勿重复操作');
  201. $snap_up->status = -1;
  202. $snap_up->save();
  203. return $this->success('下架成功');
  204. }
  205. public function grounding()
  206. {
  207. $snap_up_id = $this->request->param('snap_up_id/d', 0);
  208. if (null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->fail($this->name . '不存在');
  209. if ($snap_up->is_delete != 0) return $this->fail($this->name . '不存在');
  210. if ($snap_up->status != -1) return $this->fail($this->name . '已上架,请勿重复操作');
  211. $snap_up->status = 1;
  212. $snap_up->save();
  213. return $this->success('上架成功');
  214. }
  215. public function del()
  216. {
  217. $snap_up_id = $this->request->param('snap_up_id/d', 0);
  218. if (null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->fail($this->name . '不存在');
  219. if ($snap_up->is_delete != 0) return $this->fail($this->name . '不存在');
  220. if ($snap_up->status != -1) return $this->fail($this->name . '还未下架,请先下架在删除');
  221. $snap_up->is_delete = 1;
  222. $snap_up->save();
  223. return $this->success('删除成功');
  224. }
  225. }