StoreProduct.php 30 KB

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