StoreProduct.php 28 KB

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