StoreCombination.php 17 KB

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