StoreCoupon.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\coupon;
  12. use app\common\model\BaseModel;
  13. use app\common\model\system\merchant\Merchant;
  14. use app\common\repositories\store\coupon\StoreCouponUserRepository;
  15. use app\common\repositories\store\product\SpuRepository;
  16. class StoreCoupon extends BaseModel
  17. {
  18. /**
  19. * @return string
  20. * @author xaboy
  21. * @day 2020-03-30
  22. */
  23. public static function tablePk(): string
  24. {
  25. return 'coupon_id';
  26. }
  27. /**
  28. * @return string
  29. * @author xaboy
  30. * @day 2020-03-30
  31. */
  32. public static function tableName(): string
  33. {
  34. return 'store_coupon';
  35. }
  36. public function product()
  37. {
  38. return $this->hasMany(StoreCouponProduct::class, 'coupon_id', 'coupon_id');
  39. }
  40. public function issue()
  41. {
  42. return $this->hasOne(StoreCouponIssueUser::class, 'coupon_id', 'coupon_id');
  43. }
  44. public function svipIssue()
  45. {
  46. return $this->hasOne(StoreCouponIssueUser::class, 'coupon_id', 'coupon_id')->whereMonth('create_time');
  47. }
  48. public function merchant()
  49. {
  50. return $this->hasOne(Merchant::class, 'mer_id', 'mer_id');
  51. }
  52. public function getUsedNumAttr()
  53. {
  54. return app()->make(StoreCouponUserRepository::class)->usedNum($this->coupon_id);
  55. }
  56. public function getSendNumAttr()
  57. {
  58. return app()->make(StoreCouponUserRepository::class)->sendNum($this->coupon_id);
  59. }
  60. public function getCouponPriceAttr($value)
  61. {
  62. // 格式化为两位小数
  63. $formatted = number_format($value, 2, '.', '');
  64. // 转换格式化后的数字
  65. $formatted = rtrim($formatted, '0'); // 去除末尾的0
  66. $formatted = rtrim($formatted, '.'); // 去除末尾的小数点(如果存在)
  67. return $formatted;
  68. }
  69. public function getProductLstAttr()
  70. {
  71. $where['spu_status'] = 1;
  72. $where['mer_status'] = 1;
  73. //优惠券类型 0-店铺 1-商品券 10 平台通用券 11平台品类券 12平台跨店券
  74. switch ($this['type']) {
  75. case 0:
  76. $where['mer_id'] = $this->mer_id;
  77. break;
  78. case 1:
  79. $where['product_ids'] = StoreCouponProduct::where('coupon_id', $this->coupon_id)->limit(5)->column('product_id');
  80. break;
  81. case 10:
  82. break;
  83. case 11:
  84. $ids = StoreCouponProduct::where('coupon_id', $this->coupon_id)->limit(5)->column('product_id');
  85. $where['cate_pid'] = $ids;
  86. break;
  87. case 12:
  88. $ids = StoreCouponProduct::where('coupon_id', $this->coupon_id)->limit(5)->column('product_id');
  89. $where['mer_ids'] = $ids;
  90. break;
  91. }
  92. $where['order'] = 'none';
  93. $where['common'] = 1;
  94. $where['product_type'] = 0;
  95. $product = app()->make(SpuRepository::class)->search($where)->field(
  96. 'S.spu_id,S.product_id,S.store_name,S.image,activity_id,S.keyword,S.price,S.mer_id,spu_id,S.status,store_info,brand_id,cate_id,unit_name,S.star,S.rank,S.sort,sales,S.product_type,P.ot_price'
  97. )->limit(3)->orderRand()->select()->toArray();
  98. return $product;
  99. }
  100. }