| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace app\admin\controller;
- use \app\BaseController;
- use app\common\model\ActivityModel;
- use app\common\model\ActivityProductModel;
- use think\App;
- use think\facade\View;
- class Activity extends BaseController
- {
- private $activityModel;
- private $activityProductModel;
- public function __construct(App $app)
- {
- $this->activityModel = new ActivityModel();
- $this->activityProductModel = new ActivityProductModel();
- parent::__construct($app);
- }
- /**
- * @return \think\response\View
- * @throws \think\db\exception\DbException
- */
- public function index() {
- $list = $this->activityModel->findByPaginate($this->request->param());
- View::assign([
- 'list' => $list
- ]);
- return view();
- }
- public function add() {
- $params = $this->request->param();
- if ($this->request->isAjax()) {
- $activity_id =$this->activityModel->insert([
- 'title' => $params['title'],
- 'start_time' => strtotime($params['start_time']),
- 'end_time' => strtotime($params['end_time']),
- ],true);
- $save = [];
- if(isset($params['product_ids'])) {
- for ($i = 0; $i < count($params['product_ids']);$i++) {
- array_push($save, [
- 'activity_id' => $activity_id,
- 'product_id' => $params['product_ids'][$i],
- 'type' => $params['types'][$i],
- 'discount' => $params['discounts'][$i],
- 'reduced_price' => $params['reduced_prices'][$i],
- 'start_time' => strtotime($params['start_time']),
- 'end_time' => strtotime($params['end_time'])
- ]);
- }
- }
- if(count($save) > 0) {
- $this->activityProductModel->saveAll($save);
- }
- return $activity_id ? $this->ok(true) : $this->fail(false);
- }
- return view();
- }
- public function edit() {
- $params = $this->request->param();
- if(isset($params['id'])) {
- $activity = $this->activityModel->findById($params['id']);
- if ($this->request->isAjax()) {
- if ($activity) {
- $this->activityModel->where('id', $activity->id)->update([
- 'title' => $params['title'],
- 'start_time' => strtotime($params['start_time']),
- 'end_time' => strtotime($params['end_time']),
- ]);
- $this->activityProductModel->where("activity_id", $activity->id)->update([
- 'start_time' => strtotime($params['start_time']),
- 'end_time' => strtotime($params['end_time'])
- ]);
- return $this->ok(true);
- }
- return $this->fail(lang("Fail"));
- }
- View::assign([
- 'activity' => $activity,
- ]);
- return \view();
- }
- return $this->fail(lang("Fail"));
- }
- public function delete(\think\Request $request) {
- $params = $request->param();
- if(!isset($params['ids']))
- return $this->fail(lang("Please select the data you want to delete"));
- $this->activityModel->deleteByIds(explode(',', $params['ids']));
- $this->activityProductModel->where("activity_id", 'in', explode(',', $params['ids']))->update(['is_delete' => 1]);
- return $this->ok(true);
- }
- public function delete_relation() {
- $params = $this->request->param();
- if(isset($params['id'])) {
- $this->activityProductModel->deleteByIds([$params['id']]);
- }
- return $this->ok(true);
- }
- public function add_relation() {
- $params = $this->request->param();
- if(isset($params['product_id'])) {
- $relation = $this->activityProductModel->doesItExist($params['activity_id'], $params['product_id']);
- if($relation && $relation->id != $params['product_id'])
- return $this->fail("Fail to add. Data duplication");
- $activity = $this->activityModel->findById($params['activity_id']);
- if(!$activity)
- return $this->fail("Add fail");
- $this->activityProductModel->save([
- 'activity_id' => $params['activity_id'],
- 'product_id' => $params['product_id'],
- 'type' => $params['type'],
- 'discount' => $params['discount'],
- 'reduced_price' => $params['reduced_price'],
- 'start_time' => $activity->start_time,
- 'end_time' => $activity->end_time
- ]);
- }
- return $this->ok(true);
- }
- public function add_product($activity_product_id = null) {
- $relation = [];
- if($activity_product_id > 0) {
- $relation = $this->activityProductModel->findById($activity_product_id);
- }
- View::assign('relation', $relation);
- return view();
- }
- }
|