StoreProduct.php 28 KB

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