Merchant.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\common\model\system\merchant;
  12. use app\common\dao\store\product\ProductDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\store\coupon\StoreCouponProduct;
  15. use app\common\model\store\coupon\StoreCouponUser;
  16. use app\common\model\store\product\Product;
  17. class Merchant extends BaseModel
  18. {
  19. /**
  20. * @return string
  21. * @author xaboy
  22. * @day 2020-03-30
  23. */
  24. public static function tablePk(): string
  25. {
  26. return 'mer_id';
  27. }
  28. /**
  29. * @return string
  30. * @author xaboy
  31. * @day 2020-03-30
  32. */
  33. public static function tableName(): string
  34. {
  35. return 'merchant';
  36. }
  37. public function product()
  38. {
  39. return $this->hasMany(Product::class, 'mer_id', 'mer_id');
  40. }
  41. public function showProduct()
  42. {
  43. return $this->hasMany(Product::class, 'mer_id', 'mer_id')
  44. ->where((new ProductDao())->productShow())
  45. ->field('mer_id,product_id,store_name,image,price,is_show,status,is_gift_bag,is_good')
  46. ->order('sort desc');
  47. }
  48. public function recommend()
  49. {
  50. return $this->hasMany(Product::class, 'mer_id', 'mer_id')
  51. ->where((new ProductDao())->productShow())
  52. ->where('is_good', 1)
  53. ->field('mer_id,product_id,store_name,image,price,is_show,status,is_gift_bag,is_good,sales,create_time')
  54. ->order('is_good DESC,sort DESC,create_time DESC')
  55. ->limit(3);
  56. }
  57. public function coupon()
  58. {
  59. $time = date('Y-m-d H:i:s');
  60. return $this->hasMany(StoreCouponUser::class, 'mer_id', 'mer_id')->where('start_time', '<', $time)->where('end_time', '>', $time)
  61. ->where('is_fail', 0)->where('status', 0)->order('coupon_price DESC')
  62. ->with(['product' => function ($query) {
  63. $query->field('coupon_id,product_id');
  64. }, 'coupon' => function ($query) {
  65. $query->field('coupon_id,type');
  66. }]);
  67. }
  68. public function merchantCategory()
  69. {
  70. return $this->hasOne(MerchantCategory::class, 'merchant_category_id', 'category_id');
  71. }
  72. public function getMerCommissionRateAttr()
  73. {
  74. return $this->commission_rate > 0 ? $this->commission_rate : bcmul($this->merchantCategory->commission_rate, 100, 4);
  75. }
  76. public function getOpenReceiptAttr()
  77. {
  78. return merchantConfig($this->mer_id, 'mer_open_receipt');
  79. }
  80. public function admin()
  81. {
  82. return $this->hasOne(MerchantAdmin::class, 'mer_id', 'mer_id')->where('level', 0);
  83. }
  84. public function searchKeywordAttr($query,$value)
  85. {
  86. $query->whereLike('mer_name|mer_keyword',"%{$value}%");
  87. }
  88. public function getFinancialAlipayAttr($value)
  89. {
  90. return $value ? json_decode($value) : $value;
  91. }
  92. public function getFinancialWechatAttr($value)
  93. {
  94. return $value ? json_decode($value) : $value;
  95. }
  96. public function getFinancialBankAttr($value)
  97. {
  98. return $value ? json_decode($value) : $value;
  99. }
  100. }