StoreProduct.php 26 KB

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