Merchant.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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\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. use app\common\model\store\product\Spu;
  18. use app\common\model\store\service\StoreService;
  19. use app\common\model\system\config\SystemConfigValue;
  20. use app\common\model\system\financial\Financial;
  21. use app\common\model\system\serve\ServeOrder;
  22. use app\common\repositories\store\StoreActivityRepository;
  23. class Merchant extends BaseModel
  24. {
  25. /**
  26. * @return string
  27. * @author xaboy
  28. * @day 2020-03-30
  29. */
  30. public static function tablePk(): string
  31. {
  32. return 'mer_id';
  33. }
  34. /**
  35. * @return string
  36. * @author xaboy
  37. * @day 2020-03-30
  38. */
  39. public static function tableName(): string
  40. {
  41. return 'merchant';
  42. }
  43. public function getDeliveryWayAttr($value)
  44. {
  45. if (!$value) return [];
  46. return explode(',',$value);
  47. }
  48. public function product()
  49. {
  50. return $this->hasMany(Product::class, 'mer_id', 'mer_id');
  51. }
  52. public function config()
  53. {
  54. return $this->hasMany(SystemConfigValue::class, 'mer_id', 'mer_id');
  55. }
  56. public function getConfigAttr()
  57. {
  58. $configKeys = [
  59. 'mer_integral_status', // 积分状态
  60. 'mer_integral_rate', // 积分抵扣比例
  61. 'mer_store_stock', // 警戒库存
  62. 'mer_take_status', // 自提点状态
  63. 'mer_take_name', // 自提点名称
  64. 'mer_take_phone', // 自提点电话
  65. 'mer_take_address', // 自提点地址
  66. 'mer_take_location', // 自提点经纬度
  67. 'mer_take_day', // 自提点工作时间
  68. 'svip_coupon_merge', // 是否支持付费会员和优惠卷合并使用
  69. 'mer_take_time', // 自提点时间
  70. 'mer_open_receipt' // 是否开启发票
  71. ];
  72. return merchantConfig($this->mer_id, $configKeys);
  73. }
  74. public function showProduct()
  75. {
  76. return $this->hasMany(Product::class, 'mer_id', 'mer_id')
  77. ->where((new ProductDao())->productShow())
  78. ->field('mer_id,product_id,store_name,image,price,is_show,status,is_gift_bag,is_good')
  79. ->order('is_good DESC,sort DESC');
  80. }
  81. /**
  82. * 商户列表下的推荐
  83. * @return \think\Collection
  84. * @author Qinii
  85. * @day 4/20/22
  86. */
  87. public function getAllRecommendAttr()
  88. {
  89. $list = Product::where('mer_id', $this['mer_id'])
  90. ->where((new ProductDao())->productShow())
  91. ->field('mer_id,product_id,store_name,image,price,is_show,status,is_gift_bag,is_good,cate_id')
  92. ->order('is_good DESC, sort DESC, create_time DESC')
  93. ->limit(10)
  94. ->select()->append(['show_svip_info']);
  95. if ($list) {
  96. $list = app()->make(StoreActivityRepository::class)->getPic($list->toArray(),StoreActivityRepository::ACTIVITY_TYPE_BORDER);
  97. }
  98. return $list;
  99. }
  100. public function getCityRecommendAttr()
  101. {
  102. $list = Product::where('mer_id', $this['mer_id'])
  103. ->where((new ProductDao())->productShow())
  104. ->whereLike('delivery_way',"%1%")
  105. ->field('mer_id,product_id,store_name,image,price,is_show,status,is_gift_bag,is_good,cate_id')
  106. ->order('sort DESC, create_time DESC')
  107. ->limit(3)
  108. ->select();
  109. if ($list) {
  110. $list = app()->make(StoreActivityRepository::class)->getPic($list->toArray(),StoreActivityRepository::ACTIVITY_TYPE_BORDER);
  111. }
  112. return $list;
  113. }
  114. public function recommend()
  115. {
  116. return $this->hasMany(Product::class, 'mer_id', 'mer_id')
  117. ->where((new ProductDao())->productShow())
  118. ->where('is_good', 1)
  119. ->field('mer_id,product_id,store_name,image,price,is_show,status,is_gift_bag,is_good,sales,create_time')
  120. ->order('is_good DESC,sort DESC,create_time DESC')
  121. ->limit(3);
  122. }
  123. public function coupon()
  124. {
  125. $time = date('Y-m-d H:i:s');
  126. return $this->hasMany(StoreCouponUser::class, 'mer_id', 'mer_id')->where('start_time', '<', $time)->where('end_time', '>', $time)
  127. ->where('is_fail', 0)->where('status', 0)->order('coupon_price DESC, coupon_user_id ASC')
  128. ->with(['product' => function ($query) {
  129. $query->field('coupon_id,product_id');
  130. }, 'coupon' => function ($query) {
  131. $query->field('coupon_id,type');
  132. }]);
  133. }
  134. public function merchantRegion()
  135. {
  136. return $this->hasOne(MerchantRegion::class, 'region_id', 'region_id');
  137. }
  138. public function getServicesTypeAttr()
  139. {
  140. //0:关闭
  141. //1:系统客服
  142. //2:拨打电话
  143. //3:企业微信
  144. //4:跳转链接
  145. $config = merchantConfig($this->mer_id,['services_type','service_phone','mer_customer_url','mer_customer_corpId','mer_customer_link']);
  146. if ($config['services_type'] == 1) {
  147. $where = ['mer_id' => $this->mer_id, 'is_open' => 1,'status' => 1];
  148. $service = StoreService::where($where)->count();
  149. if (!$service) $config['services_type'] = 0;
  150. }
  151. return $config;
  152. }
  153. public function marginOrder()
  154. {
  155. return $this->hasMany(ServeOrder::class, 'mer_id','mer_id')->where('type', 10)->order('create_time DESC');
  156. }
  157. public function refundMarginOrder()
  158. {
  159. return $this->hasOne(Financial::class, 'mer_id', 'mer_id')
  160. ->where('type',1)
  161. ->where('status', -1)
  162. ->order('create_time DESC')
  163. ->limit(1);
  164. }
  165. public function merchantCategory()
  166. {
  167. return $this->hasOne(MerchantCategory::class, 'merchant_category_id', 'category_id');
  168. }
  169. public function merchantType()
  170. {
  171. return $this->hasOne(MerchantType::class, 'mer_type_id', 'type_id');
  172. }
  173. public function getMerTypeNameAttr()
  174. {
  175. return MerchantType::where('mer_type_id',$this->type_id)->value('type_name');
  176. }
  177. public function typeName()
  178. {
  179. return $this->merchantType()->bind(['type_name']);
  180. }
  181. public function categoryName()
  182. {
  183. return $this->merchantCategory()->bind(['category_name']);
  184. }
  185. public function getMerCommissionRateAttr()
  186. {
  187. return $this->commission_swtich ? $this->commission_rate : $this->merchantCategory->commission_rate;
  188. }
  189. public function getOpenReceiptAttr()
  190. {
  191. return (int)merchantConfig($this->mer_id, 'mer_open_receipt');
  192. }
  193. public function admin()
  194. {
  195. return $this->hasOne(MerchantAdmin::class, 'mer_id', 'mer_id')->where('level', 0);
  196. }
  197. public function searchKeywordAttr($query, $value)
  198. {
  199. $query->whereLike('mer_name|mer_keyword', "%{$value}%");
  200. }
  201. public function getFinancialAlipayAttr($value)
  202. {
  203. return $value ? json_decode($value) : $value;
  204. }
  205. public function getFinancialWechatAttr($value)
  206. {
  207. return $value ? json_decode($value) : $value;
  208. }
  209. public function getFinancialBankAttr($value)
  210. {
  211. return $value ? json_decode($value) : $value;
  212. }
  213. public function getMerCertificateAttr()
  214. {
  215. return merchantConfig($this->mer_id, 'mer_certificate');
  216. }
  217. public function getIssetCertificateAttr()
  218. {
  219. return count(merchantConfig($this->mer_id, 'mer_certificate') ?: []) > 0;
  220. }
  221. public function getMarginRemindStatusAttr()
  222. {
  223. if (systemConfig('margin_remind_switch') == '1' && $this->mer_state) {
  224. if ($this->is_margin == 10) {
  225. if($this->ot_margin > $this->margin) {
  226. if (!$this->margin_remind_time) {
  227. $day = systemConfig('margin_remind_day') ?: 0;
  228. if($day) {
  229. $time = strtotime(date('Y-m-d 23:59:59',strtotime("+ $day day",time())));
  230. $this->margin_remind_time = $time;
  231. } else {
  232. $this->status = 0;
  233. $this->mer_state = 0;
  234. }
  235. $this->save();
  236. }
  237. return $this->margin_remind_time;
  238. }
  239. }
  240. }
  241. return null;
  242. }
  243. public function searchMerIdsAttr($query, $value)
  244. {
  245. $query->whereIn('mer_id',$value);
  246. }
  247. }