Activity.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\admin\controller;
  3. use \app\BaseController;
  4. use app\common\model\ActivityModel;
  5. use app\common\model\ActivityProductModel;
  6. use think\App;
  7. use think\facade\View;
  8. class Activity extends BaseController
  9. {
  10. private $activityModel;
  11. private $activityProductModel;
  12. public function __construct(App $app)
  13. {
  14. $this->activityModel = new ActivityModel();
  15. $this->activityProductModel = new ActivityProductModel();
  16. parent::__construct($app);
  17. }
  18. /**
  19. * @return \think\response\View
  20. * @throws \think\db\exception\DbException
  21. */
  22. public function index() {
  23. $list = $this->activityModel->findByPaginate($this->request->param());
  24. View::assign([
  25. 'list' => $list
  26. ]);
  27. return view();
  28. }
  29. public function add() {
  30. $params = $this->request->param();
  31. if ($this->request->isAjax()) {
  32. $activity_id =$this->activityModel->insert([
  33. 'title' => $params['title'],
  34. 'start_time' => strtotime($params['start_time']),
  35. 'end_time' => strtotime($params['end_time']),
  36. ],true);
  37. $save = [];
  38. if(isset($params['product_ids'])) {
  39. for ($i = 0; $i < count($params['product_ids']);$i++) {
  40. array_push($save, [
  41. 'activity_id' => $activity_id,
  42. 'product_id' => $params['product_ids'][$i],
  43. 'type' => $params['types'][$i],
  44. 'discount' => $params['discounts'][$i],
  45. 'reduced_price' => $params['reduced_prices'][$i],
  46. 'start_time' => strtotime($params['start_time']),
  47. 'end_time' => strtotime($params['end_time'])
  48. ]);
  49. }
  50. }
  51. if(count($save) > 0) {
  52. $this->activityProductModel->saveAll($save);
  53. }
  54. return $activity_id ? $this->ok(true) : $this->fail(false);
  55. }
  56. return view();
  57. }
  58. public function edit() {
  59. $params = $this->request->param();
  60. if(isset($params['id'])) {
  61. $activity = $this->activityModel->findById($params['id']);
  62. if ($this->request->isAjax()) {
  63. if ($activity) {
  64. $this->activityModel->where('id', $activity->id)->update([
  65. 'title' => $params['title'],
  66. 'start_time' => strtotime($params['start_time']),
  67. 'end_time' => strtotime($params['end_time']),
  68. ]);
  69. $this->activityProductModel->where("activity_id", $activity->id)->update([
  70. 'start_time' => strtotime($params['start_time']),
  71. 'end_time' => strtotime($params['end_time'])
  72. ]);
  73. return $this->ok(true);
  74. }
  75. return $this->fail(lang("Fail"));
  76. }
  77. View::assign([
  78. 'activity' => $activity,
  79. ]);
  80. return \view();
  81. }
  82. return $this->fail(lang("Fail"));
  83. }
  84. public function delete(\think\Request $request) {
  85. $params = $request->param();
  86. if(!isset($params['ids']))
  87. return $this->fail(lang("Please select the data you want to delete"));
  88. $this->activityModel->deleteByIds(explode(',', $params['ids']));
  89. $this->activityProductModel->where("activity_id", 'in', explode(',', $params['ids']))->update(['is_delete' => 1]);
  90. return $this->ok(true);
  91. }
  92. public function delete_relation() {
  93. $params = $this->request->param();
  94. if(isset($params['id'])) {
  95. $this->activityProductModel->deleteByIds([$params['id']]);
  96. }
  97. return $this->ok(true);
  98. }
  99. public function add_relation() {
  100. $params = $this->request->param();
  101. if(isset($params['product_id'])) {
  102. $relation = $this->activityProductModel->doesItExist($params['activity_id'], $params['product_id']);
  103. if($relation && $relation->id != $params['product_id'])
  104. return $this->fail("Fail to add. Data duplication");
  105. $activity = $this->activityModel->findById($params['activity_id']);
  106. if(!$activity)
  107. return $this->fail("Add fail");
  108. $this->activityProductModel->save([
  109. 'activity_id' => $params['activity_id'],
  110. 'product_id' => $params['product_id'],
  111. 'type' => $params['type'],
  112. 'discount' => $params['discount'],
  113. 'reduced_price' => $params['reduced_price'],
  114. 'start_time' => $activity->start_time,
  115. 'end_time' => $activity->end_time
  116. ]);
  117. }
  118. return $this->ok(true);
  119. }
  120. public function add_product($activity_product_id = null) {
  121. $relation = [];
  122. if($activity_product_id > 0) {
  123. $relation = $this->activityProductModel->findById($activity_product_id);
  124. }
  125. View::assign('relation', $relation);
  126. return view();
  127. }
  128. }