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