StoreProduct.php 28 KB

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