Activity.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. ]);
  47. }
  48. }
  49. if(count($save) > 0) {
  50. $this->activityProductModel->saveAll($save);
  51. }
  52. return $activity_id ? $this->ok(true) : $this->fail(false);
  53. }
  54. return view();
  55. }
  56. public function edit() {
  57. $params = $this->request->param();
  58. if(isset($params['id'])) {
  59. $activity = $this->activityModel->findById($params['id']);
  60. if ($this->request->isAjax()) {
  61. if ($activity) {
  62. $this->activityModel->where('id', $activity->id)->update([
  63. 'title' => $params['title'],
  64. 'start_time' => strtotime($params['start_time']),
  65. 'end_time' => strtotime($params['end_time']),
  66. ]);
  67. return $this->ok(true);
  68. }
  69. return $this->fail(lang("Fail"));
  70. }
  71. View::assign([
  72. 'activity' => $activity,
  73. ]);
  74. return \view();
  75. }
  76. return $this->fail(lang("Fail"));
  77. }
  78. public function delete(\think\Request $request) {
  79. $params = $request->param();
  80. if(!isset($params['ids']))
  81. return $this->fail(lang("Please select the data you want to delete"));
  82. $this->activityModel->deleteByIds(explode(',', $params['ids']));
  83. return $this->ok(true);
  84. }
  85. public function delete_relation() {
  86. $params = $this->request->param();
  87. if(isset($params['id'])) {
  88. $this->activityProductModel->deleteByIds([$params['id']]);
  89. }
  90. return $this->ok(true);
  91. }
  92. public function add_relation() {
  93. $params = $this->request->param();
  94. if(isset($params['product_id'])) {
  95. $relation = $this->activityProductModel->doesItExist($params['product_id']);
  96. if($relation && $relation->id != $params['id'])
  97. return $this->fail("Fail to add. Data duplication");
  98. $this->activityProductModel->save([
  99. 'activity_id' => $params['activity_id'],
  100. 'product_id' => $params['product_id'],
  101. 'type' => $params['type'],
  102. 'discount' => $params['discount'],
  103. 'reduced_price' => $params['reduced_price'],
  104. ]);
  105. }
  106. return $this->ok(true);
  107. }
  108. public function add_product($activity_product_id = null) {
  109. $relation = [];
  110. if($activity_product_id > 0) {
  111. $relation = $this->activityProductModel->findById($activity_product_id);
  112. }
  113. View::assign('relation', $relation);
  114. return view();
  115. }
  116. }