StoreProduct.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/11
  5. */
  6. namespace app\admin\model\store;
  7. use crmeb\basic\BaseModel;
  8. use app\models\store\StoreCart;
  9. use crmeb\services\PHPExcelService;
  10. use crmeb\traits\ModelTrait;
  11. use app\admin\model\order\StoreOrder;
  12. use app\admin\model\store\StoreCategory as CategoryModel;
  13. use app\admin\model\ump\{
  14. StoreBargain, StoreCombination, StoreSeckill
  15. };
  16. /**
  17. * 产品管理 model
  18. * Class StoreProduct
  19. * @package app\admin\model\store
  20. */
  21. class StoreProduct extends BaseModel
  22. {
  23. /**
  24. * 数据表主键
  25. * @var string
  26. */
  27. protected $pk = 'id';
  28. /**
  29. * 模型名称
  30. * @var string
  31. */
  32. protected $name = 'store_product';
  33. use ModelTrait;
  34. public function getDescriptionAttr($value)
  35. {
  36. return htmlspecialchars_decode($value);
  37. }
  38. /**
  39. * 获取连表MOdel
  40. * @param $model
  41. * @return object
  42. */
  43. public static function getModelObject($where = [])
  44. {
  45. $model = new self();
  46. if (!empty($where)) {
  47. $type = $where['type'] ?? 0;
  48. switch ((int)$type) {
  49. case 1:
  50. $model = $model->where(['is_show' => 1, 'is_del' => 0]);
  51. break;
  52. case 2:
  53. $model = $model->where(['is_show' => 0, 'is_del' => 0]);
  54. break;
  55. case 3:
  56. $model = $model->where(['is_del' => 0]);
  57. break;
  58. case 4:
  59. $model = $model->where(['is_del' => 0])->whereIn('id', function ($query) {
  60. $query->name('store_product_attr_value')->where('stock', 0)->where('type', 0)->field('product_id')->select();
  61. })->where(function ($query) {
  62. $query->whereOr('stock', 0);
  63. });
  64. break;
  65. case 5:
  66. $store_stock = sys_config('store_stock');
  67. if ($store_stock < 0) $store_stock = 2;
  68. $model = $model->where(['is_show' => 1, 'is_del' => 0])->where('stock', '<=', $store_stock);
  69. break;
  70. case 6:
  71. $model = $model->where(['is_del' => 1]);
  72. break;
  73. };
  74. if (isset($where['store_name']) && $where['store_name'] != '') {
  75. $model = $model->where('store_name|keyword|id', 'LIKE', "%$where[store_name]%");
  76. }
  77. if (isset($where['cate_id']) && trim($where['cate_id']) != '') {
  78. $model = $model->whereIn('id', function ($query) use ($where) {
  79. $query->name('store_product_cate')->where('cate_id', $where['cate_id'])->field('product_id')->select();
  80. });
  81. }
  82. if (isset($where['order']) && $where['order'] != '') {
  83. $model = $model->order(self::setOrder($where['order']));
  84. } else {
  85. $model = $model->order('sort desc,id desc');
  86. }
  87. }
  88. return $model;
  89. }
  90. /**根据cateid查询产品 拼sql语句
  91. * @param $cateid
  92. * @return string
  93. */
  94. protected static function getCateSql($cateid)
  95. {
  96. $lcateid = $cateid . ',%';//匹配最前面的cateid
  97. $ccatid = '%,' . $cateid . ',%';//匹配中间的cateid
  98. $ratidid = '%,' . $cateid;//匹配后面的cateid
  99. return " `cate_id` LIKE '$lcateid' OR `cate_id` LIKE '$ccatid' OR `cate_id` LIKE '$ratidid' OR `cate_id`=$cateid";
  100. }
  101. /** 如果有子分类查询子分类获取拼接查询sql
  102. * @param $cateid
  103. * @return string
  104. */
  105. protected static function getPidSql($cateid)
  106. {
  107. $sql = self::getCateSql($cateid);
  108. $ids = CategoryModel::where('pid', $cateid)->column('id', 'id');
  109. //查询如果有子分类获取子分类查询sql语句
  110. if ($ids) foreach ($ids as $v) $sql .= " OR " . self::getcatesql($v);
  111. return $sql;
  112. }
  113. /**
  114. * 获取产品列表
  115. * @param $where
  116. * @return array
  117. */
  118. public static function ProductList($where)
  119. {
  120. $model = self::getModelObject($where);
  121. if ($where['excel'] == 0) $model = $model->page((int)$where['page'], (int)$where['limit']);
  122. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  123. foreach ($data as &$item) {
  124. $cateName = CategoryModel::where('id', 'IN', $item['cate_id'])->column('cate_name', 'id');
  125. $item['cate_name'] = is_array($cateName) ? implode(',', $cateName) : '';
  126. $item['collect'] = StoreProductRelation::where('product_id', $item['id'])->where('type', 'collect')->count();//收藏
  127. $item['like'] = StoreProductRelation::where('product_id', $item['id'])->where('type', 'like')->count();//点赞
  128. $item['stock'] = self::getStock($item['id']) > 0 ? self::getStock($item['id']) : $item['stock'];//库存
  129. $item['stock_attr'] = self::getStock($item['id']) > 0 ? true : false;//库存
  130. $item['sales_attr'] = self::getSales($item['id']);//属性销量
  131. $item['visitor'] = StoreVisit::where('product_id', $item['id'])->where('product_type', 'product')->count();
  132. }
  133. unset($item);
  134. if ($where['excel'] == 1) {
  135. $export = [];
  136. foreach ($data as $index => $item) {
  137. $export[] = [
  138. $item['store_name'],
  139. $item['store_info'],
  140. $item['cate_name'],
  141. '¥' . $item['price'],
  142. $item['stock'],
  143. $item['sales'],
  144. $item['like'],
  145. $item['collect']
  146. ];
  147. }
  148. PHPExcelService::setExcelHeader(['产品名称', '产品简介', '产品分类', '价格', '库存', '销量', '点赞人数', '收藏人数'])
  149. ->setExcelTile('产品导出', '产品信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  150. ->setExcelContent($export)
  151. ->ExcelSave();
  152. }
  153. $count = self::getModelObject($where)->count();
  154. return compact('count', 'data');
  155. }
  156. public static function getChatrdata($type, $data)
  157. {
  158. $legdata = ['销量', '数量', '点赞', '收藏'];
  159. $model = self::setWhereType(self::order('un_time asc,id desc'), $type);
  160. $list = self::getModelTime(compact('data'), $model)
  161. ->field('FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time,count(id) as count,sum(sales) as sales')
  162. ->group('un_time')
  163. ->distinct(true)
  164. ->select()
  165. ->each(function ($item) use ($data) {
  166. $item['collect'] = self::getModelTime(compact('data'), new StoreProductRelation)->where('type', 'collect')->count();
  167. $item['like'] = self::getModelTime(compact('data'), new StoreProductRelation)->where('type', 'like')->count();
  168. })->toArray();
  169. $chatrList = [];
  170. $datetime = [];
  171. $data_item = [];
  172. $itemList = [0 => [], 1 => [], 2 => [], 3 => []];
  173. foreach ($list as $item) {
  174. $itemList[0][] = $item['sales'];
  175. $itemList[1][] = $item['count'];
  176. $itemList[2][] = $item['like'];
  177. $itemList[3][] = $item['collect'];
  178. array_push($datetime, $item['un_time']);
  179. }
  180. foreach ($legdata as $key => $leg) {
  181. $data_item['name'] = $leg;
  182. $data_item['type'] = 'line';
  183. $data_item['data'] = $itemList[$key];
  184. $chatrList[] = $data_item;
  185. unset($data_item);
  186. }
  187. unset($leg);
  188. $badge = self::getbadge(compact('data'), $type);
  189. $count = self::setWhereType(self::getModelTime(compact('data'), new self()), $type)->count();
  190. return compact('datetime', 'chatrList', 'legdata', 'badge', 'count');
  191. }
  192. //获取 badge 内容
  193. public static function getbadge($where, $type)
  194. {
  195. $replenishment_num = sys_config('replenishment_num');
  196. $replenishment_num = $replenishment_num > 0 ? $replenishment_num : 20;
  197. $sum = [];
  198. $lack = 0;
  199. //获取普通产品缺货
  200. $stock1 = self::getModelTime($where, new self())->where('stock', '<', $replenishment_num)->column('stock', 'id');
  201. $sum_stock = self::where('stock', '<', $replenishment_num)->column('stock', 'id');
  202. $stk = [];
  203. foreach ($stock1 as $item) {
  204. $stk[] = $replenishment_num - $item;
  205. }
  206. $lack = bcadd($lack, array_sum($stk), 0);
  207. foreach ($sum_stock as $val) {
  208. $sum[] = $replenishment_num - $val;
  209. }
  210. unset($stk, $sum_stock, $stock1);
  211. //获取砍价缺货产品
  212. $stock1 = self::getModelTime($where, new StoreBargain())->where('stock', '<', $replenishment_num)->column('stock', 'id');
  213. $sum_stock = StoreBargain::where('stock', '<', $replenishment_num)->column('stock', 'id');
  214. $stk = [];
  215. foreach ($stock1 as $item) {
  216. $stk[] = $replenishment_num - $item;
  217. }
  218. $lack = bcadd($lack, array_sum($stk), 0);
  219. foreach ($sum_stock as $val) {
  220. $sum[] = $replenishment_num - $val;
  221. }
  222. unset($stk, $sum_stock, $stock1);
  223. //获取拼团缺货产品
  224. $stock1 = self::getModelTime($where, new StoreCombination())->where('stock', '<', $replenishment_num)->column('stock', 'id');
  225. $sum_stock = StoreCombination::where('stock', '<', $replenishment_num)->column('stock', 'id');
  226. $stk = [];
  227. foreach ($stock1 as $item) {
  228. $stk[] = $replenishment_num - $item;
  229. }
  230. $lack = bcadd($lack, array_sum($stk), 0);
  231. foreach ($sum_stock as $val) {
  232. $sum[] = $replenishment_num - $val;
  233. }
  234. unset($stk, $sum_stock, $stock1);
  235. return [
  236. [
  237. 'name' => '商品种类',
  238. 'field' => '件',
  239. 'count' => self::setWhereType(new self(), $type)->where('add_time', '<', mktime(0, 0, 0, date('m'), date('d'), date('Y')))->count(),
  240. 'content' => '商品数量总数',
  241. 'background_color' => 'layui-bg-blue',
  242. 'sum' => self::count(),
  243. 'class' => 'fa fa fa-ioxhost',
  244. ],
  245. [
  246. 'name' => '商品总数',
  247. 'field' => '件',
  248. 'count' => self::setWhereType(self::getModelTime($where, new self), $type)->sum('stock'),
  249. 'content' => '商品总数',
  250. 'background_color' => 'layui-bg-cyan',
  251. 'sum' => self::where('is_new', 1)->sum('stock'),
  252. 'class' => 'fa fa-line-chart',
  253. ],
  254. [
  255. 'name' => '活动商品',
  256. 'field' => '件',
  257. 'count' => self::getActivityProductSum($where),
  258. 'content' => '活动商品总数',
  259. 'background_color' => 'layui-bg-green',
  260. 'sum' => self::getActivityProductSum(),
  261. 'class' => 'fa fa-bar-chart',
  262. ],
  263. [
  264. 'name' => '缺货商品',
  265. 'field' => '件',
  266. 'count' => $lack,
  267. 'content' => '总商品数量',
  268. 'background_color' => 'layui-bg-orange',
  269. 'sum' => array_sum($sum),
  270. 'class' => 'fa fa-cube',
  271. ],
  272. ];
  273. }
  274. /*
  275. * 获取活动产品总和
  276. * @param array $where 查询条件
  277. * */
  278. public static function getActivityProductSum($where = false)
  279. {
  280. if ($where) {
  281. $bargain = self::getModelTime($where, new StoreBargain())->sum('stock');
  282. $pink = self::getModelTime($where, new StoreCombination())->sum('stock');
  283. $seckill = self::getModelTime($where, new StoreSeckill())->sum('stock');
  284. } else {
  285. $bargain = StoreBargain::sum('stock');
  286. $pink = StoreCombination::sum('stock');
  287. $seckill = StoreSeckill::sum('stock');
  288. }
  289. return bcadd(bcadd($bargain, $pink, 0), $seckill, 0);
  290. }
  291. public static function setWhereType($model, $type)
  292. {
  293. switch ($type) {
  294. case 1:
  295. $data = ['is_show' => 1, 'is_del' => 0];
  296. break;
  297. case 2:
  298. $data = ['is_show' => 0, 'is_del' => 0];
  299. break;
  300. case 3:
  301. $data = ['is_del' => 0];
  302. break;
  303. case 4:
  304. $data = ['is_show' => 1, 'is_del' => 0, 'stock' => 0];
  305. break;
  306. case 5:
  307. $data = ['is_show' => 1, 'is_del' => 0, 'stock' => ['elt', 1]];
  308. break;
  309. case 6:
  310. $data = ['is_del' => 1];
  311. break;
  312. }
  313. if (isset($data)) $model = $model->where($data);
  314. return $model;
  315. }
  316. /*
  317. * layui-bg-red 红 layui-bg-orange 黄 layui-bg-green 绿 layui-bg-blue 蓝 layui-bg-cyan 黑
  318. * 销量排行 top 10
  319. */
  320. public static function getMaxList($where)
  321. {
  322. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  323. $model = StoreOrder::alias('a')->join('StoreOrderCartInfo c', 'a.id=c.oid')->join('store_product b', 'b.id=c.product_id');
  324. $list = self::getModelTime($where, $model, 'a.add_time')
  325. ->group('c.product_id')
  326. ->order('p_count desc')
  327. ->limit(10)
  328. ->field(['count(c.product_id) as p_count', 'b.store_name', 'sum(b.price) as sum_price'])
  329. ->select();
  330. if (count($list)) $list = $list->toArray();
  331. $maxList = [];
  332. $sum_count = 0;
  333. $sum_price = 0;
  334. foreach ($list as $item) {
  335. $sum_count += $item['p_count'];
  336. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  337. }
  338. unset($item);
  339. foreach ($list as $key => &$item) {
  340. $item['w'] = bcdiv($item['p_count'], $sum_count, 2) * 100;
  341. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  342. $item['store_name'] = self::getSubstrUTf8($item['store_name']);
  343. }
  344. $maxList['sum_count'] = $sum_count;
  345. $maxList['sum_price'] = $sum_price;
  346. $maxList['list'] = $list;
  347. return $maxList;
  348. }
  349. //获取利润
  350. public static function ProfityTop10($where)
  351. {
  352. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  353. $model = StoreOrder::alias('a')
  354. ->join('StoreOrderCartInfo c', 'a.id=c.oid')
  355. ->join('store_product b', 'b.id=c.product_id')
  356. ->where('b.is_show', 1)
  357. ->where('b.is_del', 0);
  358. $list = self::getModelTime($where, $model, 'a.add_time')
  359. ->group('c.product_id')
  360. ->order('profity desc')
  361. ->limit(10)
  362. ->field(['count(c.product_id) as p_count', 'b.store_name', 'sum(b.price) as sum_price', '(b.price-b.cost) as profity'])
  363. ->select();
  364. if (count($list)) $list = $list->toArray();
  365. $maxList = [];
  366. $sum_count = 0;
  367. $sum_price = 0;
  368. foreach ($list as $item) {
  369. $sum_count += $item['p_count'];
  370. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  371. }
  372. foreach ($list as $key => &$item) {
  373. $item['w'] = bcdiv($item['sum_price'], $sum_price, 2) * 100;
  374. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  375. $item['store_name'] = self::getSubstrUTf8($item['store_name'], 30);
  376. }
  377. $maxList['sum_count'] = $sum_count;
  378. $maxList['sum_price'] = $sum_price;
  379. $maxList['list'] = $list;
  380. return $maxList;
  381. }
  382. //获取缺货
  383. public static function getLackList($where)
  384. {
  385. $replenishment_num = sys_config('replenishment_num');
  386. $replenishment_num = $replenishment_num > 0 ? $replenishment_num : 20;
  387. $list = self::where('stock', '<', $replenishment_num)->field(['id', 'store_name', 'stock', 'price'])->page((int)$where['page'], (int)$where['limit'])->order('stock asc')->select();
  388. if (count($list)) $list = $list->toArray();
  389. $count = self::where('stock', '<', $replenishment_num)->count();
  390. return ['count' => $count, 'data' => $list];
  391. }
  392. //获取差评
  393. public static function getnegativelist($where)
  394. {
  395. $list = self::alias('s')->join('StoreProductReply r', 's.id=r.product_id')
  396. ->field('s.id,s.store_name,s.price,count(r.product_id) as count')
  397. ->page((int)$where['page'], (int)$where['limit'])
  398. ->where('r.product_score', 1)
  399. ->order('count desc')
  400. ->group('r.product_id')
  401. ->select();
  402. if (count($list)) $list = $list->toArray();
  403. $count = self::alias('s')->join('StoreProductReply r', 's.id=r.product_id')->group('r.product_id')->where('r.product_score', 1)->count();
  404. return ['count' => $count, 'data' => $list];
  405. }
  406. public static function TuiProductList()
  407. {
  408. $perd = StoreOrder::alias('s')->join('StoreOrderCartInfo c', 's.id=c.oid')
  409. ->field('count(c.product_id) as count,c.product_id as id')
  410. ->group('c.product_id')
  411. ->where('s.status', -1)
  412. ->order('count desc')
  413. ->limit(10)
  414. ->select();
  415. if (count($perd)) $perd = $perd->toArray();
  416. foreach ($perd as &$item) {
  417. $item['store_name'] = self::where(['id' => $item['id']])->value('store_name');
  418. $item['price'] = self::where(['id' => $item['id']])->value('price');
  419. }
  420. return $perd;
  421. }
  422. //编辑库存
  423. public static function changeStock($stock, $productId)
  424. {
  425. return self::edit(compact('stock'), $productId);
  426. }
  427. //获取库存数量
  428. public static function getStock($productId)
  429. {
  430. return StoreProductAttrValue::where(['product_id' => $productId, 'type' => 0])->sum('stock');
  431. }
  432. //获取总销量
  433. public static function getSales($productId)
  434. {
  435. return StoreProductAttrValue::where(['product_id' => $productId, 'type' => 0])->sum('sales');
  436. }
  437. public static function getTierList($model = null)
  438. {
  439. if ($model === null) $model = new self();
  440. return $model->field('id,store_name')->where('is_del', 0)->select()->toArray();
  441. }
  442. /**
  443. * 设置查询条件
  444. * @param array $where
  445. * @return array
  446. */
  447. public static function setWhere($where)
  448. {
  449. $time['data'] = '';
  450. if (isset($where['start_time']) && $where['start_time'] != '' && isset($where['end_time']) && $where['end_time'] != '') {
  451. $time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
  452. } else {
  453. $time['data'] = isset($where['data']) ? $where['data'] : '';
  454. }
  455. $model = self::getModelTime($time, StoreCart::alias('a')->join('store_product b', 'a.product_id=b.id'), 'a.add_time');
  456. if (isset($where['title']) && $where['title'] != '') {
  457. $model = $model->where('b.store_name|b.id', 'like', "%$where[title]%");
  458. }
  459. return $model;
  460. }
  461. /**
  462. * 获取真实销量排行
  463. * @param array $where
  464. * @return array
  465. */
  466. public static function getSaleslists($where)
  467. {
  468. $data = self::setWhere($where)
  469. ->where('a.is_pay', 1)
  470. ->group('a.product_id')
  471. ->field(['sum(a.cart_num) * b.price as sum_price', 'sum(a.cart_num) as num_product', 'b.store_name',
  472. 'b.image', 'b.price', 'b.id'])
  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. }