123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\store\controller\statistics;
- use app\common\library\helper;
- use app\common\model\GoodsSalesRank;
- use app\store\controller\Controller;
- use app\store\model\ChannelSaleStatistics;
- use app\store\model\ChannelSaleVolumeTj;
- use app\store\model\GoodsVisitStatistics;
- use app\store\model\GoodsSaleStatistics;
- use app\store\model\RefundGoodsStatistics;
- use app\store\model\ShopsDailySalesSt;
- use app\common\service\Export as ExportService;
- use app\store\model\ShopsMonthlyBonusSt;
- use app\store\model\StoreDailySalesSt;
- use think\facade\Validate;
- /**
- * 数据概况
- * Class Data
- * @package app\store\controller\statistics
- */
- class Center extends Controller
- {
- /**
- * 数据统计主页
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function data()
- {
- $betweenTime = $this->request->param('betweenTime');
- if ($betweenTime){
- $times = between_time($betweenTime);
- }else{
- $times['end_time'] = strtotime(date('Y-m-01 00:00:00')) - 1 ;//上月的最后一秒
- $times['start_time'] =strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
- }
- $param = $this->request->param();
- // 获取数据
- /* $from = 1645545600;
- $to = 1645631940;*/
- $m = new ShopsDailySalesSt();
- $list = $m->listSt($times['start_time'],$times['end_time'],$param);
- return $this->renderSuccess(compact('list'));
- }
- public function exportData(){
- $param = $this->request->param('queryParam');
- $betweenTime = $param['betweenTime']??null;
- $type = intval($this->request->param('type',1));
- if ($betweenTime){
- $times = between_time($betweenTime);
- }else{
- $times['end_time'] = strtotime(date('Y-m-01 00:00:00')) - 1 ;//上月的最后一秒
- $times['start_time'] =strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
- }
- if ($type == 1){
- $model = new ShopsDailySalesSt();
- $data = $model->listsExport($times['start_time'],$times['end_time'],$param);
- }elseif($type == 3){
- //门店业绩排行导出
- $model = new ShopsDailySalesSt();
- $data = $model->listsRankExport($times['start_time'],$times['end_time']);
- }else{
- $from = date('Ym',$times['start_time']);
- $to = date('Ym',$times['end_time']);
- $model = new ShopsMonthlyBonusSt();
- $data = $model->listsExport($from,$to,$param);
- }
- $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls');
- return $this->renderSuccess($res,'导出成功');
- }
- /**
- * 奖励金数据统计
- * @return array
- */
- public function bonusData(){
- $betweenTime = $this->request->param('betweenTime');
- if ($betweenTime){
- $times = between_time($betweenTime);
- }else{
- $times['end_time'] = strtotime(date('Y-m-01 00:00:00')) - 1 ;//上月的最后一秒
- $times['start_time'] =strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
- }
- $param = $this->request->param();
- $from = date('Ym',$times['start_time']);
- $to = date('Ym',$times['end_time']);
- $m = new ShopsMonthlyBonusSt();
- $list = $m->listSt($from,$to,$param);
- return $this->renderSuccess(compact('list'));
- }
- /** 门店业绩排名
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function dataRank()
- {
- $betweenTime = $this->request->param('betweenTime');
- $page = $this->request->param('page',1);
- if ($betweenTime){
- $times = between_time($betweenTime);
- }else{
- $times['end_time'] = strtotime(date('Y-m-01 00:00:00')) - 1 ;//上月的最后一秒
- $times['start_time'] =strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
- }
- // 获取数据
- $m = new ShopsDailySalesSt();
- $list = $m->listStRank($times['start_time'],$times['end_time'],$page);
- return $this->renderSuccess(compact('list'));
- }
- public function getthemonth($date)
- {
- $firstday = date('Ym01', strtotime($date));
- $lastday = date('Ymd', strtotime("$firstday +1 month -1 day"));
- return array($firstday, $lastday);
- }
- /**
- * 交易数据-单月销售数据看板
- */
- public function saleVolumeBoard(){
- $betweenTime = $this->request->param('betweenTime',null);
- if (!$betweenTime){
- $betweenTime = date('Y-m');
- }
- $validate = Validate::rule('date', 'date');
- $data = [
- 'date' => $betweenTime,
- ];
- if (!$validate->check($data)) {
- return $this->renderError($validate->getError());
- }
- $times = $this->getthemonth($betweenTime);
- $from = $times[0];
- if (intval($from) < 20211001){
- return $this->renderError('不支持此范围查询');
- }
- $to = $times[1];
- $m = new StoreDailySalesSt();
- $list = $m->listAMonthSt($from,$to);
- $list = $list?$list->toArray():[];
- $_31 = [1=>31,2=>29,3=>31,4=>30,5=>31,6=>30,7=>31,8=>31,9=>30,10=>31,11=>30,12=>31];
- $lists = [];
- for ($i=1;$i<=$_31[intval(substr($betweenTime,5,2))];$i++){
- $lists[$i]['day'] = $i;
- $lists[$i]['sale_volume'] = '0.00';
- $lists[$i]['order_count'] = 0;
- foreach ($list as $item){
- if (intval($item['st_day']) == $i){
- $lists[$i]['sale_volume'] = $item['sale_volume'];
- $lists[$i]['order_count'] = $item['order_count'];
- }
- }
- }
- $sale_volumes = array_column($lists,'sale_volume');
- $order_counts = array_column($lists,'order_count');
- $days = array_column($lists,'day');
- array_walk($days,function (&$i){
- $i = $i.'日';
- });
- if ($this->request->isPost()) { // 导出数据
- $data['header'] = ['时间', '销售额(订单金额)', '成交订单数'];
- $data['filename'] = '销售额与订单数趋势对比导出';
- $data['data'] = [];
- foreach ($days as $k=>$day){
- $new_list['ref_date'] = $day;
- $new_list['sale_volumes'] = $sale_volumes[$k];
- $new_list['order_counts'] = $order_counts[$k];
- $data['data'][] = $new_list;
- }
- if (empty($data['data'])) {
- return $this->renderError('暂无数据');
- }
- $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls');
- return $this->renderSuccess($res,'导出成功');
- }
- return $this->renderSuccess(compact('sale_volumes','order_counts','days'));
- }
- /**
- * 交易数据-两月销售数据对比
- */
- public function saleBoardCompM(){
- $betweenTime = $this->request->param('betweenTime',null);
- if ($betweenTime){
- $validate = Validate::rule('date', 'date');
- $data = [
- 'date' => $betweenTime,
- ];
- if (!$validate->check($data)) {
- return $this->renderError($validate->getError());
- }
- $temps = strtotime($betweenTime);
- $times['end_time'] = strtotime(date('Y-m-01 00:00:00',strtotime('+1 month',$temps))) - 1;
- $times['start_time'] = strtotime('-1 month',$temps);
- }else{
- $times['end_time'] = strtotime(date('Y-m-01 00:00:00',strtotime('+1 month',time()))) - 1 ;//本月的最后一秒
- $times['start_time'] = mktime(0,0,0,intval(date('m')),1,intval(date('Y'))) - 1;//上月的最后一秒
- }
- $from = intval(date('Ym01',$times['start_time']));
- $to = intval(date('Ymd',$times['end_time']));
- $m = new StoreDailySalesSt();
- $list = $m->listAMonthSt($from,$to);
- $_31 = [1=>31,2=>29,3=>31,4=>30,5=>31,6=>30,7=>31,8=>31,9=>30,10=>31,11=>30,12=>31];
- $m1 = intval(date('m',$times['start_time']));
- $m2 = intval(date('m',$times['end_time']));
- //x轴坐标个数
- $days = $_31[$m1]>$_31[$m2]?$_31[$m1]:$_31[$m2];
- //$json = '[{"day":1,"value1":111,"value2":222},{"day":2,"value1":333,"value2":444}]';
- $lists = [];
- for ($i=0;$i<$days;$i++){
- $day = $i + 1;
- $lists[$i]['day'] = $day;
- $lists[$i]['value1'] = '0.00';//上月
- $lists[$i]['value2'] = '0.00';//本月
- foreach ($list as $item){
- if($item['st_day'] == $day){
- if (intval($item['int_month']) == $m1){
- $lists[$i]['value1'] = $item['sale_volume'];
- }
- if (intval($item['int_month']) == $m2){
- $lists[$i]['value2'] = $item['sale_volume'];
- }
- }
- }
- }
- $prevMonth= array_column($lists,'value1');
- $currentMonth = array_column($lists,'value2');
- $days = array_column($lists,'day');
- array_walk($days,function (&$i){
- $i = $i.'日';
- });
- if ($this->request->isPost()) { // 导出数据
- $data['header'] = ['时间', '上月(订单金额)', '本月(订单金额)'];
- $data['filename'] = '销售额趋势月度对比导出';
- $data['data'] = [];
- foreach ($days as $k=>$day){
- $new_list['ref_date'] = $day;
- $new_list['prev_month'] = $prevMonth[$k];
- $new_list['current_month'] = $currentMonth[$k];
- $data['data'][] = $new_list;
- }
- if (empty($data['data'])) {
- return $this->renderError('暂无数据');
- }
- $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls');
- return $this->renderSuccess($res,'导出成功');
- }
- return $this->renderSuccess(compact('currentMonth','prevMonth','days'));
- }
- /**
- * 交易数据-两年销售数据对比
- */
- public function saleBoardCompY(){
- $year = intval($this->request->param('betweenTime',null));
- if ($year){
- $times['end_time'] = mktime(23,59,59,12,31,$year);
- $times['start_time'] = mktime(0,0,0,1,1,$year-1);
- }else{
- $year = intval(date('Y'));
- $times['end_time'] = mktime(23,59,59,12,31,$year);
- $times['start_time'] = mktime(0,0,0,1,1,$year - 1);
- }
- $from = intval(date('Ymd',$times['start_time']));
- $to = intval(date('Ymd',$times['end_time']));
- $y1 = date('Y',$times['start_time']);
- $y2 = date('Y',$times['end_time']);
- $m = new StoreDailySalesSt();
- $list = $m->listTwoMonthSt($from,$to);
- //需要组装成前端需要的数据结构
- $lists = [['month'=>1],['month'=>2],['month'=>3],['month'=>4],['month'=>5],['month'=>6],['month'=>7],['month'=>8],['month'=>9],['month'=>10],['month'=>11],['month'=>12]];
- foreach ($lists as &$item){
- $item['value1'] = '0.00';//上月
- $item['value2'] = '0.00';//本月
- foreach ($list as $val){
- if (intval($val['int_month']) == $item['month']){
- if ($val['st_year'] == $y1){
- $item['value1'] = $val['sale_volume'];
- }
- if ($val['st_year'] == $y2){
- $item['value2'] = $val['sale_volume'];
- }
- }
- }
- }
- $prevYear = array_column($lists,'value1');
- $currentYear = array_column($lists,'value2');
- $months = array_column($lists,'month');
- array_walk($months,function (&$i){
- $i = $i.'月';
- });
- if ($this->request->isPost()) { // 导出数据
- $data['header'] = ['时间', '去年销售额', '今年销售额'];
- $data['filename'] = '销售额趋势年度对比导出';
- $data['data'] = [];
- foreach ($months as $k=>$month){
- $new_list['ref_date'] = $month;
- $new_list['prev_year'] = $prevYear[$k];
- $new_list['current_year'] = $currentYear[$k];
- $data['data'][] = $new_list;
- }
- if (empty($data['data'])) {
- return $this->renderError('暂无数据');
- }
- $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls');
- return $this->renderSuccess($res,'导出成功');
- }
- return $this->renderSuccess(compact('currentYear','prevYear','months'));
- }
- //商品销量排行列表
- public function goodsSalesRankList(){
- $betweenTime = $this->request->param('betweenTime');
- if ($betweenTime){
- $times = between_time($betweenTime);
- }else{
- $weekday = date('w')-1;
- $times['start_time'] = strtotime(date('Y-m-d',strtotime("-{$weekday} day"))) ;//当前星期的星期一
- $times['end_time'] = time();
- }
- if ($this->request->isPost()) { // 导出数据
- $model = new GoodsSalesRank();
- $data = $model->export($times['start_time'], $times['end_time']);
- if (empty($data['data'])) {
- return $this->renderError('暂无数据');
- }
- $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls');
- return $this->renderSuccess($res,'导出成功');
- }
- // 获取数据
- $m = new GoodsSalesRank();
- $list = $m->listRank($times['start_time'],$times['end_time']);
- return $this->renderSuccess(compact('list'));
- }
- /**
- *全渠道销售业绩趋势
- * @return array
- */
- public function channelSaleStat(){
- $param = $this->request->param();
- $betweenTime = $this->request->param('betweenTime');
- if ($betweenTime){
- $times = between_time($betweenTime);
- $times['start_time'] = strtotime(date('Y-m-d 00:00:00',$times['start_time']));
- $times['end_time'] = strtotime(date('Y-m-d 23:59:59',$times['end_time']));
- }else{
- $nowM = strtotime(date('Y-m-01 00:00:00'));
- $times['start_time'] =strtotime(date('Y-m-d ',strtotime('-1 month',$nowM)));
- $times['end_time'] = $nowM;
- if($param['ftype'] == 3){
- $times['end_time'] = $nowM - 86400;
- }
- }
- //dd($times);
- $model = new ChannelSaleVolumeTj();
- $volumes = $model->getList($param['ftype'],$times);
- $volumes = $volumes?$volumes->toArray():[];
- $xes = array_column($volumes,'remark');
- if ($param['ftype'] == 1){
- $xes = array_map(function($val){
- return substr($val,-2).'日';
- },$xes);
- }elseif($param['ftype'] == 3){
- $xes = array_map(function($val){
- return substr($val,-2).'月';
- },$xes);
- }
- $volumes = array_column($volumes,'sale_volume');
- return $this->renderSuccess(compact('volumes','xes'));
- }
- /**
- * 全渠道销售业绩构成
- * @return array
- */
- public function channelPieData(){
- $betweenTime = $this->request->param('betweenTime');
- if ($betweenTime){
- $times = between_time($betweenTime);
- $times['start_time'] = strtotime(date('Y-m-d 00:00:00',$times['start_time']));
- $times['end_time'] = strtotime(date('Y-m-d 23:59:59',$times['end_time']));
- }else{
- $nowM = strtotime(date('Y-m-01 00:00:00'));
- $times['start_time'] = mktime(0,0,0,intval(date('m')) - 1,1,intval(date('Y')));
- $times['end_time'] = $nowM;
- }
- $model = new ChannelSaleVolumeTj;
- $volumes = $model->getPieList($times);
- $t_volume1 = $t_volume2 = $t_volume3 = 0;
- $sale_volumes = array_column($volumes,'sale_volume');
- $t_volume = array_sum($sale_volumes);
- $y1 =$y2=$y3 = 0;
- if ($t_volume > 0){
- foreach ($volumes as $v){
- if ($v['channel_type'] == 1){
- $y1 = helper::bcadd(round($v['sale_volume']/$t_volume,4)*100,0,2);
- $t_volume1 = $v['sale_volume'];
- }
- if ($v['channel_type'] == 2){
- $y2 =helper::bcadd( round($v['sale_volume']/$t_volume,4)*100,0,2);
- $t_volume2 = $v['sale_volume'];
- }
- if ($v['channel_type'] == 3){
- $t_volume3 = $v['sale_volume'];
- }
- }
- $y3 = helper::bcsub(100 , $y1+$y2,2);
- }
- $data[] =['channel_type'=>1,'name'=>'门店渠道','value'=>$y1,'volume'=>$t_volume1];
- $data[] =['channel_type'=>2,'name'=>'推荐官渠道','value'=>$y2,'volume'=>$t_volume2];
- $data[] =['channel_type'=>3,'name'=>'自然销售渠道','value'=>$y3,'volume'=>$t_volume3];
- return $this->renderSuccess(compact('data'));
- }
- /**
- * 退款商品统计列表
- * @return array
- */
- public function refundGoodsStatisticsList(){
- // 获取数据
- $m = new RefundGoodsStatistics();
- $list = $m->getList($this->request->param());
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 导出退款商品统计
- */
- public function exportRefundGoodsStatistics(){
- $param = $this->request->param();
- $model = new RefundGoodsStatistics;
- $data = $model->export($param);
- if (empty($data['data'])) {
- return $this->renderError('暂无数据');
- }
- $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls');
- return $this->renderSuccess($res,'导出成功');
- }
- /**
- * 商品浏览数据列表
- * @return array
- */
- public function goodsVisitStatisticsList(){
- // 获取数据
- $m = new GoodsVisitStatistics();
- $list = $m->getList($this->request->param());
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 导出商品浏览数据
- */
- public function exportGoodsVisitStatistics(){
- $param = $this->request->param();
- $model = new GoodsVisitStatistics();
- $data = $model->export($param);
- if (empty($data['data'])) {
- return $this->renderError('暂无数据');
- }
- $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls');
- return $this->renderSuccess($res,'导出成功');
- }
- /**
- * 商品销售数据列表
- * @return array
- */
- public function goodsSaleStatisticsList(){
- // 获取数据
- $m = new GoodsSaleStatistics();
- $list = $m->getList($this->request->param());
- return $this->renderSuccess(compact('list'));
- }
- /**
- * 导出商品销售数据
- */
- public function exportGoodsSaleStatistics(){
- $param = $this->request->param();
- $model = new GoodsSaleStatistics();
- $data = $model->export($param);
- if (empty($data['data'])) {
- return $this->renderError('暂无数据');
- }
- $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls');
- return $this->renderSuccess($res,'导出成功');
- }
- /**
- * 渠道销售数据列表
- * @return array
- */
- public function channelSaleStatisticsList(){
- $param = $this->request->param();
- $model = new ChannelSaleStatistics();
- if ($this->request->isPost()) { // 导出数据
- $data = $model->export($param);
- if (empty($data['data'])) {
- return $this->renderError('暂无数据');
- }
- $res = ExportService::export($data['data'],$data['header'],$data['filename'],'列表','Xls');
- return $this->renderSuccess($res,'导出成功');
- }
- $list = $model->getList($param);
- return $this->renderSuccess(compact('list'));
- }
- }
|