StoreSeckill.php 13 KB

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