StoreProduct.php 27 KB

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