StoreProduct.php 27 KB

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