<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-31 20:43
// +----------------------------------------------------------------------

namespace app\admin\controller\v1;


use app\BaseController;
use app\model\admin\OrderInfo;
use app\model\admin\SiteDetail;
use app\model\admin\Tx;
use app\Request;
use library\services\UtilService;
use think\Cache;

class Index extends  BaseController
{

    /**
     * 获取配置信息
     */
    public function getIndex(Request $request){
        //今日订单量
        $post['today_order_count'] = (new \app\model\admin\Order())
                                        ->where('status','>',0)
                                        ->where('sassid',$request->site['sassid'])
                                        ->whereTime('pay_time','today')
                                        ->count();
        $post['all_order_count'] = (new \app\model\admin\Order())
            ->where('sassid',$request->site['sassid'])
            ->where('status','>',0)
            ->count();
        $post['today_t_order_count'] = (new OrderInfo)
                                            ->where('status','>',0)

                                            ->where('sassid',$request->site['sassid'])
                                            ->whereTime('time','today')
                                            ->count();
        $post['all_t_order_count'] = (new OrderInfo)
            ->where('sassid',$request->site['sassid'])
            ->where('status','>',0)
            ->count();
        //销售额
        $post['today_money'] = (new \app\model\admin\Order())
            ->where('status','>',0)
            ->whereTime('pay_time','today')

            ->where('sassid',$request->site['sassid'])
            ->sum('all_price');

        $post['all_money'] = (new \app\model\admin\Order())
            ->where('sassid',$request->site['sassid'])
            ->where('status','>',0)
            ->sum('all_price');
        $post['no_order_count'] = (new \app\model\admin\Order())
            ->where('sassid',$request->site['sassid'])->where('status','=',1)->count();
        $post['order_count'] = (new \app\model\admin\Order)
            ->where('sassid',$request->site['sassid'])->where('status','>',1)->count();

        //
        $sysData = (new \app\model\system\Sys)->where('id',1)->find();
        $post['sysInfo'] = [
            'lx_name'     => $sysData['lx_name'],
            'lx_tel'      => $sysData['lx_tel'],
            'custom_tel'  => $sysData['custom_tel'],
            'custom_qq'   => $sysData['custom_qq'],
            'weixin_logo' => $sysData['weixin_logo'],
            'tag'         => $sysData['tag']
        ];

        //获取自己用户
        $post['commission_money'] = $request->site['money'];
        //提现的钱
        $money = (new Tx)->where('sassid',$request->site['sassid'])->where('type',2)->where('is_type',0)->sum('money');
        $post['tx_commission_money'] = $money;
        //已提现的钱
        $money = (new Tx)->where('sassid',$request->site['sassid'])->where('type',2)->where('is_type',1)->sum('money');
        $post['txok_commission_money'] = $money;

        return app('json')->success($post);
    }



    public function getOrderTotal(Request $request){
        [$visitDate] = UtilService::getMore([
            ['visitDate','thirtyday']
        ],$request,true);
        $order = (new \app\model\admin\Order());
        $orderInfo = (new OrderInfo());
        //30天
        $tAr = ['name'=>[],'order_count'=>[],'order_info_count'=>[]];
        if($visitDate == 'thirtyday') {
            for ($i = 30;$i>=0;$i--) {
                $time = date('Y-m-d',strtotime('-'.$i.' day'));
                $tAr['name'][] = $time;
                $tAr['order_count'][] = $order
                                        ->whereDay('time',$time)
                                        ->where('sassid',$request->site['sassid'])
                                        ->where('status','>',0)
                                        ->count();
                $tAr['order_info_count'][] = $orderInfo->where('sassid',$request->site['sassid'])->where('status','>',0)->whereDay('time',$time)->count();
            }
        }
        return app('json')->success($tAr);
    }


    /**
     * 资金情况
     * @param Request $request
     */
    public function rechargeInit(Request $request) {
        $info = [
            'money' => $request->site['money'],
        ];
        return app('json')->success($info);
    }


    /**
     * 我要提现
     * @param Request $request
     */
    public function subTx(Request $request) {
        $redis = \think\facade\Cache::store('redis');
        $post =  UtilService::getMore(
            [
                ['bank',"","empty","请输入到账账号"],
                ['money','0',"empty","请输入提现金额"],
                ['name','','empty','请输入真实姓名']
            ],$request
        );
        $txKey = 'tx_'.md5($post['bank']);
        $ckBank = $redis->get($txKey);
        if(!empty($ckBank)) {
            return app('json')->fail("请务重复提现！");
        }
        //提现金额
        if($request->site['money'] < $post['money']) {
            return app('json')->fail("余额不足，无法提现！");
        }
        $redis->set($txKey,1,5);
        (new Tx)
            ->insert([
                'uid'     => 0,
                'money'  => $post['money'],
                'sassid' => $request->site['sassid'],
                'type'   => 2,
                'bank'   => '支付宝',
                'name'   => $post['name'],
                'code'   => $post['bank'],
                'time'   => time(),
                'is_type' => 0,
                'sassid' => $request->site['sassid']
            ]);
        (new \app\model\api\SiteDetail)->tx($post['money'],$request->site['sassid'],['time'=>date('Y-m-d H:i:s'),'money'=>$post['money']]);
        $redis->delete($txKey);
        return app('json')->success(
            '提现成功，请等待打款！'
        );
    }

}