StoreCombination.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/11
  5. */
  6. namespace app\admin\model\ump;
  7. use app\admin\model\store\StoreVisit;
  8. use crmeb\traits\ModelTrait;
  9. use crmeb\basic\BaseModel;
  10. use app\admin\model\store\StoreProductRelation;
  11. use app\admin\model\order\StoreOrder;
  12. use crmeb\services\PHPExcelService;
  13. /**
  14. * 拼团model
  15. * Class StoreCombination
  16. * @package app\admin\model\store
  17. */
  18. class StoreCombination extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'id';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'store_combination';
  30. use ModelTrait;
  31. public function getDescriptionAttr($value)
  32. {
  33. return htmlspecialchars_decode($value);
  34. }
  35. /**
  36. * 设置拼团 where 条件
  37. * @param $where
  38. * @param null $model
  39. * @return mixed
  40. */
  41. public static function setWhere($where, $model = null)
  42. {
  43. $model = $model === null ? new self() : $model;
  44. $model = $model->alias('c');
  45. $model = $model->field('c.*,p.store_name,p.price as ot_price');
  46. $model = $model->join('StoreProduct p', 'p.id=c.product_id', 'LEFT');
  47. if (isset($where['is_show']) && $where['is_show'] != '') $model = $model->where('c.is_show', $where['is_show']);
  48. if (isset($where['is_host']) && $where['is_host'] != '') $model = $model->where('c.is_host', $where['is_host']);
  49. if (isset($where['store_name']) && $where['store_name'] != '') $model = $model->where('p.store_name|p.id|c.id|c.title', 'LIKE', "%$where[store_name]%");
  50. return $model->order('c.id desc')->where('c.is_del', 0);
  51. }
  52. /**
  53. * @param $where
  54. * @return array
  55. */
  56. public static function systemPage($where)
  57. {
  58. $model = self::setWhere($where)->limit(bcmul($where['page'], $where['limit'], 0), $where['limit']);
  59. return self::page($model, function ($item) {
  60. $item['count_people_all'] = StorePink::getCountPeopleAll($item['id']);//参与人数
  61. $item['count_people_pink'] = StorePink::getCountPeoplePink($item['id']);//成团人数
  62. $item['count_people_browse'] = self::getVisitPeople($item['id']);//访问人数
  63. }, $where, $where['limit']);
  64. }
  65. /**
  66. * 导出EXCEL表格,并下载
  67. * @param $where
  68. */
  69. public static function SaveExcel($where)
  70. {
  71. $list = self::setWhere($where)->select();
  72. count($list) && $list = $list->toArray();
  73. $excel = [];
  74. foreach ($list as $item) {
  75. $item['count_people_all'] = StorePink::getCountPeopleAll($item['id']);//参与人数
  76. $item['count_people_pink'] = StorePink::getCountPeoplePink($item['id']);//成团人数
  77. $item['count_people_browse'] = self::getVisitPeople($item['id']);//访问人数
  78. $item['_stop_time'] = date('Y/m/d H:i:s', $item['stop_time']);
  79. $excel[] = [
  80. $item['id'],
  81. $item['title'],
  82. $item['ot_price'],
  83. $item['price'],
  84. $item['stock'],
  85. $item['people'],
  86. $item['count_people_browse'],
  87. $item['browse'],
  88. $item['count_people_all'],
  89. $item['count_people_pink'],
  90. $item['browse'],
  91. $item['is_show'],
  92. $item['_stop_time']
  93. ];
  94. }
  95. PHPExcelService::setExcelHeader(['编号', '拼团名称', '原价', '拼团价', '库存', '拼团人数', '访客人数', '展现量', '参与人数', '成团数量', '浏览量', '产品状态', '结束时间'])
  96. ->setExcelTile('拼团产品导出', ' ', ' 生成时间:' . date('Y-m-d H:i:s', time()))
  97. ->setExcelContent($excel)
  98. ->ExcelSave();
  99. }
  100. /**
  101. * 获取查看拼团产品人数
  102. * @param int $combinationId
  103. * @param string $productType
  104. * @return mixed
  105. */
  106. public static function getVisitPeople($combinationId = 0, $productType = 'combination')
  107. {
  108. $model = new StoreVisit();
  109. $model = $model->where('product_id', $combinationId);
  110. $model = $model->where('product_type', $productType);
  111. return $model->count();
  112. }
  113. /**
  114. * 获取查看拼团统计
  115. * @return array
  116. * @throws \think\db\exception\DataNotFoundException
  117. * @throws \think\db\exception\ModelNotFoundException
  118. * @throws \think\exception\DbException
  119. */
  120. public static function getStatistics()
  121. {
  122. $statistics = [];
  123. $statistics['browseCount'] = self::value('sum(browse) as browse');//总展现量
  124. $statistics['browseCount'] = $statistics['browseCount'] ? $statistics['browseCount'] : 0;
  125. $statistics['visitCount'] = StoreVisit::where('product_type', 'combination')->count();//访客人数
  126. $statistics['partakeCount'] = StorePink::getCountPeopleAll();//参与人数
  127. $statistics['pinkCount'] = StorePink::getCountPeoplePink();//成团数量
  128. return compact('statistics');
  129. }
  130. /**
  131. * 获取拼团总数
  132. * @return int|string
  133. */
  134. public static function getCombinationCount()
  135. {
  136. return self::where('is_del', 0)->count();
  137. }
  138. /**
  139. * 获取拼团产品ID
  140. * @return array
  141. */
  142. public static function getCombinationIdAll()
  143. {
  144. return self::where('is_del', 0)->column('id', 'id');
  145. }
  146. /**
  147. * 获取所有拼团数据
  148. * @param int $limit
  149. * @param int $length
  150. * @return array
  151. */
  152. public static function getAll($limit = 0, $length = 0)
  153. {
  154. $model = new self();
  155. $model = $model->alias('c');
  156. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  157. $model = $model->field('c.*,s.price as product_price');
  158. $model = $model->order('c.sort desc,c.id desc');
  159. $model = $model->where('c.is_show', 1);
  160. $model = $model->where('c.is_del', 0);
  161. $model = $model->where('c.start_time', '<', time());
  162. $model = $model->where('c.stop_time', '>', time());
  163. if ($limit && $length) $model = $model->limit($limit, $length);
  164. $list = $model->select();
  165. if ($list) return $list->toArray();
  166. else return [];
  167. }
  168. /**
  169. * 获取一条拼团数据
  170. * @param $id
  171. * @return mixed
  172. */
  173. public static function getCombinationOne($id)
  174. {
  175. $model = new self();
  176. $model = $model->alias('c');
  177. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  178. $model = $model->field('c.*,s.price as product_price');
  179. $model = $model->where('c.is_show', 1);
  180. $model = $model->where('c.is_del', 0);
  181. $model = $model->where('c.id', $id);
  182. $model = $model->where('c.start_time', '<', time());
  183. $model = $model->where('c.stop_time', '>', time() - 'c.effective_time');
  184. $list = $model->find();
  185. if ($list) return $list->toArray();
  186. else return [];
  187. }
  188. /**
  189. * 获取推荐的拼团产品 移动到公众号
  190. * @return mixed
  191. */
  192. public static function getCombinationHost($limit = 0)
  193. {
  194. $model = new self();
  195. $model = $model->alias('c');
  196. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  197. $model = $model->field('c.id,c.image,c.price,c.sales,c.title,c.people,s.price as product_price');
  198. $model = $model->where('c.is_del', 0);
  199. $model = $model->where('c.is_host', 1);
  200. $model = $model->where('c.is_host', 1);
  201. $model = $model->where('c.start_time', '<', time());
  202. $model = $model->where('c.stop_time', '>', time());
  203. if ($limit) $model = $model->limit($limit);
  204. $list = $model->select();
  205. if ($list) return $list->toArray();
  206. else return [];
  207. }
  208. /**
  209. * 判断库存是否足够 移动到小程序
  210. * @param $id
  211. * @param $cart_num
  212. * @return int|mixed
  213. */
  214. public static function getCombinationStock($id, $cart_num)
  215. {
  216. $stock = self::where('id', $id)->value('stock');
  217. return $stock > $cart_num ? $stock : 0;
  218. }
  219. /**
  220. * 获取产品状态 移动到小程序 移动到公众号
  221. * @param $id
  222. * @return mixed
  223. */
  224. public static function isValidCombination($id)
  225. {
  226. $model = new self();
  227. $model = $model->where('id', $id);
  228. $model = $model->where('is_del', 0);
  229. $model = $model->where('is_show', 1);
  230. return $model->count();
  231. }
  232. /**
  233. * 修改销量和库存 移动到小程序 移动到公众号
  234. * @param $num
  235. * @param $CombinationId
  236. * @return bool
  237. */
  238. public static function decCombinationStock($num, $CombinationId)
  239. {
  240. $res = false !== self::where('id', $CombinationId)->dec('stock', $num)->inc('sales', $num)->update();
  241. return $res;
  242. }
  243. /**
  244. * 拼团产品过滤条件
  245. * @param $model
  246. * @param $type
  247. * @return mixed
  248. */
  249. public static function setWhereType($model, $type, $alt = '')
  250. {
  251. switch ($type) {
  252. case 1:
  253. if ($alt)
  254. $data = [$alt . '.is_del' => 1];
  255. else
  256. $data = ['is_del' => 1];
  257. break;
  258. case 2:
  259. if ($alt)
  260. $data = [$alt . '.is_host' => 1];
  261. else
  262. $data = ['is_host' => 1];
  263. break;
  264. case 3:
  265. if ($alt)
  266. $data = [$alt . '.is_show' => 1];
  267. else
  268. $data = ['is_show' => 1];
  269. break;
  270. default:
  271. if ($alt)
  272. $data = [$alt . '.is_show' => 1, $alt . '.is_del' => 0];
  273. else
  274. $data = ['is_show' => 1, 'is_del' => 0];
  275. break;
  276. }
  277. if (isset($data)) $model = $model->where($data);
  278. return $model;
  279. }
  280. /**
  281. * 拼团产品数量
  282. * @param $where
  283. * @param $type
  284. * @return array
  285. */
  286. public static function getbadge($where, $type)
  287. {
  288. $StoreOrderModel = new StoreOrder();
  289. $replenishment_num = (int)sys_config('replenishment_num');
  290. $replenishment_num = $replenishment_num > 0 ? $replenishment_num : 20;
  291. $stock1 = self::getModelTime($where, new self())->where('stock', '<', $replenishment_num)->column('stock', 'id');
  292. $sum_stock = self::where('stock', '<', $replenishment_num)->column('stock', 'id');
  293. $stk = [];
  294. foreach ($stock1 as $item) {
  295. $stk[] = $replenishment_num - $item;
  296. }
  297. $lack = array_sum($stk);
  298. $sum = [];
  299. foreach ($sum_stock as $val) {
  300. $sum[] = $replenishment_num - $val;
  301. }
  302. return [
  303. [
  304. 'name' => '拼团商品种类',
  305. 'field' => '件',
  306. 'count' => self::setWhereType(new self(), $type)->where('add_time', '<', mktime(0, 0, 0, date('m'), date('d'), date('Y')))->count(),
  307. 'content' => '拼团商品种类总数',
  308. 'background_color' => 'layui-bg-blue',
  309. 'sum' => self::where('is_show', 1)->where('is_del', 0)->count(),
  310. 'class' => 'fa fa fa-ioxhost',
  311. ],
  312. [
  313. 'name' => '正在拼团商品',
  314. 'field' => '个',
  315. 'count' => self::setWhereType(self::getModelTime($where, self::alias('a')->join('StoreProduct t', 't.id=a.product_id'), 'a.add_time'), $type, 'a')
  316. ->where('a.start_time', '<', time())
  317. ->where('a.stop_time', '>', time())
  318. ->count(),
  319. 'content' => '正在拼团商品总库存',
  320. 'background_color' => 'layui-bg-cyan',
  321. 'sum' => self::where('a.start_time', '<', time())->alias('a')
  322. ->join('StoreProduct t', 't.id=a.product_id')
  323. ->where('a.stop_time', '>', time())->sum('a.stock'),
  324. 'class' => 'fa fa-line-chart',
  325. ],
  326. [
  327. 'name' => '拼团成功订单',
  328. 'field' => '件',
  329. 'count' => self::getModelTime($where, $StoreOrderModel)->where('combination_id', '<>', 0)->sum('total_num'),
  330. 'content' => '活动商品总数',
  331. 'background_color' => 'layui-bg-green',
  332. 'sum' => $StoreOrderModel->where('combination_id', '<>', 0)->sum('total_num'),
  333. 'class' => 'fa fa-bar-chart',
  334. ],
  335. [
  336. 'name' => '拼团缺货商品',
  337. 'field' => '件',
  338. 'count' => $lack,
  339. 'content' => '总商品数量',
  340. 'background_color' => 'layui-bg-orange',
  341. 'sum' => array_sum($sum),
  342. 'class' => 'fa fa-cube',
  343. ],
  344. ];
  345. }
  346. public static function getChatrdata($type, $data)
  347. {
  348. $legdata = ['销量', '数量', '点赞', '收藏'];
  349. $model = self::order('id desc');
  350. $list = self::getModelTime(compact('data'), $model)
  351. ->field('FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time,count(id) as count,sum(sales) as sales')
  352. ->group('un_time')
  353. ->distinct(true)
  354. ->select()
  355. ->each(function ($item) use ($data) {
  356. $item['collect'] = self::getModelTime(compact('data'), new StoreProductRelation)->where('type', 'collect')->count();
  357. $item['like'] = self::getModelTime(compact('data'), new StoreProductRelation())->where('type', 'like')->count();
  358. })->toArray();
  359. $chatrList = [];
  360. $datetime = [];
  361. $data_item = [];
  362. $itemList = [0 => [], 1 => [], 2 => [], 3 => []];
  363. foreach ($list as $item) {
  364. $itemList[0][] = $item['sales'];
  365. $itemList[1][] = $item['count'];
  366. $itemList[2][] = $item['like'];
  367. $itemList[3][] = $item['collect'];
  368. array_push($datetime, $item['un_time']);
  369. }
  370. foreach ($legdata as $key => $leg) {
  371. $data_item['name'] = $leg;
  372. $data_item['type'] = 'line';
  373. $data_item['data'] = $itemList[$key];
  374. $chatrList[] = $data_item;
  375. unset($data_item);
  376. }
  377. unset($leg);
  378. $badge = self::getbadge(compact('data'), $type);
  379. $count = self::setWhereType(self::getModelTime(compact('data'), new self()), $type)->count();
  380. return compact('datetime', 'chatrList', 'legdata', 'badge', 'count');
  381. }
  382. /**
  383. * 获取拼团利润
  384. * @param $where
  385. * @return array
  386. */
  387. public static function ProfityTop10($where)
  388. {
  389. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  390. $model = StoreOrder::alias('a')->join('store_combination b', 'b.id = a.combination_id')->where('a.paid', 1);
  391. $list = self::getModelTime($where, $model, 'a.add_time')->group('a.seckill_id')->order('profity desc')->limit(10)
  392. ->field('count(a.combination_id) as p_count,b.title as store_name,sum(b.price) as sum_price,(b.price-b.cost) as profity')
  393. ->select();
  394. if (count($list)) $list = $list->toArray();
  395. $maxList = [];
  396. $sum_count = 0;
  397. $sum_price = 0;
  398. foreach ($list as $item) {
  399. $sum_count += $item['p_count'];
  400. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  401. }
  402. foreach ($list as $key => &$item) {
  403. $item['w'] = bcdiv($item['sum_price'], $sum_price, 2) * 100;
  404. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  405. $item['store_name'] = self::getSubstrUTf8($item['store_name'], 30);
  406. }
  407. $maxList['sum_count'] = $sum_count;
  408. $maxList['sum_price'] = $sum_price;
  409. $maxList['list'] = $list;
  410. return $maxList;
  411. }
  412. public static function getMaxList($where)
  413. {
  414. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  415. $model = StoreOrder::alias('a')->join('store_combination b', 'b.id=a.combination_id')->where('a.paid', 1);
  416. $list = self::getModelTime($where, $model, 'a.add_time')->group('a.combination_id')->order('p_count desc')->limit(10)
  417. ->field('count(a.combination_id) as p_count,b.title as store_name,sum(b.price) as sum_price')->select();
  418. if (count($list)) $list = $list->toArray();
  419. $maxList = [];
  420. $sum_count = 0;
  421. $sum_price = 0;
  422. foreach ($list as $item) {
  423. $sum_count += $item['p_count'];
  424. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  425. }
  426. unset($item);
  427. foreach ($list as $key => &$item) {
  428. $item['w'] = bcdiv($item['p_count'], $sum_count, 2) * 100;
  429. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  430. $item['store_name'] = self::getSubstrUTf8($item['store_name']);
  431. }
  432. $maxList['sum_count'] = $sum_count;
  433. $maxList['sum_price'] = $sum_price;
  434. $maxList['list'] = $list;
  435. return $maxList;
  436. }
  437. /**
  438. * 拼团产品退货
  439. * @param array $where
  440. * @return mixed
  441. */
  442. public static function getBargainRefundList($where = [])
  443. {
  444. $model = StoreOrder::alias('a')->join('store_combination b', 'b.id=a.combination_id');
  445. $list = self::getModelTime($where, $model, 'a.add_time')->where('a.refund_status', '<>', 0)->group('a.combination_id')
  446. ->order('count desc')->page((int)$where['page'], (int)$where['limit'])
  447. ->field('count(a.combination_id) as count,b.title as store_name,sum(b.price) as sum_price')
  448. ->select();
  449. if (count($list)) $list = $list->toArray();
  450. return $list;
  451. }
  452. /**
  453. * TODO 获取某个字段值
  454. * @param $id
  455. * @param string $field
  456. * @return mixed
  457. */
  458. public static function getCombinationField($id, $field = 'title')
  459. {
  460. return self::where('id', $id)->value($field);
  461. }
  462. }