<?php
namespace app\controllers\merchant;
use Illuminate\Support\Facades\DB;
use Yii;
use yii\web\Controller;
use app\models\common\StoreBill;
use app\models\common\Statistics;
use app\models\common\Financial;
class IndexController  extends CommonController{

    public function actionGetMerchantRow(){
        $request = Yii::$app->request;
        if ($request->isPost){
            $uniacid=$this->wqData['uniacid'];
            $storeId=$this->wqData['storeId'];
            if(!$uniacid||!$storeId){
                return $this->result(2,'门店不存在');
            }
            $money=StoreBill::getMerchantMoney($uniacid,$storeId);
            $goodNum=StoreBill::getGoodNum($uniacid,$storeId);
            $startTime=strtotime(date("Y-m-d",time()));
            $endTime=$startTime+60*60*24;
            $revenueMoney=Statistics::revenue($uniacid,$storeId,$startTime,$endTime);
            $orderNum=Statistics::payOkNum($uniacid,$storeId,$startTime,$endTime);
            $data=array(
                'money'=>$money,
                'goodNum'=>$goodNum,
                'shopName'=>(new \yii\db\Query())
                    ->from('{{%ybwm_store}}')
                    ->where('uniacid=:uniacid AND id=:id',[':uniacid'=>$uniacid,'id'=>$storeId])
                    ->one()['name']?:'',
                'revenueMoney'=>$revenueMoney,
                'orderNum'=>$orderNum,
                'payCount'=>Financial::getEffective($startTime,$endTime,$uniacid,$storeId,[]),
                'todayTotal'=>Financial::getTurnover($startTime,$endTime,$uniacid,$storeId,[]),
            );
            return $this->result(1,'成功',$data);
        }
    }
    //门店概况
    public function actionStoreIndex(){
        $result=axios_request();
        $uniacid=$this->wqData['uniacid'];
        $storeId=$result['storeId']?:$this->wqData['storeId'];
        $storeInfo=(new \yii\db\Query())
            ->from('{{%ybwm_store}}')
            ->where(['uniacid'=>$uniacid,'id'=>$storeId])
            ->one();
        $startTime=strtotime(date("Y-m-d",time()));
        $endTime=$startTime+60*60*24;
        //今日订单数
        $sum=Statistics::getPayNum($uniacid,$storeId);
        $totalSum=Statistics::getPayNum($uniacid,$storeId,$startTime,$endTime);
        //今日营业额
        $money=Statistics::getPayMoney($uniacid,$storeId)?:0.00;
        $totalMoney=Statistics::getPayMoney($uniacid,$storeId,$startTime,$endTime);
        //付款人数
        $payUser=Statistics::getPayPeople($uniacid,$storeId);
        $totalPayUser=Statistics::getPayPeople($uniacid,$storeId,$startTime,$endTime);
        $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
        //预计收入
        $yujiSum=Statistics::revenue($uniacid,$storeId,$startTime,$endTime);
        $yesterSum=Statistics::revenue($uniacid,$storeId,$beginYesterday,$endYesterday);
        //有效订单
        $payCount=Statistics::payOkNum($uniacid,$storeId,$startTime,$endTime);
        $yesPayCount=Statistics::payOkNum($uniacid,$storeId,$beginYesterday,$endYesterday);
        //付款人数
        $userCount=Statistics::getPayPeople($uniacid,$storeId,$startTime,$endTime);
        $yesuserCount=Statistics::getPayPeople($uniacid,$storeId,$beginYesterday,$endYesterday);
        //外卖订单
        $query=(new \yii\db\Query())
            ->from('{{%ybwm_bill}}')
            ->where(['uniacid'=>$uniacid,'storeId'=>$storeId]);
        $sumMoney=$query->sum('money');
        $outMoney=$query->andWhere(['origin'=>1])->sum('money');
        $data=array(
            'totalSum'=>$totalSum,      //今日订单数
            'sum'=>$sum,           //总订单数
            'totalMoney'=>$totalMoney?:'0.00',    //今日营业额
            'money'=>$money?:'0.00',         //总营业额
            'totalPayUser'=>$totalPayUser,  //付款人数
            'payUser'=>$payUser,       //总付款人数
            'yujiSum'=>$yujiSum?:'0.00',       //预计收入
            'yesterSum'=>$yesterSum?:'0.00',     //昨日预计收入
            'payCount'=>$payCount,      //有效订单
            'yesPayCount'=>$yesPayCount,   //昨日有效订单
            'userCount'=>$userCount,     //付款人数
            'yesuserCount'=>$yesuserCount,  //昨日付款人数
            'sumMoney'=>$sumMoney?:'0.00',      //营业额
            'outMoney'=>$outMoney?:'0.00',      //外卖营业额
            'goodsModel'=>$storeInfo['goodsModel']
        );
        return $this->result(1,'成功',$data);
    }

    public function actionGetHelpList(){
        $page=Yii::$app->request->get('page')?Yii::$app->request->get('page'):1;
        $num=($page-1)*10;
        $limit=10;
        $data= (new \yii\db\Query())
            ->from('{{%ybwm_helps}}')
            ->where(['deleteAt'=>0,'uniacid'=>$this->wqData['uniacid'],'otherType'=>2])
            ->orderBy('sort asc,id desc')
            ->offset($num)
            ->limit($limit)
            ->all();
        foreach ($data as $k=>$v){
            $data[$k]['body']=$v['body'];
            $data[$k]['createdAt']=date('Y-m-d H:i:s',$v['createdAt']);
        }
        $count=(new \yii\db\Query()) ->from('{{%ybwm_notice}}')->where(['deleteAt'=>0,'uniacid'=>$this->wqData['uniacid']])->count();
        return $this->result(1,'成功',$data,$count);
    }

    public function actionGetHelpDetail(){
        $result=axios_request();
        $data= (new \yii\db\Query())
            ->from('{{%ybwm_helps}}')
            ->where(['id'=>$result['id']])
            ->one();
        $data['createdAt']=date('Y-m-d H:i:s',$data['createdAt']);
        return $this->result(1,'成功',$data);
     }
}