<?php
class pjlPcAction extends baseAction
{
	//VIP明细
	function vipList()
	{
		LibRule::Check('PC01');
		$Params = mvc::$URL_PARAMS;
		$nowPage = isset($Params['page']) && $Params['page'] > 0 ? (int) $Params['page']:1;
		$perPage = isset($_COOKIE['perPage'])? $_COOKIE['perPage'] : 20;

		$PjlBase= new PjlBase();
		$where = '';
		foreach($Params as $k=>$v)
        {
            $v = trim($v);
            if($v == '') continue;
            switch($k)
            {
				case 'betime':
					$t = explode(' ~ ', $v);
					$where .= sprintf(" and uvip_atime >= %d and uvip_atime < %d", strtotime($t[0]), strtotime($t[1])+86400);
				break;
				case 'uvip_uid':
					$where .= sprintf(" and %s = %d", $k, $v);
				break;
				case 'uvip_name':
					$where .= sprintf(" and %s = '%s'", $k, LibFc::Escape($v));
				break;
				case 'money':
					if($v == 'has') $where .= ' and uvip_paymoney > 0';
				break;
				// 添加商品详情页的直接跳转
				case 'uid':
					$where .= sprintf(" and uvip_uid = %d", $v);
				break;
            }
        }
		$table = 'pl_user_vip left join pl_shop on shop_pu_id=uvip_uid';
		
		$res = $PjlBase->get($table, ['count(*) as count'], $where, true);
		$total = $res['data']['count']; 
		$res = $PjlBase->get($table, ['*'], sprintf("%s order by uvip_id desc limit %d,%d", $where, ($nowPage-1)*$perPage, $perPage));
		$lists = $res['data'];

		$YpwUser = new YpwUser();
		$res = $YpwUser->getUserBase(array_column($lists, 'uvip_uid'));
		LibTpl::Set('userArr',$res['data']);

		$res = $PjlBase->get('pl_user_vip', ['distinct uvip_name']);
		LibTpl::Set('levelArr', $res['data']);

		$res = $PjlBase->get('pl_vip_level', ['*'], ' and vip_id > 1 order by vip_id desc');
		LibTpl::Set('allLevel', array_column($res['data'], null, 'vip_id'));

		$Page = LibPage::Show($total, $perPage);
		LibTpl::Set('Page', $Page);
		LibTpl::Set('Params', $Params);
		LibTpl::Set('lists', $lists);
		LibTpl::Set('title', 'VIP购买记录');
		LibTpl::Put();
	}


	//VIP开通情况
	function vipTj()
	{
		LibRule::Check('PT00');
		$Params = mvc::$URL_PARAMS;
		$PjlBase= new PjlBase();
		$typeArr = ['vip'=>'开通VIP', 'cz'=>'余额充值'];
		$where['vip'] =' and uvl_status = 2 AND uvl_pay_time > 0';
		$where['cz'] =' and moc_status = 2 AND moc_pay_time > 0';
		$Params['betime'] = empty($Params['betime'])? date('Y-m-d', strtotime("-1 month")).' ~ '.date('Y-m-d'): $Params['betime'];

		foreach($Params as $k=>$v)
        {
            $v = trim($v);
            if($v == '') continue;
            switch($k)
            {
				case 'betime':
					$t = explode(' ~ ', $v);
					$where['vip'] .= sprintf(" and uvl_pay_time >= %d and uvl_pay_time < %d", strtotime($t[0]), strtotime($t[1])+86400);
					$where['cz'] .= sprintf(" and moc_pay_time >= %d and moc_pay_time < %d", strtotime($t[0]), strtotime($t[1])+86400);
				break;
            }
        }

		$where['vip'] .= " GROUP BY FROM_UNIXTIME(uvl_pay_time, '%Y-%m-%d') ORDER BY FROM_UNIXTIME(uvl_pay_time, '%Y-%m-%d') desc";
		$where['cz'] .= " GROUP BY FROM_UNIXTIME(moc_pay_time, '%Y-%m-%d') ORDER BY FROM_UNIXTIME(moc_pay_time, '%Y-%m-%d') desc";
		$tjArr = [];
		$sum = [];
		foreach($typeArr as $type=>$tname)
		{
			$sum[$type] = ['paymoney'=>0.00, 'count'=>0];
			switch($type)
			{
				case 'vip':
					$res = $PjlBase->get('pl_user_vip_list', ["FROM_UNIXTIME(uvl_pay_time, '%Y-%m-%d') as pay_date, sum(uvl_paymoney) as paymoney,  count(*) as count"], $where[$type]);
				break;
				case 'cz':
					$res = $PjlBase->get('pl_money_chongzhi', ["FROM_UNIXTIME(moc_pay_time, '%Y-%m-%d') as pay_date, sum(moc_paymoney) as paymoney,  count(*) as count"], $where[$type]);
				break;
			}
			foreach($res['data'] as $v)
			{
				$sum[$type]['paymoney'] += $v['paymoney'];
				$sum[$type]['count'] += $v['count'];
				$tjArr[$v['pay_date']][$type] = $v;
			}
		}
		krsort($tjArr);
		LibTpl::Set('typeArr', $typeArr);
		LibTpl::Set('sum',$sum);
		LibTpl::Set('tjArr',$tjArr);
		LibTpl::Set('Params',$Params);
		LibTpl::Set('title','自助业务统计-跑街令');
		LibTpl::Put();
	}


