StorePromotionsAuxiliaryServices.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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\services\activity\promotions;
  12. use app\dao\activity\promotions\StorePromotionsAuxiliaryDao;
  13. use app\services\product\sku\StoreProductAttrValueServices;
  14. use app\services\BaseServices;
  15. /**
  16. * 优惠活动辅助表
  17. * Class StorePromotionsAuxiliaryServices
  18. * @package app\services\activity\promotions
  19. * @mixin StorePromotionsAuxiliaryDao
  20. */
  21. class StorePromotionsAuxiliaryServices extends BaseServices
  22. {
  23. /**
  24. * @param StorePromotionsAuxiliaryDao $dao
  25. */
  26. public function __construct(StorePromotionsAuxiliaryDao $dao)
  27. {
  28. $this->dao = $dao;
  29. }
  30. /**
  31. * 优惠活动关联保存
  32. * @param int $promotionsId
  33. * @param int $type
  34. * @param array $promotionsAuxiliaryData
  35. * @param array $couponData
  36. * @param array $giveProductData
  37. * @return bool
  38. */
  39. public function savePromotionsRelation(int $promotionsId, int $type, array $promotionsAuxiliaryData, array $couponData = [], array $giveProductData = [], bool $isAttr = true)
  40. {
  41. $this->dao->delete(['promotions_id' => $promotionsId]);
  42. if ($promotionsAuxiliaryData) $this->savePromotionsProducts($promotionsId, $type, $promotionsAuxiliaryData, $isAttr);
  43. if ($couponData) $this->savePromotionsGiveCoupon($promotionsId, $couponData);
  44. if ($giveProductData) $this->savePromotionsGiveProducts($promotionsId, $giveProductData);
  45. return true;
  46. }
  47. /**
  48. * 设置活动关联商品
  49. * @param int $promotionsId
  50. * @param int $type
  51. * @param array $productIds
  52. * @return bool
  53. */
  54. public function savePromotionsProducts(int $promotionsId, int $type, array $promotionsAuxiliaryData, bool $isAttr = true)
  55. {
  56. if ($promotionsAuxiliaryData) {
  57. $data = [];
  58. $unitData = ['type' => 1, 'promotions_id' => $promotionsId ,'product_partake_type' => $type];
  59. switch ($type) {
  60. case 1://所有商品
  61. $data[] = $unitData;
  62. break;
  63. case 2:
  64. case 3:
  65. if ($isAttr) {
  66. $productIds = array_column($promotionsAuxiliaryData, 'product_id');
  67. $promotionsAuxiliaryData = array_combine($productIds, $promotionsAuxiliaryData);
  68. /** @var StoreProductAttrValueServices $skuValueServices */
  69. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  70. foreach ($productIds as $productId) {
  71. $unique = $promotionsAuxiliaryData[$productId]['unique'] ?? [];
  72. $skuCount = $skuValueServices->count(['product_id' => $productId, 'type' => 0]);
  73. $unitData['product_id'] = $productId;
  74. $unitData['is_all'] = count($unique) >= $skuCount ? 1 : 0;
  75. $unitData['unique'] = implode(',', $unique);
  76. $data[] = $unitData;
  77. }
  78. } else {
  79. $productIds = $promotionsAuxiliaryData['product_id'] ?? [];
  80. foreach ($productIds as $productId) {
  81. $unitData['product_id'] = $productId;
  82. $data[] = $unitData;
  83. }
  84. }
  85. break;
  86. case 4://品牌
  87. $brandIds = $promotionsAuxiliaryData['brand_id'] ?? [];
  88. if ($brandIds) {
  89. foreach ($brandIds as $id) {
  90. $unitData['brand_id'] = $id;
  91. $data[] = $unitData;
  92. }
  93. }
  94. break;
  95. case 5://标签
  96. $storeLabelIds = $promotionsAuxiliaryData['store_label_id'] ?? [];
  97. if ($storeLabelIds) {
  98. foreach ($storeLabelIds as $id) {
  99. $unitData['store_label_id'] = $id;
  100. $data[] = $unitData;
  101. }
  102. }
  103. break;
  104. }
  105. if ($data) $this->dao->saveAll($data);
  106. $this->setPromotionsAuxiliaryCache($promotionsId, $data);
  107. }
  108. return true;
  109. }
  110. /**
  111. * 设置活动关联赠送优惠券
  112. * @param int $promotionsId
  113. * @param array $couponData
  114. * @return bool
  115. */
  116. public function savePromotionsGiveCoupon(int $promotionsId, array $couponData)
  117. {
  118. if ($couponData) {
  119. $data = [];
  120. $couponIds = array_column($couponData, 'give_coupon_id');
  121. $couponData = array_combine($couponIds, $couponData);
  122. foreach ($couponIds as $couponId) {
  123. $data[] = [
  124. 'type' => 2,
  125. 'promotions_id' => $promotionsId,
  126. 'coupon_id' => $couponId,
  127. 'limit_num' => $couponData[$couponId]['give_coupon_num'] ?? 0,
  128. 'surplus_num' => $couponData[$couponId]['give_coupon_num'] ?? 0
  129. ];
  130. }
  131. $this->dao->saveAll($data);
  132. }
  133. return true;
  134. }
  135. /**
  136. * 设置活动关联赠送商品
  137. * @param int $promotionsId
  138. * @param array $giveProductData
  139. * @return bool
  140. */
  141. public function savePromotionsGiveProducts(int $promotionsId, array $giveProductData)
  142. {
  143. if ($giveProductData) {
  144. $data = [];
  145. foreach ($giveProductData as $product) {
  146. $data[] = [
  147. 'type' => 3,
  148. 'promotions_id' => $promotionsId,
  149. 'product_id' => $product['give_product_id'],
  150. 'limit_num' => $product['give_product_num'] ?? 0,
  151. 'surplus_num' => $product['give_product_num'] ?? 0,
  152. 'unique' => $product['unique'] ?? ''
  153. ];
  154. }
  155. $this->dao->saveAll($data);
  156. }
  157. return true;
  158. }
  159. /**
  160. * 优惠活动关联赠品限量处理
  161. * @param array $promotions_id
  162. * @param int $type
  163. * @param int $id
  164. * @param bool $is_dec
  165. * @param string $unique
  166. * @param int $num
  167. * @return bool
  168. */
  169. public function updateLimit(array $promotions_id, int $type, int $id, bool $is_dec = true, string $unique = '', int $num = 1)
  170. {
  171. if (!$promotions_id) return true;
  172. $where = ['promotions_id' => $promotions_id, 'type' => $type];
  173. if ($type == 2) {
  174. $where['coupon_id'] = $id;
  175. } else {
  176. $where['product_id'] = $id;
  177. $where['unique'] = $unique;
  178. }
  179. $info = $this->dao->get($where);
  180. if ($info) {
  181. if ($is_dec) {
  182. if ($info['surplus_num'] < $num) {
  183. $surplus_num = 0;
  184. } else {
  185. $surplus_num = bcsub((string)$info['surplus_num'], (string)$num, 0);
  186. }
  187. } else {
  188. $surplus_num = bcadd((string)$info['surplus_num'], (string)$num, 0);
  189. }
  190. $this->dao->update($info['id'], ['surplus_num' => $surplus_num]);
  191. }
  192. return true;
  193. }
  194. /**
  195. * 设置优惠活动关联缓存
  196. * @param int $promotions_id
  197. * @param array $data
  198. * @param int $product_partake_type
  199. * @return array
  200. */
  201. public function setPromotionsAuxiliaryCache(int $promotions_id, array $data, int $product_partake_type = 1)
  202. {
  203. $key ='cache_promotions_auxiliary_' . $promotions_id;
  204. $cacheData = [];
  205. if ($data) {
  206. switch ($product_partake_type) {
  207. case 1://所有商品
  208. break;
  209. case 2:
  210. case 3:
  211. $cacheData = array_unique(array_column($data, 'product_id'));
  212. break;
  213. case 4://品牌
  214. $cacheData = array_unique(array_column($data, 'brand_id'));
  215. break;
  216. case 5://标签
  217. $cacheData = array_unique(array_column($data, 'store_label_id'));
  218. break;
  219. }
  220. }
  221. $this->dao->cacheHander()->delete($key);
  222. $this->dao->cacheTag()->set($key, $cacheData);
  223. return $cacheData;
  224. }
  225. /**
  226. * 更新缓存
  227. * @param int $promotions_id
  228. * @param string $key
  229. * @return array
  230. * @throws \think\db\exception\DataNotFoundException
  231. * @throws \think\db\exception\DbException
  232. * @throws \think\db\exception\ModelNotFoundException
  233. */
  234. public function updatePromotionsAuxiliaryCache(int $promotions_id)
  235. {
  236. $data = $this->dao->getList(['promotions_id' => $promotions_id, 'type' => 1]);
  237. $cacheData = [];
  238. if ($data) {
  239. $product_partake_type = $data[0]['product_partake_type'] ?? 0;
  240. $cacheData = $this->setPromotionsAuxiliaryCache($promotions_id, $data, $product_partake_type);
  241. }
  242. return $cacheData;
  243. }
  244. /**
  245. * 获取商品关联缓存
  246. * @param int $product_id
  247. * @param array $type
  248. * @param bool $isCache
  249. * @return array
  250. * @throws \think\db\exception\DataNotFoundException
  251. * @throws \think\db\exception\DbException
  252. * @throws \think\db\exception\ModelNotFoundException
  253. */
  254. public function getPromotionsAuxiliaryCache(int $promotions_id, bool $isCache = false)
  255. {
  256. $key = 'cache_promotions_auxiliary_' . $promotions_id;
  257. $data = $this->dao->cacheHander()->get($key);
  258. if (!$data || $isCache) {
  259. $data = $this->updatePromotionsAuxiliaryCache($promotions_id);
  260. }
  261. return $data;
  262. }
  263. }