StoreCombination.php 18 KB

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