StoreProduct.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/12
  6. */
  7. namespace app\models\store;
  8. use app\admin\model\store\StoreProductAttrValue as StoreProductAttrValueModel;
  9. use app\models\system\SystemUserLevel;
  10. use app\models\user\UserLevel;
  11. use crmeb\basic\BaseModel;
  12. use crmeb\services\GroupDataService;
  13. use crmeb\services\workerman\ChannelService;
  14. use crmeb\traits\ModelTrait;
  15. use app\models\store\{
  16. StoreBargain, StoreCombination, StoreSeckill
  17. };
  18. use app\models\user\UserSearch;
  19. use think\facade\Config;
  20. /**
  21. * TODO 产品Model
  22. * Class StoreProduct
  23. * @package app\models\store
  24. */
  25. class StoreProduct extends BaseModel
  26. {
  27. /**
  28. * 数据表主键
  29. * @var string
  30. */
  31. protected $pk = 'id';
  32. /**
  33. * 模型名称
  34. * @var string
  35. */
  36. protected $name = 'store_product';
  37. use ModelTrait;
  38. protected function getSliderImageAttr($value)
  39. {
  40. $sliderImage = json_decode($value, true) ?: [];
  41. foreach ($sliderImage as &$item) {
  42. $item = str_replace('\\', '/', $item);
  43. }
  44. return $sliderImage;
  45. }
  46. protected function getImageAttr($value)
  47. {
  48. return str_replace('\\', '/', $value);
  49. }
  50. public function getDescriptionAttr($value)
  51. {
  52. return htmlspecialchars_decode($value);
  53. }
  54. public static function getValidProduct($productId, $field = 'add_time,browse,cate_id,code_path,ficti,give_integral,id,image,is_sub,is_bargain,is_benefit,is_best,is_del,is_hot,is_new,is_postage,is_seckill,is_show,keyword,mer_id,mer_use,ot_price,postage,price,sales,slider_image,sort,stock,store_info,store_name,unit_name,vip_price,spec_type,IFNULL(sales,0) + IFNULL(ficti,0) as fsales,video_link,cost')
  55. {
  56. $product = self::where('is_del', 0)->where('is_show', 1)->where('id', $productId)->field($field)->find();
  57. if ($product) {
  58. $product = $product->toArray();
  59. if (Config::get('app.show_benefit')) {
  60. $rate = getRate($product['cate_id']);
  61. $product['reputation'] = '赔付' . getReputation($product, $rate);
  62. }
  63. unset($product['cost']);
  64. return $product;
  65. }
  66. else return false;
  67. }
  68. public static function getGoodList($limit = 18, $field = '*')
  69. {
  70. $list = self::validWhere()->where('is_good', 1)->order('sort desc,id desc')->limit($limit)->field($field)->select();
  71. $list = count($list) ? $list->toArray() : [];
  72. if (!empty($list)) {
  73. foreach ($list as $k => $v) {
  74. $list[$k]['activity'] = []; // self::activity($v['id']);
  75. }
  76. }
  77. return $list;
  78. }
  79. public static function validWhere()
  80. {
  81. return self::where('is_del', 0)->where('is_show', 1)->where('mer_id', 0);
  82. }
  83. public static function getProductList($data, $uid)
  84. {
  85. $sId = $data['sid']; // category id
  86. $cId = $data['cid']; // parent category id
  87. $keyword = $data['keyword'];
  88. $priceOrder = $data['priceOrder'];
  89. $salesOrder = $data['salesOrder'];
  90. $news = $data['news'];
  91. $page = $data['page'];
  92. $limit = $data['limit'];
  93. $type = $data['type']; // 某些模板需要购物车数量 1 = 需要查询,0 = 不需要
  94. $model = self::validWhere();
  95. if ($sId) {
  96. $model->whereIn('id', function ($query) use ($sId) {
  97. $query->name('store_product_cate')->where('cate_id', $sId)->field('product_id')->select();
  98. });
  99. } elseif ($cId) {
  100. $model->whereIn('id', function ($query) use ($cId) {
  101. $query->name('store_product_cate')->whereIn('cate_id', function ($q) use ($cId) {
  102. $q->name('store_category')->where('pid', $cId)->field('id')->select();
  103. })->field('product_id')->select();
  104. });
  105. }
  106. if (!empty($keyword)) {
  107. $model->where('keyword|store_name', 'LIKE', htmlspecialchars("%$keyword%"));
  108. UserSearch::InsertHistory($uid ?? 1, $keyword);
  109. }
  110. if ($news != 0) $model->where('is_new', 1);
  111. $baseOrder = '';
  112. if ($priceOrder) $baseOrder = $priceOrder == 'desc' ? 'price DESC' : 'price ASC';
  113. // if($salesOrder) $baseOrder = $salesOrder == 'desc' ? 'sales DESC' : 'sales ASC';//真实销量
  114. if ($salesOrder) $baseOrder = $salesOrder == 'desc' ? 'sales DESC' : 'sales ASC';//虚拟销量
  115. if ($baseOrder) $baseOrder .= ', ';
  116. $model->order($baseOrder . 'sort DESC, add_time DESC');
  117. $show_benefit = Config::get('app.show_benefit', false);
  118. $fields = 'id,store_name,cate_id,image,IFNULL(sales,0) + IFNULL(ficti,0) as sales,price,stock,spec_type';
  119. if ($show_benefit) {
  120. $fields .= ',cost';
  121. }
  122. $list = $model->page((int)$page, (int)$limit)->field($fields)->select()->each(function ($item)
  123. use ($uid, $type, $show_benefit, $sId) {
  124. if ($type) {
  125. if ($item['spec_type']) {
  126. $item['is_att'] = StoreProductAttrValueModel::where(['product_id' => $item['id'], 'type' => 0])->count() ? true : false;
  127. } else {
  128. $item['is_att'] = false;
  129. }
  130. if ($uid) $item['cart_num'] = StoreCart::where('is_pay', 0)->where('is_del', 0)->where('is_new', 0)->where('type', 'product')->where('product_id', $item['id'])->where('uid', $uid)->value('cart_num');
  131. else $item['cart_num'] = 0;
  132. if (is_null($item['cart_num'])) $item['cart_num'] = 0;
  133. }
  134. // show benefit
  135. if ($show_benefit) {
  136. $rate = getRate($sId);
  137. $item['reputation'] = '赔付' . getReputation($item, $rate);
  138. unset($item['cost']);
  139. }
  140. });
  141. $list = count($list) ? $list->toArray() : [];
  142. if (!empty($list)) {
  143. foreach ($list as $k => $v) {
  144. $list[$k]['activity'] = []; // self::activity($v['id']);
  145. }
  146. }
  147. return self::setLevelPrice($list, $uid);
  148. }
  149. /*
  150. * 分类搜索
  151. * @param string $value
  152. * @return array
  153. * */
  154. public static function getSearchStorePage($keyword, $page, $limit, $uid, $cutApart = [' ', ',', '-'])
  155. {
  156. $model = self::validWhere();
  157. $keyword = trim($keyword);
  158. if (strlen($keyword)) {
  159. $cut = false;
  160. foreach ($cutApart as $val) {
  161. if (strstr($keyword, $val) !== false) {
  162. $cut = $val;
  163. break;
  164. }
  165. }
  166. if ($cut !== false) {
  167. $keywordArray = explode($cut, $keyword);
  168. $sql = [];
  169. foreach ($keywordArray as $item) {
  170. $sql[] = '(`store_name` LIKE "%' . $item . '%" OR `keyword` LIKE "%' . $item . '%")';
  171. }
  172. $model = $model->where(implode(' OR ', $sql));
  173. } else {
  174. $model = $model->where('store_name|keyword', 'LIKE', "%$keyword%");
  175. }
  176. }
  177. $list = $model->field('id,store_name,cate_id,image,ficti as sales,price,stock')->page($page, $limit)->select();
  178. $list = count($list) ? $list->toArray() : [];
  179. if (!empty($list)) {
  180. foreach ($list as $k => $v) {
  181. $list[$k]['activity'] = []; // self::activity($v['id']);
  182. }
  183. }
  184. return self::setLevelPrice($list, $uid);
  185. }
  186. /**
  187. * 新品产品
  188. * @param string $field
  189. * @param int $limit
  190. * @return false|\PDOStatement|string|\think\Collection
  191. */
  192. public static function getNewProduct($field = '*', $limit = 0, $uid = 0, bool $bool = true, $page = 0, $limits = 0)
  193. {
  194. if (!$limit && !$bool) return [];
  195. $model = self::where('is_new', 1)->where('is_del', 0)->where('mer_id', 0)
  196. ->where('stock', '>', 0)->where('is_show', 1)->field($field)
  197. ->order('sort DESC, id DESC');
  198. if ($limit) $model->limit($limit);
  199. if ($page) $model->page((int)$page, (int)$limits);
  200. $list = $model->select();
  201. $list = count($list) ? $list->toArray() : [];
  202. if (!empty($list)) {
  203. foreach ($list as $k => $v) {
  204. $list[$k]['activity'] = []; // self::activity($v['id']);
  205. }
  206. }
  207. return self::setLevelPrice($list, $uid);
  208. }
  209. /**
  210. * 热卖产品
  211. * @param string $field
  212. * @param int $limit
  213. * @return false|\PDOStatement|string|\think\Collection
  214. */
  215. public static function getHotProduct($field = '*', $limit = 0, $uid = 0, $page = 0, $limits = 0)
  216. {
  217. $model = self::where('is_hot', 1)->where('is_del', 0)->where('mer_id', 0)
  218. ->where('stock', '>', 0)->where('is_show', 1)->field($field)
  219. ->order('sort DESC, id DESC');
  220. if ($limit) $model->limit($limit);
  221. if ($page) $model->page((int)$page, (int)$limits);
  222. $list = $model->select();
  223. $list = count($list) ? $list->toArray() : [];
  224. if (!empty($list)) {
  225. foreach ($list as $k => $v) {
  226. $list[$k]['activity'] = []; // self::activity($v['id']);
  227. }
  228. }
  229. return self::setLevelPrice($list, $uid);
  230. }
  231. /**
  232. * 热卖产品
  233. * @param string $field
  234. * @param int $page
  235. * @param int $limit
  236. * @return array|\think\Collection
  237. * @throws \think\db\exception\DataNotFoundException
  238. * @throws \think\db\exception\ModelNotFoundException
  239. * @throws \think\exception\DbException
  240. */
  241. public static function getHotProductLoading($field = '*', $page = 0, $limit = 0)
  242. {
  243. if (!$limit) return [];
  244. $model = self::where('is_hot', 1)->where('is_del', 0)->where('mer_id', 0)
  245. ->where('stock', '>', 0)->where('is_show', 1)->field($field)
  246. ->order('sort DESC, id DESC');
  247. if ($page) $model->page($page, $limit);
  248. $list = $model->select();
  249. if (is_object($list)) return $list->toArray();
  250. return $list;
  251. }
  252. /**
  253. * 精品产品
  254. * @param string $field
  255. * @param int $limit
  256. * @return false|\PDOStatement|string|\think\Collection
  257. */
  258. public static function getBestProduct($field = '*', $limit = 0, $uid = 0, bool $bool = true, $page = 0, $limits = 0)
  259. {
  260. if (!$limit && !$bool) return [];
  261. $model = self::where('is_best', 1)->where('is_del', 0)->where('mer_id', 0)
  262. ->where('stock', '>', 0)->where('is_show', 1)->field($field)
  263. ->order('sort DESC, id DESC');
  264. if ($limit) $model->limit($limit);
  265. if ($page) $model->page((int)$page, (int)$limits);
  266. $list = $model->select();
  267. $list = count($list) ? $list->toArray() : [];
  268. if (!empty($list)) {
  269. foreach ($list as $k => $v) {
  270. $list[$k]['activity'] = []; // self::activity($v['id']);
  271. }
  272. }
  273. return self::setLevelPrice($list, $uid);
  274. }
  275. /**
  276. * 设置会员价格
  277. * @param object | array $list 产品列表
  278. * @param int $uid 用户uid
  279. * @return array
  280. * */
  281. public static function setLevelPrice($list, $uid, $isSingle = false)
  282. {
  283. if (is_object($list)) $list = count($list) ? $list->toArray() : [];
  284. if (!sys_config('vip_open')) {
  285. if (is_array($list)) return $list;
  286. return $isSingle ? $list : 0;
  287. }
  288. $levelId = UserLevel::getUserLevel($uid);
  289. if ($levelId) {
  290. $discount = UserLevel::getUserLevelInfo($levelId, 'discount');
  291. $discount = bcsub(1, bcdiv($discount, 100, 2), 2);
  292. } else {
  293. $discount = SystemUserLevel::getLevelDiscount();
  294. $discount = bcsub(1, bcdiv($discount, 100, 2), 2);
  295. }
  296. //如果不是数组直接执行减去会员优惠金额
  297. if (!is_array($list))
  298. //不是会员原价返回
  299. if ($levelId)
  300. //如果$isSingle==true 返回优惠后的总金额,否则返回优惠的金额
  301. return $isSingle ? bcsub($list, bcmul($discount, $list, 2), 2) : bcmul($discount, $list, 2);
  302. else
  303. return $isSingle ? $list : 0;
  304. //当$list为数组时$isSingle==true为一维数组 ,否则为二维
  305. if ($isSingle)
  306. $list['vip_price'] = isset($list['price']) ? bcsub($list['price'], bcmul($discount, $list['price'], 2), 2) : 0;
  307. else
  308. foreach ($list as &$item) {
  309. $item['vip_price'] = isset($item['price']) ? bcsub($item['price'], bcmul($discount, $item['price'], 2), 2) : 0;
  310. }
  311. return $list;
  312. }
  313. /**
  314. * 优惠产品
  315. * @param string $field
  316. * @param int $limit
  317. * @return false|\PDOStatement|string|\think\Collection
  318. */
  319. public static function getBenefitProduct($field = '*', $limit = 0, $page = 0, $limits = 0)
  320. {
  321. $model = self::where('is_benefit', 1)
  322. ->where('is_del', 0)->where('mer_id', 0)->where('stock', '>', 0)
  323. ->where('is_show', 1)->field($field)
  324. ->order('sort DESC, id DESC');
  325. if ($limit) $model->limit($limit);
  326. if ($page) $model->page((int)$page, (int)$limits);
  327. $data = $model->select();
  328. if (count($data) > 0) {
  329. foreach ($data as $k => $v) {
  330. $data[$k]['activity'] = []; // self::activity($v['id']);
  331. }
  332. }
  333. return $data;
  334. }
  335. public static function cateIdBySimilarityProduct($cateId, $field = '*', $limit = 0)
  336. {
  337. $pid = StoreCategory::cateIdByPid($cateId) ?: $cateId;
  338. $cateList = StoreCategory::pidByCategory($pid, 'id') ?: [];
  339. $cid = [$pid];
  340. foreach ($cateList as $cate) {
  341. $cid[] = $cate['id'];
  342. }
  343. $model = self::where('cate_id', 'IN', $cid)->where('is_show', 1)->where('is_del', 0)
  344. ->field($field)->order('sort DESC,id DESC');
  345. if ($limit) $model->limit($limit);
  346. return $model->select();
  347. }
  348. public static function isValidProduct($productId)
  349. {
  350. return self::be(['id' => $productId, 'is_del' => 0, 'is_show' => 1]) > 0;
  351. }
  352. /**
  353. * 获取单个商品得属性unique
  354. * @param int $productId
  355. * @param int $type
  356. * @return bool|mixed
  357. */
  358. public static function getSingleAttrUnique(int $productId, int $id = 0, int $type = 0)
  359. {
  360. if ($type != 2 && self::be(['id' => $productId, 'spec_type' => 1])) {
  361. return false;
  362. } else {
  363. $unique = StoreProductAttr::storeProductAttrValueDb()->where(['product_id' => $id ?: $productId, 'type' => $type])->value('unique');
  364. return $unique ?: false;
  365. }
  366. }
  367. public static function getProductStock($productId, $uniqueId = '')
  368. {
  369. return $uniqueId == '' ?
  370. self::where('id', $productId)->value('stock') ?: 0
  371. : StoreProductAttr::uniqueByStock($uniqueId);
  372. }
  373. /**
  374. * 加销量减销量
  375. * @param $num
  376. * @param $productId
  377. * @param string $unique
  378. * @return bool
  379. */
  380. public static function decProductStock($num, $productId, $unique = '')
  381. {
  382. if ($unique) {
  383. $res = false !== StoreProductAttrValueModel::decProductAttrStock($productId, $unique, $num, 0);
  384. $res = $res && self::where('id', $productId)->dec('stock', $num)->inc('sales', $num)->update();
  385. } else {
  386. $res = false !== self::where('id', $productId)->dec('stock', $num)->inc('sales', $num)->update();
  387. }
  388. if ($res) {
  389. $stock = self::where('id', $productId)->value('stock');
  390. $replenishment_num = sys_config('store_stock') ?? 0;//库存预警界限
  391. if ($replenishment_num >= $stock) {
  392. try {
  393. ChannelService::instance()->send('STORE_STOCK', ['id' => $productId]);
  394. } catch (\Exception $e) {
  395. }
  396. }
  397. }
  398. return $res;
  399. }
  400. /**
  401. * 减少销量,增加库存
  402. * @param int $num 增加库存数量
  403. * @param int $productId 产品id
  404. * @param string $unique 属性唯一值
  405. * @return boolean
  406. */
  407. public static function incProductStock($num, $productId, $unique = '')
  408. {
  409. $product = self::where('id', $productId)->field(['sales', 'stock'])->find();
  410. if (!$product) return true;
  411. if ($product->sales > 0) $product->sales = bcsub($product->sales, $num, 0);
  412. if ($product->sales < 0) $product->sales = 0;
  413. $res = true;
  414. if ($unique) {
  415. $res = false !== StoreProductAttrValueModel::incProductAttrStock($productId, $unique, $num);
  416. }
  417. $product->stock = bcadd($product->stock, $num, 0);
  418. $res = $res && $product->save();
  419. return $res;
  420. }
  421. /**
  422. * 获取产品分销佣金最低和最高
  423. * @param $storeInfo
  424. * @param $productValue
  425. * @return int|string
  426. */
  427. public static function getPacketPrice($storeInfo, $productValue)
  428. {
  429. $store_brokerage_ratio = sys_config('store_brokerage_ratio');
  430. $store_brokerage_ratio = bcdiv($store_brokerage_ratio, 100, 2);
  431. if (isset($storeInfo['is_sub']) && $storeInfo['is_sub'] == 1) {
  432. $Maxkey = self::getArrayMax($productValue, 'brokerage');
  433. $Minkey = self::getArrayMin($productValue, 'brokerage');
  434. $maxPrice = bcadd(isset($productValue[$Maxkey]) ? $productValue[$Maxkey]['brokerage'] : 0, 0, 0);
  435. $minPrice = bcadd(isset($productValue[$Minkey]) ? $productValue[$Minkey]['brokerage'] : 0, 0, 0);
  436. } else {
  437. $Maxkey = self::getArrayMax($productValue, 'price');
  438. $Minkey = self::getArrayMin($productValue, 'price');
  439. $maxPrice = bcmul($store_brokerage_ratio, bcadd(isset($productValue[$Maxkey]) ? $productValue[$Maxkey]['price'] : 0, 0, 0), 0);
  440. $minPrice = bcmul($store_brokerage_ratio, bcadd(isset($productValue[$Minkey]) ? $productValue[$Minkey]['price'] : 0, 0, 0), 0);
  441. }
  442. if ($minPrice == 0 && $maxPrice == 0)
  443. return 0;
  444. else
  445. return $minPrice . '~' . $maxPrice;
  446. }
  447. /**
  448. * 获取二维数组中最大的值
  449. * @param $arr
  450. * @param $field
  451. * @return int|string
  452. */
  453. public static function getArrayMax($arr, $field)
  454. {
  455. $temp = [];
  456. foreach ($arr as $k => $v) {
  457. $temp[] = $v[$field];
  458. }
  459. if (!count($temp)) return 0;
  460. $maxNumber = max($temp);
  461. foreach ($arr as $k => $v) {
  462. if ($maxNumber == $v[$field]) return $k;
  463. }
  464. return 0;
  465. }
  466. /**
  467. * 获取二维数组中最小的值
  468. * @param $arr
  469. * @param $field
  470. * @return int|string
  471. */
  472. public static function getArrayMin($arr, $field)
  473. {
  474. $temp = [];
  475. foreach ($arr as $k => $v) {
  476. $temp[] = $v[$field];
  477. }
  478. if (!count($temp)) return 0;
  479. $minNumber = min($temp);
  480. foreach ($arr as $k => $v) {
  481. if ($minNumber == $v[$field]) return $k;
  482. }
  483. return 0;
  484. }
  485. /**
  486. * 产品名称 图片
  487. * @param array $productIds
  488. * @return array
  489. */
  490. public static function getProductStoreNameOrImage(array $productIds)
  491. {
  492. return self::whereIn('id', $productIds)->column('store_name,image', 'id');
  493. }
  494. /**
  495. * TODO 获取某个字段值
  496. * @param $id
  497. * @param string $field
  498. * @return mixed
  499. */
  500. public static function getProductField($id, $field = 'store_name')
  501. {
  502. if (is_array($id))
  503. return self::where('id', 'in', $id)->field($field)->select();
  504. else
  505. return self::where('id', $id)->value($field);
  506. }
  507. /**
  508. * 获取产品返佣金额
  509. * @param array $cartId
  510. * @param bool $type true = 一级返佣, fasle = 二级返佣
  511. * @return int|string
  512. */
  513. public static function getProductBrokerage(array $cartId, bool $type = true)
  514. {
  515. $cartInfo = StoreOrderCartInfo::whereIn('cart_id', $cartId)->column('cart_info');
  516. $oneBrokerage = 0;//一级返佣金额
  517. $twoBrokerage = 0;//二级返佣金额
  518. $sumProductPrice = 0;//非指定返佣商品总金额
  519. foreach ($cartInfo as $value) {
  520. $product = json_decode($value, true);
  521. $cartNum = $product['cart_num'] ?? 0;
  522. if (isset($product['productInfo'])) {
  523. $productInfo = $product['productInfo'];
  524. //指定返佣金额
  525. if (isset($productInfo['is_sub']) && $productInfo['is_sub'] == 1) {
  526. $oneBrokerage = bcadd($oneBrokerage, bcmul($cartNum, $productInfo['attrInfo']['brokerage'] ?? 0, 2), 2);
  527. $twoBrokerage = bcadd($twoBrokerage, bcmul($cartNum, $productInfo['attrInfo']['brokerage_two'] ?? 0, 2), 2);
  528. } else {
  529. //比例返佣
  530. if (isset($productInfo['attrInfo'])) {
  531. $sumProductPrice = bcadd($sumProductPrice, bcmul($cartNum, $productInfo['attrInfo']['price'] ?? 0, 2), 2);
  532. } else {
  533. $sumProductPrice = bcadd($sumProductPrice, bcmul($cartNum, $productInfo['price'] ?? 0, 2), 2);
  534. }
  535. }
  536. }
  537. }
  538. if ($type) {
  539. //获取后台一级返佣比例
  540. $storeBrokerageRatio = sys_config('store_brokerage_ratio');
  541. //一级返佣比例 小于等于零时直接返回 不返佣
  542. if ($storeBrokerageRatio <= 0) {
  543. return $oneBrokerage;
  544. }
  545. //计算获取一级返佣比例
  546. $brokerageRatio = bcdiv($storeBrokerageRatio, 100, 2);
  547. $brokeragePrice = bcmul($sumProductPrice, $brokerageRatio, 2);
  548. //固定返佣 + 比例返佣 = 一级总返佣金额
  549. return bcadd($oneBrokerage, $brokeragePrice, 2);
  550. } else {
  551. //获取二级返佣比例
  552. $storeBrokerageTwo = sys_config('store_brokerage_two');
  553. //二级返佣比例小于等于0 直接返回
  554. if ($storeBrokerageTwo <= 0) {
  555. return $twoBrokerage;
  556. }
  557. //计算获取二级返佣比例
  558. $brokerageRatio = bcdiv($storeBrokerageTwo, 100, 2);
  559. $brokeragePrice = bcmul($sumProductPrice, $brokerageRatio, 2);
  560. //固定返佣 + 比例返佣 = 二级总返佣金额
  561. return bcadd($twoBrokerage, $brokeragePrice, 2);
  562. }
  563. }
  564. /**
  565. * 获取商品在此时段活动优先类型
  566. */
  567. public static function activity($id, $status = true)
  568. {
  569. $activity = self::where('id', $id)->value('activity');
  570. if (!$activity) $activity = '1,2,3';//如果老商品没有活动顺序,默认活动顺序,秒杀-砍价-拼团
  571. $activity = explode(',', $activity);
  572. $activityId = [];
  573. $time = 0;
  574. $seckillId = StoreSeckill::where('is_del', 0)->where('status', 1)->where('start_time', '<=', time())->where('stop_time', '>=', time() - 86400)->where('product_id', $id)->field('id,time_id')->select();
  575. if ($seckillId) {
  576. foreach ($seckillId as $v) {
  577. $timeInfo = GroupDataService::getDataNumber((int)$v['time_id']);
  578. if ($timeInfo && isset($timeInfo['time']) && isset($timeInfo['continued'])) {
  579. if (date('H') >= $timeInfo['time'] && date('H') < ($timeInfo['time'] + $timeInfo['continued'])) {
  580. $activityId[1] = $v['id'];
  581. $time = strtotime(date("Y-m-d"), time()) + 3600 * ($timeInfo['time'] + $timeInfo['continued']);
  582. }
  583. }
  584. }
  585. }
  586. $bargainId = StoreBargain::where('is_del', 0)->where('status', 1)->where('start_time', '<=', time())->where('stop_time', '>=', time())->where('product_id', $id)->value('id');
  587. if ($bargainId) $activityId[2] = $bargainId;
  588. $combinationId = StoreCombination::where('is_del', 0)->where('is_show', 1)->where('start_time', '<=', time())->where('stop_time', '>=', time())->where('product_id', $id)->value('id');
  589. if ($combinationId) $activityId[3] = $combinationId;
  590. $data = [];
  591. foreach ($activity as $k => $v) {
  592. if (array_key_exists($v, $activityId)) {
  593. if ($status) {
  594. $data['type'] = $v;
  595. $data['id'] = $activityId[$v];
  596. if ($v == 1) $data['time'] = $time;
  597. break;
  598. } else {
  599. $arr['type'] = $v;
  600. $arr['id'] = $activityId[$v];
  601. if ($v == 1) $arr['time'] = $time;
  602. $data[] = $arr;
  603. }
  604. }
  605. }
  606. return $data;
  607. }
  608. }