StoreSeckill.php 13 KB

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