StoreProduct.php 28 KB

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