StorePromotions.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\v1\marketing\promotions;
  12. use app\controller\admin\AuthController;
  13. use app\services\activity\promotions\StorePromotionsServices;
  14. use think\facade\App;
  15. /**
  16. * 促销活动
  17. * Class StorePromotions
  18. * @package app\controller\admin\v1\marketing\promotions
  19. */
  20. class StorePromotions extends AuthController
  21. {
  22. /**
  23. * StorePromotions constructor.
  24. * @param App $app
  25. * @param StorePromotionsServices $services
  26. */
  27. public function __construct(App $app, StorePromotionsServices $services)
  28. {
  29. parent::__construct($app);
  30. $this->services = $services;
  31. }
  32. /**
  33. * 显示资源列表
  34. *
  35. * @return \think\Response
  36. */
  37. public function index($type)
  38. {
  39. $where = $this->request->getMore([
  40. [['status', 's'], ''],
  41. ['threshold_type', ''],
  42. ['n_piece_n_discount', ''],
  43. [['name', 's'], '']
  44. ]);
  45. $where['promotions_type'] = $type;
  46. $where['type'] = 1;
  47. $where['store_id'] = 0;
  48. $where['pid'] = 0;
  49. $where['is_del'] = 0;
  50. return $this->success($this->services->systemPage($where));
  51. }
  52. /**
  53. * 详情
  54. * @param $id
  55. * @return mixed
  56. */
  57. public function getInfo($id)
  58. {
  59. $info = $this->services->getInfo($id);
  60. return $this->success(compact('info'));
  61. }
  62. /**
  63. * 保存折扣促销活动
  64. * @param $type
  65. * @param $id
  66. * @return mixed
  67. */
  68. public function saveDiscount($type, $id)
  69. {
  70. $data = $this->request->postMore([
  71. [['promotions_cate', 'd'], 1],//优惠类型
  72. [['name', 's'], ''],//名称
  73. [['threshold_type', 'd'], 1],//门槛类型1:满N元2:满N件
  74. ['threshold', 0],//优惠门槛
  75. [['discount_type', 'd'], 1],//优惠类型1:满减2:满折
  76. [['n_piece_n_discount', 'd'], 3],//n件n折类型:1:第二件半件2:买1送1 3:自定义
  77. ['discount', 0],//优惠
  78. [['give_integral', 'd'], 0],//赠送积分
  79. ['give_coupon_id', []],//赠送优惠券ID
  80. ['give_product_id', []],//赠送商品ID
  81. ['give_product_unique', []],//赠送商品规格唯一值
  82. [['is_label', 'd'], 0],
  83. ['label_id', []],//关联标签
  84. [['product_partake_type', 'd'], 1],//商品参与类型
  85. ['product_id', []],//关联商品
  86. ['brand_id', []],//关联品牌ID
  87. ['store_label_id', []],//关联商品标签ID
  88. [['is_limit', 'd'], 0],//是否限量
  89. [['limit_num', 'd'], 0],//限量个数
  90. [['is_overlay', 'd'], 0],
  91. ['overlay', []],//叠加方式方式
  92. ['section_time', []],//时间
  93. [['sort', 'd'], 0],//排序
  94. ['applicable_type', 1],//适用门店类型
  95. ['applicable_store_id', []],//适用门店IDS
  96. ]);
  97. $data['promotions_type'] = $type;
  98. if ($data['promotions_type'] == 2) {
  99. $data['threshold_type'] = 2;
  100. }
  101. if (!$data['is_label']) {
  102. $data['label_id'] = [];
  103. }
  104. if (!$data['is_overlay']) {
  105. $data['overlay'] = [];
  106. }
  107. unset($data['is_label'], $data['is_overlay']);
  108. $data['promotions'][] = [
  109. 'threshold_type' => $data['threshold_type'],
  110. 'threshold' => $data['threshold'],
  111. 'discount_type' => $data['discount_type'],
  112. 'n_piece_n_discount' => $data['n_piece_n_discount'],
  113. 'discount' => $data['discount'],
  114. 'give_integral' => $data['give_integral'],
  115. 'give_coupon_id' => [],
  116. 'give_product_id' => [],
  117. 'give_product_unique' => [],
  118. ];
  119. if ($data['product_partake_type'] == 2 && !$data['product_id']) {
  120. return $this->fail('请选择要参与活动的商品');
  121. }
  122. if ($data['product_partake_type'] == 4 && !$data['brand_id']) {
  123. return $this->fail('请选择要参与活动的商品品牌');
  124. }
  125. if ($data['product_partake_type'] == 5 && !$data['store_label_id']) {
  126. return $this->fail('请选择要参与活动的商品标签');
  127. }
  128. if ($data['applicable_type'] == 1) {
  129. $data['applicable_store_id'] = [];
  130. } elseif ($data['applicable_type'] == 2) {
  131. if (!$data['applicable_store_id']) {
  132. return $this->fail('请选择要适用门店');
  133. }
  134. }
  135. $this->services->saveData((int)$id, $data);
  136. return $this->success('保存成功');
  137. }
  138. /**
  139. * 保存促销活动
  140. * @param $type
  141. * @param $id
  142. * @return mixed
  143. */
  144. public function save($type, $id)
  145. {
  146. $data = $this->request->postMore([
  147. [['promotions_cate', 'd'], 1],//优惠类型
  148. [['threshold_type', 'd'], 1],//门槛类型1:满N元2:满N件
  149. [['name', 's'], ''],//名称
  150. [['is_label', 'd'], 0],
  151. ['label_id', []],//关联标签
  152. [['product_partake_type', 'd'], 1],//商品参与类型
  153. ['product_id', []],//关联商品ID
  154. ['brand_id', []],//关联品牌ID
  155. ['store_label_id', []],//关联商品标签ID
  156. [['is_limit', 'd'], 0],//是否限量
  157. [['limit_num', 'd'], 0],//限量个数
  158. [['is_overlay', 'd'], 0],
  159. ['overlay', []],//叠加方式方式
  160. ['section_time', []],//时间
  161. [['sort', 'd'], 0],//排序
  162. ['promotions', []],//阶梯优惠数组
  163. ['applicable_type', 1],//适用门店类型
  164. ['applicable_store_id', []],//适用门店IDS
  165. ]);
  166. $data['promotions_type'] = $type;
  167. if (!$data['is_label']) {
  168. $data['label_id'] = [];
  169. }
  170. if (!$data['is_overlay']) {
  171. $data['overlay'] = [];
  172. }
  173. if ($data['product_partake_type'] == 2 && !$data['product_id']) {
  174. return $this->fail('请选择要参与活动的商品');
  175. }
  176. if ($data['product_partake_type'] == 4 && !$data['brand_id']) {
  177. return $this->fail('请选择要参与活动的商品品牌');
  178. }
  179. if ($data['product_partake_type'] == 5 && !$data['store_label_id']) {
  180. return $this->fail('请选择要参与活动的商品标签');
  181. }
  182. if ($data['applicable_type'] == 1) {
  183. $data['applicable_store_id'] = [];
  184. } elseif ($data['applicable_type'] == 2) {
  185. if (!$data['applicable_store_id']) {
  186. return $this->fail('请选择要适用门店');
  187. }
  188. }
  189. unset($data['is_label'], $data['is_overlay']);
  190. $this->services->saveData((int)$id, $data);
  191. return $this->success('保存成功');
  192. }
  193. /**
  194. * 删除指定资源
  195. *
  196. * @param int $id
  197. * @return \think\Response
  198. */
  199. public function delete($id)
  200. {
  201. if (!$id) return $this->fail('缺少参数');
  202. $this->services->update(['id|pid' => $id], ['is_del' => 1]);
  203. return $this->success('删除成功!');
  204. }
  205. /**
  206. * 修改状态
  207. * @param $id
  208. * @param $status
  209. * @return mixed
  210. */
  211. public function setStatus($id, $status)
  212. {
  213. $this->services->update($id, ['status' => $status, 'update_time' => time()]);
  214. return $this->success($status == 0 ? '关闭成功' : '开启成功');
  215. }
  216. }