	//人工开通VIP
	function manualOpenPcVip()
	{
		LibRule::Check('PC02');
		$LibYpApi = new LibYpApi();
		$data = $_POST;
		$data['remark'] = mvc::$cfg['LOGUSER']['urname'].'人工开通。'.$data['remark'];
		$res = $LibYpApi->Get('pjl/manualOpenPcVip', [], $data);
		LibFc::ajaxJsonEncode($res);
	}

	/**
	 * @method 分配日志
	 * @author hongzhang
	 * @copyright 2024-09-10
	 */
	public function assignLog()
	{
		LibRule::Check('PC01');
		$Params = mvc::$URL_PARAMS;
		$ccfTypeArr = ['general'=>'general', 'service'=>'service','register'=>'register'];
		$table = 'crm_customer_fenpei ccf
		left join crm_customer ctr on ccf.ccf_ctr_id = ctr.ctr_id
		left join yp_paojieling.dl_worker dw1 on ccf.ccf_old_au_uid = dw1.dwo_uid
		left join yp_paojieling.dl_worker dw2 on ccf.ccf_new_au_uid = dw2.dwo_uid';
		$feilds = ['ccf.ccf_ctr_id','ccf.ccf_old_au_uid','ccf.ccf_new_au_uid','ccf.ccf_type','ccf.ccf_remark','ccf.ccf_atime','ctr.ctr_comname','dw1.dwo_name as old_name','dw2.dwo_name as new_name'];
		$where = '';
		$order = 'order by ccf_atime DESC';
		$perPage = isset($_COOKIE['perPage']) ? $_COOKIE['perPage'] : 20;
		$page = isset($Params['page']) ? $Params['page'] : 1;
		foreach ($Params as $key => $val) {
			if(!$val) continue;
			$val =  LibFc::Escape($val);
			switch ($key) {
				case 'ctr_comname':
					$where .=sprintf(" AND ctr.ctr_comname like '%%%s%%'",$val);
					break;
				case 'dwo_name':
					$where .=sprintf(" AND (dw1.dwo_name like '%%%s%%' or dw2.dwo_name like '%%%s%%')",$val,$val);
					break;
				case 'ccf_type':
					$where .=sprintf(" AND ccf.ccf_type = '%s'",$val);
					break;
				case 'ccf_atime':
					$t = explode(' - ', (string)$val);
					$where .= sprintf(" AND ccf.ccf_atime >= %d and ccf.ccf_atime < %d", strtotime($t[0]), strtotime($t[1])+86400);
					break;
			}
		}

		$CrmBase = new CrmBase();
		$res = $CrmBase->get($table, ['count(*) as counts'], $where,true);
		$list = [];
		$total = $res['data']['counts'];
		if ($total> 0) {
			$res = $CrmBase->get($table,$feilds,sprintf("%s %s limit %d,%d",$where,$order,($page-1)*$perPage,$perPage));
			$list = $res['data'];
		}
		LibTpl::Set('Params',$Params);
		LibTpl::Set('Page',LibPage::Show($total,$perPage,true));
		LibTpl::Set('list',$list);
		LibTpl::Set('ccfTypeArr', $ccfTypeArr);
		LibTpl::Set('title', '分配日志');
		LibTpl::Put();
	}

}