Activity.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_relation() {
  79. $params = $this->request->param();
  80. if(isset($params['id'])) {
  81. $this->activityProductModel->deleteByIds([$params['id']]);
  82. }
  83. return $this->ok(true);
  84. }
  85. public function add_relation() {
  86. $params = $this->request->param();
  87. if(isset($params['id'])) {
  88. $relation = $this->activityProductModel->doesItExist($params['product_id']);
  89. if($relation && $relation->id != $params['id'])
  90. return $this->fail("Fail to add. Data duplication");
  91. $this->activityProductModel->save([
  92. 'activity_id' => $relation->id,
  93. 'product_id' => $params['product_id'],
  94. 'type' => $params['type'],
  95. 'discount' => $params['discount'],
  96. 'reduced_price' => $params['reduced_price'],
  97. ]);
  98. }
  99. return $this->ok(true);
  100. }
  101. public function add_product($activity_product_id = null) {
  102. $relation = [];
  103. if($activity_product_id > 0) {
  104. $relation = $this->activityProductModel->findById($activity_product_id);
  105. }
  106. View::assign('relation', $relation);
  107. return view();
  108. }
  109. }