<?php
class indexAction extends baseAction
{
	function index()
	{
        $PjlBase = new PjlBase();
        $res = $PjlBase->get('dl_company left join dl_company_target on dct_dco_id=dco_id', ['dl_company.dco_isyp'], sprintf(' and dco_id=%d and dct_begin <=%d and dct_end >=%d', $_SESSION['_dcoid'], time(), time()), true);
        LibTpl::Set('comArr', $res['data']);
		LibTpl::Set('title', mvc::$cfg['TITLE']);
		LibTpl::Put();
	}

	function main()
	{
		$PjlBase = new PjlBase();
		$where = sprintf(" and dwi_dco_id=%d  and dwi_dwo_uid =%d order by dwi_invcode", $_SESSION['_dcoid'], $_SESSION['_userid']);
		$res = $PjlBase->get('dl_worker_invcode', ['*'], $where);
		$invArr = $res['data'];
		LibTpl::Set('invArr', $invArr);

		$res = $PjlBase->get('dl_company left join dl_company_target on dct_dco_id=dco_id', ['*'], sprintf(' and dco_id=%d and dct_begin <=%d and dct_end >=%d', $_SESSION['_dcoid'], time(), time()), true);
		$comArr = $res['data'];

		//代理区域
		$res = $PjlBase->get('dl_company_target_pcts JOIN yp_province_city ON pc_id=dctp_pc_id', ['pc_name'], sprintf(' and dctp_dct_id=%d', $comArr['dct_id']));
		$comArr['pc_names'] = array_column($res['data'], 'pc_name');

		LibTpl::Set('comArr', $comArr);

		//我的提成
		$tjTimeArr = [
			'thism'=>date('Y-m-01').' ~ '.date('Y-m-d'),
			'lastm'=>date('Y-m-01', strtotime('-1 month')).' ~ '.date('Y-m-d', strtotime(date('Y-m-01'))-1),
			'thisy'=>date('Y-01-01').' ~ '.date('Y-m-d'),
			'total'=>''
		];

		$tjArr = [];

		$rule04 = LibFc::checkDlRule('DL04', false);
		$tjType = ['self'=>sprintf(" and dml_dco_id=%d  and dml_dwo_uid =%d", $_SESSION['_dcoid'], $_SESSION['_userid'])];
		if($rule04) $tjType['com'] = sprintf(" and dml_dco_id=%d", $_SESSION['_dcoid']);

		foreach($tjTimeArr as $k=>$v)
		{
			foreach($tjType as $kk=>$where)
			{
				if(!empty($v))
				{
					$t = explode(' ~ ', $v);
					$where .= sprintf(" and dml_atime >= %d and dml_atime <= %d", strtotime($t[0]), strtotime($t[1].' 23:59:59'));
				}
				$res = $PjlBase->get('dl_order_list', ['sum(dml_money_pay) as dml_money_pay, sum(dml_money_selftc) as dml_money_selftc, sum(dml_money_ywytc) as dml_money_ywytc'], $where, true);
				$tjArr[$kk][$k] = $res['data'];
			}
		}
		LibTpl::Set('tjTimeArr', $tjTimeArr);
		LibTpl::Set('tjArr', $tjArr);
		LibTpl::Set('title',  '工作台-'.mvc::$cfg['TITLE']);
		LibTpl::Put();
	}


	function pjlReport()
	{
		exit('暂停公开');
		$LibYpApi = new LibYpApi();
		$res = $LibYpApi->get('index/getTjReportUrl', ['site'=>'PJL', 'uid'=>$_SESSION['_userid']]);
		header("Location: ".$res['data']);
	}

	/**
	 * @method 获取最近一个月统计数据
	 * @author zhengweihua 
	 * @copyright 2023-05-22
	 */
	public function getRecentTjList()
	{	
		$Params =  mvc::$URL_PARAMS;
		$end = date('Ymd',time());
		$start = date('Ymd',strtotime("-1 month"));
		$shopId = LibFc::Int($Params['shop_id']);
		$showRes = (new BaseDb(mvc::$cfg['DB']['API_TJ']))->get('tj_pjl_shop_date',['tpsd_date','tpsd_devcode_count','tpsd_pv_count'],sprintf(" and tpsd_shop_id = %d and tpsd_date between '%s' and '%s' order by tpsd_date desc",$shopId,$start,$end));
		LibFc::ajaxJsonEncode($showRes);
	}
}
