StoreSeckillActive.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\model\store;
  12. use app\common\model\BaseModel;
  13. use app\common\model\store\order\StoreOrderProduct;
  14. use app\common\model\store\product\Product;
  15. use app\common\repositories\store\StoreActivityRepository;
  16. use think\model\concern\SoftDelete;
  17. use think\model\relation\HasMany;
  18. class StoreSeckillActive extends BaseModel
  19. {
  20. protected $autoWriteTimestamp = true;
  21. protected $createTime = 'create_time';
  22. protected $updateTime = 'update_time';
  23. protected $deleteTime = 'delete_time';
  24. use SoftDelete;
  25. protected $append = ['status_text', 'seckill_time_text_arr','stop_time'];
  26. /**
  27. * 设置主键
  28. * @return string
  29. * @author Qinii
  30. * @day 2020-07-30
  31. */
  32. public static function tablePk(): string
  33. {
  34. return 'seckill_active_id';
  35. }
  36. /**
  37. * 设置表名
  38. * @return string
  39. * @author Qinii
  40. * @day 2020-07-30
  41. */
  42. public static function tableName(): string
  43. {
  44. return 'store_seckill_active';
  45. }
  46. /**
  47. * 开始日期修改器
  48. * @param $value
  49. * @param $data
  50. * @return false|int|void
  51. * FerryZhao 2024/4/11
  52. */
  53. protected static function setStartDayAttr($value, $data)
  54. {
  55. if ($value) {
  56. return date('Y-m-d', strtotime($value));
  57. }
  58. }
  59. /**
  60. * 标识修改器
  61. * @param $value
  62. * @param $data
  63. * @return false|string|void
  64. * FerryZhao 2024/4/24
  65. */
  66. protected static function setSignAttr($value, $data)
  67. {
  68. return 1;
  69. }
  70. /**
  71. * 结束日期修改器
  72. * @param $value
  73. * @param $data
  74. * @return false|int|void
  75. * FerryZhao 2024/4/11
  76. */
  77. protected static function setEndDayAttr($value, $data)
  78. {
  79. if(isset($data['seckill_time_ids']) && $data['seckill_time_ids']){
  80. $data['seckill_time_ids'] = explode(',', $data['seckill_time_ids']);
  81. $seckillEndTime = app()->make(StoreSeckillTime::class)->where(['status'=>1])->whereIn('seckill_time_id',$data['seckill_time_ids'])->order('end_time','desc')->value('end_time');
  82. $endTime = $seckillEndTime * 60 * 60;
  83. $endTime = $seckillEndTime == 24 ? $endTime - 1 : $endTime;
  84. if ($value) {
  85. return date('Y-m-d H:i:s',strtotime(date('Y-m-d',strtotime($value))) + $endTime);
  86. }
  87. return $endTime;
  88. }
  89. }
  90. /**
  91. * 活动状态
  92. * @param $value
  93. * @param $data
  94. * @return int|string|void
  95. * FerryZhao 2024/4/19
  96. */
  97. public static function setActiveStatusAttr($value,$data)
  98. {
  99. $startTime = strtotime($data['start_day']);
  100. $endTime = strtotime($data['end_day']. '23:59:59');
  101. if($startTime > time()){
  102. return 0;
  103. }else if($endTime < time()){
  104. return '-1';
  105. }else if($startTime <= time() && $endTime >= time()){
  106. return 1;
  107. }
  108. }
  109. /**
  110. * @return array
  111. */
  112. public function getSeckillTimesAttr()
  113. {
  114. if ($this->seckill_time_ids) {
  115. return StoreSeckillTime::whereIn('seckill_time_id',$this->seckill_time_ids)
  116. ->where('status',1)
  117. ->column('title,start_time,end_time,seckill_time_id');
  118. }
  119. return [];
  120. }
  121. /**
  122. * 开始日期获取器
  123. * @param $value
  124. * @param $data
  125. * @return false|int|void
  126. * FerryZhao 2024/4/11
  127. */
  128. protected static function getStartDayAttr($value, $data)
  129. {
  130. if ($value) {
  131. return date('Y-m-d', strtotime($value));
  132. }
  133. }
  134. /**
  135. * 结束日期获取器
  136. * @param $value
  137. * @param $data
  138. * @return false|int|void
  139. * FerryZhao 2024/4/11
  140. */
  141. protected static function getEndDayAttr($value, $data)
  142. {
  143. if ($value) {
  144. return date('Y-m-d', strtotime($value));
  145. }
  146. }
  147. /**
  148. * 活动场次获取器
  149. * @param $value
  150. * @param $data
  151. * @return void
  152. * FerryZhao 2024/4/11
  153. */
  154. protected static function getSeckillTimeIdsAttr($value, $data)
  155. {
  156. if (!empty($value)) {
  157. return explode(',', $value);
  158. }
  159. }
  160. /**
  161. * 平台一级商品分类获取器
  162. * @param $value
  163. * @param $data
  164. * @return void
  165. * FerryZhao 2024/4/11
  166. */
  167. protected static function getProductCategoryIdsAttr($value, $data)
  168. {
  169. if (!empty($value)) {
  170. return explode(',', $value);
  171. }
  172. }
  173. /**
  174. * 状态说明获取器
  175. * @param $value
  176. * @param $data
  177. * @return void
  178. * FerryZhao 2024/4/11
  179. */
  180. protected static function getStatusTextAttr($value, $data)
  181. {
  182. $statusTextArray = [
  183. '0'=>'未开始',
  184. '1'=>'进行中',
  185. '-1'=>'已结束'
  186. ];
  187. return $statusTextArray[$data['active_status']];
  188. }
  189. /**
  190. * 时间场次获取器
  191. * @param $value
  192. * @param $data
  193. * @return void
  194. * FerryZhao 2024/4/13
  195. */
  196. protected function getSeckillTimeTextArrAttr($value, $data)
  197. {
  198. $timeList = [];
  199. if (isset($data['seckill_time_ids'])) {
  200. $list = app()->make(StoreSeckillTime::class)->whereIn('seckill_time_id', explode(',', $data['seckill_time_ids']))->field('start_time,end_time')->select();
  201. foreach ($list as $item) {
  202. $timeList[] = $item['start_time_text'] . ' - ' . $item['end_time_text'];
  203. }
  204. }
  205. return $timeList;
  206. }
  207. /**
  208. * 氛围图获取器
  209. * @param $value
  210. * @param $data
  211. * @return void
  212. * FerryZhao 2024/4/18
  213. */
  214. public function getAtmospherePicAttr($value, $data)
  215. {
  216. $storeActivity = app()->make(StoreActivityRepository::class);
  217. $pic = app()->make(StoreActivity::class)->where(
  218. [
  219. 'scope_type' => $storeActivity::TYPE_MUST_SECKILL_ACTIVE,
  220. 'activity_type' => $storeActivity::ACTIVITY_TYPE_ATMOSPHERE,
  221. 'link_id' => $data['seckill_active_id']
  222. ]
  223. )->value('pic');
  224. return $pic ?: '';
  225. }
  226. /**
  227. * 氛围图获取器
  228. * @param $value
  229. * @param $data
  230. * @return void
  231. * FerryZhao 2024/4/18
  232. */
  233. public function getBorderPicAttr($value, $data)
  234. {
  235. $storeActivity = app()->make(StoreActivityRepository::class);
  236. $pic = app()->make(StoreActivity::class)->where(
  237. [
  238. 'scope_type' => $storeActivity::TYPE_MUST_SECKILL_ACTIVE,
  239. 'activity_type' => $storeActivity::ACTIVITY_TYPE_BORDER,
  240. 'link_id' => $data['seckill_active_id']
  241. ]
  242. )->value('pic');
  243. return $pic ?: '';
  244. }
  245. /**
  246. * 关联订单商品
  247. * @return void
  248. * FerryZhao 2024/4/26
  249. */
  250. public function seckillStoreOrderProduct()
  251. {
  252. return $this->hasMany(StoreOrderProduct::class, 'activity_id', 'seckill_active_id')->where(['product_type'=>1]);
  253. }
  254. /**
  255. * 停止时间获取器
  256. * @param $value
  257. * @param $data
  258. * @return void
  259. * FerryZhao 2024/4/28
  260. */
  261. public function getStopTimeAttr($value,$data)
  262. {
  263. $time = app()->make(StoreSeckillTime::class)->whereIn('seckill_time_id', explode(',', $data['seckill_time_ids']))->field('start_time,end_time')->max('end_time');
  264. $date = date('Y-m-d',strtotime($data['end_day'])).' '.$time.':00:00';
  265. return strtotime($date);
  266. }
  267. }