Activity.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. namespace app\admin\model\shopro\activity;
  3. use app\admin\model\shopro\Common;
  4. use traits\model\SoftDelete;
  5. use app\admin\model\shopro\goods\Goods;
  6. use addons\shopro\facade\Activity as ActivityFacade;
  7. use app\admin\model\shopro\activity\SkuPrice as ActivitySkuPriceModel;
  8. class Activity extends Common
  9. {
  10. use SoftDelete;
  11. protected $name = 'shopro_activity';
  12. protected $deleteTime = 'deletetime';
  13. protected $type = [
  14. 'rules' => 'json',
  15. 'prehead_time' => 'timestamp',
  16. 'start_time' => 'timestamp',
  17. 'end_time' => 'timestamp',
  18. ];
  19. // 追加属性
  20. protected $append = [
  21. 'status',
  22. 'status_text',
  23. 'type_text',
  24. // 'end_time_unix' // 不需要了
  25. ];
  26. public function classifies()
  27. {
  28. return [
  29. 'activity' => [
  30. 'groupon' => '拼团',
  31. 'groupon_ladder' => '阶梯拼团',
  32. // 'groupon_lucky' => '幸运拼团',
  33. 'seckill' => '秒杀',
  34. ],
  35. 'promo' => [
  36. 'full_reduce' => '满减',
  37. 'full_discount' => '满折',
  38. 'full_gift' => '满赠',
  39. 'free_shipping' => '满邮',
  40. ],
  41. 'app' => [
  42. 'signin' => '签到'
  43. ]
  44. ];
  45. }
  46. public function typeList()
  47. {
  48. return [
  49. 'groupon' => '拼团',
  50. 'groupon_ladder' => '阶梯拼团',
  51. // 'groupon_lucky' => '幸运拼团',
  52. 'seckill' => '秒杀',
  53. 'full_reduce' => '满减',
  54. 'full_discount' => '满折',
  55. 'full_gift' => '满赠',
  56. 'free_shipping' => '满邮',
  57. 'signin' => '签到',
  58. ];
  59. }
  60. /**
  61. * 获取活动的互斥活动
  62. *
  63. * @param string $current_activity_type
  64. * @return array
  65. */
  66. public function getMutexActivityTypes($current_activity_type)
  67. {
  68. $activityTypes = [];
  69. switch ($current_activity_type) {
  70. case 'groupon':
  71. $activityTypes = ['groupon'];
  72. break;
  73. case 'groupon_ladder':
  74. $activityTypes = ['groupon_ladder'];
  75. break;
  76. case 'groupon_lucky':
  77. $activityTypes = ['groupon_lucky'];
  78. break;
  79. case 'seckill':
  80. $activityTypes = ['seckill'];
  81. break;
  82. case 'full_reduce':
  83. $activityTypes = ['full_reduce', 'full_discount'];
  84. break;
  85. case 'full_discount':
  86. $activityTypes = ['full_reduce', 'full_discount'];
  87. break;
  88. case 'free_shipping':
  89. $activityTypes = ['free_shipping'];
  90. break;
  91. case 'full_gift':
  92. $activityTypes = ['full_gift'];
  93. break;
  94. case 'signin':
  95. $activityTypes = ['signin'];
  96. break;
  97. }
  98. return $activityTypes;
  99. }
  100. /**
  101. * 根据类型获取 classify
  102. *
  103. * @param string $type
  104. * @return string
  105. */
  106. public function getClassify($type)
  107. {
  108. $classifys = $this->classifies();
  109. $activitys = array_keys($classifys['activity']);
  110. $promos = array_keys($classifys['promo']);
  111. $apps = array_keys($classifys['app']);
  112. $classify = null;
  113. if (in_array($type, $activitys)) {
  114. $classify = 'activity';
  115. } else if (in_array($type, $promos)) {
  116. $classify = 'promo';
  117. } else if (in_array($type, $apps)) {
  118. $classify = 'app';
  119. }
  120. return $classify;
  121. }
  122. /**
  123. * status 组合 (在thinkphp5 where Closure 中,不能直接使用 scope,特殊场景下用来代替下面的 scopeNostart scopePrehead 等)
  124. *
  125. * @param [type] $query
  126. * @param [type] $status
  127. * @return void
  128. */
  129. public function scopeStatusComb($query, $status)
  130. {
  131. return $query->where(function ($query) use ($status) {
  132. foreach ($status as $st) {
  133. $query->whereOr(function ($query) use ($st) {
  134. switch($st) {
  135. case 'nostart':
  136. $query->where('start_time', '>', time());
  137. break;
  138. case 'prehead':
  139. $query->where('prehead_time', '<=', time())->where('start_time', '>', time());
  140. break;
  141. case 'ing':
  142. $query->where('start_time', '<=', time())->where('end_time', '>=', time());
  143. break;
  144. case 'show':
  145. $query->where('prehead_time', '<=', time())->where('end_time', '>=', time());
  146. break;
  147. case 'ended':
  148. $query->where('end_time', '<', time());
  149. break;
  150. default:
  151. error_stop('status 状态错误');
  152. }
  153. });
  154. }
  155. });
  156. }
  157. /**
  158. * 未开始的活动
  159. *
  160. * @param think\query\Query $query
  161. * @return void
  162. */
  163. public function scopeNostart($query)
  164. {
  165. return $query->where('start_time', '>', time());
  166. }
  167. /**
  168. * 预售的活动
  169. *
  170. * @param think\query\Query $query
  171. * @return void
  172. */
  173. public function scopePrehead($query)
  174. {
  175. return $query->where('prehead_time', '<=', time())->where('start_time', '>', time());
  176. }
  177. /**
  178. * 进行中的活动
  179. *
  180. * @param think\query\Query $query
  181. * @return void
  182. */
  183. public function scopeIng($query)
  184. {
  185. return $query->where('start_time', '<=', time())->where('end_time', '>=', time());
  186. }
  187. /**
  188. * 已经开始预售,并且没有结束的活动
  189. *
  190. * @param think\query\Query $query
  191. * @return void
  192. */
  193. public function scopeShow($query)
  194. {
  195. return $query->where('prehead_time', '<=', time())->where('end_time', '>=', time());
  196. }
  197. /**
  198. * 已经结束的活动
  199. *
  200. * @param think\query\Query $query
  201. * @return void
  202. */
  203. public function scopeEnded($query)
  204. {
  205. return $query->where('end_time', '<', time());
  206. }
  207. /**
  208. * 修改器 classify
  209. *
  210. * @param string $value
  211. * @param array $data
  212. * @return integer|null
  213. */
  214. public function setClassifyAttr($value, $data)
  215. {
  216. $classify = $value ?: ($data['classify'] ?? null);
  217. if (!$classify) {
  218. $type = $data['type'] ?? null; // 活动类型
  219. $classify = $this->getClassify($type);
  220. }
  221. return $classify;
  222. }
  223. /**
  224. * 修改器 预热时间
  225. *
  226. * @param string $value
  227. * @param array $data
  228. * @return integer|null
  229. */
  230. public function setPreheadTimeAttr($value, $data)
  231. {
  232. // promo 类型 prehead_time 永远等于 start_time
  233. $value = (isset($data['classify']) && $data['classify'] == 'promo') ? $data['start_time'] : ($value ?: $data['start_time']);
  234. return $this->attrFormatUnix($value);
  235. }
  236. /**
  237. * 修改器 开始时间
  238. *
  239. * @param string $value
  240. * @return integer|null
  241. */
  242. public function setStartTimeAttr($value)
  243. {
  244. return $this->attrFormatUnix($value);
  245. }
  246. /**
  247. * 修改器 结束时间
  248. *
  249. * @param string $value
  250. * @return integer|null
  251. */
  252. public function setEndTimeAttr($value)
  253. {
  254. return $this->attrFormatUnix($value);
  255. }
  256. public function getStatusAttr($value, $data)
  257. {
  258. return $this->getStatusCode($data['prehead_time'], $data['start_time'], $data['end_time']);
  259. }
  260. public function getStatusTextAttr($value, $data)
  261. {
  262. return $this->getStatusText($this->status);
  263. }
  264. public function getGoodsListAttr($value, $data)
  265. {
  266. if ($data['goods_ids']) {
  267. $goods = Goods::field('id,title,price,sales,image,status')->whereIn('id', $data['goods_ids'])->select();
  268. $goods = collection($goods)->toArray(); // 全部转数组
  269. $goodsIds = array_column($goods, 'id');
  270. $activitySkuPrices = ActivitySkuPriceModel::where('activity_id', $data['id'])->whereIn('goods_id', $goodsIds)->order('id', 'asc')->select();
  271. $activitySkuPrices = collection($activitySkuPrices)->toArray();
  272. // 后台编辑活动时,防止不编辑规格无法提交问题
  273. foreach ($goods as &$gd) {
  274. // 处理 $gd['activity_sku_prices']
  275. $gd['activity_sku_prices'] = [];
  276. foreach ($activitySkuPrices as $skuPrice) {
  277. if ($skuPrice['goods_id'] == $gd['id']) {
  278. $gd['activity_sku_prices'][] = $skuPrice;
  279. }
  280. }
  281. // 处理活动规格,数据
  282. foreach ($gd['activity_sku_prices'] as $key => $skuPrice) {
  283. $skuPrice = ActivityFacade::showSkuPrice($data['type'], $skuPrice);
  284. $gd['activity_sku_prices'][$key] = $skuPrice;
  285. }
  286. }
  287. }
  288. return $goods ?? [];
  289. }
  290. public function getRulesAttr($value, $data)
  291. {
  292. $rules = $data['rules'] ? json_decode($data['rules'], true) : [];
  293. $type = $data['type'];
  294. // 获取各个活动规则相关的特殊数据
  295. $rules = ActivityFacade::rulesInfo($type, $rules);
  296. return $rules;
  297. }
  298. /**
  299. * 通过时间判断活动状态
  300. *
  301. * @param integer $prehead_time 预热时间
  302. * @param integer $start_time 开始时间
  303. * @param integer $end_time 结束时间
  304. * @return string
  305. */
  306. public static function getStatusCode($prehead_time, $start_time, $end_time)
  307. {
  308. // 转为时间戳,(从 redis 中取出来的是 时间格式)
  309. if (($prehead_time && $prehead_time > time()) || (!$prehead_time && $start_time > time())) {
  310. $status = 'nostart'; // 未开始
  311. } else if ($prehead_time && $prehead_time < time() && $start_time > time()) {
  312. $status = 'prehead'; // 预热
  313. } else if ($start_time < time() && $end_time > time()) {
  314. $status = 'ing';
  315. } else if ($end_time < time()) {
  316. $status = 'ended';
  317. }
  318. return $status ?? 'ended';
  319. }
  320. /**
  321. * 判断活动状态中文
  322. *
  323. * @param string $status 活动状态
  324. * @return string
  325. */
  326. public static function getStatusText($status)
  327. {
  328. if ($status == 'nostart') {
  329. $status_text = '未开始';
  330. } elseif ($status == 'prehead') {
  331. $status_text = '预热中';
  332. } elseif ($status == 'ing') {
  333. $status_text = '进行中';
  334. } elseif ($status == 'ended') {
  335. $status_text = '已结束';
  336. }
  337. return $status_text ?? '已结束';
  338. }
  339. public function getEndTimeUnixAttr($value, $data)
  340. {
  341. return isset($data['end_time']) ? $this->getData('end_time') : 0;
  342. }
  343. public function activitySkuPrices()
  344. {
  345. return $this->hasMany(SkuPrice::class, 'activity_id');
  346. }
  347. }