<?php
/**
 * 大屏控制器  威海卡卡
 *
 * ============================================================================
 * 版权所有 2017北京素玄科技，并保留所有权利。
 *
 * 网站地址: http://www.suxuantech.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经允许的情况下，您不能对本系统代码做任何修改 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 如有修改需求，请联系素玄科技有限公司：contact@suxuantech.cn
 * ============================================================================
 * $Author: lilong<lilong@suxuantech.cn> $
 */
namespace app\crm\controller;
use think\Controller;
use app\index\controller\Base;
use think\Queue;
use think\Db;
use think\Request;

class Whkk extends Base{

	// 今天
	protected  $today = [
		'start'=>'', 'end'  =>'',
	];
	// 本月一号和当天
	protected  $thisMonth = [
		'start'=>'', 'end'  =>'',
	];
	// 本月每一天 
	protected  $thisMonthAllDay = [];
	//网销店 shop_code 
	protected  $shop_code_wx = ['BMSG03'];
	//门市店 shop_code 
	protected  $shop_code_ms = ['BMSG01'];
	//爱婴中心 shop_code 
	protected  $shop_code_ay = [];
	//市场部 shop_code 
	protected  $shop_code_sc = [];
	// 初始化
	public function _initialize(){
		parent::_initialize();
		// 今天
		$this->today['start']=  date('Y-m-d 00:00:00');
		$this->today['end']  =  date('Y-m-d 23:59:59');
		// 昨天
		$this->yestoday['start']= date('Y-m-d 00:00:00',strtotime('-1 day'));
		$this->yestoday['end']  = date('Y-m-d 23:59:59',strtotime('-1 day'));
		// 本月
		$this->thisMonth['start']= date('Y-m-01 00:00:00');
		$this->thisMonth['end']  = date('Y-m-d 23:59:59');
		// 上一个月
		$this->thisMonths['start']= date('Y-m-01 00:00:00', strtotime("-1 month"));
		$this->thisMonths['end']  = date('Y-m-d 23:59:59', strtotime("-1 month"));
		// 年
		$this->thisYear['start']= date('Y-01-01 00:00:00');
		$this->thisYear['end']  = date('Y-12-t 23:59:59');
		//事业部
		$this->brandclass_id = '';
		// 本月每一天
		$date = getdateArea('this_month');
		// $date = getdateArea('last_month');
		$timezone = getDateFromRange($date[0], $date[1]);
		$this->thisMonthAllDay = $timezone;

		//自然周
		$natureStartDate = date("Y-m-01");
		$t = date("t",time());
		$a = date("w",strtotime($natureStartDate));
		if($a==0){
		 	$natureStartDate = date("Y-m-d",strtotime("-6 days",strtotime($natureStartDate)));
		}elseif($a!=1){
		 	$a = $a-1;
		 	$natureStartDate = date("Y-m-d",strtotime("-$a days",strtotime($natureStartDate)));
		}
		$natureEndDate =  date('Y-m-d', strtotime(date('Y-m-01', strtotime(date("Y-m-d")))." +1 month -1 day"));
		$b = date("w",strtotime($natureEndDate));
		if($b != 0){
			$c = 7-$b;
			$natureEndDate = date("Y-m-d",strtotime("+$c days",strtotime($natureEndDate)));
		}
		$this->natureDate['start'] = date('Y-m-d 00:00:00', strtotime($natureStartDate));
		$this->natureDate['end'] = date('Y-m-d 23:59:59', strtotime($natureEndDate));
		$FromRange = getDateFromRange($natureStartDate, $natureEndDate);
		foreach ($FromRange as $key => $value) {
			if ($key < $t){
				# code...
			}else{
				unset($FromRange[$key]);
			}
		}
		$this->natureAllDay = $FromRange;

	}
	// PC大屏首页
	public function index(){

		// 验证权限
		$auth = controller('crm/Bigscreen', 'lib')->checkBigScreenAuth();
		if ($auth === false) {
			controller('crm/Index','controller')->index();
			exit;
		}

		$names = input('names');
		$shop = db_config('banmaDapingShop');
		if ($names) {
			foreach ($shop as $key => $value) {
				if ($value['name'] == $names) {
					$this->shop_code_ms = [$value['shop_code_s']];
					$this->shop_code_wx = [$value['shop_code_d']];
				}
			}
		}else{
			$this->shop_code_ms = ['BMSG01'];
			$this->shop_code_wx = ['BMSG03'];
		}

		// 页面上的时间
		$thisMonth = $this->thisMonth;
		$start= date( 'Y-m-d',strtotime($thisMonth['start']));
		$end  = date( 'Y-m-d',strtotime($thisMonth['end']));
		$this->assign('start',$start);
		$this->assign('end',$end);
		$this->assign('today',date('Y-m-d'));
		$this->assign('shop',$shop);
		$this->assign('names',$names);

		return $this->fetch();
	}
	public function mobile(){

		// 验证权限
		$auth = controller('crm/Bigscreen', 'lib')->checkBigScreenAuth();
		if ($auth === false) {
			controller('crm/Index','controller')->index();
			exit;
		}

		// 页面上的时间
		$thisMonth = $this->thisMonth;
		$start= date( 'Y-m-d',strtotime($thisMonth['start']));
		$end  = date( 'Y-m-d',strtotime($thisMonth['end']));
		$this->assign('start',$start);
		$this->assign('end',$end);
		$this->assign('today',date('Y-m-d'));
		return $this->fetch('index');
	}
	//-------------------------------------- 第一部分 实时数据 开始 --------------------------------------//
	public function getData1(){

		$input = input('post.');
		$thisMonth = $this->thisMonth;
		$today =  $this->today;
		//本月
		$monthStart = $input['start'] ?: date( 'Y-m-d',strtotime($thisMonth['start']) );
		$monthEnd  = $input['end'] ?: date( 'Y-m-d',strtotime($thisMonth['end']) );
		$param['start']= $monthStart;
		$param['end']  = $monthEnd;
		$param['brandclass_id'] = $this->brandclass_id;
		$param['shop_code_wx'] = $this->shop_code_wx;
		$param['shop_code_ms'] = $this->shop_code_ms;

		$globalset = db('s_globalset')->find();
		$param['globalset'] = $globalset;

		//本日
		$todayStart = $input['start'] ?: date( 'Y-m-d',strtotime($today['start']) );
		$todayEnd  = $input['end'] ?: date( 'Y-m-d',strtotime($today['end']) );
		// $params['start'] = $todayStart;
		// $params['end'] = $todayEnd;
		// $params['brandclass_id'] = $this->brandclass_id;
		// $params['shop_code_wx'] = $this->shop_code_wx;
		// $params['shop_code_ms'] = $this->shop_code_ms;
		
		//总客资
		$zongKz = controller('crm/whkk','lib')->getMaoKeZiLiang(['start'=>$monthStart,'end'=>$monthEnd]);
		$returnData['zongKz'] = count($zongKz);

		//总产值金额
		$monthYingshouData = controller('crm/whkk','lib')->getYingshou(['start'=>$monthStart,'end'=>$monthEnd,'shop_code_ms'=>$this->shop_code_ms,'shop_code_wx'=>$this->shop_code_wx]);
		// $monthYingshou = 0;
		// foreach ($monthYingshouData as $key => $value) {
		// 	$monthYingshou += $value['money'];
		// }
		$returnData['monthYingshou'] = $monthYingshouData['money'];
		// echo $monthYingshou.'<br>';

		//总支出金额
		$monthZhichuData = controller('crm/whkk','lib')->getChannelCost($param);
		$monthZhichu = 0;
		foreach ($monthZhichuData as $key => $value) {
			$monthZhichu += $value['cost'];
		}
		$returnData['monthZhichu'] = $monthZhichu;
		// echo $monthZhichu.'<br>';

		
		// echo $trchb.'<br>';

		//网络投放
		$returnData['wltf'] = $monthZhichu;

		
		// echo $wlcz.'<br>';

		//网络客资
		$wlkzData = controller('crm/whkk','lib')->getMaoKeZiLiang(['start'=>$monthStart,'end'=>$monthEnd,'shop_code_wx'=>$this->shop_code_wx]);
		$wlkz = count($wlkzData);
		$returnData['wlkz'] = $wlkz;
		
		//网络单数
		$wlds = controller('crm/whkk','lib')->getqqddShu(['start'=>$monthStart,'end'=>$monthEnd,'shop_code_wx'=>$this->shop_code_wx]);
		$returnData['wlds'] = $wlds['count'];

		//网络转化率
		$wlzhl = round(($returnData['wlds']/$returnData['wlkz'])*100,1);
		$returnData['wlzhl'] = $wlzhl.'%';

		//前 客资
		$qqkzData = controller('crm/whkk','lib')->getMaoKeZiLiang($param);
		$qqkz = count($qqkzData);
		$returnData['qqkz'] = $qqkz;

		//前期业绩
		$qqyjData = controller('crm/whkk','lib')->getqqyj(['start'=>$monthStart,'end'=>$monthEnd,'shop_code_ms'=>$this->shop_code_ms,'shop_code_wx'=>$this->shop_code_wx]);
		// $qqyj = 0;
		// foreach ($qqyjData as $key => $value) {
		// 	$qqyj += $value['payment_money'];
		// }
		$returnData['qqyj'] = $qqyjData[0]['money'];

		//前期订单
		$qqdd =controller('crm/whkk','lib')->getqqddShu(['start'=>$monthStart,'end'=>$monthEnd,'shop_code_ms'=>$this->shop_code_ms]);
		$returnData['qqdd'] = $qqdd['count'];

		//网转业绩
		$wzyjData = controller('crm/whkk','lib')->getWangZhuanYeJi($param);
		$wzyj = 0;
		foreach ($wzyjData as $key => $value) {
			$wzyj += $value['payment_money'];
		}
		$returnData['wzyj'] = number_format($wzyj,2,'.','');
		
		//网转单数
		$wzds = count($wzyjData);
		$returnData['wzds'] = $wzds;

		//选改单数	
		$xgds = controller('crm/whkk','lib')->getXuanGaiDanShu($param);
		$returnData['xgds'] = $xgds;

		//选片产值
		$xpczData = controller('crm/whkk','lib')->getXuanPianChanZhi($param);
		$xpcz = 0;
		foreach ($xpczData as $key => $value) {
			$xpcz += $value['payment_money'];
		}
		$returnData['xpcz'] = number_format($xpcz,2,'.','');

		//选片当销
		$xpdxData = controller('crm/whkk','lib')->getXuanPianDangXiao($param);
		$xpdx = $xpdxData[0]['money'];
		$returnData['xpdx'] = $xpdx;

		//选片卡扣
		$xpkkData = controller('crm/whkk','lib')->getXuanPianKaKou($param);
		$xpkk = $xpkkData[0]['money'];
		$returnData['xpkk'] = $xpkk;

		//正价当销均单
		$zjdxjdData = controller('crm/whkk','lib')->getZhengJiaDangXiaoJunJia($param);
		// $zjdxjCount = count($zjdxjdData);
		// $zjdxjdMoney = 0;
		// foreach ($zjdxjdData as $key => $value) {
		// 	$zjdxjdMoney += $value['shixiao_money'];
		// }
		// $zjdxjd = round($zjdxjdMoney/$zjdxjCount,1);
		$zjdxjd = $zjdxjdData;

		
		//实收正价均单
		$sszjjdzData = controller('crm/whkk','lib')->getShiShouZhengJiaDangXiaoJunJia($param);
		// $sszjjdzCount = count($sszjjdzData);
		// $sszjjdzMoney = 0;
		// foreach ($sszjjdzData as $key => $value) {
		// 	$sszjjdzMoney += $value['shixiao_money'];
		// }
		// $sszjjdz = round($sszjjdzMoney/$sszjjdzCount,1);
		$returnData['zjdxjd'] = $zjdxjd.' &nbsp;&nbsp;  ('.$sszjjdzData.')';

		//体验当销均单
		$tydxjdData = controller('crm/whkk','lib')->getTiYanDangXiaoJunJia($param);
		// $tydxjdCount = count($tydxjdData);
		// $tydxjdMoney = 0;
		// foreach ($tydxjdData as $key => $value) {
		// 	$tydxjdMoney += $value['shixiao_money'];
		// }
		// $tydxjd = round($tydxjdMoney/$tydxjdCount,1);
		$tydxjd = $tydxjdData;

		//实收体验均单
		$sstyjdData = controller('crm/whkk','lib')->getShiShouTiYanDangXiaoJunJia($param);
		// $sstyjdCount = count($sstyjdData);
		// $sstyjdMoney = 0;
		// foreach ($sstyjdData as $key => $value) {
		// 	$sstyjdMoney += $value['shixiao_money'];
		// }
		// $sstyjd = round($sstyjdMoney/$sstyjdCount,1);
		$sstyjd = $sstyjdData;
		$returnData['tydxjd'] = $tydxjd.' &nbsp;&nbsp;  ('.$sstyjd.')';		

		//选片实收续订
		$xpssxdData = controller('crm/whkk','lib')->getXuanPianShiShouXuDing($param);
		$xpssxd = $xpssxdData[0]['money'];
		$returnData['xpssxd'] = $xpssxd;

		//选片卡扣续订
		$xpkkxdData = controller('crm/whkk','lib')->getXuanPianKaKouXuDing($param);
		$xpkkxd = $xpkkxdData[0]['money'];
		$returnData['xpkkxd'] = $xpkkxd;

		//约选单数
		$yxds = controller('crm/whkk','lib')->getYueXuanDanShu($param);

		//实选单数
		$sxds = controller('crm/whkk','lib')->getShiXuanDanShu($param);
		$returnData['sxds'] = $sxds;

		//完选单数
		$wxds = controller('crm/whkk','lib')->getWanXuanDanShu($param);
		$returnData['yxds'] = $yxds+$wxds;
		$returnData['wxds'] =  $wxds.' &nbsp;&nbsp;  ('.round(($wxds/$returnData['yxds'])*100,1).'%)';


		//网选总产值
		$wxzczData = controller('crm/whkk','lib')->getWangXuanZongChanZhi($param);
		$wxzcz = 0;
		foreach ($wxzczData as $key => $value) {
			$wxzcz += $value['money'];
		}
		$returnData['wxzcz'] = number_format($wxzcz,2,'.','');

		//网选当销
		$wxdxData = controller('crm/whkk','lib')->getWangXuanDangXiao($param);
		$wxdx = $wxdxData[0]['money'];
		$returnData['wxdx'] = $wxdx;

		//网选约选单数
		$wxyxds = controller('crm/whkk','lib')->getWangXuanYueXuanDanShu($param);
		
		//网选改单数
		$wxgds = controller('crm/whkk','lib')->getWangXuanGaiDanShu($param);
		
		//网选实选单数
		$wxsxds = controller('crm/whkk','lib')->getWangXuanShiXuanDanShu($param);
		$returnData['wxsxds'] = $wxsxds;

		//网选完选单数
		$wxwxds = controller('crm/whkk','lib')->getWangXuanWanXuanDanShu($param);
		$returnData['wxwxds'] = $wxwxds;
		$returnData['wxyxds'] = $wxyxds+$wxgds+$wxwxds;
		$returnData['wxgds'] = $wxgds.' &nbsp;&nbsp;  ('.round(($wxgds/$returnData['wxyxds'])*100,1).'%)';

		//拍摄金额
		// $psje = 0;
		$psje = controller('crm/whkk','lib')->getPaiSheJine($param);
		$psje = $psje[0]['money'];
		$returnData['psje'] = $psje;

		//实拍单量
		$spdl = controller('crm/whkk','lib')->getShiPaiDanLiang($param);
		$returnData['spdl'] = $spdl;

		//完拍单量
		$wpdl = controller('crm/whkk','lib')->getWanPaiDanLiang($param);
		$returnData['wpdl'] = $wpdl;

		//拍改单数
		$pgds = controller('crm/whkk','lib')->getPaiGaiDanShu($param);
		
		//约拍单量
		$ypdl = controller('crm/whkk','lib')->getYuePaiDanLiang($param);
		$returnData['ypdl'] = $pgds+$wpdl+$ypdl;
		$returnData['pgds'] = $pgds.' &nbsp;&nbsp;  ('.round(($pgds/$returnData['ypdl'])*100,1).'%)';

		//拍摄服装
		$psfz = controller('crm/whkk','lib')->getPaiSheFuZhuang($param);
		$psfz = $psfz[0]['count'];
		$returnData['psfz'] = $psfz;

		//体验库存单量
		$tykcdl = controller('crm/whkk','lib')->getTiYanKuCunDanLiang($param);
		$returnData['tykcdl'] = $tykcdl[0]['count'];

		//库存单量
		$kcdl = controller('crm/whkk','lib')->getKuCunDanLiang($param);
		$returnData['kcdl'] = $kcdl[0]['count'];

		//库存金额
		$kcje = controller('crm/whkk','lib')->getKuCunJine($param);
		$returnData['kcje'] = $kcje[0]['money'];

		//网络产值
		$wlczData = controller('crm/whkk','lib')->getWxchanzhi($param);
		$wlcz = 0;
		foreach ($wlczData as $key => $value) {
			$wlcz += $value['total_money'];
		}
		$returnData['wlcz'] = number_format($wlcz+$wzyj+$wxzcz,2,'.','');

		//网络部产值 
		$returnData['wlcz_wlb'] = number_format($wlcz,2,'.','');

		//投入产出比
		// $trchb = '1:'.round(($returnData['wlcz']-$returnData['wxzcz'])/$returnData['wltf'],1);
		$trchb = '1:'.round($returnData['wltf']/$returnData['wlcz_wlb'],1);
		$returnData['trchb'] = $trchb.' &nbsp;&nbsp;  (1:'.round($returnData['wlcz']/$returnData['wltf'],1).')';

		//总实销
		$returnData['zongSx'] = number_format($xpdx+$psje,2,'.','');

		//1次未拍
		// $param[''];
		$wp = controller('crm/whkk','lib')->getWeiPai($param);

		foreach ($wp as $key => $value) {
			if($value['wpcunt'] == 1){
				$returnData['ycwp'] = $value['count'];
			}elseif($value['wpcunt'] == 2){
				$returnData['lcwp'] = $value['count']*2;
			}elseif($value['wpcunt'] > 2){
				$returnData['scwp'] += $value['count']*$value['wpcunt'];
			}
		}

		//上门
		$returnData['shangmen'] = controller('crm/whkk','lib')->getShangMen($param);

		return json($returnData);
	}
	//-------------------------------------- 第一部分 实时数据 结束 --------------------------------------//
	//-------------------------------------- 第一部分 产值数据 开始 --------------------------------------//
	//产值数据-网络
	public function wangluo(){
		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));
        
        // $start = '20200101';
        // $end = '20210131';

        $list = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_paymentcontent pay','o.orderId=pay.orderId','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            // ->where('pay.suoshu',['in',['前期款','前期补款','一销退款']])
            ->where('isshishou','实收')
            ->where("pay.shop_code",['in',$this->shop_code_wx])
            ->where('pay.paymentdate', 'between', [$start,$end])
            ->group('pay.paymentdate,s.channel_info_name,pay.shop_code,o.shop_code')
            ->field('sum(pay.payment_money) as money,pay.paymentdate,s.channel_info_name,pay.shop_code as pshop_code,o.shop_code as oshop_code')
            ->select();
        //自然周
        $natureStart = date('Ymd',strtotime($this->natureDate['start']));
        $natureEnd = date('Ymd',strtotime($this->natureDate['end']));
		$natureList = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_paymentcontent pay','o.orderId=pay.orderId','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            // ->where('pay.suoshu',['in',['前期款','前期补款','一销退款']])
            ->where('isshishou','实收')
            ->where("pay.shop_code",['in',$this->shop_code_wx])
            ->where('pay.paymentdate', 'between', [$natureStart,$natureEnd])
            ->group('pay.paymentdate,s.channel_info_name,pay.shop_code,o.shop_code')
            ->field('sum(pay.payment_money) as money,pay.paymentdate,s.channel_info_name,pay.shop_code as pshop_code,o.shop_code as oshop_code')
            ->select();
        
        $returnData['sql'] = db()->getLastSql();
        $data = [];
        $weekData = [];

        $xmdList = [];
        $dyList = [];
        $wxList = [];
        $qtList = [];
        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($list as $k => $v) {
        		if($day == $v['paymentdate']){
    				if($v['channel_info_name'] == '新美大' || $v['channel_info_name'] == '抖音' || $v['channel_info_name'] == '朋友圈推广'){
						$data[$key]['data'][$v['channel_info_name']] = $v['money'];
        			}
    				$data[$key]['data']['网销实收'] += $v['money'];

    				if($v['channel_info_name'] == '新美大'){
						$xmdList[$day] = $v['money'];
        			}elseif(!isset($xmdList[$day])){
	    				$xmdList[$day] = 0;
	    			}
	    			if($v['channel_info_name'] == '抖音'){
						$dyList[$day] = $v['money'];
        			}elseif(!isset($dyList[$day])){
	    				$dyList[$day] = 0;
	    			}
        			if($v['channel_info_name'] == '朋友圈推广'){
						$wxList[$day] = $v['money'];
        			}elseif(!isset($wxList[$day])){
	    				$wxList[$day] = 0;
	    			}
    				$qtList[$key] += $v['money'];
        		}else{
        			$xmdList[$key] = 0;
			        $dyList[$key] = 0;
			        $wxList[$key] = 0;
			        $qtList[$key] = 0;
        		}
        	}
        }
        //自然周 周值数据
        $natureData = [];
        foreach ($this->natureAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$natureData[$key]['date'] = $day;
        	$natureData[$key]['day'] = date('d',strtotime($value));
        	foreach ($natureList as $k => $v) {
        		if($day == $v['paymentdate']){
    				if($v['channel_info_name'] == '新美大' || $v['channel_info_name'] == '抖音' || $v['channel_info_name'] == '朋友圈推广'){
						$natureData[$key]['data'][$v['channel_info_name']] = $v['money'];
        			}
    				$natureData[$key]['data']['网销实收'] += $v['money'];
        		}
        	}
        }
        //周值
        $i = 0;
        foreach ($natureData as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}	
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }
        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        $jsonDataZong = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        		$jsonDataZong[$k2] += $v2 ?:0;
        	}
        }
        $returnData['data'] = $jsonData;	//数据
        $returnData['dataz'] = $jsonDataZong;	//数据
        $returnData['head'] = ['day','网销实收','新美大','抖音','朋友圈推广'];

        $returnData['xmdList'] = array_values($xmdList);
        $returnData['dyList'] = array_values($dyList);
        $returnData['wxList'] = array_values($wxList);
        $returnData['qtList'] = array_values($qtList);
        
        return json($returnData);
	}
	//产值数据-前期
	public function qianqi(){
		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));
        
        // $start = '20200101';
        // $end = '20210131';

        //前期
        $list = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_paymentcontent pay','o.orderId=pay.orderId','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            ->where('pay.suoshu',['in',['前期款','前期补款','一销退款']])
            ->where('isshishou','实收')
            ->where("pay.shop_code",['in',$this->shop_code_ms])
            // ->where("o.shop_code",['in',$this->shop_code_ms])
            ->where('pay.paymentdate', 'between', [$start,$end])
            ->group('pay.paymentdate,s.channel_info_name,o.shop_code,pay.shop_code')
            ->field('sum(pay.payment_money) as money,pay.paymentdate,s.channel_info_name,o.shop_code as oshop_code,pay.shop_code as pshop_code')
            ->select();

        //自然周
        $natureStart = date('Ymd',strtotime($this->natureDate['start']));
        $natureEnd = date('Ymd',strtotime($this->natureDate['end']));
        $natureList = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_paymentcontent pay','o.orderId=pay.orderId','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            ->where('pay.suoshu',['in',['前期款','前期补款','一销退款']])
            ->where('isshishou','实收')
            ->where("pay.shop_code",['in',$this->shop_code_ms])
            // ->where("o.shop_code",['in',$this->shop_code_ms])
            ->where('pay.paymentdate', 'between', [$natureStart,$natureEnd])
            ->group('pay.paymentdate,s.channel_info_name,o.shop_code,pay.shop_code')
            ->field('sum(pay.payment_money) as money,pay.paymentdate,s.channel_info_name,o.shop_code as oshop_code,pay.shop_code as pshop_code')
            ->select();
        // //其它
        // $list2 = db('c_customerinfo')->alias('c')
        //     ->join('c_customerorder o','c.customerid=o.customerid','left')
        //     ->join('c_paymentcontent pay','o.orderId=pay.orderId','left')
        //     ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
        //     // ->where('pay.suoshu',['in',['前期款','前期补款','一销退款']])
        //     // ->where('isshishou','实收')
        //     // ->where("pay.shop_code",['in',$this->shop_code_wx])
        //     ->where('pay.paymentdate', 'between', [$start,$end])
        //     ->group('pay.paymentdate')
        //     ->field('sum(pay.payment_money) as payment_money,o.targetdate,s.channel_info_name')
        //     ->select();
            // echo db()->getLastSql();die;

        $returnData['sql'] = db()->getLastSql();
        $data = [];
        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($list as $k => $v) {
        		if($day == $v['paymentdate']){
        			if(($v['oshop_code'] != $v['pshop_code'] && $v['channel_info_name'] != '新美大' && $v['channel_info_name'] != '抖音' && $v['channel_info_name'] != '朋友圈推广') || $v['oshop_code'] == $v['pshop_code']){
        				$data[$key]['data']['前期实收'] += $v['money'];
        			}elseif($v['channel_info_name'] == '新美大' || $v['channel_info_name'] == '抖音' || $v['channel_info_name'] == '朋友圈推广'){
						$data[$key]['data'][$v['channel_info_name']] = $v['money'];
        			}
        		}
        	}
        	// foreach ($list2 as $k2 => $v2) {
        	// 	if($day == $v2['targetdate']){
        	// 		$data[$key]['data']['其它'] = $v2['payment_money'];
        	// 	}
        	// }
        }
        //自然周 周值数据
        $natureData = [];
        foreach ($this->natureAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$natureData[$key]['date'] = $day;
        	$natureData[$key]['day'] = date('d',strtotime($value));
        	foreach ($natureList as $k => $v) {
        		if($day == $v['paymentdate']){
        			if(($v['oshop_code'] != $v['pshop_code'] && $v['channel_info_name'] != '新美大' && $v['channel_info_name'] != '抖音' && $v['channel_info_name'] != '朋友圈推广') || $v['oshop_code'] == $v['pshop_code']){
        				$natureData[$key]['data']['前期实收'] += $v['money'];
        			}elseif($v['channel_info_name'] == '新美大' || $v['channel_info_name'] == '抖音' || $v['channel_info_name'] == '朋友圈推广'){
						$natureData[$key]['data'][$v['channel_info_name']] = $v['money'];
        			}
        		}
        	}
        }

        //周值
        $weekData = [];
        $i = 0;
        foreach ($natureData as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }
        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        $jsonDataZong = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        		$jsonDataZong[$k2] += $v2 ?:0;
        	}
        }

        $returnData['data'] = $jsonData;	//数据
        $returnData['dataz'] = $jsonDataZong;	//数据
        $returnData['head'] = ['day','前期实收','新美大','抖音','朋友圈推广'];
        
        return json($returnData);
	}
	//产值数据-选片
	public function xuanpian(){
		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));

        //自然周
        $natureStart = date('Ymd',strtotime($this->natureDate['start']));
        $natureEnd = date('Ymd',strtotime($this->natureDate['end']));

        $tiyanmoney = db('s_globalset')->value('tiyanmoney');
        // $start = '20200101';
        // $end = '20210131';
        //总产值
        $where['pay.shop_code'] = ['in',$this->shop_code_ms];

        $zkzData =db('c_customerinfo')->alias('a')
            ->join('c_customerorder b','a.customerid=b.customerid','left')
            ->join('c_paymentcontent pay','b.orderId=pay.orderId','left')
            // ->join('crm_s_channel s','s.channel_name=a.customer_channel','left')
            ->where('suoshu',['in',['后期款','后期补款','二销退款']])
            ->where('isshishou','实收')
            ->where('pay.paymentdate', 'between', [$start,$end])
            ->where($where)
            // ->where(function($query){
            // 	$query->where('s.channel_info_name',null)->whereor('s.channel_info_name',['not in',['新美大','朋友圈推广','抖音']]);
            // })
            ->group('pay.paymentdate')
            ->field('sum(pay.payment_money) as payment_money,pay.paymentdate')
            ->select();
            $returnData['总产值'] = db()->getLastSql();

        $natureZkzData =db('c_customerinfo')->alias('a')
            ->join('c_customerorder b','a.customerid=b.customerid','left')
            ->join('c_paymentcontent pay','b.orderId=pay.orderId','left')
            // ->join('crm_s_channel s','s.channel_name=a.customer_channel','left')
            ->where('suoshu',['in',['后期款','后期补款','二销退款']])
            ->where('isshishou','实收')
            ->where('pay.paymentdate', 'between', [$natureStart,$natureEnd])
            ->where($where)
            // ->where(function($query){
            // 	$query->where('s.channel_info_name',null)->whereor('s.channel_info_name',['not in',['新美大','朋友圈推广','抖音']]);
            // })
            ->group('pay.paymentdate')
            ->field('sum(pay.payment_money) as payment_money,pay.paymentdate')
            ->select();
        //当销
        $dxData = db('c_customerinfo')->alias('a')
            ->join('c_customerorder b','a.customerid=b.customerid','left')
            ->join('c_paymentcontent pay','b.orderId=pay.orderId','left')
            // ->join('crm_s_channel s','s.channel_name=a.customer_channel','left')
            ->where('suoshu',['in',['后期款','后期补款','二销退款']])
            // ->where('isshishou','实收')
            ->where('pay.paymentdate', 'between', [$start,$end])
            // ->where(function($query){
            // 	$query->where('s.channel_info_name',null)->whereor('s.channel_info_name',['not in',['新美大','朋友圈推广','抖音']]);
            // })
            ->where('fundname','后期单销')
            ->where($where)
            ->where(function ($query) {
			    $query->where('paytype', 'VIP卡')->whereor('isshishou', '实收');
			})
            ->group('pay.paymentdate')
            ->field('sum(pay.payment_money) as payment_money,pay.paymentdate')
            ->select();
            $returnData['当销'] = db()->getLastSql();

        $natureDxData = db('c_customerinfo')->alias('a')
            ->join('c_customerorder b','a.customerid=b.customerid','left')
            ->join('c_paymentcontent pay','b.orderId=pay.orderId','left')
            // ->join('crm_s_channel s','s.channel_name=a.customer_channel','left')
            ->where('suoshu',['in',['后期款','后期补款','二销退款']])
            // ->where('isshishou','实收')
            ->where('pay.paymentdate', 'between', [$natureStart,$natureEnd])
            // ->where(function($query){
            // 	$query->where('s.channel_info_name',null)->whereor('s.channel_info_name',['not in',['新美大','朋友圈推广','抖音']]);
            // })
            ->where('fundname','后期单销')
            ->where($where)
            ->where(function ($query) {
			    $query->where('paytype', 'VIP卡')->whereor('isshishou', '实收');
			})
            ->group('pay.paymentdate')
            ->field('sum(pay.payment_money) as payment_money,pay.paymentdate')
            ->select();

        //卡扣
        $kkData = db('c_customerinfo')->alias('a')
            ->join('c_customerorder b','a.customerid=b.customerid','left')
            ->join('c_paymentcontent pay','b.orderId=pay.orderId','left')
            // ->join('crm_s_channel s','s.channel_name=a.customer_channel','left')
            ->where('suoshu',['in',['后期款','后期补款','二销退款']])
            ->where('paytype','VIP卡')
            ->where('pay.paymentdate', 'between', [$start,$end])
            // ->where(function($query){
            // 	$query->where('s.channel_info_name',null)->whereor('s.channel_info_name',['not in',['新美大','朋友圈推广','抖音']]);
            // })
            ->where('fundname','后期单销')
            ->where($where)
            ->group('pay.paymentdate')
            ->field('sum(pay.payment_money) as payment_money,pay.paymentdate')
            ->select();
        $natureKkData = db('c_customerinfo')->alias('a')
            ->join('c_customerorder b','a.customerid=b.customerid','left')
            ->join('c_paymentcontent pay','b.orderId=pay.orderId','left')
            // ->join('crm_s_channel s','s.channel_name=a.customer_channel','left')
            ->where('suoshu',['in',['后期款','后期补款','二销退款']])
            ->where('paytype','VIP卡')
            ->where('pay.paymentdate', 'between', [$natureStart,$natureEnd])
            // ->where(function($query){
            // 	$query->where('s.channel_info_name',null)->whereor('s.channel_info_name',['not in',['新美大','朋友圈推广','抖音']]);
            // })
            ->where('fundname','后期单销')
            ->where($where)
            ->group('pay.paymentdate')
            ->field('sum(pay.payment_money) as payment_money,pay.paymentdate')
            ->select();
            $returnData['卡扣'] = db()->getLastSql();
        //正均单
        //$zjdData = Db::query("select count(*) as count,(select ifnull(sum(payment_money),0) from c_paymentcontent pay where pay.orderId = b.orderId and pay.currentfc = sp.currentfc and suoshu in('后期款','后期补款','二销退款') and fundname='后期单销' ) as shixiao_money,sp.sptend_day from c_customerinfo a join c_customerorder b on a.customerid=b.customerid join c_customersp sp on sp.orderId=b.orderId  join c_customerphoto p on sp.orderId=p.orderId and sp.currentfc=p.currentfc join crm_s_channel s on s.channel_name=a.customer_channel where p.qqphotomoney>='".$tiyanmoney."' and sp.sptend_day between '".$start."' and '".$end."' and sp.shop_code = '".$this->shop_code_ms[0]."' and (s.channel_info_name is null or s.channel_info_name not in ('新美大','朋友圈推广','抖音')) group by sp.sptend_day");

        $zjdCount = Db::query("select count(*) as count,sp.sptend_day from c_customerinfo a join c_customerorder b on a.customerid=b.customerid join c_customersp sp on sp.orderId=b.orderId  join c_customerphoto p on sp.orderId=p.orderId and sp.currentfc=p.currentfc  AND p.islast = 1 where p.qqphotomoney>='".$tiyanmoney."' and sp.sptend_day between '".$start."' and '".$end."' and sp.shop_code = '".$this->shop_code_ms[0]."' group by sp.sptend_day");
        $natureZjdCount = Db::query("select count(*) as count,sp.sptend_day from c_customerinfo a join c_customerorder b on a.customerid=b.customerid join c_customersp sp on sp.orderId=b.orderId  join c_customerphoto p on sp.orderId=p.orderId and sp.currentfc=p.currentfc  AND p.islast = 1 where p.qqphotomoney>='".$tiyanmoney."' and sp.sptend_day between '".$natureStart."' and '".$natureEnd."' and sp.shop_code = '".$this->shop_code_ms[0]."' group by sp.sptend_day");

        //周值
        $zjdMoney = Db::query("select sum(shixiao_money) as shixiao_money,sptend_day from (
			SELECT
			(
					SELECT
						ifnull(sum(payment_money), 0)
					FROM
						c_paymentcontent pay
					WHERE
						pay.orderId = b.orderId
					AND pay.currentfc = sp.currentfc
					AND suoshu IN (
						'后期款',
						'后期补款',
						'二销退款'
					)
					AND fundname = '后期单销'
				) AS shixiao_money,
				sp.sptend_day
			FROM
				c_customerinfo a
			JOIN c_customerorder b ON a.customerid = b.customerid
			JOIN c_customersp sp ON sp.orderId = b.orderId
			JOIN c_customerphoto p ON sp.orderId = p.orderId
			AND sp.currentfc = p.currentfc AND p.islast = 1
			WHERE
				p.qqphotomoney >= '".$tiyanmoney."'
			AND sp.sptend_day BETWEEN '".$start."'
			AND '".$end."'
			AND sp.shop_code = '".$this->shop_code_ms[0]."'
			) t 
			GROUP BY
				t.sptend_day");

         //周值
        $natureZjdMoney = Db::query("select sum(shixiao_money) as shixiao_money,sptend_day from (
			SELECT
			(
					SELECT
						ifnull(sum(payment_money), 0)
					FROM
						c_paymentcontent pay
					WHERE
						pay.orderId = b.orderId
					AND pay.currentfc = sp.currentfc
					AND suoshu IN (
						'后期款',
						'后期补款',
						'二销退款'
					)
					AND fundname = '后期单销'
				) AS shixiao_money,
				sp.sptend_day
			FROM
				c_customerinfo a
			JOIN c_customerorder b ON a.customerid = b.customerid
			JOIN c_customersp sp ON sp.orderId = b.orderId
			JOIN c_customerphoto p ON sp.orderId = p.orderId
			AND sp.currentfc = p.currentfc AND p.islast = 1
			WHERE
				p.qqphotomoney >= '".$tiyanmoney."'
			AND sp.sptend_day BETWEEN '".$natureStart."'
			AND '".$natureEnd."'
			AND sp.shop_code = '".$this->shop_code_ms[0]."'
			) t 
			GROUP BY
				t.sptend_day");

        //免均单
        // $mjdData = Db::query("select count(*) as count,(select ifnull(sum(payment_money),0) from c_paymentcontent pay where pay.orderId = b.orderId and pay.currentfc = sp.currentfc and suoshu in('后期款','后期补款','二销退款') and fundname='后期单销' ) as shixiao_money,sp.sptend_day from c_customerinfo a join c_customerorder b on a.customerid=b.customerid join c_customersp sp on sp.orderId=b.orderId join c_customerphoto p on sp.orderId=p.orderId and sp.currentfc=p.currentfc join crm_s_channel s on s.channel_name=a.customer_channel where p.qqphotomoney<'".$tiyanmoney."' and sp.sptend_day between '".$start."' and '".$end."' and sp.shop_code = '".$this->shop_code_ms[0]."' and (s.channel_info_name is null or s.channel_info_name not in ('新美大','朋友圈推广','抖音'))  group by sp.sptend_day");
        $mjdCount =  Db::query("select count(*) as count,sp.sptend_day from c_customerinfo a join c_customerorder b on a.customerid=b.customerid join c_customersp sp on sp.orderId=b.orderId  join c_customerphoto p on sp.orderId=p.orderId and sp.currentfc=p.currentfc  AND p.islast = 1 where p.qqphotomoney<'".$tiyanmoney."' and sp.sptend_day between '".$start."' and '".$end."' and sp.shop_code = '".$this->shop_code_ms[0]."' group by sp.sptend_day");

        $natureMjdCount =  Db::query("select count(*) as count,sp.sptend_day from c_customerinfo a join c_customerorder b on a.customerid=b.customerid join c_customersp sp on sp.orderId=b.orderId  join c_customerphoto p on sp.orderId=p.orderId and sp.currentfc=p.currentfc  AND p.islast = 1 where p.qqphotomoney<'".$tiyanmoney."' and sp.sptend_day between '".$natureStart."' and '".$natureEnd."' and sp.shop_code = '".$this->shop_code_ms[0]."' group by sp.sptend_day");

        $mjdMoney =  Db::query("select sum(shixiao_money) as shixiao_money,sptend_day from (
			SELECT
			(
					SELECT
						ifnull(sum(payment_money), 0)
					FROM
						c_paymentcontent pay
					WHERE
						pay.orderId = b.orderId
					AND pay.currentfc = sp.currentfc
					AND suoshu IN (
						'后期款',
						'后期补款',
						'二销退款'
					)
					AND fundname = '后期单销'
				) AS shixiao_money,
				sp.sptend_day
			FROM
				c_customerinfo a
			JOIN c_customerorder b ON a.customerid = b.customerid
			JOIN c_customersp sp ON sp.orderId = b.orderId
			JOIN c_customerphoto p ON sp.orderId = p.orderId
			AND sp.currentfc = p.currentfc AND p.islast = 1
			WHERE
				p.qqphotomoney < '".$tiyanmoney."'
			AND sp.sptend_day BETWEEN '".$start."'
			AND '".$end."'
			AND sp.shop_code = '".$this->shop_code_ms[0]."'
			) t 
			GROUP BY
				t.sptend_day");

        $natureMjdMoney =  Db::query("select sum(shixiao_money) as shixiao_money,sptend_day from (
			SELECT
			(
					SELECT
						ifnull(sum(payment_money), 0)
					FROM
						c_paymentcontent pay
					WHERE
						pay.orderId = b.orderId
					AND pay.currentfc = sp.currentfc
					AND suoshu IN (
						'后期款',
						'后期补款',
						'二销退款'
					)
					AND fundname = '后期单销'
				) AS shixiao_money,
				sp.sptend_day
			FROM
				c_customerinfo a
			JOIN c_customerorder b ON a.customerid = b.customerid
			JOIN c_customersp sp ON sp.orderId = b.orderId
			JOIN c_customerphoto p ON sp.orderId = p.orderId
			AND sp.currentfc = p.currentfc AND p.islast = 1
			WHERE
				p.qqphotomoney < '".$tiyanmoney."'
			AND sp.sptend_day BETWEEN '".$start."'
			AND '".$end."'
			AND sp.shop_code = '".$this->shop_code_ms[0]."'
			) t 
			GROUP BY
				t.sptend_day");
        
        
        $data = [];
        $weekData = [];
        $jd = '';
        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($zkzData as $k => $v) {
        		if($day == $v['paymentdate']){
					$data[$key]['data']['总产值'] = $v['payment_money'];
        		}
        	}
        	foreach ($dxData as $k2 => $v2) {
        		if($day == $v2['paymentdate']){
        			$data[$key]['data']['当销'] = $v2['payment_money'];
        		}
        	}
        	foreach ($kkData as $k3 => $v3) {
        		if($day == $v3['paymentdate']){
        			$data[$key]['data']['卡扣'] = $v3['payment_money'];
        		}
        	}
        	foreach ($zjdCount as $k4 => $v4) {
        		foreach ($zjdMoney as $k5 => $v5) {
        			if($day == $v4['sptend_day'] && $day == $v5['sptend_day']){
	        			$data[$key]['data']['正均单'] = round($v5['shixiao_money']/$v4['count'],1);
	        			$jd['z']['shixiao_money'] += $v5['shixiao_money'];
	        			$jd['z']['count'] += $v4['count'];
	        		}	
        		}
        	}
        	foreach ($mjdCount as $k6 => $v6) {
        		foreach ($mjdMoney as $k7 => $v7) {
        			if($day == $v6['sptend_day'] && $day == $v7['sptend_day']){
	        			$data[$key]['data']['免均单'] = round($v7['shixiao_money']/$v6['count'],1);
	        			$jd['m']['shixiao_money'] += $v7['shixiao_money'];
	        			$jd['m']['count'] += $v6['count'];
	        		}	
        		}
        	}
        }
        $natureData = [];
        foreach ($this->natureAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$natureData[$key]['date'] = $day;
        	$natureData[$key]['day'] = date('d',strtotime($value));
        	foreach ($zkzData as $k => $v) {
        		if($day == $v['paymentdate']){
					$natureData[$key]['data']['总产值'] = $v['payment_money'];
        		}
        	}
        	foreach ($dxData as $k2 => $v2) {
        		if($day == $v2['paymentdate']){
        			$natureData[$key]['data']['当销'] = $v2['payment_money'];
        		}
        	}
        	foreach ($kkData as $k3 => $v3) {
        		if($day == $v3['paymentdate']){
        			$natureData[$key]['data']['卡扣'] = $v3['payment_money'];
        		}
        	}
        }


        //周值
        $i = 0;
        foreach ($natureData as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	foreach ($natureZjdCount as $k1 => $v1) {
        		foreach ($natureZjdMoney as $k2 => $v2) {
        			if($value['date'] == $v1['sptend_day'] && $value['date'] == $v2['sptend_day']){
	        			$weekData[$i]['zjd_money'] += $v2['shixiao_money'];
	        			$weekData[$i]['zjd_count'] += $v1['count'];
	        			$weekData[$i]['zjd'] = round($weekData[$i]['zjd_money']/$weekData[$i]['zjd_count'],1);
	        		}	
        		}
        	}
        	foreach ($natureMjdCount as $k3 => $v3) {
        		foreach ($natureMjdMoney as $k4 => $v4) {
        			if($value['date'] == $v3['sptend_day'] && $value['date'] == $v4['sptend_day']){
	        			$weekData[$i]['mjd_money'] += $v4['shixiao_money'];
	        			$weekData[$i]['mjd_count'] += $v3['count'];
	        			$weekData[$i]['mjd'] = round($weekData[$i]['mjd_money']/$weekData[$i]['mjd_count'],1);
	        		}	
        		}
        	}
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }

        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        $jsonDataZong = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        		$jsonDataZong[$k2] += $v2 ?:0;
        	}
        }
        $jsonDataZong['正均单'] = round($jd['z']['shixiao_money']/$jd['z']['count'],1);
        $jsonDataZong['免均单'] = round($jd['m']['shixiao_money']/$jd['m']['count'],1);
        $returnData['jd'] = $jd;
        $returnData['data'] = $jsonData;	//数据
        $returnData['dataz'] = $jsonDataZong;	//数据
        $returnData['head'] = ['day','总产值','当销','卡扣','正均单','免均单'];
        
        return json($returnData);
	}
	//产值数据-摄化
	public function shehua(){
		
		//本月
		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));

        //自然周
        $natureStart = date('Ymd',strtotime($this->natureDate['start']));
        $natureEnd = date('Ymd',strtotime($this->natureDate['end']));
        
        $where['p.shop_code'] = ['in',$this->shop_code_ms];
        
        $tiyanmoney = db('s_globalset')->value('tiyanmoney');
        //正价值
        $zjzData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where($where)
            ->where('qqphotomoney',['>=',$tiyanmoney])
            ->where('p.photodate', 'between', [$start,$end])
            ->group('p.photodate')
            ->field('sum(qqphotomoney) as money,p.photodate')
            ->select();
        $returnData['zjz']  = db()->getLastSql();
        $natureZjzData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where($where)
            ->where('qqphotomoney',['>=',$tiyanmoney])
            ->where('p.photodate', 'between', [$natureStart,$natureEnd])
            ->group('p.photodate')
            ->field('sum(qqphotomoney) as money,p.photodate')
            ->select();
        //体验值
        $tyzData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where($where)
            ->where('qqphotomoney',['<',$tiyanmoney])
            ->where('p.photodate', 'between', [$start,$end])
            ->group('p.photodate')
            ->field('sum(qqphotomoney) as money,p.photodate')
            ->select();
        $returnData['tyz']  = db()->getLastSql();
        $natureTyzData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where($where)
            ->where('qqphotomoney',['<',$tiyanmoney])
            ->where('p.photodate', 'between', [$natureStart,$natureEnd])
            ->group('p.photodate')
            ->field('sum(qqphotomoney) as money,p.photodate')
            ->select();
        $data = [];
        $weekData = [];
        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($zjzData as $k => $v) {
        		if($day == $v['photodate']){
					$data[$key]['data']['正价值'] = $v['money'];
        		}
        	}
        	foreach ($tyzData as $k2 => $v2) {
        		if($day == $v2['photodate']){
        			$data[$key]['data']['体验值'] = $v2['money'];
        		}
        	}
        }

        $natureData = [];
        foreach ($this->natureAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$natureData[$key]['date'] = $day;
        	$natureData[$key]['day'] = date('d',strtotime($value));
        	foreach ($natureZjzData as $k => $v) {
        		if($day == $v['photodate']){
					$natureData[$key]['data']['正价值'] = $v['money'];
        		}
        	}
        	foreach ($natureTyzData as $k2 => $v2) {
        		if($day == $v2['photodate']){
        			$natureData[$key]['data']['体验值'] = $v2['money'];
        		}
        	}
        }
        //周值
        $i = 0;
        foreach ($natureData as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }
        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        $jsonDataZong = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        		$jsonDataZong[$k2] += $v2 ?:0;
        	}
        }
        $returnData['data'] = $jsonData;	//数据
        $returnData['dataz'] = $jsonDataZong;	//数据
        $returnData['head'] = ['day','正价值','体验值'];
        
        return json($returnData);
	}
	//产值数据-上门
	public function shangmen(){
		//本月
		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));

        //自然周
        $natureStart = date('Ymd',strtotime($this->natureDate['start']));
        $natureEnd = date('Ymd',strtotime($this->natureDate['end']));
        
        $staffData = db('c_staffinfo')->where('department_name','爱婴部')->field('staffname')->select();
        $staff = array_column($staffData, 'staffname');
        //总产值
        $zczData = db('c_paymentcontent')
            ->where('paymentdate', 'between', [$start,$end])
            ->where('isshishou','实收')
            // ->where('cashierman',['in',$staff])
            // ->where('currentfc',1)
            ->where('fundname',['in',['上门订单','上门选片','上门补款','上门退款']])
            ->group('paymentdate')
            ->field('sum(payment_money) as money,paymentdate')
            ->select();
        $returnData['zcz'] = db()->getLastSql();
        $natureZczData = db('c_paymentcontent')
            ->where('paymentdate', 'between', [$natureStart,$natureEnd])
            ->where('isshishou','实收')
            // ->where('cashierman',['in',$staff])
            // ->where('currentfc',1)
            ->where('fundname',['in',['上门订单','上门选片','上门补款','上门退款']])
            ->group('paymentdate')
            ->field('sum(payment_money) as money,paymentdate')
            ->select();

        //订单值
        $ddzData = db('c_customerinfo')->alias('a')
            ->join('c_customerorder b','a.customerid=b.customerid','left')
            ->join('c_paymentcontent pay','b.orderId=pay.orderId','left')
            ->where('pay.paymentdate', 'between', [$start,$end])
            ->where('isshishou','实收')
            ->where('suoshu',['in',['前期款','前期补款','一销退款']])
            ->where('pay.fundname',['in',['上门订单','上门选片','上门补款','上门退款']])
            // ->where('b.storeconsuitant1',['in',$staff])
            ->group('pay.paymentdate')
            ->field('ifnull(sum(pay.payment_money),0) as money,pay.paymentdate')
            ->select();
        $returnData['ddz'] = db()->getLastSql();
        $natureDdzData = db('c_customerinfo')->alias('a')
            ->join('c_customerorder b','a.customerid=b.customerid','left')
            ->join('c_paymentcontent pay','b.orderId=pay.orderId','left')
            ->where('pay.paymentdate', 'between', [$natureStart,$natureEnd])
            ->where('isshishou','实收')
            ->where('suoshu',['in',['前期款','前期补款','一销退款']])
            ->where('pay.fundname',['in',['上门订单','上门选片','上门补款','上门退款']])
            // ->where('b.storeconsuitant1',['in',$staff])
            ->group('pay.paymentdate')
            ->field('ifnull(sum(pay.payment_money),0) as money,pay.paymentdate')
            ->select();

        //选片值
        $xpData = db('c_paymentcontent')
            ->where('paymentdate', 'between', [$start,$end])
            ->where('isshishou','实收')
            ->where('suoshu',['in',['后期款','后期补款','二销退款']])
            ->where('fundname',['in',['上门订单','上门选片','上门补款','上门退款']])
            // ->where('cashierman',['in',$staff])
            ->group('paymentdate')
            ->field('sum(payment_money) as money,paymentdate')
            ->select();
        $returnData['xp'] = db()->getLastSql();
        $natureXpData = db('c_paymentcontent')
            ->where('paymentdate', 'between', [$natureStart,$natureEnd])
            ->where('isshishou','实收')
            ->where('suoshu',['in',['后期款','后期补款','二销退款']])
            ->where('fundname',['in',['上门订单','上门选片','上门补款','上门退款']])
            // ->where('cashierman',['in',$staff])
            ->group('paymentdate')
            ->field('sum(payment_money) as money,paymentdate')
            ->select();

      	//拍摄值
      	$deal = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where('cameraman',['in',$staff])
            // ->where('sptstate',1)
            ->where('p.photodate', 'between', [$start,$end])
            ->group('p.photodate')
            ->field('sum(qqphotomoney) as money,p.photodate')
            ->select();
        $returnData['deal'] = db()->getLastSql();

        $natureDeal = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where('cameraman',['in',$staff])
            // ->where('sptstate',1)
            ->where('p.photodate', 'between', [$natureStart,$natureEnd])
            ->group('p.photodate')
            ->field('sum(qqphotomoney) as money,p.photodate')
            ->select();

        $data = [];
        $weekData = [];
        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($zczData as $k => $v) {
        		if($day == $v['paymentdate']){
					$data[$key]['data']['总产值'] = $v['money'];
        		}
        	}
        	foreach ($ddzData as $k2 => $v2) {
        		if($day == $v2['paymentdate']){
        			$data[$key]['data']['订单值'] = $v2['money'];
        		}
        	}
        	foreach ($xpData as $k3 => $v3) {
        		if($day == $v3['paymentdate']){
        			$data[$key]['data']['选片值'] = $v3['money'];
        		}
        	}
        	foreach ($deal as $k4 => $v4) {
        		if($day == $v4['photodate']){
        			$data[$key]['data']['拍摄值'] = $v4['money'];
        		}
        	}
        }
        $natureData = [];
        foreach ($this->natureAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$natureData[$key]['date'] = $day;
        	$natureData[$key]['day'] = date('d',strtotime($value));
        	foreach ($natureZczData as $k => $v) {
        		if($day == $v['paymentdate']){
					$natureData[$key]['data']['总产值'] = $v['money'];
        		}
        	}
        	foreach ($natureDdzData as $k2 => $v2) {
        		if($day == $v2['paymentdate']){
        			$natureData[$key]['data']['订单值'] = $v2['money'];
        		}
        	}
        	foreach ($natureXpData as $k3 => $v3) {
        		if($day == $v3['paymentdate']){
        			$natureData[$key]['data']['选片值'] = $v3['money'];
        		}
        	}
        	foreach ($natureDeal as $k4 => $v4) {
        		if($day == $v4['photodate']){
        			$natureData[$key]['data']['拍摄值'] = $v4['money'];
        		}
        	}
        }
        //周值
        $i = 0;
        foreach ($natureData as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }
        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        $jsonDataZong = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        		$jsonDataZong[$k2] += $v2 ?:0;
        	}
        }
        $returnData['data'] = $jsonData;	//数据
        $returnData['dataz'] = $jsonDataZong;	//数据
        $returnData['head'] = ['day','总产值','订单值','选片值','拍摄值'];
        
        return json($returnData);
	}
	//产值数据-市场
	public function shichang(){

		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));

        //自然周
        $natureStart = date('Ymd',strtotime($this->natureDate['start']));
        $natureEnd = date('Ymd',strtotime($this->natureDate['end']));
        
        //总实收
        $zssData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            ->join('c_paymentcontent pay','pay.orderId=o.orderId','left')
            ->where('pay.shop_code',['in',$this->shop_code_ms])
            ->where('pay.paymentdate', 'between', [$start,$end])
            ->where('isshishou','实收')
            ->group('pay.paymentdate')
            ->where('s.channel_info_name',['in',['市场活动','市场公关']])
            ->field('sum(pay.payment_money) as money,paymentdate')
            ->select();
        $returnData['czSql'] = db()->getLastSql();
        $natureZssData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            ->join('c_paymentcontent pay','pay.orderId=o.orderId','left')
            ->where('pay.shop_code',['in',$this->shop_code_ms])
            ->where('pay.paymentdate', 'between', [$natureStart,$natureEnd])
            ->where('isshishou','实收')
            ->group('pay.paymentdate')
            ->where('s.channel_info_name',['in',['市场活动','市场公关']])
            ->field('sum(pay.payment_money) as money,paymentdate')
            ->select();
        //总产值
        $zczData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_paymentcontent pay','o.orderId=pay.orderId','left')
            ->where('isshishou','实收')
            ->where('pay.shop_code',['in',[$this->shop_code_wx[0],$this->shop_code_ms[0]]])
            ->where('pay.paymentdate', 'between', [$start,$end])
            ->group('pay.paymentdate')
            ->field('sum(pay.payment_money) as money,pay.paymentdate')
            ->select();
        $natureZczData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_paymentcontent pay','o.orderId=pay.orderId','left')
            ->where('isshishou','实收')
            ->where('pay.shop_code',['in',[$this->shop_code_wx[0],$this->shop_code_ms[0]]])
            ->where('pay.paymentdate', 'between', [$natureStart,$natureEnd])
            ->group('pay.paymentdate')
            ->field('sum(pay.payment_money) as money,pay.paymentdate')
            ->select();

        $where['is_status'] = 1;
        $where['c_from'] = 1;
        $start = date('Y-m-d 00:00:00',strtotime($this->thisMonth['start']));
        $end = date('Y-m-d 23:59:59',strtotime($this->thisMonth['end']));
        //客资量
        $kzlData = Db::query("SELECT DATE_FORMAT(c.dj_day,'%Y%m%d') as day,count(c.id) AS count FROM crm_c_customerinfo c left join crm_s_channel s on c.customer_channel = s.channel_name WHERE c.dj_day BETWEEN '".$start."' AND '".$end."' AND c.is_status = 1 AND c.c_from = 1 and c.shop_code in ('".$this->shop_code_wx[0]."','".$this->shop_code_ms[0]."') AND s.channel_info_name in ('市场活动','市场公关')  GROUP BY DATE_FORMAT(c.dj_day,'%Y%m%d')");
        $natureKzlData = Db::query("SELECT DATE_FORMAT(c.dj_day,'%Y%m%d') as day,count(c.id) AS count FROM crm_c_customerinfo c left join crm_s_channel s on c.customer_channel = s.channel_name WHERE c.dj_day BETWEEN '".$natureStart."' AND '".$natureEnd."' AND c.is_status = 1 AND c.c_from = 1 and c.shop_code in ('".$this->shop_code_wx[0]."','".$this->shop_code_ms[0]."') AND s.channel_info_name in ('市场活动','市场公关')  GROUP BY DATE_FORMAT(c.dj_day,'%Y%m%d')");

		$returnData['kzSql'] ="SELECT DATE_FORMAT(c.dj_day,'%Y%m%d') as day,count(c.id) AS count FROM crm_c_customerinfo c left join crm_s_channel s on c.customer_channel = s.channel_name WHERE c.dj_day BETWEEN '".$start."' AND '".$end."' AND c.is_status = 1 AND c.c_from = 1 and c.shop_code in ('".$this->shop_code_wx[0]."','".$this->shop_code_ms[0]."') AND s.channel_info_name in ('市场活动','市场公关')  GROUP BY DATE_FORMAT(c.dj_day,'%Y%m%d')";
        //进店量
        // $jdData = Db::query("SELECT DATE_FORMAT(dj_day,'%Y%m%d') as day,count(id) AS count FROM crm_c_customerinfo WHERE dj_day BETWEEN '".$start."' AND '".$end."' AND is_status = 1 AND c_from = 1 AND is_intostore = 2 GROUP BY DATE_FORMAT(dj_day,'%Y%m%d')");

        $data = [];
        $weekData = [];
        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($zssData as $k => $v) {
        		if($day == $v['paymentdate']){
					$data[$key]['data']['总实收'] = round($v['money'],0);
        		}
        	}
        	foreach ($zczData as $k => $v) {
        		if($day == $v['paymentdate']){
					$data[$key]['data']['总产值'] = round($v['money'],0);
        		}
        	}
        	
        	foreach ($kzlData as $k2 => $v2) {
        		if($day == $v2['day']){
        			$data[$key]['data']['客资量'] = $v2['count'];
        		}
        	}
        	// foreach ($jdData as $k3 => $v3) {
        	// 	if($day == $v2['day']){
        	// 		$data[$key]['data']['进店量'] = $v2['count'];
        	// 	}
        	// }
        }

        $natureData = [];
        foreach ($this->natureAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$natureData[$key]['date'] = $day;
        	$natureData[$key]['day'] = date('d',strtotime($value));
        	foreach ($natureZssData as $k => $v) {
        		if($day == $v['paymentdate']){
					$natureData[$key]['data']['总实收'] = round($v['money'],0);
        		}
        	}
        	foreach ($natureZczData as $k => $v) {
        		if($day == $v['paymentdate']){
					$natureData[$key]['data']['总产值'] = round($v['money'],0);
        		}
        	}
        	
        	foreach ($natureKzlData as $k2 => $v2) {
        		if($day == $v2['day']){
        			$natureData[$key]['data']['客资量'] = $v2['count'];
        		}
        	}
        }
        //周值
        $i = 0;
        foreach ($natureData as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }
        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        $jsonDataZong = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        		$jsonDataZong[$k2] += $v2 ?:0;
        	}
        }
        $returnData['data'] = $jsonData;	//数据
        $returnData['dataz'] = $jsonDataZong;	//数据
        // $returnData['head'] = ['day','总产值','客资量','进店量'];
        $returnData['head'] = ['day','总产值','总实收','客资量'];
        
        return json($returnData);
	}
	//-------------------------------------- 第一部分 产值数据 结束 --------------------------------------//
	//产能数据-网络
	public function wangluo2(){
		//本月
		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));

        //自然周
        $natureStart = date('Ymd',strtotime($this->natureDate['start']));
        $natureEnd = date('Ymd',strtotime($this->natureDate['end']));

        $danData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            ->where("o.shop_code",['in',$this->shop_code_wx])
            ->where('o.targetdate', 'between', [$start,$end])
            ->where('o.blankoutannal',['<>',1])
            ->group('o.targetdate,s.channel_info_name')
            ->field('count(o.orderId) as count,o.targetdate,s.channel_info_name')
            ->select();
        $natureDanData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            ->where("o.shop_code",['in',$this->shop_code_wx])
            ->where('o.targetdate', 'between', [$start,$end])
            ->where('o.blankoutannal',['<>',1])
            ->group('o.targetdate,s.channel_info_name')
            ->field('count(o.orderId) as count,o.targetdate,s.channel_info_name')
            ->select();
        $returnData['danshu'] = db()->getLastSql();

		$start = date('Y-m-d H:i:s',strtotime($this->thisMonth['start']));
        $end = date('Y-m-d H:i:s',strtotime($this->thisMonth['end']));

        //自然周
        $natureStart = date('Y-m-d H:i:s',strtotime($this->natureDate['start']));
        $natureEnd = date('Y-m-d H:i:s',strtotime($this->natureDate['end']));

        $kzData = Db::query("SELECT DATE_FORMAT(dj_day,'%Y%m%d') as day,count(c.id) as count,s.channel_info_name  FROM crm_c_customerinfo c JOIN crm_s_channel s on c.customer_channel = s.channel_name WHERE dj_day BETWEEN '".$start."' AND '".$end."' AND is_status = 1 AND c_from = 1 AND c.shop_code in ('".$this->shop_code_wx[0]."') GROUP BY DATE_FORMAT(dj_day,'%Y%m%d'),s.channel_info_name");
        $natureKzData = Db::query("SELECT DATE_FORMAT(dj_day,'%Y%m%d') as day,count(c.id) as count,s.channel_info_name  FROM crm_c_customerinfo c JOIN crm_s_channel s on c.customer_channel = s.channel_name WHERE dj_day BETWEEN '".$natureStart."' AND '".$natureEnd."' AND is_status = 1 AND c_from = 1 AND c.shop_code in ('".$this->shop_code_wx[0]."') GROUP BY DATE_FORMAT(dj_day,'%Y%m%d'),s.channel_info_name");
        $returnData['kezi'] = "SELECT DATE_FORMAT(dj_day,'%Y%m%d') as day,count(c.id) as count,s.channel_info_name  FROM crm_c_customerinfo c JOIN crm_s_channel s on c.customer_channel = s.channel_name WHERE dj_day BETWEEN '".$start."' AND '".$end."' AND is_status = 1 AND c_from = 1 AND c.shop_code in ('".$this->shop_code_wx[0]."') GROUP BY DATE_FORMAT(dj_day,'%Y%m%d')";

        $data = [];
        $weekData = [];
        $mddsList = [];
        $mdkzList = [];
        $dydsList = [];
        $dykzList = [];
        $wxdsList = [];
        $wxkzList = [];
        $qtdsList = [];
        $qtkzList = [];

        $month = [];
        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('d',strtotime($value));
        	$month[] = $day;
        	$date = date('Ymd',strtotime($value)); 
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($danData as $k => $v) {
        		$qqdsDay = date('d',strtotime($v['targetdate']));
        		if($day == $qqdsDay){
	        		if($v['channel_info_name'] == '新美大' || $v['channel_info_name'] == '抖音' || $v['channel_info_name'] == '朋友圈推广'){
						$data[$key]['data'][$v['channel_info_name']] = $v['count'];
	    			}
	    			$data[$key]['data']['其它'] += $v['count'];
	    			
	    			if($v['channel_info_name'] == '新美大'){
	    				$mddsList[$day] = $v['count'];
	    			}elseif(!isset($mddsList[$day])){
	    				$mddsList[$day] = 0;
	    			}
	    			if($v['channel_info_name'] == '抖音'){
	    				$dydsList[$day] = $v['count'];
	    			}elseif(!isset($dydsList[$day])){
	    				$dydsList[$day] = 0;
	    			}
	    			if($v['channel_info_name'] == '朋友圈推广'){
	    				$wxdsList[$day] = $v['count'];
	    			}elseif(!isset($wxdsList[$day])){
	    				$wxdsList[$day] = 0;
	    			}
	    			$qtdsList[$day] += $v['count'];
	    			// if($v['channel_info_name'] != '新美大' && $v['channel_info_name'] != '抖音' && $v['channel_info_name'] != '朋友圈推广'){

	    			// }elseif(!isset($qtdsList[$day])){
	    			// 	$qtdsList[$day] = 0;
	    			// }
	    		}else{
	    			if(!isset($mddsList[$day])){
	    				$mddsList[$day] = 0;
	    			}
	    			if(!isset($dydsList[$day])){
	    				$dydsList[$day] = 0;
	    			}
	    			if(!isset($wxdsList[$day])){
	    				$wxdsList[$day] = 0;
	    			}
	    			if(!isset($qtdsList[$day])){
	    				$qtdsList[$day] = 0;
	    			}
	    		}
        	}
        	foreach ($kzData as $k => $v) {
        		$qqkzDay = date('d',strtotime($v['day']));
        		if($qqkzDay == $day){

        			if($v['channel_info_name'] == '新美大'){
	    				$mdkzList[$day] = $v['count'];
	    				$data[$key]['data']['新美大客资'] = $v['count'];
	    			}else{
	    				$mdkzList[$day] = 0;
	    			}
	    			if($v['channel_info_name'] == '抖音'){
	    				$dykzList[$day] = $v['count'];
	    				$data[$key]['data']['抖音客资'] = $v['count'];
	    			}else{
	    				$dykzList[$day] = 0;
	    			}
	    			if($v['channel_info_name'] == '朋友圈推广'){
	    				$wxkzList[$day] = $v['count'];
	    				$data[$key]['data']['朋友圈推广客资'] = $v['count'];
	    			}else{
	    				$wxkzList[$day] = 0;
	    			}
	    			$data[$key]['data']['其它客资'] += $v['count'];
	    			$qtkzList[$day] += $v['count'];
	    			// if($v['channel_info_name'] != '新美大' && $v['channel_info_name'] != '抖音' && $v['channel_info_name'] != '朋友圈推广'){
	    				
	    			// }else{
	    			// 	$qtkzList[] = 0;
	    			// }
        		}else{
        			if(!isset($mdkzList[$day])){
	    				$mdkzList[$day] = 0;
	    			}
	    			if(!isset($dykzList[$day])){
	    				$dykzList[$day] = 0;
	    			}
	    			if(!isset($wxkzList[$day])){
	    				$wxkzList[$day] = 0;
	    			}
	    			if(!isset($qtkzList[$day])){
	    				$qtkzList[$day] = 0;
	    			}
        		}
        	}
        }

        $natureData = [];
        foreach ($this->natureAllDay as $key => $value) {
        	$day = date('d',strtotime($value));
        	$date = date('Ymd',strtotime($value)); 
        	$natureData[$key]['date'] = $day;
        	$natureData[$key]['day'] = date('d',strtotime($value));
        	foreach ($natureDanData as $k => $v) {
        		$qqdsDay = date('d',strtotime($v['targetdate']));
        		if($day == $qqdsDay){
	        		if($v['channel_info_name'] == '新美大' || $v['channel_info_name'] == '抖音' || $v['channel_info_name'] == '朋友圈推广'){
						$natureData[$key]['data'][$v['channel_info_name']] = $v['count'];
	    			}
	    			$natureData[$key]['data']['其它'] += $v['count'];
	    		}
        	}
        	foreach ($natureKzData as $k => $v) {
        		$qqkzDay = date('d',strtotime($v['day']));
        		if($qqkzDay == $day){
        			if($v['channel_info_name'] == '新美大'){
	    				$natureData[$key]['data']['新美大客资'] = $v['count'];
	    			}
	    			if($v['channel_info_name'] == '抖音'){
	    				$natureData[$key]['data']['抖音客资'] = $v['count'];
	    			}
	    			if($v['channel_info_name'] == '朋友圈推广'){
	    				$natureData[$key]['data']['朋友圈推广客资'] = $v['count'];
	    			}
	    			$natureData[$key]['data']['其它客资'] += $v['count'];
        		}
        	}
        }
        //周值
        $i = 0;
        foreach ($natureData as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }
        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        $jsonDataZong = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        		$jsonDataZong[$k2] += $v2 ?:0;
        	}
        }
        $jsonDataZong['网销单数'] = array_sum(array_values($qtdsList));
        $jsonDataZong['网销客资'] = array_sum(array_values($qtkzList));
        $jsonDataZong['美大单数'] = array_sum(array_values($mddsList));
        $jsonDataZong['美大客资'] = array_sum(array_values($mdkzList));
        $jsonDataZong['抖音单数'] = array_sum(array_values($dydsList));
        $jsonDataZong['抖音客资'] = array_sum(array_values($dykzList));
        $jsonDataZong['朋友圈推广单数'] = array_sum(array_values($wxdsList));
        $jsonDataZong['朋友圈推广客资'] = array_sum(array_values($wxkzList));

        $returnData['data'] = $jsonData;	//数据
        $returnData['dataz'] = $jsonDataZong;	//数据
        $returnData['head'] = ['day','新美大','抖音','朋友圈推广','其它'];

		$returnData['month'] = $month;
		$returnData['mddsList'] = array_values($mddsList);
		$returnData['mdkzList'] = array_values($mdkzList);
		$returnData['dydsList'] = array_values($dydsList);
		$returnData['dykzList'] = array_values($dykzList);

		$returnData['wxdsList'] = array_values($wxdsList);
		$returnData['wxkzList'] = array_values($wxkzList);
		$returnData['qtdsList'] = array_values($qtdsList);
		$returnData['qtkzList'] = array_values($qtkzList);
        
        return json($returnData);
	}
	//产能数据-前期
	public function qianqi2(){
		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));

        //自然周
        $natureStart = date('Ymd',strtotime($this->natureDate['start']));
        $natureEnd = date('Ymd',strtotime($this->natureDate['end']));
        
        //前期
        $qqdsData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->where("o.shop_code",['in',$this->shop_code_ms[0]])
            ->where('o.targetdate', 'between', [$start,$end])
            ->group('o.targetdate')
            ->field('count(c.id) as count,o.targetdate as paymentdate')
            ->select();
        $returnData['qqdsSql1'] = db()->getLastSql();
        $returnQqdsData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->where("o.shop_code",['in',$this->shop_code_ms[0]])
            ->where('o.targetdate', 'between', [$natureStart,$natureEnd])
            ->group('o.targetdate')
            ->field('count(c.id) as count,o.targetdate as paymentdate')
            ->select();

        $qqdsData1 = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_paymentcontent pay','o.orderId=pay.orderId','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            ->where("o.shop_code",['in',$this->shop_code_wx[0]])
            ->where('pay.paymentdate', 'between', [$start,$end])
            ->where('pay.shop_code',['in',$this->shop_code_ms[0]])
            ->where('pay.suoshu',['in',['前期款','前期补款','一销退款']])
            ->group('pay.paymentdate,s.channel_info_name')
            ->field('count(DISTINCT o.orderId) as count,pay.paymentdate,s.channel_info_name')
            ->select();
        $returnData['qqdsSql2'] = db()->getLastSql();
        $returnQqdsData1 = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_paymentcontent pay','o.orderId=pay.orderId','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            ->where("o.shop_code",['in',$this->shop_code_wx[0]])
            ->where('pay.paymentdate', 'between', [$natureStart,$natureEnd])
            ->where('pay.shop_code',['in',$this->shop_code_ms[0]])
            ->where('pay.suoshu',['in',['前期款','前期补款','一销退款']])
            ->group('pay.paymentdate,s.channel_info_name')
            ->field('count(DISTINCT o.orderId) as count,pay.paymentdate,s.channel_info_name')
            ->select();

      //   if (is_array($qqdsData) && is_array($qqdsData1)) {
    		// foreach ($qqdsData as $key => $value) {
    		// 	foreach ($qqdsData1 as $k2 => $v2) {
    		// 		if($v2['channel_info_name'] == '新美大' || $v2['channel_info_name'] == '抖音' || $v2['channel_info_name'] == '朋友圈推广'){
    		// 			if($value['paymentdate'] == $v2['paymentdate']){
	    	// 				$qqdsData[$key]['count'] = $qqdsData[$key]['count']+$v2['count'];
	    	// 			}else{
	    	// 				array_push($qqdsData,$v2);
	    	// 			}

    		// 		}
    		// 	}
    		// }	
      //   }
        
     //    //合并数组
     //    if (is_array($qqdsData) && is_array($qqdsData1)) {
	    // 	$yplData = array();
	    // 	foreach ($qqdsData1 as $key=>$value) {
	    //  		if (array_key_exists($key, $qqdsData)) {
	    //   			$yplData[$key] = $value + $qqdsData[$key];
	    //   			unset($qqdsData[$key]);
	    //  		} else {
	    //   			$yplData[$key] = $value;
	    //  		}
	    // 	}
	    // 	$yplData =  $yplData+$qqdsData;
	   	// }

        $kzstart = date('Y-m-d H:i:s',strtotime($this->thisMonth['start']));
        $kzend = date('Y-m-d H:i:s',strtotime($this->thisMonth['end']));

        $natureStart = date('Y-m-d H:i:s',strtotime($this->natureDate['start']));
        $natureEnd = date('Y-m-d H:i:s',strtotime($this->natureDate['end']));

        $qqkzData = Db::query("SELECT DATE_FORMAT(dj_day,'%Y%m%d') as day,count(id) AS count FROM crm_c_customerinfo WHERE dj_day BETWEEN '".$kzstart."' AND '".$kzend."' AND is_status = 1 AND c_from = 1 AND shop_code in ('".$this->shop_code_ms[0]."') GROUP BY DATE_FORMAT(dj_day,'%Y%m%d')");
        $natureQqkzData = Db::query("SELECT DATE_FORMAT(dj_day,'%Y%m%d') as day,count(id) AS count FROM crm_c_customerinfo WHERE dj_day BETWEEN '".$natureStart."' AND '".$natureEnd."' AND is_status = 1 AND c_from = 1 AND shop_code in ('".$this->shop_code_ms[0]."') GROUP BY DATE_FORMAT(dj_day,'%Y%m%d')");
        $returnData['kzsql'] = "SELECT DATE_FORMAT(dj_day,'%Y%m%d') as day,count(id) AS count FROM crm_c_customerinfo WHERE dj_day BETWEEN '".$kzstart."' AND '".$kzend."' AND is_status = 1 AND c_from = 1 AND shop_code in ('".$this->shop_code_ms[0]."') GROUP BY DATE_FORMAT(dj_day,'%Y%m%d')";

        //其它
       /* $Data = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            ->where("o.shop_code",['not in',$this->shop_code_ms])
            ->where('o.targetdate', 'between', [$start,$end])
            ->group('o.targetdate,s.channel_info_name')
            ->field('count(o.orderId) as count,o.targetdate,s.channel_info_name')
            ->select();*/
        // $qtkzData = Db::query("SELECT DATE_FORMAT(dj_day,'%Y%m%d') as day,count(id) AS count FROM crm_c_customerinfo WHERE dj_day BETWEEN '".$start."' AND '".$end."' AND is_status = 1 AND c_from = 1 AND add_shop_code not in ('".$this->shop_code_ms."') GROUP BY DATE_FORMAT(dj_day,'%Y%m%d')");
          

        $returnData['qtsql'] = db()->getLastSql();
        $data = [];
        $weekData = [];
        $qqdsList = [];
        $qqkzList = [];
        $xmdList = [];
        $dyList = [];
        $wxList = [];

        // $qtdsList = [];
        // $qtkzList = [];
        $month = [];
        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('d',strtotime($value));
        	$month[] = $day;
        	
        	$date = date('Ymd',strtotime($value));
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($qqdsData as $k => $v) {
        		$qqdsDay = date('d',strtotime($v['paymentdate']));
        		if($day == $qqdsDay){
					$qqdsList[$key] = $v['count'];
					$data[$key]['data']['前期单数'] = $v['count'];
        		}elseif(!isset($qqdsList[$key])){
        			$qqdsList[$key] = 0;
        		}
        		if($v['paymentdate'] == $date){
        			$data[$key]['data']['前期单数'] = $v['count'];
        		}
        	}
        	foreach ($qqkzData as $k => $v) {
        		$qqkzDay = date('d',strtotime($v['day']));
        		if($day == $qqkzDay){
					$qqkzList[$key] = $v['count'];
        		}elseif(!isset($qqkzList[$key])){
        			$qqkzList[$key] = 0;
        		}
        		if($v['day'] == $date){
        			$data[$key]['data']['前期客资'] = $v['count'];
        		}
        	}
        	foreach ($qqdsData1 as $k => $v) {
        		$qtdsDay = date('d',strtotime($v['paymentdate']));
        		if($day == $qtdsDay){
        			if($v['channel_info_name'] == '新美大'){
        				$xmdList[$key] = $v['count'];
        				$data[$key]['data']['新美大'] = $v['count'];
        			}elseif(!isset($xmdList[$key])){
						$xmdList[$key] = 0;
        			}
        			if($v['channel_info_name'] == '抖音'){
						$dyList[$key] = $v['count'];
						$data[$key]['data']['抖音'] = $v['count'];
        			}elseif(!isset($dyList[$key])){
						$dyList[$key] = 0;
        			}
        			if($v['channel_info_name'] == '朋友圈推广'){
						$wxList[$key] = $v['count'];
						$data[$key]['data']['朋友圈推广'] = $v['count'];
        			}elseif(!isset($wxList[$key])){
        				$wxList[$key] = 0;
        			}
        		}else{
	    			if(!isset($xmdList[$key])){
	    				$xmdList[$key] = 0;
	    			}
	    			if(!isset($dyList[$key])){
	    				$dyList[$key] = 0;
	    			}
	    			if(!isset($wxList[$key])){
	    				$wxList[$key] = 0;
	    			}
	    		}
        	}
        }

        $natureData = [];
        foreach ($this->natureAllDay as $key => $value) {
        	$day = date('d',strtotime($value));
        	
        	$date = date('Ymd',strtotime($value));
        	$natureData[$key]['date'] = $day;
        	$natureData[$key]['day'] = date('d',strtotime($value));
        	foreach ($returnQqdsData as $k => $v) {
        		$qqdsDay = date('d',strtotime($v['paymentdate']));
        		if($day == $qqdsDay){
					$natureData[$key]['data']['前期单数'] = $v['count'];
        		}
        		if($v['paymentdate'] == $date){
        			$natureData[$key]['data']['前期单数'] = $v['count'];
        		}
        	}
        	foreach ($natureQqkzData as $k => $v) {
        		$qqkzDay = date('d',strtotime($v['day']));
        		if($v['day'] == $date){
        			$natureData[$key]['data']['前期客资'] = $v['count'];
        		}
        	}
        	foreach ($returnQqdsData1 as $k => $v) {
        		$qtdsDay = date('d',strtotime($v['paymentdate']));
        		if($day == $qtdsDay){
        			if($v['channel_info_name'] == '新美大'){
        				$natureData[$key]['data']['新美大'] = $v['count'];
        			}
        			if($v['channel_info_name'] == '抖音'){
						$natureData[$key]['data']['抖音'] = $v['count'];
        			}
        			if($v['channel_info_name'] == '朋友圈推广'){
						$natureData[$key]['data']['朋友圈推广'] = $v['count'];
        			}
        		}
        	}
        }

        //周值
        $i = 0;
        foreach ($natureData as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }
        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        $jsonDataZong = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        		$jsonDataZong[$k2] += $v2 ?:0;
        	}
        }
        $returnData['data'] = $jsonData;	//数据
        $returnData['dataz'] = $jsonDataZong;	//数据
        $returnData['head'] = ['day','前期单数','新美大','抖音','朋友圈推广'];

		$returnData['month'] = $month;
		$returnData['qqdsList'] = $qqdsList;
		$returnData['qqkzList'] = $qqkzList;
		$returnData['xmdList'] = array_values($xmdList);
		$returnData['dyList'] = array_values($dyList);
		$returnData['wxList'] = array_values($wxList);


		// $returnData['qtdsList'] = $qtdsList;
		// $returnData['qtkzList'] = $qtkzList;
        
        return json($returnData);
	}
	//产能数据-选片
	public function xuanpian2(){
		//本月
		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));

        //自然周
        $natureStart = date('Ymd',strtotime($this->natureDate['start']));
        $natureEnd = date('Ymd',strtotime($this->natureDate['end']));
        
        //总选片
        $zxpData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp sp','o.orderId=sp.orderId','left')
            // ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            // ->where(function($query){
            // 	$query->where('s.channel_info_name',null)->whereor('s.channel_info_name',['not in',['新美大','朋友圈推广','抖音']]);
            // })
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('sptend_day', 'between', [$start,$end])
            ->group('sptend_day')
            ->field('sptend_day,count(c.id) as count')
            ->select();
        $returnData['总选片'] = db()->getLastSql();
        $natureZxpData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp sp','o.orderId=sp.orderId','left')
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('sptend_day', 'between', [$natureStart,$natureEnd])
            ->group('sptend_day')
            ->field('sptend_day,count(c.id) as count')
            ->select();

        //正价单
        $tiyanmoney = db('s_globalset')->value('tiyanmoney');
 		$zjdData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp sp','o.orderId=sp.orderId','left')
            ->join('c_customerphoto p','p.orderId=sp.orderId and p.currentfc=sp.currentfc and p.islast=1','left')
            // ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            // ->where(function($query){
            // 	$query->where('s.channel_info_name',null)->whereor('s.channel_info_name',['not in',['新美大','朋友圈推广','抖音']]);
            // })
            ->where('p.qqphotomoney',['>=',$tiyanmoney])
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('sptend_day', 'between', [$start,$end])
            ->group('sptend_day')
            ->field('sptend_day,count(c.id) as count')
            ->select();
        $natureZjdData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp sp','o.orderId=sp.orderId','left')
            ->join('c_customerphoto p','p.orderId=sp.orderId and p.currentfc=sp.currentfc and p.islast=1','left')
            ->where('p.qqphotomoney',['>=',$tiyanmoney])
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('sptend_day', 'between', [$natureStart,$natureEnd])
            ->group('sptend_day')
            ->field('sptend_day,count(c.id) as count')
            ->select();

        //体验单
        $tydData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp sp','o.orderId=sp.orderId','left')
            ->join('c_customerphoto p','p.orderId=sp.orderId and p.currentfc=sp.currentfc and p.islast=1','left')
            // ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            // ->where(function($query){
            // 	$query->where('s.channel_info_name',null)->whereor('s.channel_info_name',['not in',['新美大','朋友圈推广','抖音']]);
            // })
            ->where('p.qqphotomoney',['<',$tiyanmoney])
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('sptend_day', 'between', [$start,$end])
            ->group('sptend_day')
            ->field('sptend_day,count(c.id) as count')
            ->select();

        $natureTydData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp sp','o.orderId=sp.orderId','left')
            ->join('c_customerphoto p','p.orderId=sp.orderId and p.currentfc=sp.currentfc and p.islast=1','left')
            // ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            // ->where(function($query){
            // 	$query->where('s.channel_info_name',null)->whereor('s.channel_info_name',['not in',['新美大','朋友圈推广','抖音']]);
            // })
            ->where('p.qqphotomoney',['<',$tiyanmoney])
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('sptend_day', 'between', [$natureStart,$natureEnd])
            ->group('sptend_day')
            ->field('sptend_day,count(c.id) as count')
            ->select();

        //改期量
        $gqlData1 = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp sp','o.orderId=sp.orderId','left')
            // ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            // ->where(function($query){
            // 	$query->where('s.channel_info_name',null)->whereor('s.channel_info_name',['not in',['新美大','朋友圈推广','抖音']]);
            // })
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('sptstate',['<>',1])
            ->where('selectday', 'between', [$start,$end])
            ->group('selectday')
            ->field('selectday,count(c.id) as count')
            ->select();
        $natureQqlData1 = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp sp','o.orderId=sp.orderId','left')
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('sptstate',['<>',1])
            ->where('selectday', 'between', [$natureStart,$natureEnd])
            ->group('selectday')
            ->field('selectday,count(c.id) as count')
            ->select();

        //总
        // $gqlData2 = Db::query("SELECT count(DISTINCT sp.orderId,selectday) as count,selectday FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customerspgq sp ON b.orderId = sp.orderId join crm_s_channel s on s.channel_name=a.customer_channel WHERE (s.channel_info_name is null or s.channel_info_name not in ('新美大','朋友圈推广','抖音')) and selectday BETWEEN '".$start."' AND '".$end."' AND sp.shop_code IN ('".$this->shop_code_ms."') AND selectday=DATE_FORMAT(qgtime,'%Y%m%d') AND CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',selectday) NOT IN (SELECT CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',selectday) FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customersp sp ON b.orderId = sp.orderId WHERE selectday BETWEEN '".$start."' AND '".$end."') group by selectday");
        //总
        $gqlData2 = Db::query("SELECT count(DISTINCT sp.orderId,selectday) as count,selectday FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customerspgq sp ON b.orderId = sp.orderId WHERE  selectday BETWEEN '".$start."' AND '".$end."' AND sp.shop_code IN ('".$this->shop_code_ms."') AND selectday=DATE_FORMAT(qgtime,'%Y%m%d') AND CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',selectday) NOT IN (SELECT CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',selectday) FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customersp sp ON b.orderId = sp.orderId WHERE selectday BETWEEN '".$start."' AND '".$end."') group by selectday");

        $natureGqlData2 = Db::query("SELECT count(DISTINCT sp.orderId,selectday) as count,selectday FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customerspgq sp ON b.orderId = sp.orderId WHERE  selectday BETWEEN '".$natureStart."' AND '".$natureEnd."' AND sp.shop_code IN ('".$this->shop_code_ms."') AND selectday=DATE_FORMAT(qgtime,'%Y%m%d') AND CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',selectday) NOT IN (SELECT CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',selectday) FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customersp sp ON b.orderId = sp.orderId WHERE selectday BETWEEN '".$natureStart."' AND '".$natureEnd."') group by selectday");
        $gqlData = array_merge($gqlData1,$gqlData2);

        $natureGqlData = array_merge($natureQqlData1,$natureGqlData2);

        $data = [];
        $weekData = [];
        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($zxpData as $k => $v) {
        		if($day == $v['sptend_day']){
					$data[$key]['data']['总选片'] = $v['count'];
        		}
        	}
        	foreach ($zjdData as $k2 => $v2) {
        		if($day == $v2['sptend_day']){
        			$data[$key]['data']['正价单'] = $v2['count'];
        		}
        	}
        	foreach ($tydData as $k3 => $v3) {
        		if($day == $v3['sptend_day']){
        			$data[$key]['data']['体验单'] = $v3['count'];
        		}
        	}
        	foreach ($gqlData as $k4 => $v4) {
        		if($day == $v4['selectday']){
        			$data[$key]['data']['改期量'] = $v4['count'];
        		}
        	}
        }

        $natureData = [];
        foreach ($this->natureAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$natureData[$key]['date'] = $day;
        	$natureData[$key]['day'] = date('d',strtotime($value));
        	foreach ($zxpData as $k => $v) {
        		if($day == $v['sptend_day']){
					$natureData[$key]['data']['总选片'] = $v['count'];
        		}
        	}
        	foreach ($zjdData as $k2 => $v2) {
        		if($day == $v2['sptend_day']){
        			$natureData[$key]['data']['正价单'] = $v2['count'];
        		}
        	}
        	foreach ($tydData as $k3 => $v3) {
        		if($day == $v3['sptend_day']){
        			$natureData[$key]['data']['体验单'] = $v3['count'];
        		}
        	}
        	foreach ($gqlData as $k4 => $v4) {
        		if($day == $v4['selectday']){
        			$natureData[$key]['data']['改期量'] = $v4['count'];
        		}
        	}
        }
        //周值
        $i = 0;
        foreach ($natureData as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }
        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        $jsonDataZong = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        		$jsonDataZong[$k2] += $v2 ?:0;
        	}
        }
        $returnData['data'] = $jsonData;	//数据
        $returnData['dataz'] = $jsonDataZong;	//数据
        $returnData['head'] = ['day','总选片','正价单','体验单','改期量'];
        
        return json($returnData);
	}
	//-------------------------------------- 第一部分 产能数据 开始 --------------------------------------//
	//产能数据-摄化
	public function shehua2(){
		//本月
		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));

        //自然周
        $natureStart = date('Ymd',strtotime($this->natureDate['start']));
        $natureEnd = date('Ymd',strtotime($this->natureDate['end']));

        $tiyanmoney = db('s_globalset')->value('tiyanmoney');
        //约拍量
        $deal1 = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto sp','o.orderId=sp.orderId','left')
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('sp.photodate', 'between', [$start,$end])
            ->field('count(o.orderId) as count,sp.photodate')
            ->group('sp.photodate')
            ->select();
        $returnData['sql'] = db()->getLastSql();

        $natureDeal1 = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto sp','o.orderId=sp.orderId','left')
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('sp.photodate', 'between', [$natureStart,$natureEnd])
            ->field('count(o.orderId) as count,sp.photodate')
            ->group('sp.photodate')
            ->select();

        //总
        $deal2 = Db::query("SELECT count(DISTINCT sp.orderId,photodate) as count,photodate FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customerphotogq sp ON b.orderId = sp.orderId WHERE photodate BETWEEN '".$start."' AND '".$end."' AND sp.shop_code IN ('".$this->shop_code_ms."') AND CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',photodate) NOT IN (SELECT CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',photodate) FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customerphoto sp ON b.orderId = sp.orderId WHERE photodate BETWEEN '".$start."' AND '".$end."') group by photodate");

        $natureDeal2 = Db::query("SELECT count(DISTINCT sp.orderId,photodate) as count,photodate FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customerphotogq sp ON b.orderId = sp.orderId WHERE photodate BETWEEN '".$natureStart."' AND '".$natureEnd."' AND sp.shop_code IN ('".$this->shop_code_ms."') AND CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',photodate) NOT IN (SELECT CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',photodate) FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customerphoto sp ON b.orderId = sp.orderId WHERE photodate BETWEEN '".$natureStart."' AND '".$natureEnd."') group by photodate");
		// $yplData = array_merge($deal1,$deal2);
        $returnData['sql2'] = db()->getLastSql();

        //合并数组
        if (is_array($natureDeal1) && is_array($natureDeal2)) {
	    	$natureYplData = array();
	    	foreach ($natureDeal2 as $key=>$value) {
	     		if (array_key_exists($key, $natureDeal1)) {
	      			$natureYplData[$key] = $value + $natureDeal1[$key];
	      			unset($natureDeal1[$key]);
	     		} else {
	      			$natureYplData[$key] = $value;
	     		}
	    	}
	    	$natureYplData =  $natureYplData+$natureDeal1;
	   	}

	   	//合并数组
        if (is_array($deal1) && is_array($deal2)) {
	    	$yplData = array();
	    	foreach ($deal2 as $key=>$value) {
	     		if (array_key_exists($key, $deal1)) {
	      			$yplData[$key] = $value + $deal1[$key];
	      			unset($deal1[$key]);
	     		} else {
	      			$yplData[$key] = $value;
	     		}
	    	}
	    	$yplData =  $yplData+$deal1;
	   	}

        //实拍量
        $tiyanmoney = db('s_globalset')->value('tiyanmoney');
 		$splData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto sp','o.orderId=sp.orderId','left')
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where(function ($query) {
			    $query->where('photostate', 1)->whereor('photostate', 2);
			})
            ->where('photodate', 'between', [$start,$end])
            ->field('photodate,count(c.id) as count')
            ->group('photodate')
            ->select();
        $natureSplData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto sp','o.orderId=sp.orderId','left')
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where(function ($query) {
			    $query->where('photostate', 1)->whereor('photostate', 2);
			})
            ->where('photodate', 'between', [$natureStart,$natureEnd])
            ->field('photodate,count(c.id) as count')
            ->group('photodate')
            ->select();
        $returnData['splSql'] = db()->getLastSql();

        //完拍量
        $wplData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto sp','o.orderId=sp.orderId','left')
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('photostate',2)
            ->where('photodate', 'between', [$start,$end])
            ->group('photodate')
            ->field('photodate,count(c.id) as count')
            ->select();
        $natureWplData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto sp','o.orderId=sp.orderId','left')
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('photostate',2)
            ->where('photodate', 'between', [$natureStart,$natureEnd])
            ->group('photodate')
            ->field('photodate,count(c.id) as count')
            ->select();
        $returnData['wplSql'] = db()->getLastSql();

        //正价单
        $zjdData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where('p.shop_code',['in',$this->shop_code_ms])
            ->where('qqphotomoney',['>=',$tiyanmoney])
            ->where('p.photodate', 'between', [$start,$end])
            ->group('p.photodate')
            ->field('count(o.orderId) as count,p.photodate')
            ->select();
        $natureZjdData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where('p.shop_code',['in',$this->shop_code_ms])
            ->where('qqphotomoney',['>=',$tiyanmoney])
            ->where('p.photodate', 'between', [$natureStart,$natureEnd])
            ->group('p.photodate')
            ->field('count(o.orderId) as count,p.photodate')
            ->select();
        $returnData['zjdSql'] = db()->getLastSql();

        //体验单
        $tydData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where('p.shop_code',['in',$this->shop_code_ms])
            ->where('qqphotomoney',['<',$tiyanmoney])
            ->where('p.photodate', 'between', [$start,$end])
            ->group('p.photodate')
            ->field('count(o.orderId) as count,p.photodate')
            ->select();
        $natureTydData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where('p.shop_code',['in',$this->shop_code_ms])
            ->where('qqphotomoney',['<',$tiyanmoney])
            ->where('p.photodate', 'between', [$natureStart,$natureEnd])
            ->group('p.photodate')
            ->field('count(o.orderId) as count,p.photodate')
            ->select();
        $returnData['tydSql'] = db()->getLastSql();

        $tyspData = Db::query("select p.orderId,currentfc,photostate,photodate,
                (select ifnull(sum(yejiprice),0) from c_consumptioncontent c where c.orderId = p.orderId and c.servicenumber = p.currentfc ) as yeji,
                (select ifnull(sum(qqphotomoney),0) from c_customerphoto pp where pp.orderId = p.orderId and pp.currentfc = p.currentfc ) as sqqphotomoney, 
                (select ifnull(count(id),0) from c_customerphoto ppp where ppp.orderId = p.orderId and ppp.currentfc = p.currentfc and ppp.photostate='2') as paiwan 
                from c_customerphoto p join c_customerorder b on p.orderId = b.orderId join c_customerinfo a on a.customerid = b.customerid WHERE (p.shop_code = '".$this->shop_code_ms[0]."' or p.shop_code = '".$this->shop_code_wx[0]."') and p.photodate between '".$start."' and '".$end."' and photostate in ('1','2')");
        $natureTyspData = Db::query("select p.orderId,currentfc,photostate,photodate,
                (select ifnull(sum(yejiprice),0) from c_consumptioncontent c where c.orderId = p.orderId and c.servicenumber = p.currentfc ) as yeji,
                (select ifnull(sum(qqphotomoney),0) from c_customerphoto pp where pp.orderId = p.orderId and pp.currentfc = p.currentfc ) as sqqphotomoney, 
                (select ifnull(count(id),0) from c_customerphoto ppp where ppp.orderId = p.orderId and ppp.currentfc = p.currentfc and ppp.photostate='2') as paiwan 
                from c_customerphoto p join c_customerorder b on p.orderId = b.orderId join c_customerinfo a on a.customerid = b.customerid WHERE (p.shop_code = '".$this->shop_code_ms[0]."' or p.shop_code = '".$this->shop_code_wx[0]."') and p.photodate between '".$natureStart."' and '".$natureEnd."' and photostate in ('1','2')");
        $returnData['tyspSql'] = "select p.orderId,currentfc,photostate,photodate,
                (select ifnull(sum(yejiprice),0) from c_consumptioncontent c where c.orderId = p.orderId and c.servicenumber = p.currentfc ) as yeji,
                (select ifnull(sum(qqphotomoney),0) from c_customerphoto pp where pp.orderId = p.orderId and pp.currentfc = p.currentfc ) as sqqphotomoney, 
                (select ifnull(count(id),0) from c_customerphoto ppp where ppp.orderId = p.orderId and ppp.currentfc = p.currentfc and ppp.photostate='2') as paiwan 
                from c_customerphoto p join c_customerorder b on p.orderId = b.orderId join c_customerinfo a on a.customerid = b.customerid WHERE (p.shop_code = '".$this->shop_code_ms[0]."' or p.shop_code = '".$this->shop_code_wx[0]."') and p.photodate between '".$start."' and '".$end."'";

        $data = [];
        $weekData = [];
        $ypList = [];
        $wpList = [];
        $spList = [];

        $tyspList = [];
        $tywpList = [];
        $zjspList = [];
        $zjwpList = [];

        $tyspLists = [];
        $month = [];

        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$month[] = date('d',strtotime($value));
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($yplData as $k => $v) {
        		if($day == $v['photodate']){
					$data[$key]['data']['约拍量'] = $v['count'];
					$ypList[$key] = $v['count'];
        		}elseif(!isset($ypList[$key])){
					$ypList[$key] = 0;
        		}
        	}
        	foreach ($splData as $k2 => $v2) {
        		if($day == $v2['photodate']){
        			$data[$key]['data']['实拍量'] = $v2['count'];
        			$wpList[$key] = $v2['count'];
        		}elseif(!isset($wpList[$key])){
					$wpList[$key] = 0;
        		}
        	}
        	foreach ($wplData as $k3 => $v3) {
        		if($day == $v3['photodate']){
        			$data[$key]['data']['完拍量'] = $v3['count'];
        			$spList[$key] = $v3['count'];
        		}elseif(!isset($spList[$key])){
					$spList[$key] = 0;
        		}
        	}
        	foreach ($tyspData as $k => $v) {
        		if($v['photodate'] == $day){
        			if($v['paiwan'] > 0 && $v['sqqphotomoney'] < $tiyanmoney){
        				$data[$key]['data']['体验实拍单'] = $data[$key]['data']['体验实拍单']+1;
        				$tyspList[$day] = $tyspList[$day]+1;
        				$tyspLists[$day]['orderId'][] = $v['orderId'];
        			}elseif($v['paiwan'] <= 0 && $v['yeji'] < $tiyanmoney){
        				$data[$key]['data']['体验实拍单'] = $data[$key]['data']['体验实拍单']+1;
						$tyspList[$day] = $tyspList[$day]+1;
						$tyspLists[$day]['orderId2'][] = $v['orderId'];
        			}elseif(!isset($data[$key]['data']['体验实拍单'])){
        				$data[$key]['data']['体验实拍单'] = 0;
        				$tyspList[$day] = 0;
        			}

        			if($v['paiwan'] > 0 && $v['sqqphotomoney'] < $tiyanmoney && $v['photostate'] == 2){
        				$data[$key]['data']['体验完拍单'] = $data[$key]['data']['体验完拍单']+1;
        				$tywpList[$day] = $tywpList[$day]+1;
        			}elseif(!isset($data[$key]['data']['体验完拍单'])){
        				$tywpList[$day] = 0;
        				$data[$key]['data']['体验完拍单'] = 0;
        			}

        			if($v['paiwan'] > 0 && $v['sqqphotomoney'] >= $tiyanmoney){
        				$zjspList[$day] = $zjspList[$day]+1;
        				$data[$key]['data']['正价实拍单'] = $data[$key]['data']['正价实拍单']+1;
        			}elseif($v['paiwan'] <= 0 && $v['yeji'] >= $tiyanmoney){
						$zjspList[$day] = $zjspList[$day]+1;
						$data[$key]['data']['正价实拍单'] = $data[$key]['data']['正价实拍单']+1;
        			}elseif(!isset($data[$key]['data']['正价实拍单'])){
        				$zjspList[$day] = 0;
        				$data[$key]['data']['正价实拍单'] = 0;
        			}
        			
        			if($v['paiwan'] > 0 && $v['sqqphotomoney'] >= $tiyanmoney && $v['photostate'] == 2){
        				$data[$key]['data']['正价完拍单'] = $data[$key]['data']['正价完拍单']+1;
        				$zjwpList[$day] = $zjwpList[$day]+1;
        			}elseif(!isset($data[$key]['data']['正价完拍单'])){
        				$data[$key]['data']['正价完拍单'] = 0;
        				$zjwpList[$day] = 0;
        			}

        		}else{
        			if(!isset($tyspList[$day])){
        				$tyspList[$day] = 0;
        			}
        			if(!isset($tywpList[$day])){
        				$tywpList[$day] = 0;
        			}
        			if(!isset($zjspList[$day])){
        				$zjspList[$day] = 0;
        			}
        			if(!isset($zjwpList[$day])){
        				$zjwpList[$day] = 0;
        			}
        		}
        	}
        }

        $natureData = [];
        foreach ($this->natureAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$natureData[$key]['date'] = $day;
        	$natureData[$key]['day'] = date('d',strtotime($value));
        	foreach ($natureYplData as $k => $v) {
        		if($day == $v['photodate']){
					$natureData[$key]['data']['约拍量'] = $v['count'];
        		}
        	}
        	foreach ($natureSplData as $k2 => $v2) {
        		if($day == $v2['photodate']){
        			$natureData[$key]['data']['实拍量'] = $v2['count'];
        		}
        	}
        	foreach ($natureWplData as $k3 => $v3) {
        		if($day == $v3['photodate']){
        			$natureData[$key]['data']['完拍量'] = $v3['count'];
        		}
        	}
        	foreach ($natureTyspData as $k => $v) {
        		if($v['photodate'] == $day){
        			if($v['paiwan'] > 0 && $v['sqqphotomoney'] < $tiyanmoney){
        				$natureData[$key]['data']['体验实拍单'] = $natureData[$key]['data']['体验实拍单']+1;
        			}elseif($v['paiwan'] <= 0 && $v['yeji'] < $tiyanmoney){
        				$natureData[$key]['data']['体验实拍单'] = $natureData[$key]['data']['体验实拍单']+1;
        			}elseif(!isset($natureData[$key]['data']['体验实拍单'])){
        				$natureData[$key]['data']['体验实拍单'] = 0;
        			}

        			if($v['paiwan'] > 0 && $v['sqqphotomoney'] < $tiyanmoney && $v['photostate'] == 2){
        				$natureData[$key]['data']['体验完拍单'] = $natureData[$key]['data']['体验完拍单']+1;
        			}elseif(!isset($natureData[$key]['data']['体验完拍单'])){
        				$natureData[$key]['data']['体验完拍单'] = 0;
        			}

        			if($v['paiwan'] > 0 && $v['sqqphotomoney'] >= $tiyanmoney){
        				$natureData[$key]['data']['正价实拍单'] = $natureData[$key]['data']['正价实拍单']+1;
        			}elseif($v['paiwan'] <= 0 && $v['yeji'] >= $tiyanmoney){
						$natureData[$key]['data']['正价实拍单'] = $natureData[$key]['data']['正价实拍单']+1;
        			}elseif(!isset($natureData[$key]['data']['正价实拍单'])){
        				$natureData[$key]['data']['正价实拍单'] = 0;
        			}
        			
        			if($v['paiwan'] > 0 && $v['sqqphotomoney'] >= $tiyanmoney && $v['photostate'] == 2){
        				$natureData[$key]['data']['正价完拍单'] = $natureData[$key]['data']['正价完拍单']+1;
        			}elseif(!isset($natureData[$key]['data']['正价完拍单'])){
        				$natureData[$key]['data']['正价完拍单'] = 0;
        			}
        		}
        	}
        }
        //周值
        $i = 0;
        foreach ($natureData as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }
        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        $jsonDataZong = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        		$jsonDataZong[$k2] += $v2 ?:0;
        	}
        }
        $returnData['data'] = $jsonData;	//数据
        $returnData['dataz'] = $jsonDataZong;	//数据
        $returnData['head'] = ['day','约拍量','实拍量','完拍量','正价完拍单','正价实拍单','体验完拍单','体验实拍单'];

        $returnData['month'] = $month;
        $returnData['ypList'] = array_values($ypList);
        $returnData['wpList'] = array_values($wpList);
        $returnData['spList'] = array_values($spList);
        $returnData['tyspList'] = array_values($tyspList);
        $returnData['tywpList'] = array_values($tywpList);
        $returnData['zjspList'] = array_values($zjspList);
        $returnData['zjwpList'] = array_values($zjwpList);
        
        return json($returnData);
	}
	//产能数据-上门
	public function shangmen2(){
		//本月
		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));

        //自然周
        $natureStart = date('Ymd',strtotime($this->natureDate['start']));
        $natureEnd = date('Ymd',strtotime($this->natureDate['end']));
        
        $staffData = db('c_staffinfo')->where('department_name','爱婴部')->field('staffname')->select();
        $staff = array_column($staffData, 'staffname');
        //约拍量
        $yplData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where('cameraman',['in',$staff])
            ->where('p.photodate', 'between', [$start,$end])
            ->field('count(o.orderId) as count,p.photodate')
            ->group('p.photodate')
            ->select();

        $natureYplData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where('cameraman',['in',$staff])
            ->where('p.photodate', 'between', [$natureStart,$natureEnd])
            ->field('count(o.orderId) as count,p.photodate')
            ->group('p.photodate')
            ->select();

        //实拍量
        $splData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where('cameraman',['in',$staff])
            ->where('photostate',1)
            ->whereOr('photostate',2)
            ->where('p.photodate', 'between', [$start,$end])
            ->field('count(o.orderId) as count,p.photodate')
            ->group('p.photodate')
            ->select();

        $natureSplData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where('cameraman',['in',$staff])
            ->where('photostate',1)
            ->whereOr('photostate',2)
            ->where('p.photodate', 'between', [$natureStart,$natureEnd])
            ->field('count(o.orderId) as count,p.photodate')
            ->group('p.photodate')
            ->select();

        //选片量
        $xplData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp p','o.orderId=p.orderId ','left')
            ->where('selectman',['in',$staff])
            ->where('p.sptend_day', 'between', [$start,$end])
            ->field('count(o.orderId) as count,p.sptend_day')
            ->group('p.sptend_day')
            ->select();

        $natureXplData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp p','o.orderId=p.orderId ','left')
            ->where('selectman',['in',$staff])
            ->where('p.sptend_day', 'between', [$natureStart,$natureEnd])
            ->field('count(o.orderId) as count,p.sptend_day')
            ->group('p.sptend_day')
            ->select();
        
        $data = [];
        $weekData = [];
        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($yplData as $k => $v) {
        		if($day == $v['sptend_day']){
					$data[$key]['data']['约拍量'] = $v['count'];
        		}
        	}
        	foreach ($splData as $k2 => $v2) {
        		if($day == $v2['sptend_day']){
        			$data[$key]['data']['实拍量'] = $v2['count'];
        		}
        	}
        	foreach ($xplData as $k3 => $v3) {
        		if($day == $v3['sptend_day']){
        			$data[$key]['data']['选片量'] = $v3['count'];
        		}
        	}
        }

        $natureData = [];
        foreach ($this->natureAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$natureData[$key]['date'] = $day;
        	$natureData[$key]['day'] = date('d',strtotime($value));
        	foreach ($yplData as $k => $v) {
        		if($day == $v['sptend_day']){
					$natureData[$key]['data']['约拍量'] = $v['count'];
        		}
        	}
        	foreach ($splData as $k2 => $v2) {
        		if($day == $v2['sptend_day']){
        			$natureData[$key]['data']['实拍量'] = $v2['count'];
        		}
        	}
        	foreach ($xplData as $k3 => $v3) {
        		if($day == $v3['sptend_day']){
        			$natureData[$key]['data']['选片量'] = $v3['count'];
        		}
        	}
        }
        //周值
        $i = 0;
        foreach ($natureData as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }
        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        	}
        }
        $returnData['data'] = $jsonData;	//数据
        $returnData['head'] = ['day','约拍量','实拍量','选片量'];
        
        return json($returnData);
	}
	//暂弃用
	public function shichang2(){
		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));
        
        // $start = '20200101';
        // $end = '20210131';
        //库存金额
      	//.............

        //单次库存
        $tiyanmoney = db('s_globalset')->value('tiyanmoney');
 		$splData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto sp','o.orderId=sp.orderId','left')
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('photostate',1)
            ->whereOr('photostate',2)
            ->where('photodate', 'between', [$start,$end])
            ->field('photodate,count(c.id) as count')
            ->select();

        //完拍量
        $wplData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto sp','o.orderId=sp.orderId','left')
            ->where('sp.shop_code',['in',$this->shop_code_ms])
            ->where('photostate',2)
            ->where('photodate', 'between', [$start,$end])
            ->field('photodate,count(c.id) as count')
            ->select();

        //正价单
        $zjdData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where('p.shop_code',['in',$this->shop_code_ms])
            ->where('qqphotomoney',['>=',$tiyanmoney])
            ->where('p.photodate', 'between', [$start,$end])
            ->group('p.photodate')
            ->field('count(o.orderId) as count,p.photodate')
            ->select();
        //体验单
        $tydData = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customerphoto p','o.orderId=p.orderId ','left')
            ->where('p.shop_code',['in',$this->shop_code_ms])
            ->where('qqphotomoney',['<',$tiyanmoney])
            ->where('p.photodate', 'between', [$start,$end])
            ->group('p.photodate')
            ->field('count(o.orderId) as count,p.photodate')
            ->select();
        
        $data = [];
        $weekData = [];
        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($yplData as $k => $v) {
        		if($day == $v['sptend_day']){
					$data[$key]['data']['约拍量'] = $v['count'];
        		}
        	}
        	foreach ($splData as $k2 => $v2) {
        		if($day == $v2['sptend_day']){
        			$data[$key]['data']['实拍量'] = $v2['count'];
        		}
        	}
        	foreach ($wplData as $k3 => $v3) {
        		if($day == $v3['sptend_day']){
        			$data[$key]['data']['完拍量'] = $v3['count'];
        		}
        	}
        	foreach ($zjdData as $k4 => $v4) {
        		if($day == $v4['selectday']){
        			$data[$key]['data']['正价单'] = $v4['count'];
        		}
        	}
        	foreach ($tydData as $k4 => $v4) {
        		if($day == $v4['selectday']){
        			$data[$key]['data']['体验单'] = $v4['count'];
        		}
        	}
        }
        //周值
        $i = 0;
        foreach ($data as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }
        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        	}
        }
        $returnData['data'] = $jsonData;	//数据
        $returnData['head'] = ['day','约拍量','实拍量','完拍量','正价单','体验单'];
        
        return json($returnData);
	}

	public function wangxuan(){
		//本月
		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));

        //自然周
        $natureStart = date('Ymd',strtotime($this->natureDate['start']));
        $natureEnd = date('Ymd',strtotime($this->natureDate['end']));
 		
        $where['p.shop_code'] = ['in',$this->shop_code_ms];
        $list = db('c_paymentcontent')->alias('p')
            ->join('c_customerorder o','p.orderId=o.orderId','left')
            ->join('c_customerinfo c','o.customerid=c.customerid','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            ->where('s.channel_info_name',['in',['新美大','朋友圈推广','抖音']])
            ->where('p.suoshu',['in',['后期款','后期补款','二销退款']])
            ->where('p.isshishou','实收')
            ->where('p.paymentdate', 'between', [$start,$end])
            ->where($where)
            ->group('p.paymentdate,s.channel_info_name')
            ->field('sum(p.payment_money) as payment_money,p.paymentdate,s.channel_info_name')
            ->select();

        $natureList = db('c_paymentcontent')->alias('p')
            ->join('c_customerorder o','p.orderId=o.orderId','left')
            ->join('c_customerinfo c','o.customerid=c.customerid','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            ->where('s.channel_info_name',['in',['新美大','朋友圈推广','抖音']])
            ->where('p.suoshu',['in',['后期款','后期补款','二销退款']])
            ->where('p.isshishou','实收')
            ->where('p.paymentdate', 'between', [$natureStart,$natureEnd])
            ->where($where)
            ->group('p.paymentdate,s.channel_info_name')
            ->field('sum(p.payment_money) as payment_money,p.paymentdate,s.channel_info_name')
            ->select();
        $returnData['sql'] = db()->getLastSql();

        $data = [];
        $weekData = [];
        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($list as $k => $v) {
        		if($day == $v['paymentdate']){
        			if($v['channel_info_name'] == '新美大' || $v['channel_info_name'] == '抖音' || $v['channel_info_name'] == '朋友圈推广'){
						$data[$key]['data'][$v['channel_info_name']] = $v['payment_money'];
        			}
        			$data[$key]['data']['网选总实收'] += $v['payment_money'];
        		}
        	}
        }

        $natureData = [];
        foreach ($this->natureAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$natureData[$key]['date'] = $day;
        	$natureData[$key]['day'] = date('d',strtotime($value));
        	foreach ($natureList as $k => $v) {
        		if($day == $v['paymentdate']){
        			if($v['channel_info_name'] == '新美大' || $v['channel_info_name'] == '抖音' || $v['channel_info_name'] == '朋友圈推广'){
						$natureData[$key]['data'][$v['channel_info_name']] = $v['payment_money'];
        			}
        			$natureData[$key]['data']['网选总实收'] += $v['payment_money'];
        		}
        	}
        }
        //周值
        $i = 0;
        foreach ($natureData as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}	
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }
        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        $jsonDataZong = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        		$jsonDataZong[$k2] += $v2 ?:0;
        	}
        }
        $returnData['data'] = $jsonData;	//数据
        $returnData['dataz'] = $jsonDataZong;	//数据
        $returnData['head'] = ['day','网选总实收','新美大','抖音','朋友圈推广'];
        
        return json($returnData);
	}

	public function wangxuan2(){
		//本月
		$start = date('Ymd',strtotime($this->thisMonth['start']));
        $end = date('Ymd',strtotime($this->thisMonth['end']));

        //自然周
        $natureStart = date('Ymd',strtotime($this->natureDate['start']));
        $natureEnd = date('Ymd',strtotime($this->natureDate['end']));

        $where['o.shop_code'] = ['in',$this->shop_code_wx];

        $list = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp sp','o.orderId=sp.orderId','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            ->where($where)
            ->group('sptend_day,s.channel_info_name')
            // ->where('sptstate',1)
            ->where('sptend_day', 'between', [$start,$end])
            ->field('count(sp.orderId) as count,sptend_day,s.channel_info_name')
            ->select();

        $natureList = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp sp','o.orderId=sp.orderId','left')
            ->join('crm_s_channel s','s.channel_name=c.customer_channel','left')
            ->where($where)
            ->group('sptend_day,s.channel_info_name')
            // ->where('sptstate',1)
            ->where('sptend_day', 'between', [$natureStart,$natureEnd])
            ->field('count(sp.orderId) as count,sptend_day,s.channel_info_name')
            ->select();

            // echo db()->getLastSql();die;

        //网Xuan改单数
        $deal1 = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp sp','o.orderId=sp.orderId','left')
            ->where($where)
            ->where('sptstate',['<>',1])
            ->where('selectday', 'between', [$start,$end])
            ->group('selectday')
            ->column('count(o.orderId) as count,selectday','selectday');
        $natureDeal1 = db('c_customerinfo')->alias('c')
            ->join('c_customerorder o','c.customerid=o.customerid','left')
            ->join('c_customersp sp','o.orderId=sp.orderId','left')
            ->where($where)
            ->where('sptstate',['<>',1])
            ->where('selectday', 'between', [$natureStart,$natureEnd])
            ->group('selectday')
            ->column('count(o.orderId) as count,selectday','selectday');

        //总
        $deal2 = Db::query("SELECT DISTINCT count(sp.orderId) as count,selectday FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customerspgq sp ON b.orderId = sp.orderId WHERE selectday BETWEEN '".$start."' AND '".$end."' AND b.shop_code IN ('".$this->shop_code_wx."') AND selectday=DATE_FORMAT(qgtime,'%Y%m%d') AND CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',selectday) NOT IN (SELECT CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',selectday) FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customersp sp ON b.orderId = sp.orderId WHERE selectday BETWEEN '".$start."' AND '".$end."') group by selectday");
        //总
        $natureDeal2 = Db::query("SELECT DISTINCT count(sp.orderId) as count,selectday FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customerspgq sp ON b.orderId = sp.orderId WHERE selectday BETWEEN '".$natureStart."' AND '".$natureEnd."' AND b.shop_code IN ('".$this->shop_code_wx."') AND selectday=DATE_FORMAT(qgtime,'%Y%m%d') AND CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',selectday) NOT IN (SELECT CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',selectday) FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customersp sp ON b.orderId = sp.orderId WHERE selectday BETWEEN '".$natureStart."' AND '".$natureEnd."') group by selectday");

        $deal3 = [];
        foreach ($deal2 as $key => $value) {
        	$deal3[$value['selectday']] = $value['count'];
        }

        $natureDeal3 = [];
        foreach ($natureDeal2 as $key => $value) {
        	$natureDeal3[$value['selectday']] = $value['count'];
        }
        //echo "SELECT DISTINCT count(sp.orderId) as count,selectday FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customerspgq sp ON b.orderId = sp.orderId WHERE selectday BETWEEN '".$start."' AND '".$end."' AND b.shop_code IN ('".$param['shop_code_wx']."') AND selectday=DATE_FORMAT(qgtime,'%Y%m%d') AND CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',selectday) NOT IN (SELECT CONCAT(sp.orderId,'_-_',sp.currentfc,'_-_',selectday) FROM c_customerinfo a JOIN c_customerorder b ON a.customerid = b.customerid JOIN c_customersp sp ON b.orderId = sp.orderId WHERE selectday BETWEEN '".$start."' AND '".$end."') group by selectday";die;

        //合并数组
        if (is_array($deal1) && is_array($deal3)) {
	    	$arrayMerge = array();
	    	foreach ($deal3 as $key=>$value) {
	     		if (array_key_exists($key, $deal1)) {
	      			$arrayMerge[$key] = $value + $deal1[$key];
	      			unset($deal1[$key]);
	     		} else {
	      			$arrayMerge[$key] = $value;
	     		}
	    	}
	    	$arrayMerge =  $arrayMerge+$deal1;
	   	}

	   	//合并数组
        if (is_array($natureDeal1) && is_array($natureDeal3)) {
	    	$natureArrayMerge = array();
	    	foreach ($natureDeal3 as $key=>$value) {
	     		if (array_key_exists($key, $natureDeal1)) {
	      			$natureArrayMerge[$key] = $value + $natureDeal1[$key];
	      			unset($natureDeal1[$key]);
	     		} else {
	      			$natureArrayMerge[$key] = $value;
	     		}
	    	}
	    	$natureArrayMerge =  $natureArrayMerge+$natureDeal1;
	   	}

        $data = [];
        $weekData = [];
        foreach ($this->thisMonthAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$data[$key]['date'] = $day;
        	$data[$key]['day'] = date('d',strtotime($value));
        	foreach ($list as $k => $v) {
        		if($day == $v['sptend_day']){
        			if($v['channel_info_name'] == '新美大' || $v['channel_info_name'] == '抖音' || $v['channel_info_name'] == '朋友圈推广'){
						$data[$key]['data'][$v['channel_info_name']] = $v['count'];
        			}
        			$data[$key]['data']['网单总选单数']+= $v['count'];
        		}
        	}
        	foreach ($arrayMerge as $k => $v) {
        		if($day == $k){
        			$data[$key]['data']['网单改期数'] = $v;
        		}
        	}
        }


        $natureData = [];
        foreach ($this->natureAllDay as $key => $value) {
        	$day = date('Ymd',strtotime($value));
        	$natureData[$key]['date'] = $day;
        	$natureData[$key]['day'] = date('d',strtotime($value));
        	foreach ($natureList as $k => $v) {
        		if($day == $v['sptend_day']){
        			if($v['channel_info_name'] == '新美大' || $v['channel_info_name'] == '抖音' || $v['channel_info_name'] == '朋友圈推广'){
						$natureData[$key]['data'][$v['channel_info_name']] = $v['count'];
        			}
        			$natureData[$key]['data']['网单总选单数']+= $v['count'];
        		}
        	}
        	foreach ($natureArrayMerge as $k => $v) {
        		if($day == $k){
        			$natureData[$key]['data']['网单改期数'] = $v;
        		}
        	}
        }
        
        //周值
        $i = 0;
        foreach ($natureData as $key => $value) {
        	if(!empty($value['data'])){
        		foreach ($value['data'] as $k2 => $v2) {
	        		$weekData[$i][$k2] = $v2+$weekData[$i][$k2];
	        	}	
        	}else{
        		$weekData[$i][$i] = 0;
        	}
        	
        	if($value['day']%7 == 0){
        		$i++;
        	}
        }
        $returnData['weekdata'] = $weekData;	//周值

        //统计图数据格式
        $jsonData = [];
        $jsonDataZong = [];
        foreach($data as $key=>$value){
        	$jsonData[$key]['day'] = $value['day'];
        	foreach ($value['data'] as $k2 => $v2) {
        		$jsonData[$key][$k2] = $v2;
        		$jsonDataZong[$k2] += $v2 ?:0;
        	}
        }
        $returnData['data'] = $jsonData;	//数据
        $returnData['dataz'] = $jsonDataZong;	//数据
        $returnData['head'] = ['day','网单总选单数','新美大','抖音','朋友圈推广','网单改期数'];
        
        return json($returnData);
	}
	//-------------------------------------- 第一部分 产能数据 结束 --------------------------------------//
}