Activity.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace app\admin\controller\shopro\activity;
  3. use think\Db;
  4. use app\admin\controller\shopro\Common;
  5. use app\admin\model\shopro\activity\Activity as ActivityModel;
  6. use app\admin\model\shopro\goods\Goods as GoodsModel;
  7. use app\admin\model\shopro\goods\Sku as SkuModel;
  8. use app\admin\model\shopro\goods\SkuPrice as SkuPriceModel;
  9. use addons\shopro\library\activity\Activity as ActivityManager;
  10. use addons\shopro\library\activity\traits\CheckActivity;
  11. use addons\shopro\facade\Activity as ActivityFacade;
  12. /**
  13. * 活动管理
  14. */
  15. class Activity extends Common
  16. {
  17. use CheckActivity;
  18. protected $noNeedRight = ['getType', 'select', 'skus'];
  19. protected $manager = null;
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new ActivityModel;
  25. $this->manager = ActivityFacade::instance();
  26. }
  27. /**
  28. * 查看
  29. *
  30. * @return string|Json
  31. * @throws \think\Exception
  32. * @throws DbException
  33. */
  34. public function index()
  35. {
  36. $type = $this->request->param('type', null);
  37. if (!$this->request->isAjax()) {
  38. if ($type) {
  39. return $this->view->fetch('shopro/activity/activity/index');
  40. }
  41. return $this->view->fetch('shopro/activity/activity/activity');
  42. }
  43. $activities = $this->model->sheepFilter()->where('type', $type)->paginate(request()->param('list_rows', 10))->toArray();
  44. $items = $activities['data'];
  45. // 关联活动的商品
  46. $goodsIds = array_values(array_filter(array_column($items, 'goods_ids')));
  47. $goodsIdsArr = [];
  48. foreach ($goodsIds as $ids) {
  49. $idsArr = explode(',', $ids);
  50. $goodsIdsArr = array_merge($goodsIdsArr, $idsArr);
  51. }
  52. $goodsIdsArr = array_values(array_filter(array_unique($goodsIdsArr)));
  53. if ($goodsIdsArr) {
  54. // 查询商品
  55. $goods = GoodsModel::where('id', 'in', $goodsIdsArr)->select();
  56. $goods = array_column($goods, null, 'id');
  57. }
  58. foreach ($items as $key => $activity) {
  59. $items[$key]['goods'] = [];
  60. if ($activity['goods_ids']) {
  61. $idsArr = explode(',', $activity['goods_ids']);
  62. foreach ($idsArr as $id) {
  63. if (isset($goods[$id])) {
  64. $items[$key]['goods'][] = $goods[$id];
  65. }
  66. }
  67. }
  68. }
  69. $activities['data'] = $items;
  70. $this->success('获取成功', null, $activities);
  71. }
  72. // 获取数据类型
  73. public function getType()
  74. {
  75. if (!$this->request->isAjax()) {
  76. return $this->view->fetch();
  77. }
  78. $typeList = $this->model->typeList();
  79. $result = [
  80. 'type_list' => $typeList,
  81. ];
  82. $data = [];
  83. foreach ($result as $key => $list) {
  84. $data[$key][] = ['name' => '全部', 'type' => 'all'];
  85. foreach ($list as $k => $v) {
  86. $data[$key][] = [
  87. 'name' => $v,
  88. 'type' => $k
  89. ];
  90. }
  91. }
  92. $this->success('获取成功', null, $data);
  93. }
  94. /**
  95. * 添加
  96. */
  97. public function add()
  98. {
  99. if (!$this->request->isAjax()) {
  100. return $this->view->fetch();
  101. }
  102. $params = $this->request->only([
  103. 'title', 'goods_ids', 'type', 'prehead_time', 'start_time', 'end_time',
  104. 'rules', 'richtext_id', 'richtext_title', 'goods_list'
  105. ]);
  106. if (isset($params['goods_list'])) {
  107. $params['goods_list'] = json_decode($params['goods_list'], true);
  108. }
  109. $this->svalidate($params, ".add");
  110. Db::transaction(function () use ($params) {
  111. $this->manager->save($params);
  112. });
  113. $this->success('保存成功');
  114. }
  115. /**
  116. * 详情
  117. *
  118. * @param $id
  119. * @return \think\Response
  120. */
  121. public function detail($id)
  122. {
  123. $activity = $this->model->where('id', $id)->find();
  124. if (!$activity) {
  125. $this->error(__('No Results were found'));
  126. }
  127. $activity->goods_list = $activity->goods_list;
  128. $activity->rules = $activity->rules;
  129. $this->success('获取成功', null, $activity);
  130. }
  131. /**
  132. * 编辑(支持批量)
  133. */
  134. public function edit($id = null)
  135. {
  136. if (!$this->request->isAjax()) {
  137. return $this->view->fetch('add');
  138. }
  139. $params = $this->request->only([
  140. 'title', 'goods_ids', 'prehead_time', 'start_time', 'end_time',
  141. 'rules', 'richtext_id', 'richtext_title', 'goods_list'
  142. ]);
  143. if (isset($params['goods_list'])) {
  144. $params['goods_list'] = json_decode($params['goods_list'], true);
  145. }
  146. $this->svalidate($params);
  147. $id = explode(',', $id);
  148. $items = $this->model->whereIn('id', $id)->select();
  149. Db::transaction(function () use ($items, $params) {
  150. foreach ($items as $activity) {
  151. $this->manager->update($activity, $params);
  152. }
  153. });
  154. $this->success('更新成功');
  155. }
  156. /**
  157. * 获取活动商品规格并且初始化
  158. */
  159. public function skus()
  160. {
  161. if (!$this->request->isAjax()) {
  162. return $this->view->fetch();
  163. }
  164. $params = $this->request->param();
  165. $id = $params['id'] ?? 0;
  166. $goods_id = $params['goods_id'] ?? 0;
  167. $activity_type = $params['activity_type'] ?? '';
  168. $start_time = $params['start_time'] ?? '';
  169. $end_time = $params['end_time'] ?? '';
  170. $prehead_time = $params['prehead_time'] ?? '';
  171. if ($start_time && $end_time && $activity_type) {
  172. // 如果存在开始结束时间,并且是要修改
  173. $goodsList = [$goods_id => ['id' => $goods_id]];
  174. $this->checkActivityConflict([
  175. 'type' => $activity_type,
  176. 'classify' => $this->model->getClassify($activity_type),
  177. 'start_time' => $start_time,
  178. 'end_time' => $end_time,
  179. 'prehead_time' => $prehead_time
  180. ], $goodsList, $id);
  181. }
  182. // 商品规格
  183. $skus = SkuModel::with('children')->where('goods_id', $goods_id)->where('parent_id', 0)->select();
  184. // 获取规格
  185. $skuPrices = SkuPriceModel::with(['activity_sku_price' => function ($query) use ($id) {
  186. $query->where('activity_id', $id);
  187. }])->where('goods_id', $goods_id)->select();
  188. //编辑
  189. $activitySkuPrices = [];
  190. foreach ($skuPrices as $k => $skuPrice) {
  191. $activitySkuPrices[$k] = $skuPrice->activity_sku_price ? $skuPrice->activity_sku_price->toArray() : [];
  192. // 活动规格数据初始化
  193. if (!$activitySkuPrices[$k]) {
  194. $activitySkuPrices[$k]['id'] = 0;
  195. $activitySkuPrices[$k]['status'] = 'down';
  196. $activitySkuPrices[$k]['price'] = '';
  197. $activitySkuPrices[$k]['stock'] = '';
  198. $activitySkuPrices[$k]['sales'] = '0';
  199. $activitySkuPrices[$k]['goods_sku_price_id'] = $skuPrice->id;
  200. }
  201. // 个性化初始化每个活动的 规格 字段
  202. $activitySkuPrices[$k] = $this->manager->showSkuPrice($activity_type, $activitySkuPrices[$k]);
  203. }
  204. $this->success('获取成功', null, [
  205. 'skus' => $skus,
  206. 'sku_prices' => $skuPrices,
  207. 'activity_sku_prices' => $activitySkuPrices,
  208. ]);
  209. }
  210. public function select()
  211. {
  212. if (!$this->request->isAjax()) {
  213. return $this->view->fetch();
  214. }
  215. $type = $this->request->param('type');
  216. $activities = $this->model->sheepFilter()->whereIn('type', $type)->paginate(request()->param('list_rows', 10))->toArray();
  217. $this->success('获取成功', null, $activities);
  218. }
  219. /**
  220. * 删除(支持批量)
  221. *
  222. * @param $id
  223. * @return \think\Response
  224. */
  225. public function delete($id)
  226. {
  227. if (empty($id)) {
  228. $this->error(__('Parameter %s can not be empty', 'id'));
  229. }
  230. $list = $this->model->where('id', 'in', $id)->select();
  231. $result = Db::transaction(function () use ($list) {
  232. $count = 0;
  233. foreach ($list as $item) {
  234. $count += $this->manager->delete($item);
  235. }
  236. return $count;
  237. });
  238. if ($result) {
  239. $this->success('删除成功', null, $result);
  240. } else {
  241. $this->error(__('No rows were deleted'));
  242. }
  243. }
  244. public function recyclebin()
  245. {
  246. if (!$this->request->isAjax()) {
  247. return $this->view->fetch();
  248. }
  249. $type = $this->request->param('type');
  250. $activities = $this->model->onlyTrashed()->sheepFilter()->where('type', $type)->paginate($this->request->param('list_rows', 10));
  251. $this->success('获取成功', null, $activities);
  252. }
  253. }