StoreProduct.php 28 KB

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