Merchant.php 3.5 KB

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