Activity.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace addons\shopro\library\activity;
  3. use addons\shopro\facade\ActivityRedis;
  4. use addons\shopro\library\activity\contract\ActivityInterface;
  5. use addons\shopro\library\activity\contract\ActivityGetterInterface;
  6. class Activity
  7. {
  8. /**
  9. * 活动model
  10. */
  11. public $model = null;
  12. public $redis = null;
  13. protected $type = null;
  14. protected $rules = null;
  15. protected $hasRedis = null;
  16. protected $getters = [];
  17. public function __construct($model_name)
  18. {
  19. $this->hasRedis = has_redis();
  20. $this->model = new $model_name;
  21. $this->redis = ActivityRedis::instance();
  22. }
  23. /**
  24. * 添加活动
  25. *
  26. * @param array $params
  27. * @return void
  28. */
  29. public function save($params)
  30. {
  31. $this->rules = $params['rules'];
  32. $this->type = $params['type'];
  33. $params['classify'] = $this->model->getClassify($this->type); // 设置 classify
  34. $params['prehead_time'] = in_array($params['classify'], ['promo', 'app']) ? '' : ($params['prehead_time'] ?? ''); // 触发触发器,promo 不能设置 prehead_time
  35. // 检测活动之间的冲突
  36. $this->checkActivity($params);
  37. // 保存活动
  38. $this->model->allowField(true)->save($params);
  39. // 保存活动其他数据
  40. $this->saveOther($params);
  41. if ($this->hasRedis) {
  42. $this->redis->setActivity($this->model);
  43. }
  44. }
  45. /**
  46. * 更新活动
  47. *
  48. * @param \think\Model $activity
  49. * @param array $params
  50. * @return void
  51. */
  52. public function update($activity, $params)
  53. {
  54. $this->model = $activity;
  55. $this->rules = $params['rules'];
  56. $this->type = $activity->type;
  57. $params['type'] = $activity->type; // 活动类型不可编辑,赋值活动本身的 type
  58. $params['classify'] = $this->model->getClassify($this->type); // 设置 classify
  59. $params['prehead_time'] = in_array($params['classify'], ['promo', 'app']) ? '' : ($params['prehead_time'] ?? ''); // 触发触发器,promo 不能设置 prehead_time
  60. if ($activity->status == 'ended') {
  61. error_stop('活动已结束');
  62. }
  63. // 检测活动之间的冲突
  64. $params = $this->checkActivity($params, $this->model->id);
  65. $activities = $activity->classifies()['activity'];
  66. $activities = array_keys($activities);
  67. if ($activity->status == 'ing') {
  68. if (in_array($activity->type, $activities)) {
  69. // 活动正在进行中,只能改结束时间
  70. $params = [
  71. 'title' => $params['title'],
  72. 'end_time' => $params['end_time'],
  73. 'goods_list' => $params['goods_list'],
  74. 'richtext_id' => $params['richtext_id'],
  75. 'richtext_title' => $params['richtext_title'],
  76. ];
  77. }
  78. }
  79. // 保存活动
  80. $this->model->allowField(true)->save($params);
  81. // 保存活动其他数据
  82. $this->saveOther($params);
  83. if ($this->hasRedis) {
  84. $this->redis->setActivity($this->model);
  85. }
  86. }
  87. /**
  88. * 删除活动
  89. *
  90. * @param \think\Model $activity
  91. * @return void
  92. */
  93. public function delete($activity)
  94. {
  95. if ($this->hasRedis) {
  96. $this->redis->delActivity($activity);
  97. }
  98. return $activity->delete();
  99. }
  100. /**
  101. * 活动规格相关数据展示
  102. *
  103. * @param string $type
  104. * @param array $rules
  105. * @return array
  106. */
  107. public function showSkuPrice($type, $skuPrice)
  108. {
  109. $skuPrice = $this->provider($type)->showSkuPrice($skuPrice);
  110. return $skuPrice;
  111. }
  112. /**
  113. * 活动规则相关信息
  114. *
  115. * @param string $type
  116. * @param array $rules
  117. * @return array
  118. */
  119. public function rulesInfo($type, $rules)
  120. {
  121. $this->rules = $rules;
  122. $this->type = $type;
  123. $activity = $this->provider()->rulesInfo($type, $rules);
  124. return $activity;
  125. }
  126. /**
  127. * 校验活动特有的数据
  128. *
  129. * @param array $params
  130. * @param string $type
  131. * @return array
  132. */
  133. public function checkActivity($params, $activity_id = 0, $type = null)
  134. {
  135. return $this->provider($type)->check($params, $activity_id);
  136. }
  137. /**
  138. * 保存活动特有的数据
  139. *
  140. * @param array $params
  141. * @param string $type
  142. * @return void
  143. */
  144. public function saveOther($params, $type = null)
  145. {
  146. return $this->provider($type)->save($this->model, $params);
  147. }
  148. /**
  149. * 格式化促销标签
  150. *
  151. * @param array $rules
  152. * @param string $type
  153. * @return array
  154. */
  155. public function formatRuleTags($rules, $type = null)
  156. {
  157. return $this->provider($type)->formatTags($rules, $type);
  158. }
  159. /**
  160. * 格式化促销标签
  161. *
  162. * @param array $rules
  163. * @param string $type
  164. * @return array
  165. */
  166. public function formatRuleTexts($rules, $type = null)
  167. {
  168. return $this->provider($type)->formatTexts($rules, $type);
  169. }
  170. /**
  171. * 用活动覆盖商品数据
  172. *
  173. * @param \think\Model|array $goods
  174. * @return void
  175. */
  176. public function recoverSkuPrices($goods, $activity)
  177. {
  178. $skuPrices = $this->provider($activity['type'])->recoverSkuPrices($goods, $activity);
  179. return $skuPrices;
  180. }
  181. /**
  182. * 活动购买检测(仅处理活动,不处理促销)
  183. *
  184. * @param array $buyInfo
  185. * @param array $activity
  186. * @return array
  187. */
  188. public function buyCheck($buyInfo, $activity)
  189. {
  190. if ($activity) {
  191. return $this->provider($activity['type'])->buyCheck($buyInfo, $activity);
  192. }
  193. return $buyInfo;
  194. }
  195. /**
  196. * 活动购买检测(仅处理活动,不处理促销)
  197. *
  198. * @param array $buyInfo
  199. * @param array $activity
  200. * @return array
  201. */
  202. public function buy($buyInfo, $activity)
  203. {
  204. if ($activity) {
  205. return $this->provider($activity['type'])->buy($buyInfo, $activity);
  206. }
  207. return $buyInfo;
  208. }
  209. /**
  210. * 购买成功
  211. *
  212. * @param array|object $order
  213. * @param array|object $user
  214. * @return array
  215. */
  216. public function buyOk($order, $user)
  217. {
  218. if ($order->activity_type) {
  219. $this->provider($order->activity_type)->buyOk($order, $user);
  220. }
  221. if ($order->promo_types) {
  222. $promoTypes = explode(',', $order->promo_types);
  223. foreach ($promoTypes as $promo_type) {
  224. $this->provider($promo_type)->buyOk($order, $user);
  225. }
  226. }
  227. return $order;
  228. }
  229. /**
  230. * 购买失败(释放库存,剪掉销量,移除参团数据)
  231. *
  232. * @param array|object $order
  233. * @param string $type 失败类型:invalid=订单取消,关闭;refund=退款
  234. * @return array
  235. */
  236. public function buyFail($order, $type)
  237. {
  238. if ($order->activity_type) {
  239. $this->provider($order->activity_type)->buyFail($order, $type);
  240. }
  241. if ($order->promo_types) {
  242. $promoTypes = explode(',', $order->promo_types);
  243. foreach ($promoTypes as $promo_type) {
  244. $this->provider($promo_type)->buyFail($order, $type);
  245. }
  246. }
  247. return $order;
  248. }
  249. /**
  250. * 获取促销优惠信息
  251. *
  252. * @param array $promo
  253. * @param array $data
  254. * @return array
  255. */
  256. public function getPromoInfo($promo, array $data = [])
  257. {
  258. return $this->provider($promo['type'])->getPromoInfo($promo, $data);
  259. }
  260. /**
  261. * 活动提供器
  262. *
  263. * @param string $type
  264. * @return ActivityInterface
  265. */
  266. public function provider($type = null)
  267. {
  268. $type = $type ?: $this->type;
  269. $class = "\\addons\\shopro\\library\\activity\\provider\\" . \think\helper\Str::studly($type);
  270. if (class_exists($class)) {
  271. return new $class($this);
  272. }
  273. error_stop('活动类型不支持');
  274. }
  275. /**
  276. * 获取活动提供器
  277. *
  278. * @param string $getter
  279. * @return ActivityGetterInterface
  280. */
  281. public function getter($getter = null)
  282. {
  283. $getter = $getter ? $getter : $this->defaultGetter();
  284. if (isset($this->getters[$getter])) {
  285. return $this->getters[$getter];
  286. }
  287. $class = "\\addons\\shopro\\library\\activity\\getter\\" . \think\helper\Str::studly($getter);
  288. if (class_exists($class)) {
  289. return $this->getters[$getter] = new $class($this);
  290. }
  291. error_stop('活动类型不支持');
  292. }
  293. /**
  294. * 获取默认获取器
  295. *
  296. * @return string
  297. */
  298. public function defaultGetter()
  299. {
  300. return $this->hasRedis ? 'redis' : 'db';
  301. }
  302. public function __call($funcname, $arguments)
  303. {
  304. return $this->getter()->{$funcname}(...$arguments);
  305. }
  306. }