StoreProduct.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. <?php
  2. namespace app\admin\model\store;
  3. use crmeb\basic\BaseModel;
  4. use app\models\store\StoreCart;
  5. use crmeb\services\PHPExcelService;
  6. use crmeb\traits\ModelTrait;
  7. use app\admin\model\order\StoreOrder;
  8. use app\admin\model\store\StoreCategory as CategoryModel;
  9. use app\admin\model\ump\{
  10. StoreBargain,
  11. StoreCombination,
  12. StoreSeckill
  13. };
  14. /**
  15. * 产品管理 model
  16. * Class StoreProduct
  17. * @package app\admin\model\store
  18. */
  19. class StoreProduct extends BaseModel
  20. {
  21. /**
  22. * 数据表主键
  23. * @var string
  24. */
  25. protected $pk = 'id';
  26. /**
  27. * 模型名称
  28. * @var string
  29. */
  30. protected $name = 'store_product';
  31. use ModelTrait;
  32. public function getDescriptionAttr($value)
  33. {
  34. return htmlspecialchars_decode($value);
  35. }
  36. /**
  37. * 获取连表Model
  38. * @param $model
  39. * @return object
  40. */
  41. public static function getModelObject($where = [])
  42. {
  43. $model = new self();
  44. if (!empty($where)) {
  45. $type = $where['type'] ?? 0;
  46. switch ((int)$type) {
  47. case 1:
  48. $model = $model->where(['is_show' => 1, 'is_del' => 0]);
  49. break;
  50. case 2:
  51. $model = $model->where(['is_show' => 0, 'is_del' => 0]);
  52. break;
  53. case 3:
  54. $model = $model->where(['is_del' => 0]);
  55. break;
  56. case 4:
  57. $model = $model->where(['is_del' => 0])->whereIn('id', function ($query) {
  58. $query->name('store_product_attr_value')->where('stock', 0)->where('type', 0)->field('product_id')->select();
  59. })->where(function ($query) {
  60. $query->whereOr('stock', 0);
  61. });
  62. break;
  63. case 5:
  64. $store_stock = sys_config('store_stock');
  65. if ($store_stock < 0) $store_stock = 2;
  66. $model = $model->where(['is_show' => 1, 'is_del' => 0])->where('stock', '<=', $store_stock);
  67. break;
  68. case 6:
  69. $model = $model->where(['is_del' => 1]);
  70. break;
  71. };
  72. if (isset($where['store_name']) && $where['store_name'] != '') {
  73. $model = $model->where('store_name|keyword|id', 'LIKE', "%$where[store_name]%");
  74. }
  75. if (isset($where['cate_id']) && trim($where['cate_id']) != '') {
  76. $model = $model->whereIn('id', function ($query) use ($where) {
  77. $query->name('store_product_cate')->where('cate_id', $where['cate_id'])->field('product_id')->select();
  78. });
  79. }
  80. if (isset($where['order']) && $where['order'] != '') {
  81. $model = $model->order(self::setOrder($where['order']));
  82. } else {
  83. $model = $model->order('sort desc,id desc');
  84. }
  85. }
  86. return $model;
  87. }
  88. /**根据cateid查询产品 拼sql语句
  89. * @param $cateid
  90. * @return string
  91. */
  92. protected static function getCateSql($cateid)
  93. {
  94. $lcateid = $cateid . ',%'; //匹配最前面的cateid
  95. $ccatid = '%,' . $cateid . ',%'; //匹配中间的cateid
  96. $ratidid = '%,' . $cateid; //匹配后面的cateid
  97. return " `cate_id` LIKE '$lcateid' OR `cate_id` LIKE '$ccatid' OR `cate_id` LIKE '$ratidid' OR `cate_id`=$cateid";
  98. }
  99. /** 如果有子分类查询子分类获取拼接查询sql
  100. * @param $cateid
  101. * @return string
  102. */
  103. protected static function getPidSql($cateid)
  104. {
  105. $sql = self::getCateSql($cateid);
  106. $ids = CategoryModel::where('pid', $cateid)->column('id', 'id');
  107. //查询如果有子分类获取子分类查询sql语句
  108. if ($ids) foreach ($ids as $v) $sql .= " OR " . self::getcatesql($v);
  109. return $sql;
  110. }
  111. /**
  112. * 获取产品列表
  113. * @param $where
  114. * @return array
  115. */
  116. public static function ProductList($where)
  117. {
  118. $model = self::getModelObject($where);
  119. if ($where['excel'] == 0) $model = $model->page((int)$where['page'], (int)$where['limit']);
  120. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  121. foreach ($data as &$item) {
  122. $cateName = CategoryModel::where('id', 'IN', $item['cate_id'])->column('cate_name', 'id');
  123. $item['cate_name'] = is_array($cateName) ? implode(',', $cateName) : '';
  124. $item['collect'] = StoreProductRelation::where('product_id', $item['id'])->where('type', 'collect')->count(); //收藏
  125. $item['like'] = StoreProductRelation::where('product_id', $item['id'])->where('type', 'like')->count(); //点赞
  126. $item['stock'] = self::getStock($item['id']) > 0 ? self::getStock($item['id']) : $item['stock']; //库存
  127. $item['stock_attr'] = self::getStock($item['id']) > 0 ? true : false; //库存
  128. $item['sales_attr'] = self::getSales($item['id']); //属性销量
  129. $item['visitor'] = StoreVisit::where('product_id', $item['id'])->where('product_type', 'product')->count();
  130. }
  131. unset($item);
  132. if ($where['excel'] == 1) {
  133. $export = [];
  134. foreach ($data as $index => $item) {
  135. $export[] = [
  136. $item['store_name'],
  137. $item['store_info'],
  138. $item['cate_name'],
  139. '¥' . $item['price'],
  140. $item['stock'],
  141. $item['sales'],
  142. $item['like'],
  143. $item['collect']
  144. ];
  145. }
  146. PHPExcelService::setExcelHeader(['产品名称', '产品简介', '产品分类', '价格', '库存', '销量', '点赞人数', '收藏人数'])
  147. ->setExcelTile('产品导出', '产品信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  148. ->setExcelContent($export)
  149. ->ExcelSave();
  150. }
  151. $count = self::getModelObject($where)->count();
  152. return compact('count', 'data');
  153. }
  154. public static function getChatrdata($type, $data)
  155. {
  156. $legdata = ['销量', '数量', '点赞', '收藏'];
  157. $model = self::setWhereType(self::order('un_time asc,id desc'), $type);
  158. $list = self::getModelTime(compact('data'), $model)
  159. ->field('FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time,count(id) as count,sum(sales) as sales')
  160. ->group('un_time')
  161. ->distinct(true)
  162. ->select()
  163. ->each(function ($item) use ($data) {
  164. $item['collect'] = self::getModelTime(compact('data'), new StoreProductRelation)->where('type', 'collect')->count();
  165. $item['like'] = self::getModelTime(compact('data'), new StoreProductRelation)->where('type', 'like')->count();
  166. })->toArray();
  167. $chatrList = [];
  168. $datetime = [];
  169. $data_item = [];
  170. $itemList = [0 => [], 1 => [], 2 => [], 3 => []];
  171. foreach ($list as $item) {
  172. $itemList[0][] = $item['sales'];
  173. $itemList[1][] = $item['count'];
  174. $itemList[2][] = $item['like'];
  175. $itemList[3][] = $item['collect'];
  176. array_push($datetime, $item['un_time']);
  177. }
  178. foreach ($legdata as $key => $leg) {
  179. $data_item['name'] = $leg;
  180. $data_item['type'] = 'line';
  181. $data_item['data'] = $itemList[$key];
  182. $chatrList[] = $data_item;
  183. unset($data_item);
  184. }
  185. unset($leg);
  186. $badge = self::getbadge(compact('data'), $type);
  187. $count = self::setWhereType(self::getModelTime(compact('data'), new self()), $type)->count();
  188. return compact('datetime', 'chatrList', 'legdata', 'badge', 'count');
  189. }
  190. //获取 badge 内容
  191. public static function getbadge($where, $type)
  192. {
  193. $replenishment_num = sys_config('replenishment_num');
  194. $replenishment_num = $replenishment_num > 0 ? $replenishment_num : 20;
  195. $sum = [];
  196. $lack = 0;
  197. //获取普通产品缺货
  198. $stock1 = self::getModelTime($where, new self())->where('stock', '<', $replenishment_num)->column('stock', 'id');
  199. $sum_stock = self::where('stock', '<', $replenishment_num)->column('stock', 'id');
  200. $stk = [];
  201. foreach ($stock1 as $item) {
  202. $stk[] = $replenishment_num - $item;
  203. }
  204. $lack = bcadd($lack, array_sum($stk), 0);
  205. foreach ($sum_stock as $val) {
  206. $sum[] = $replenishment_num - $val;
  207. }
  208. unset($stk, $sum_stock, $stock1);
  209. //获取砍价缺货产品
  210. $stock1 = self::getModelTime($where, new StoreBargain())->where('stock', '<', $replenishment_num)->column('stock', 'id');
  211. $sum_stock = StoreBargain::where('stock', '<', $replenishment_num)->column('stock', 'id');
  212. $stk = [];
  213. foreach ($stock1 as $item) {
  214. $stk[] = $replenishment_num - $item;
  215. }
  216. $lack = bcadd($lack, array_sum($stk), 0);
  217. foreach ($sum_stock as $val) {
  218. $sum[] = $replenishment_num - $val;
  219. }
  220. unset($stk, $sum_stock, $stock1);
  221. //获取拼团缺货产品
  222. $stock1 = self::getModelTime($where, new StoreCombination())->where('stock', '<', $replenishment_num)->column('stock', 'id');
  223. $sum_stock = StoreCombination::where('stock', '<', $replenishment_num)->column('stock', 'id');
  224. $stk = [];
  225. foreach ($stock1 as $item) {
  226. $stk[] = $replenishment_num - $item;
  227. }
  228. $lack = bcadd($lack, array_sum($stk), 0);
  229. foreach ($sum_stock as $val) {
  230. $sum[] = $replenishment_num - $val;
  231. }
  232. unset($stk, $sum_stock, $stock1);
  233. return [
  234. [
  235. 'name' => '商品种类',
  236. 'field' => '件',
  237. 'count' => self::setWhereType(new self(), $type)->where('add_time', '<', mktime(0, 0, 0, date('m'), date('d'), date('Y')))->count(),
  238. 'content' => '商品数量总数',
  239. 'background_color' => 'layui-bg-blue',
  240. 'sum' => self::count(),
  241. 'class' => 'fa fa fa-ioxhost',
  242. ],
  243. [
  244. 'name' => '商品总数',
  245. 'field' => '件',
  246. 'count' => self::setWhereType(self::getModelTime($where, new self), $type)->sum('stock'),
  247. 'content' => '商品总数',
  248. 'background_color' => 'layui-bg-cyan',
  249. 'sum' => self::where('is_new', 1)->sum('stock'),
  250. 'class' => 'fa fa-line-chart',
  251. ],
  252. [
  253. 'name' => '活动商品',
  254. 'field' => '件',
  255. 'count' => self::getActivityProductSum($where),
  256. 'content' => '活动商品总数',
  257. 'background_color' => 'layui-bg-green',
  258. 'sum' => self::getActivityProductSum(),
  259. 'class' => 'fa fa-bar-chart',
  260. ],
  261. [
  262. 'name' => '缺货商品',
  263. 'field' => '件',
  264. 'count' => $lack,
  265. 'content' => '总商品数量',
  266. 'background_color' => 'layui-bg-orange',
  267. 'sum' => array_sum($sum),
  268. 'class' => 'fa fa-cube',
  269. ],
  270. ];
  271. }
  272. /*
  273. * 获取活动产品总和
  274. * @param array $where 查询条件
  275. * */
  276. public static function getActivityProductSum($where = false)
  277. {
  278. if ($where) {
  279. $bargain = self::getModelTime($where, new StoreBargain())->sum('stock');
  280. $pink = self::getModelTime($where, new StoreCombination())->sum('stock');
  281. $seckill = self::getModelTime($where, new StoreSeckill())->sum('stock');
  282. } else {
  283. $bargain = StoreBargain::sum('stock');
  284. $pink = StoreCombination::sum('stock');
  285. $seckill = StoreSeckill::sum('stock');
  286. }
  287. return bcadd(bcadd($bargain, $pink, 0), $seckill, 0);
  288. }
  289. public static function setWhereType($model, $type)
  290. {
  291. switch ($type) {
  292. case 1:
  293. $data = ['is_show' => 1, 'is_del' => 0];
  294. break;
  295. case 2:
  296. $data = ['is_show' => 0, 'is_del' => 0];
  297. break;
  298. case 3:
  299. $data = ['is_del' => 0];
  300. break;
  301. case 4:
  302. $data = ['is_show' => 1, 'is_del' => 0, 'stock' => 0];
  303. break;
  304. case 5:
  305. $data = ['is_show' => 1, 'is_del' => 0, 'stock' => ['elt', 1]];
  306. break;
  307. case 6:
  308. $data = ['is_del' => 1];
  309. break;
  310. }
  311. if (isset($data)) $model = $model->where($data);
  312. return $model;
  313. }
  314. /*
  315. * layui-bg-red 红 layui-bg-orange 黄 layui-bg-green 绿 layui-bg-blue 蓝 layui-bg-cyan 黑
  316. * 销量排行 top 10
  317. */
  318. public static function getMaxList($where)
  319. {
  320. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  321. $model = StoreOrder::alias('a')->join('StoreOrderCartInfo c', 'a.id=c.oid')->join('store_product b', 'b.id=c.product_id');
  322. $list = self::getModelTime($where, $model, 'a.add_time')
  323. ->group('c.product_id')
  324. ->order('p_count desc')
  325. ->limit(10)
  326. ->field(['count(c.product_id) as p_count', 'b.store_name', 'sum(b.price) as sum_price'])
  327. ->select();
  328. if (count($list)) $list = $list->toArray();
  329. $maxList = [];
  330. $sum_count = 0;
  331. $sum_price = 0;
  332. foreach ($list as $item) {
  333. $sum_count += $item['p_count'];
  334. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  335. }
  336. unset($item);
  337. foreach ($list as $key => &$item) {
  338. $item['w'] = bcdiv($item['p_count'], $sum_count, 2) * 100;
  339. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  340. $item['store_name'] = self::getSubstrUTf8($item['store_name']);
  341. }
  342. $maxList['sum_count'] = $sum_count;
  343. $maxList['sum_price'] = $sum_price;
  344. $maxList['list'] = $list;
  345. return $maxList;
  346. }
  347. //获取利润
  348. public static function ProfityTop10($where)
  349. {
  350. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  351. $model = StoreOrder::alias('a')
  352. ->join('StoreOrderCartInfo c', 'a.id=c.oid')
  353. ->join('store_product b', 'b.id=c.product_id')
  354. ->where('b.is_show', 1)
  355. ->where('b.is_del', 0);
  356. $list = self::getModelTime($where, $model, 'a.add_time')
  357. ->group('c.product_id')
  358. ->order('profity desc')
  359. ->limit(10)
  360. ->field(['count(c.product_id) as p_count', 'b.store_name', 'sum(b.price) as sum_price', '(b.price-b.cost) as profity'])
  361. ->select();
  362. if (count($list)) $list = $list->toArray();
  363. $maxList = [];
  364. $sum_count = 0;
  365. $sum_price = 0;
  366. foreach ($list as $item) {
  367. $sum_count += $item['p_count'];
  368. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  369. }
  370. foreach ($list as $key => &$item) {
  371. $item['w'] = bcdiv($item['sum_price'], $sum_price, 2) * 100;
  372. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  373. $item['store_name'] = self::getSubstrUTf8($item['store_name'], 30);
  374. }
  375. $maxList['sum_count'] = $sum_count;
  376. $maxList['sum_price'] = $sum_price;
  377. $maxList['list'] = $list;
  378. return $maxList;
  379. }
  380. //获取缺货
  381. public static function getLackList($where)
  382. {
  383. $replenishment_num = sys_config('replenishment_num');
  384. $replenishment_num = $replenishment_num > 0 ? $replenishment_num : 20;
  385. $list = self::where('stock', '<', $replenishment_num)->field(['id', 'store_name', 'stock', 'price'])->page((int)$where['page'], (int)$where['limit'])->order('stock asc')->select();
  386. if (count($list)) $list = $list->toArray();
  387. $count = self::where('stock', '<', $replenishment_num)->count();
  388. return ['count' => $count, 'data' => $list];
  389. }
  390. //获取差评
  391. public static function getnegativelist($where)
  392. {
  393. $list = self::alias('s')->join('StoreProductReply r', 's.id=r.product_id')
  394. ->field('s.id,s.store_name,s.price,count(r.product_id) as count')
  395. ->page((int)$where['page'], (int)$where['limit'])
  396. ->where('r.product_score', 1)
  397. ->order('count desc')
  398. ->group('r.product_id')
  399. ->select();
  400. if (count($list)) $list = $list->toArray();
  401. $count = self::alias('s')->join('StoreProductReply r', 's.id=r.product_id')->group('r.product_id')->where('r.product_score', 1)->count();
  402. return ['count' => $count, 'data' => $list];
  403. }
  404. public static function TuiProductList()
  405. {
  406. $perd = StoreOrder::alias('s')->join('StoreOrderCartInfo c', 's.id=c.oid')
  407. ->field('count(c.product_id) as count,c.product_id as id')
  408. ->group('c.product_id')
  409. ->where('s.status', -1)
  410. ->order('count desc')
  411. ->limit(10)
  412. ->select();
  413. if (count($perd)) $perd = $perd->toArray();
  414. foreach ($perd as &$item) {
  415. $item['store_name'] = self::where(['id' => $item['id']])->value('store_name');
  416. $item['price'] = self::where(['id' => $item['id']])->value('price');
  417. }
  418. return $perd;
  419. }
  420. //编辑库存
  421. public static function changeStock($stock, $productId)
  422. {
  423. return self::edit(compact('stock'), $productId);
  424. }
  425. //获取库存数量
  426. public static function getStock($productId)
  427. {
  428. return StoreProductAttrValue::where(['product_id' => $productId, 'type' => 0])->sum('stock');
  429. }
  430. //获取总销量
  431. public static function getSales($productId)
  432. {
  433. return StoreProductAttrValue::where(['product_id' => $productId, 'type' => 0])->sum('sales');
  434. }
  435. public static function getTierList($model = null)
  436. {
  437. if ($model === null) $model = new self();
  438. return $model->field('id,store_name')->where('is_del', 0)->select()->toArray();
  439. }
  440. /**
  441. * 设置查询条件
  442. * @param array $where
  443. * @return object
  444. */
  445. public static function setWhere($where)
  446. {
  447. $time['data'] = '';
  448. if (isset($where['start_time']) && $where['start_time'] != '' && isset($where['end_time']) && $where['end_time'] != '') {
  449. $time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
  450. } else {
  451. $time['data'] = isset($where['data']) ? $where['data'] : '';
  452. }
  453. $model = self::getModelTime($time, StoreCart::alias('a')->join('store_product b', 'a.product_id=b.id'), 'a.add_time');
  454. if (isset($where['title']) && $where['title'] != '') {
  455. $model = $model->where('b.store_name|b.id', 'like', "%$where[title]%");
  456. }
  457. return $model;
  458. }
  459. /**
  460. * 获取真实销量排行
  461. * @param array $where
  462. * @return array
  463. */
  464. public static function getSaleslists($where)
  465. {
  466. $data = self::setWhere($where)
  467. ->where('a.is_pay', 1)
  468. ->group('a.product_id')
  469. ->field([
  470. 'sum(a.cart_num) * b.price as sum_price', 'sum(a.cart_num) as num_product', 'b.store_name',
  471. 'b.image', 'b.price', 'b.id'
  472. ])
  473. ->order('num_product desc')
  474. ->whereIn('a.product_id', function ($query) {
  475. $query->name('store_order_cart_info')->alias('k')->join('store_order q', 'q.id = k.oid')
  476. ->where(['q.paid' => 1, 'q.refund_status' => 0])->field('k.product_id')->select();
  477. })
  478. ->page((int)$where['page'], (int)$where['limit'])
  479. ->select();
  480. $count = self::setWhere($where)->where('a.is_pay', 1)->group('a.product_id')->count();
  481. $data = count($data) ? $data->toArray() : [];
  482. return compact('data', 'count');
  483. }
  484. public static function SaveProductExport($where)
  485. {
  486. $list = self::setWhere($where)
  487. ->where('a.is_pay', 1)
  488. ->field(['sum(a.cart_num) as num_product', 'b.store_name', 'b.image', 'b.price', 'b.id'])
  489. ->order('num_product desc')
  490. ->group('a.product_id')
  491. ->select();
  492. $export = [];
  493. foreach ($list as $item) {
  494. $export[] = [
  495. $item['id'],
  496. $item['store_name'],
  497. $item['price'],
  498. bcmul($item['num_product'], $item['price'], 2),
  499. $item['num_product'],
  500. ];
  501. }
  502. PHPExcelService::setExcelHeader(['商品编号', '商品名称', '商品售价', '销售额', '销量'])
  503. ->setExcelTile('产品销量排行', '产品销量排行', ' 生成时间:' . date('Y-m-d H:i:s', time()))
  504. ->setExcelContent($export)
  505. ->ExcelSave();
  506. }
  507. /*
  508. * 单个商品详情的头部查询
  509. * $id 商品id
  510. * $where 条件
  511. */
  512. public static function getProductBadgeList($id, $where)
  513. {
  514. $data['data'] = $where;
  515. $list = self::setWhere($data)
  516. ->field(['sum(a.cart_num) as num_product', 'b.id', 'b.price'])
  517. ->where('a.is_pay', 1)
  518. ->group('a.product_id')
  519. ->order('num_product desc')
  520. ->select();
  521. //排名
  522. $ranking = 0;
  523. //销量
  524. $xiaoliang = 0;
  525. //销售额 数组
  526. $list_price = [];
  527. foreach ($list as $key => $item) {
  528. if ($item['id'] == $id) {
  529. $ranking = $key + 1;
  530. $xiaoliang = $item['num_product'];
  531. }
  532. $value['sum_price'] = $item['price'] * $item['num_product'];
  533. $value['id'] = $item['id'];
  534. $list_price[] = $value;
  535. }
  536. //排序
  537. $list_price = self::my_sort($list_price, 'sum_price', SORT_DESC);
  538. //销售额排名
  539. $rank_price = 0;
  540. //当前销售额
  541. $num_price = 0;
  542. if ($list_price !== false && is_array($list_price)) {
  543. foreach ($list_price as $key => $item) {
  544. if ($item['id'] == $id) {
  545. $num_price = $item['sum_price'];
  546. $rank_price = $key + 1;
  547. continue;
  548. }
  549. }
  550. }
  551. return [
  552. [
  553. 'name' => '销售额排名',
  554. 'field' => '名',
  555. 'count' => $rank_price,
  556. 'background_color' => 'layui-bg-blue',
  557. ],
  558. [
  559. 'name' => '销量排名',
  560. 'field' => '名',
  561. 'count' => $ranking,
  562. 'background_color' => 'layui-bg-blue',
  563. ],
  564. [
  565. 'name' => '商品销量',
  566. 'field' => '名',
  567. 'count' => $xiaoliang,
  568. 'background_color' => 'layui-bg-blue',
  569. ],
  570. [
  571. 'name' => '点赞次数',
  572. 'field' => '个',
  573. 'count' => StoreProductRelation::where('product_id', $id)->where('type', 'like')->count(),
  574. 'background_color' => 'layui-bg-blue',
  575. ],
  576. [
  577. 'name' => '销售总额',
  578. 'field' => '元',
  579. 'count' => $num_price,
  580. 'background_color' => 'layui-bg-blue',
  581. 'col' => 12,
  582. ],
  583. ];
  584. }
  585. /*
  586. * 处理二维数组排序
  587. * $arrays 需要处理的数组
  588. * $sort_key 需要处理的key名
  589. * $sort_order 排序方式
  590. * $sort_type 类型 可不填写
  591. */
  592. public static function my_sort($arrays, $sort_key, $sort_order = SORT_ASC, $sort_type = SORT_NUMERIC)
  593. {
  594. if (is_array($arrays)) {
  595. foreach ($arrays as $array) {
  596. if (is_array($array)) {
  597. $key_arrays[] = $array[$sort_key];
  598. } else {
  599. return false;
  600. }
  601. }
  602. }
  603. if (isset($key_arrays)) {
  604. array_multisort($key_arrays, $sort_order, $sort_type, $arrays);
  605. return $arrays;
  606. }
  607. return false;
  608. }
  609. /*
  610. * 查询单个商品的销量曲线图
  611. *
  612. */
  613. public static function getProductCurve($where)
  614. {
  615. $list = self::setWhere($where)
  616. ->where('a.product_id', $where['id'])
  617. ->where('a.is_pay', 1)
  618. ->field(['FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as _add_time', 'sum(a.cart_num) as num'])
  619. ->group('_add_time')
  620. ->order('_add_time asc')
  621. ->select();
  622. $seriesdata = [];
  623. $date = [];
  624. $zoom = '';
  625. foreach ($list as $item) {
  626. $date[] = $item['_add_time'];
  627. $seriesdata[] = $item['num'];
  628. }
  629. if (count($date) > $where['limit']) $zoom = $date[$where['limit'] - 5];
  630. return compact('seriesdata', 'date', 'zoom');
  631. }
  632. /*
  633. * 查询单个商品的销售列表
  634. *
  635. */
  636. public static function getSalelList($where)
  637. {
  638. return self::setWhere($where)
  639. ->where('a.product_id', $where['id'])
  640. ->where('a.is_pay', 1)
  641. ->join('user c', 'c.uid=a.uid')
  642. ->field(['FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as _add_time', 'c.nickname', 'b.price', 'a.id', 'a.cart_num as num'])
  643. ->page((int)$where['page'], (int)$where['limit'])
  644. ->select();
  645. }
  646. /**
  647. * TODO 获取某个字段值
  648. * @param $id
  649. * @param string $field
  650. * @return mixed
  651. */
  652. public static function getProductField($id, $field = 'store_name')
  653. {
  654. return self::where('id', $id)->value($field);
  655. }
  656. public static function getProductProvider($id)
  657. {
  658. return self::alias('p')->join('store_product_provider s', 'p.provider_id=s.id')
  659. ->where('p.id', $id)->field('p.store_name, p.sources, s.*')->find();
  660. }
  661. /**
  662. * 获取库存低于 @stock 的商品数量
  663. */
  664. public static function getProductNumStockLessThan(int $stock): int
  665. {
  666. return self::where('stock', '<=', $stock)->where('is_show', 1)->where('is_del', 0)->count();
  667. }
  668. }