StoreSeckill.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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\order\StoreOrder;
  8. use app\admin\model\store\StoreProductRelation;
  9. use app\admin\model\system\SystemGroupData;
  10. use crmeb\traits\ModelTrait;
  11. use crmeb\basic\BaseModel;
  12. use app\admin\model\store\StoreProduct;
  13. use crmeb\services\PHPExcelService;
  14. /**
  15. * Class StoreSeckill
  16. * @package app\admin\model\store
  17. */
  18. class StoreSeckill extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'id';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'store_seckill';
  30. use ModelTrait;
  31. public function getDescriptionAttr($value)
  32. {
  33. return htmlspecialchars_decode($value);
  34. }
  35. /**
  36. * 秒杀产品过滤条件
  37. * @param $model
  38. * @param $type
  39. * @return mixed
  40. */
  41. public static function setWhereType($model, $type)
  42. {
  43. switch ($type) {
  44. case 1:
  45. $data = ['status' => 0, 'is_del' => 0];
  46. break;
  47. case 2:
  48. $data = ['status' => 1, 'is_del' => 0];
  49. break;
  50. case 3:
  51. $data = ['status' => 1, 'is_del' => 0, 'stock' => 0];
  52. break;
  53. case 4:
  54. $data = ['status' => 1, 'is_del' => 0, 'stock' => ['elt', 1]];
  55. break;
  56. case 5:
  57. $data = ['is_del' => 1];
  58. break;
  59. }
  60. if (isset($data)) $model = $model->where($data);
  61. return $model;
  62. }
  63. /**
  64. * 秒杀产品数量 图标展示
  65. * @param $type
  66. * @param $data
  67. * @return array
  68. */
  69. public static function getChatrdata($type, $data)
  70. {
  71. $legdata = ['销量', '数量', '点赞', '收藏'];
  72. $model = self::setWhereType(self::order('id desc'), $type);
  73. $list = self::getModelTime(compact('data'), $model)
  74. ->field('FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time,count(id) as count,sum(sales) as sales')
  75. ->group('un_time')
  76. ->distinct(true)
  77. ->select()
  78. ->each(function ($item) use ($data) {
  79. $item['collect'] = self::getModelTime(compact('data'), new StoreProductRelation)->where('type', 'collect')->count();
  80. $item['like'] = self::getModelTime(compact('data'), new StoreProductRelation())->where('type', 'like')->count();
  81. })->toArray();
  82. $chatrList = [];
  83. $datetime = [];
  84. $data_item = [];
  85. $itemList = [0 => [], 1 => [], 2 => [], 3 => []];
  86. foreach ($list as $item) {
  87. $itemList[0][] = $item['sales'];
  88. $itemList[1][] = $item['count'];
  89. $itemList[2][] = $item['like'];
  90. $itemList[3][] = $item['collect'];
  91. array_push($datetime, $item['un_time']);
  92. }
  93. foreach ($legdata as $key => $leg) {
  94. $data_item['name'] = $leg;
  95. $data_item['type'] = 'line';
  96. $data_item['data'] = $itemList[$key];
  97. $chatrList[] = $data_item;
  98. unset($data_item);
  99. }
  100. unset($leg);
  101. $badge = self::getbadge(compact('data'), $type);
  102. $count = self::setWhereType(self::getModelTime(compact('data'), new self()), $type)->count();
  103. return compact('datetime', 'chatrList', 'legdata', 'badge', 'count');
  104. }
  105. /**
  106. * 秒杀产品数量
  107. * @param $where
  108. * @param $type
  109. * @return array
  110. */
  111. public static function getbadge($where, $type)
  112. {
  113. $StoreOrderModel = new StoreOrder();
  114. $replenishment_num = sys_config('replenishment_num');
  115. $replenishment_num = $replenishment_num > 0 ? $replenishment_num : 20;
  116. $stock1 = self::getModelTime($where, new self())->where('stock', '<', $replenishment_num)->column('stock', 'id');
  117. $sum_stock = self::where('stock', '<', $replenishment_num)->column('stock', 'id');
  118. $stk = [];
  119. foreach ($stock1 as $item) {
  120. $stk[] = $replenishment_num - $item;
  121. }
  122. $lack = array_sum($stk);
  123. $sum = [];
  124. foreach ($sum_stock as $val) {
  125. $sum[] = $replenishment_num - $val;
  126. }
  127. return [
  128. [
  129. 'name' => '商品种类',
  130. 'field' => '件',
  131. 'count' => self::setWhereType(new self(), $type)->where('add_time', '<', mktime(0, 0, 0, date('m'), date('d'), date('Y')))->count(),
  132. 'content' => '商品种类总数',
  133. 'background_color' => 'layui-bg-blue',
  134. 'sum' => self::count(),
  135. 'class' => 'fa fa fa-ioxhost',
  136. ],
  137. [
  138. 'name' => '新增商品',
  139. 'field' => '件',
  140. 'count' => self::setWhereType(self::getModelTime($where, new self), $type)->sum('stock'),
  141. 'content' => '新增商品总数',
  142. 'background_color' => 'layui-bg-cyan',
  143. 'sum' => self::where('status', 1)->sum('stock'),
  144. 'class' => 'fa fa-line-chart',
  145. ],
  146. [
  147. 'name' => '秒杀成功商品件数',
  148. 'field' => '件',
  149. 'count' => self::getModelTime($where, $StoreOrderModel)->where('seckill_id', '<>', 0)->sum('total_num'),
  150. 'content' => '秒杀成功商品总件数',
  151. 'background_color' => 'layui-bg-green',
  152. 'sum' => $StoreOrderModel->where('seckill_id', '<>', 0)->sum('total_num'),
  153. 'class' => 'fa fa-bar-chart',
  154. ],
  155. [
  156. 'name' => '缺货商品',
  157. 'field' => '件',
  158. 'count' => $lack,
  159. 'content' => '总商品数量',
  160. 'background_color' => 'layui-bg-orange',
  161. 'sum' => array_sum($sum),
  162. 'class' => 'fa fa-cube',
  163. ],
  164. ];
  165. }
  166. /**
  167. * 销量排行 top 10
  168. * layui-bg-red 红 layui-bg-orange 黄 layui-bg-green 绿 layui-bg-blue 蓝 layui-bg-cyan 黑
  169. */
  170. public static function getMaxList($where)
  171. {
  172. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  173. $model = StoreOrder::alias('a')->join('store_seckill b', 'b.id=a.seckill_id')->where('a.paid', 1);
  174. $list = self::getModelTime($where, $model, 'a.add_time')->group('a.seckill_id')->order('p_count desc')->limit(10)
  175. ->field(['count(a.seckill_id) as p_count', 'b.title as store_name', 'sum(b.price) as sum_price'])->select();
  176. if (count($list)) $list = $list->toArray();
  177. $maxList = [];
  178. $sum_count = 0;
  179. $sum_price = 0;
  180. foreach ($list as $item) {
  181. $sum_count += $item['p_count'];
  182. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  183. }
  184. unset($item);
  185. foreach ($list as $key => &$item) {
  186. $item['w'] = bcdiv($item['p_count'], $sum_count, 2) * 100;
  187. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  188. $item['store_name'] = self::getSubstrUTf8($item['store_name']);
  189. }
  190. $maxList['sum_count'] = $sum_count;
  191. $maxList['sum_price'] = $sum_price;
  192. $maxList['list'] = $list;
  193. return $maxList;
  194. }
  195. /**
  196. * 获取秒杀利润
  197. * @param $where
  198. * @return array
  199. */
  200. public static function ProfityTop10($where)
  201. {
  202. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  203. $model = StoreOrder::alias('a')->join('store_seckill b', 'b.id = a.seckill_id')->where('a.paid', 1);
  204. $list = self::getModelTime($where, $model, 'a.add_time')->group('a.seckill_id')->order('profity desc')->limit(10)
  205. ->field(['count(a.seckill_id) as p_count', 'b.title as store_name', 'sum(b.price) as sum_price', '(b.price-b.cost) as profity'])
  206. ->select();
  207. if (count($list)) $list = $list->toArray();
  208. $maxList = [];
  209. $sum_count = 0;
  210. $sum_price = 0;
  211. foreach ($list as $item) {
  212. $sum_count += $item['p_count'];
  213. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  214. }
  215. foreach ($list as $key => &$item) {
  216. $item['w'] = bcdiv($item['sum_price'], $sum_price, 2) * 100;
  217. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  218. $item['store_name'] = self::getSubstrUTf8($item['store_name'], 30);
  219. }
  220. $maxList['sum_count'] = $sum_count;
  221. $maxList['sum_price'] = $sum_price;
  222. $maxList['list'] = $list;
  223. return $maxList;
  224. }
  225. /**
  226. * 获取秒杀缺货
  227. * @param $where
  228. * @return array
  229. */
  230. public static function getLackList($where)
  231. {
  232. $replenishment_num = sys_config('replenishment_num');
  233. $replenishment_num = $replenishment_num > 0 ? $replenishment_num : 20;
  234. $list = self::where('stock', '<', $replenishment_num)->field(['id', 'title as store_name', 'stock', 'price'])->page((int)$where['page'], (int)$where['limit'])->order('stock asc')->select();
  235. if (count($list)) $list = $list->toArray();
  236. $count = self::where('stock', '<', $replenishment_num)->count();
  237. return ['count' => $count, 'data' => $list];
  238. }
  239. /**
  240. * 秒杀产品评价
  241. * @param array $where
  242. * @return array
  243. */
  244. public static function getNegativeList($where = array())
  245. {
  246. $replenishment_num = 3;
  247. return [];
  248. }
  249. /**
  250. * 秒杀产品退货
  251. * @param array $where
  252. * @return mixed
  253. */
  254. public static function getBargainRefundList($where = array())
  255. {
  256. $model = StoreOrder::alias('a')->join('store_seckill b', 'b.id=a.seckill_id');
  257. $list = self::getModelTime($where, $model, 'a.add_time')->where('a.refund_status', '<>', 0)->group('a.seckill_id')->order('count desc')->page((int)$where['page'], (int)$where['limit'])
  258. ->field(['count(a.seckill_id) as count', 'b.title as store_name', 'sum(b.price) as sum_price'])->select();
  259. if (count($list)) $list = $list->toArray();
  260. return $list;
  261. }
  262. /**
  263. * @param $where
  264. * @return array
  265. */
  266. public static function systemPage($where)
  267. {
  268. $model = new self;
  269. $model = $model->alias('s');
  270. // $model = $model->join('StoreProduct p','p.id=s.product_id');
  271. if ($where['status'] != '') $model = $model->where('s.status', $where['status']);
  272. if ($where['store_name'] != '') $model = $model->where('s.title|s.id', 'LIKE', "%$where[store_name]%");
  273. $model = $model->page(bcmul($where['page'], $where['limit'], 0), $where['limit']);
  274. $model = $model->order('s.id desc');
  275. $model = $model->where('s.is_del', 0);
  276. return self::page($model, function ($item) {
  277. $item['store_name'] = StoreProduct::where('id', $item['product_id'])->value('store_name');
  278. if ($item['status']) {
  279. if ($item['start_time'] > time())
  280. $item['start_name'] = '活动未开始';
  281. else if (bcadd($item['stop_time'], 86400) < time())
  282. $item['start_name'] = '活动已结束';
  283. else if (bcadd($item['stop_time'], 86400) > time() && $item['start_time'] < time()) {
  284. $config = SystemGroupData::get($item['time_id']);
  285. if ($config) {
  286. $arr = json_decode($config->value, true);
  287. $now_hour = date('H', time());
  288. $start_hour = $arr['time']['value'];
  289. $continued = $arr['continued']['value'];
  290. $end_hour = $start_hour + $continued;
  291. if ($start_hour > $now_hour) {
  292. $item['start_name'] = '活动未开始';
  293. } elseif ($end_hour < $now_hour) {
  294. $item['start_name'] = '活动已结束';
  295. } else {
  296. $item['start_name'] = '正在进行中';
  297. }
  298. } else {
  299. $item['start_name'] = '正在进行中';
  300. }
  301. }
  302. } else $item['start_name'] = '关闭';
  303. }, $where, $where['limit']);
  304. }
  305. public static function SaveExcel($where)
  306. {
  307. $model = new self;
  308. if ($where['status'] != '') $model = $model->where('status', $where['status']);
  309. if ($where['store_name'] != '') $model = $model->where('title|id', 'LIKE', "%$where[store_name]%");
  310. $list = $model->order('id desc')->where('is_del', 0)->select();
  311. count($list) && $list = $list->toArray();
  312. $excel = [];
  313. foreach ($list as $item) {
  314. $item['store_name'] = StoreProduct::where('id', $item['product_id'])->value('store_name');
  315. if ($item['status']) {
  316. if ($item['start_time'] > time())
  317. $item['start_name'] = '活动未开始';
  318. else if ($item['stop_time'] < time())
  319. $item['start_name'] = '活动已结束';
  320. else if ($item['stop_time'] > time() && $item['start_time'] < time())
  321. $item['start_name'] = '正在进行中';
  322. } else $item['start_name'] = '关闭';
  323. $excel[] = [
  324. $item['id'],
  325. $item['title'],
  326. $item['info'],
  327. $item['ot_price'],
  328. $item['price'],
  329. $item['stock'],
  330. $item['start_name'],
  331. $item['stop_time'],
  332. $item['stop_time'],
  333. $item['status'] ? '开启' : '关闭',
  334. ];
  335. }
  336. PHPExcelService::setExcelHeader(['编号', '活动标题', '活动简介', '原价', '秒杀价', '库存', '秒杀状态', '结束时间', '状态'])
  337. ->setExcelTile('秒杀产品导出', ' ', ' 生成时间:' . date('Y-m-d H:i:s', time()))
  338. ->setExcelContent($excel)
  339. ->ExcelSave();
  340. }
  341. /**
  342. * 获取秒杀产品id
  343. * @return array
  344. */
  345. public static function getSeckillIdAll()
  346. {
  347. return self::where('is_del', 0)->column('id', 'id');
  348. }
  349. /**
  350. * 获取秒杀的所有产品
  351. * @return int|string
  352. */
  353. public static function getSeckillCount()
  354. {
  355. return self::where('is_del', 0)->count();
  356. }
  357. /**
  358. * TODO 获取某个字段值
  359. * @param $id
  360. * @param string $field
  361. * @return mixed
  362. */
  363. public static function getSeckillField($id, $field = 'title')
  364. {
  365. return self::where('id', $id)->value($field);
  366. }
  367. /**
  368. * 判断产品当前时间段是否有秒杀活动
  369. * @param $product_id
  370. * @param $time_id
  371. * @return array|null|\think\Model
  372. * @throws \think\db\exception\DataNotFoundException
  373. * @throws \think\db\exception\DbException
  374. * @throws \think\db\exception\ModelNotFoundException
  375. */
  376. public static function checkSeckill($product_id, $time_id)
  377. {
  378. return self::where('product_id', $product_id)->where('time_id', $time_id)->where('is_del',0)->find();
  379. }
  380. }