StoreProduct.php 26 KB

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