StorePromotions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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\model\activity\promotions;
  12. use app\model\activity\coupon\StoreCouponIssue;
  13. use app\model\product\product\StoreProduct;
  14. use crmeb\basic\BaseModel;
  15. use crmeb\traits\ModelTrait;
  16. use think\Model;
  17. /**
  18. * 促销活动活动
  19. * Class StorePromotions
  20. * @package app\model\activity\promotions
  21. */
  22. class StorePromotions extends BaseModel
  23. {
  24. use ModelTrait;
  25. /**
  26. * 数据表主键
  27. * @var string
  28. */
  29. protected $pk = 'id';
  30. /**
  31. * 模型名称
  32. * @var string
  33. */
  34. protected $name = 'store_promotions';
  35. protected $updateTime = false;
  36. /**
  37. * 优惠叠加
  38. * @param $value
  39. * @return string
  40. */
  41. protected function setDiscountUseAttr($value)
  42. {
  43. if ($value) {
  44. return is_array($value) ? implode(',', $value) : $value;
  45. }
  46. return '';
  47. }
  48. /**
  49. * 优惠叠加
  50. * @param $value
  51. * @return array|false|string[]
  52. */
  53. protected function getDiscountUseAttr($value)
  54. {
  55. if ($value) {
  56. return is_string($value) ? explode(',', $value) : $value;
  57. }
  58. return [];
  59. }
  60. /**
  61. * 标签
  62. * @param $value
  63. * @return array|mixed
  64. */
  65. protected function getLabelIdAttr($value)
  66. {
  67. if ($value) {
  68. return is_string($value) ? explode(',', $value) : $value;
  69. }
  70. return [];
  71. }
  72. /**
  73. * 关联商品
  74. * @param $value
  75. * @return array|mixed
  76. */
  77. protected function getProductIdAttr($value)
  78. {
  79. if ($value) {
  80. return is_string($value) ? explode(',', $value) : $value;
  81. }
  82. return [];
  83. }
  84. /**
  85. * 叠加使用
  86. * @param $value
  87. * @return array|mixed
  88. */
  89. protected function getOverlayAttr($value)
  90. {
  91. if ($value) {
  92. return is_string($value) ? explode(',', $value) : $value;
  93. }
  94. return [];
  95. }
  96. /**
  97. * 赠送优惠券
  98. * @param $value
  99. * @return array|mixed
  100. */
  101. protected function getIGiveCouponIdAttr($value)
  102. {
  103. if ($value) {
  104. return is_string($value) ? explode(',', $value) : $value;
  105. }
  106. return [];
  107. }
  108. /**
  109. * 赠送商品
  110. * @param $value
  111. * @return array|mixed
  112. */
  113. protected function getIGiveProductIdAttr($value)
  114. {
  115. if ($value) {
  116. return is_string($value) ? explode(',', $value) : $value;
  117. }
  118. return [];
  119. }
  120. /**
  121. * 赠送商品
  122. * @param $value
  123. * @return array|mixed
  124. */
  125. protected function getIGiveProductUniqueAttr($value)
  126. {
  127. if ($value) {
  128. return is_string($value) ? explode(',', $value) : $value;
  129. }
  130. return [];
  131. }
  132. /**
  133. * 添加时间获取器
  134. * @param $value
  135. * @return false|string
  136. */
  137. protected function getAddTimeAttr($value)
  138. {
  139. if ($value) return date('Y-m-d H:i:s', (int)$value);
  140. return '';
  141. }
  142. /**
  143. * 适用门店信息
  144. * @param $value
  145. * @return string
  146. */
  147. protected function setApplicableStoreIdAttr($value)
  148. {
  149. if ($value) {
  150. return is_array($value) ? implode(',', $value) : $value;
  151. }
  152. return '';
  153. }
  154. /**
  155. * 适用门店信息
  156. * @param $value
  157. * @return array|false|string[]
  158. */
  159. protected function getApplicableStoreIdAttr($value)
  160. {
  161. if ($value) {
  162. return is_string($value) ? array_map('intval', array_filter(explode(',', $value))) : $value;
  163. }
  164. return [];
  165. }
  166. /**
  167. * 关联商品
  168. * @return \think\model\relation\HasMany
  169. */
  170. public function products()
  171. {
  172. return $this->hasMany(StorePromotionsAuxiliary::class, 'promotions_id', 'id')->where('type',1)->where('product_partake_type', 2);
  173. }
  174. /**
  175. * 关联优惠券
  176. * @return \think\model\relation\HasOne
  177. */
  178. public function giveCoupon()
  179. {
  180. return $this->hasMany(StorePromotionsAuxiliary::class, 'promotions_id', 'id')->where('type',2);
  181. }
  182. /**
  183. * 赠送商品
  184. * @return \think\model\relation\HasOne
  185. */
  186. public function giveProducts()
  187. {
  188. return $this->hasMany(StorePromotionsAuxiliary::class, 'promotions_id', 'id')->where('type',3);
  189. }
  190. /**
  191. * 阶梯优惠
  192. * @return \think\model\relation\HasMany
  193. */
  194. public function promotions()
  195. {
  196. return $this->hasMany(self::class, 'pid', 'id')->order('id asc');
  197. }
  198. /**
  199. * 关联品牌
  200. * @return \think\model\relation\HasMany
  201. */
  202. public function brands()
  203. {
  204. return $this->hasMany(StorePromotionsAuxiliary::class, 'promotions_id', 'id')->where('type',1)->where('product_partake_type', 4);
  205. }
  206. /**
  207. * 关联商品标签
  208. * @return \think\model\relation\HasMany
  209. */
  210. public function productLabels()
  211. {
  212. return $this->hasMany(StorePromotionsAuxiliary::class, 'promotions_id', 'id')->where('type',1)->where('product_partake_type', 5);
  213. }
  214. /**
  215. * ID搜索器
  216. * @param Model $query
  217. * @param $value
  218. */
  219. public function searchIdAttr($query, $value)
  220. {
  221. if (is_array($value)) {
  222. if ($value) $query->whereIn('id', $value);
  223. } else {
  224. if ($value) $query->where('id', $value);
  225. }
  226. }
  227. /**
  228. * pid搜索器
  229. * @param Model $query
  230. * @param $value
  231. */
  232. public function searchPidAttr($query, $value)
  233. {
  234. if ($value !== '') $query->where('pid', $value);
  235. }
  236. /**
  237. * type搜索器
  238. * @param Model $query
  239. * @param $value
  240. */
  241. public function searchTypeAttr($query, $value)
  242. {
  243. if ($value) $query->where('type', $value);
  244. }
  245. /**
  246. * store_id搜索器
  247. * @param $query
  248. * @param $value
  249. */
  250. public function searchStoreIdAttr($query, $value)
  251. {
  252. if ($value !== '') $query->where('store_id', $value);
  253. }
  254. /**
  255. * promotions_type搜索器
  256. * @param $query
  257. * @param $value
  258. */
  259. public function searchPromotionsTypeAttr($query, $value)
  260. {
  261. if ($value !== '') {
  262. if (is_array($value)) {
  263. if ($value) $query->whereIn('promotions_type', $value);
  264. } else {
  265. if ($value) $query->where('promotions_type', $value);
  266. }
  267. }
  268. }
  269. /**
  270. * promotions_cate搜索器
  271. * @param Model $query
  272. * @param $value
  273. */
  274. public function searchPromotionsCateAttr($query, $value)
  275. {
  276. if ($value !== '') $query->where('promotions_cate', $value);
  277. }
  278. /**
  279. * name搜索器
  280. * @param Model $query
  281. * @param $value
  282. */
  283. public function searchNameAttr($query, $value)
  284. {
  285. if ($value !== '') $query->whereLike('id|name|desc', "%" . $value . "%");
  286. }
  287. /**
  288. * threshold_type搜索器
  289. * @param Model $query
  290. * @param $value
  291. */
  292. public function searchThresholdTypeAttr($query, $value)
  293. {
  294. if ($value !== '') $query->where('threshold_type', $value);
  295. }
  296. /**
  297. * discount_type搜索器
  298. * @param Model $query
  299. * @param $value
  300. */
  301. public function searchDiscountTypeAttr($query, $value)
  302. {
  303. if ($value !== '') $query->where('discount_type', $value);
  304. }
  305. /**
  306. * n_piece_n_discount搜索器
  307. * @param Model $query
  308. * @param $value
  309. */
  310. public function searchNPieceNDiscountAttr($query, $value)
  311. {
  312. if ($value !== '') $query->where('n_piece_n_discount', $value);
  313. }
  314. /**
  315. * product_partake_type搜索器
  316. * @param Model $query
  317. * @param $value
  318. */
  319. public function searchProductPartakeTypeAttr($query, $value)
  320. {
  321. if ($value !== '') {
  322. if (is_array($value)) {
  323. if ($value) $query->whereIn('product_partake_type', $value);
  324. } else {
  325. if ($value) $query->where('product_partake_type', $value);
  326. }
  327. }
  328. }
  329. /**
  330. * 是否删除搜索器
  331. * @param Model $query
  332. * @param $value
  333. * @param $data
  334. */
  335. public function searchIsDelAttr($query, $value, $data)
  336. {
  337. $query->where('is_del', $value ?? 0);
  338. }
  339. /**
  340. * 状态搜索器
  341. * @param Model $query
  342. * @param $value
  343. * @param $data
  344. */
  345. public function searchStatusAttr($query, $value, $data)
  346. {
  347. if ($value != '') $query->where('status', $value);
  348. }
  349. /**
  350. * 适用门店类型搜索器
  351. * @param Model $query
  352. * @param $value
  353. */
  354. public function searchApplicableTypeAttr($query, $value)
  355. {
  356. if (is_array($value)) {
  357. if ($value) $query->whereIn('applicable_type', $value);
  358. } else {
  359. if ($value !== '') $query->where('applicable_type', $value);
  360. }
  361. }
  362. /**
  363. * 活动事件搜索器
  364. * @param $query
  365. * @param $value
  366. * @param $data
  367. * @return void
  368. */
  369. public function searchActivityTimeAttr($query, $value, $data)
  370. {
  371. if ($value) {
  372. $startTime = $endTime = 0;
  373. if (is_array($value)) {
  374. $startTime = trim($value[0] ?? 0);
  375. $endTime = trim($value[1] ?? 0);
  376. } elseif (is_string($value)) {
  377. if (strstr($value, '-') !== false) {
  378. [$startTime, $endTime] = explode('-', $value);
  379. $startTime = trim($startTime);
  380. $endTime = trim($endTime);
  381. }
  382. }
  383. if ($startTime || $endTime) {
  384. try {
  385. date('Y-m-d', $startTime);
  386. } catch (\Throwable $e) {
  387. $startTime = strtotime($startTime);
  388. }
  389. try {
  390. date('Y-m-d', $endTime);
  391. } catch (\Throwable $e) {
  392. $endTime = strtotime($endTime);
  393. }
  394. if ($startTime == $endTime || $endTime == strtotime(date('Y-m-d', $endTime))) {
  395. $endTime = $endTime + 86399;
  396. }
  397. $query->where(function ($b) use($startTime, $endTime) {
  398. $b->whereBetween('start_time', [$startTime, $endTime])->whereOr(function ($q) use ($startTime, $endTime) {
  399. $q->whereBetween('stop_time', [$startTime, $endTime]);
  400. })->whereOr(function ($q) use ($startTime, $endTime) {
  401. $q->where('start_time', '<=', $startTime)->where('stop_time', '>=', $endTime);
  402. });
  403. });
  404. }
  405. }
  406. }
  407. }