ActivityFrameServices.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. declare (strict_types=1);
  12. namespace app\services\activity\activityFrame;
  13. use app\dao\activity\promotions\StorePromotionsDao;
  14. use app\services\activity\promotions\StorePromotionsAuxiliaryServices;
  15. use app\services\BaseServices;
  16. use app\services\product\brand\StoreBrandServices;
  17. use app\services\product\category\StoreProductCategoryServices;
  18. use app\services\product\label\StoreProductLabelServices;
  19. use app\services\product\product\StoreProductRelationServices;
  20. use app\services\product\product\StoreProductServices;
  21. use crmeb\exceptions\AdminException;
  22. use think\exception\ValidateException;
  23. use \crmeb\traits\OptionTrait;
  24. /**
  25. * 活动边框
  26. * Class ActivityFrameServices
  27. * @package app\services\activity\activityFrame
  28. * @mixin StorePromotionsDao
  29. */
  30. class ActivityFrameServices extends BaseServices
  31. {
  32. use OptionTrait;
  33. /**
  34. * 活动类型
  35. * @var string[]
  36. */
  37. protected $promotionsType = [
  38. 1 => '限时折扣',
  39. 2 => '第N件N折',
  40. 3 => '满减满折',
  41. 4 => '满送',
  42. 5 => '活动边框',
  43. 6 => '活动背景',
  44. ];
  45. /**
  46. * 参与活动商品类型
  47. * @var string[]
  48. */
  49. protected $productPartakeType = [
  50. 1 => '全部商品',
  51. 2 => '指定商品参与',
  52. 3 => '指定商品不参与',
  53. 4 => '指定品牌参与',
  54. 5 => '指定商品标签参与',
  55. ];
  56. /**
  57. * StorePromotionsServices constructor.
  58. * @param StorePromotionsDao $dao
  59. */
  60. public function __construct(StorePromotionsDao $dao)
  61. {
  62. $this->dao = $dao;
  63. }
  64. /**
  65. * 后台获取活动边框列表
  66. * @param array $where
  67. * @return array
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function systemPage(array $where)
  73. {
  74. [$page, $limit] = $this->getPageValue();
  75. $list = $this->dao->getList($where, '*', $page, $limit);
  76. $count = 0;
  77. if ($list) {
  78. /** @var StoreProductServices $storeProductServices */
  79. $storeProductServices = app()->make(StoreProductServices::class);
  80. /** @var StoreProductRelationServices $storeProductRelationServices */
  81. $storeProductRelationServices = app()->make(StoreProductRelationServices::class);
  82. /** @var StorePromotionsAuxiliaryServices $promotionsAuxiliaryServices */
  83. $promotionsAuxiliaryServices = app()->make(StorePromotionsAuxiliaryServices::class);
  84. foreach ($list as &$item) {
  85. if ($item['status']) {
  86. if ($item['start_time'] > time()){
  87. $item['start_status'] = 0;
  88. $item['start_name'] = '未开始';
  89. } else if ((int)$item['stop_time'] < time()) {
  90. $item['start_name'] = '已结束';
  91. $item['start_status'] = -1;
  92. } else if ((int)$item['stop_time'] > time() && $item['start_time'] < time()) {
  93. $item['start_name'] = '进行中';
  94. $item['start_status'] = 1;
  95. }
  96. } else {
  97. $item['start_status'] = -1;
  98. $item['start_name'] = '已结束';
  99. }
  100. $item['start_time'] = $item['start_time'] ? date('Y-m-d H:i:s', $item['start_time']) : '';
  101. $item['stop_time'] = $item['stop_time'] ? date('Y-m-d H:i:s', $item['stop_time']) : '';
  102. $promotionsAuxiliaryData = $promotionsAuxiliaryServices->getPromotionsAuxiliaryCache($item['id']);
  103. switch ($item['product_partake_type']) {
  104. case 1://所有商品
  105. $item['product_count'] = $storeProductServices->count(['is_show' => 1, 'is_del' => 0, 'type' => [0 ,2], 'is_verify' => 1]);
  106. break;
  107. case 2://选中商品参与
  108. $product_ids = $promotionsAuxiliaryData;
  109. $item['product_count'] = $product_ids ? $storeProductServices->count(['is_show' => 1, 'is_del' => 0, 'id' => $product_ids, 'is_verify' => 1]) : 0;
  110. break;
  111. case 3:
  112. $item['product_count'] = 0;
  113. break;
  114. case 4://品牌
  115. $product_ids = $promotionsAuxiliaryData ? $storeProductRelationServices->getIdsByWhere(['type' => 2, 'relation_id' => $promotionsAuxiliaryData]) : [];
  116. $item['product_count'] = $product_ids ? $storeProductServices->count(['is_show' => 1, 'is_del' => 0, 'id' => $product_ids, 'type' => [0 ,2], 'is_verify' => 1]) : 0;
  117. break;
  118. case 5://商品标签
  119. $product_ids = $promotionsAuxiliaryData ? $storeProductRelationServices->getIdsByWhere(['type' => 3, 'relation_id' => $promotionsAuxiliaryData]) : [];
  120. $item['product_count'] = $product_ids ? $storeProductServices->count(['is_show' => 1, 'is_del' => 0, 'id' => $product_ids, 'type' => [0 ,2], 'is_verify' => 1]) : 0;
  121. break;
  122. }
  123. }
  124. $count = $this->dao->count($where);
  125. }
  126. return compact('list', 'count');
  127. }
  128. /**
  129. * 获取活动边框想你去那个
  130. * @param int $id
  131. * @return array
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. public function getInfo(int $id)
  137. {
  138. $info = $this->dao->get($id, ['*'], ['products' => function ($query) {
  139. $query->field('promotions_id,product_id,unique')->with(['productInfo']);
  140. }, 'brands' => function ($query) {
  141. $query->field('promotions_id,brand_id')->with(['brandInfo' => function ($q) { $q->field('id,brand_name');}]);
  142. }, 'productLabels' => function ($query) {
  143. $query->field('promotions_id,store_label_id')->with(['productLabelInfo' => function ($q) { $q->field('id,label_name');}]);
  144. }]);
  145. if (!$info) {
  146. throw new AdminException('数据不存在');
  147. }
  148. $info = $info->toArray();
  149. $info['start_time'] = $info['start_time'] ? date('Y-m-d H:i:s', $info['start_time']) : '';
  150. $info['stop_time'] = $info['stop_time'] ? date('Y-m-d H:i:s', $info['stop_time']) : '';
  151. $products = [];
  152. if (isset($info['products']) && $info['products']) {
  153. foreach ($info['products'] as $item) {
  154. $product = is_object($item) ? $item->toArray() : $item;
  155. $product = array_merge($product, $product['productInfo'] ?? []);
  156. unset($product['productInfo']);
  157. $products[] = $product;
  158. }
  159. if ($products) {
  160. $cateIds = implode(',', array_column($products, 'cate_id'));
  161. /** @var StoreProductCategoryServices $categoryService */
  162. $categoryService = app()->make(StoreProductCategoryServices::class);
  163. $cateList = $categoryService->getCateParentAndChildName($cateIds);
  164. foreach ($products as $key => &$item) {
  165. $item['cate_name'] = '';
  166. if (isset($item['cate_id']) && $item['cate_id']) {
  167. $cate_ids = explode(',', $item['cate_id']);
  168. $cate_name = $categoryService->getCateName($cate_ids, $cateList);
  169. if ($cate_name) {
  170. $item['cate_name'] = is_array($cate_name) ? implode(',', $cate_name) : '';
  171. }
  172. }
  173. }
  174. }
  175. }
  176. $info['products'] = $products;
  177. $info['brand_id'] = isset($info['brands']) && $info['brands'] ? array_column($info['brands'], 'brand_id') : [];
  178. $info['store_label_id'] = [];
  179. if (isset($info['productLabels']) && $info['productLabels']) {
  180. foreach ($info['productLabels'] as $label) {
  181. if (isset($label['productLabelInfo']) && $label['productLabelInfo']) {
  182. $info['store_label_id'][] = $label['productLabelInfo'];
  183. }
  184. }
  185. }
  186. unset($info['brands'], $info['productLabels']);
  187. return $info;
  188. }
  189. /**
  190. * 保存活动边框
  191. * @param int $id
  192. * @param array $data
  193. * @return bool
  194. */
  195. public function saveData(int $id, array $data)
  196. {
  197. if (!$data['section_time'] || count($data['section_time']) != 2) {
  198. throw new AdminException('请选择活动时间');
  199. }
  200. [$start_time, $end_time] = $data['section_time'];
  201. if (strtotime($end_time) < time()) {
  202. throw new AdminException('活动结束时间不能小于当前时间');
  203. }
  204. if ($id) {
  205. $info = $this->dao->get((int)$id);
  206. if (!$info) {
  207. throw new AdminException('数据不存在');
  208. }
  209. }
  210. $data['start_time'] = strtotime($start_time);
  211. $data['stop_time'] = strtotime($end_time);
  212. $promotionsAuxiliaryData = [];
  213. switch ($data['product_partake_type']) {
  214. case 1://全部
  215. $data['product_id'] = [];
  216. break;
  217. case 2://指定ID参与
  218. case 3://指定ID不参与
  219. /** @var StoreProductServices $storeProductServices */
  220. $storeProductServices = app()->make(StoreProductServices::class);
  221. $count = $storeProductServices->count(['is_show' => 1, 'is_del' => 0, 'id' => $data['product_id']]);
  222. $productCount = count(array_unique($data['product_id']));
  223. if ($count != $productCount) {
  224. throw new AdminException('选择商品中有已下架或移入回收站');
  225. }
  226. $promotionsAuxiliaryData['product_id'] = $data['product_id'];
  227. break;
  228. case 4://指定品牌
  229. $data['brand_id'] = array_unique($data['brand_id']);
  230. /** @var StoreBrandServices $storeBrandServices */
  231. $storeBrandServices = app()->make(StoreBrandServices::class);
  232. $brandCount = $storeBrandServices->count(['is_show' => 1, 'is_del' => 0, 'id' => $data['brand_id']]);
  233. if (count(array_unique($data['brand_id'])) != $brandCount) {
  234. throw new AdminException('选择商品品牌中有已下架或删除的');
  235. }
  236. $promotionsAuxiliaryData['brand_id'] = $data['brand_id'];
  237. break;
  238. case 5://指定商品标签
  239. $data['store_label_id'] = array_unique($data['store_label_id']);
  240. /** @var StoreProductLabelServices $storeProductLabelServices */
  241. $storeProductLabelServices = app()->make(StoreProductLabelServices::class);
  242. $labelCount = $storeProductLabelServices->count(['id' => $data['store_label_id']]);
  243. if (count(array_unique($data['store_label_id'])) != $labelCount) {
  244. throw new ValidateException('选择商品标签中有已下架或删除的');
  245. }
  246. $promotionsAuxiliaryData['store_label_id'] = $data['store_label_id'];
  247. break;
  248. default:
  249. throw new ValidateException('暂不支持该类型商品');
  250. break;
  251. }
  252. unset($data['section_time'], $data['promotions']);
  253. $this->transaction(function () use ($id, $data, $promotionsAuxiliaryData) {
  254. $time = time();
  255. $data['update_time'] = $time;
  256. if ($id) {
  257. $this->dao->update($id, $data);
  258. } else {
  259. $data['add_time'] = $time;
  260. $res = $this->dao->save($data);
  261. $id = $res->id;
  262. }
  263. /** @var StorePromotionsAuxiliaryServices $storePromotionsAuxiliaryServices */
  264. $storePromotionsAuxiliaryServices = app()->make(StorePromotionsAuxiliaryServices::class);
  265. //保存关联数据
  266. $storePromotionsAuxiliaryServices->savePromotionsRelation((int)$id, (int)$data['product_partake_type'], $promotionsAuxiliaryData, [], [], false);
  267. });
  268. return true;
  269. }
  270. }