<?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;

class Commission extends  BaseController
{

    /**
     * 获取配置信息
     */
    public function total(Request $request)
    {
        //剩余佣金
        $post['money'] = $request->site['money'];
        //提现的钱
        $money = (new Tx)->where('sassid', $request->site['sassid'])->where('type', 2)->where('is_type', 0)->sum('money');
        $post['tx_money'] = empty($money) ? 0 : $money;
        //已提现的钱
        $money = (new Tx)->where('sassid', $request->site['sassid'])->where('type', 2)->where('is_type', 1)->sum('money');
        $post['tx_ok_money'] =empty($money) ? 0 : $money;
        $post['total'] = $post['money'] + $post['tx_money'] +  $post['tx_ok_money'];
        return app('json')->success($post);
    }


    /**
     *
     * @param Request $request
     */
    public function order(Request $request) {
        $pageCount = 10;
        $post = UtilService::getMore([
            ['page',1],
            ['order_id',''],
            ['start_time',''],
            ['end_time',''],
            ['code',''],
            ['type','']
        ],$request);
        $where = [];
        $where['s.sassid'] = $request->site['sassid'];
        if(!empty($post['order_id'])) {
            $where['s.order_id'] = ['%'.$post['order_id'].'%','whereLike'];
        }

        if(!empty($post['start_time'])) {
            $where['s.time'] = [[strtotime($post['start_time']),strtotime($post['end_time'])],'whereBetween'];
        }

        if(!empty($post['code'])) {
            $where['s.code'] = $post['code'];
        }

        if(!empty($post['type'])) {
            $where['s.type'] = $post['type'];
        }

        [$total,$data] =(new SiteDetail)->getList($post['page'],$where,$pageCount,'*','s.id desc');
        $post = [];
        $data =  UtilService::getParam([
            ['code','code'],
            ['time','time','date("Y-m-d H:i:s",$1)'],
            ['title','title'],
            ['content','content'],
            ['money','money'],
            ['v','v'],
            ['type','type'],
            ['order_id','order_id'],
            ['all_price','all_price']
        ],$data);
        $post['pageSize'] = $pageCount;
        $post['pageCount'] = $total;
        $post['data'] = (array)$data;
        return app('json')->success($post);
    }



    /**
     * 提现列表
     * @param Request $request
     */
    public function txList(Request $request) {
        $pageSize = 10;
        $post =  UtilService::getMore(
            [
                ['page',1]
            ],$request
        );
        $where['sassid'] =$request->site['sassid'];
        $where['type'] = 2;
        $tx = new Tx();
        list($pageCount,$data) = $tx->getList($post['page'],$where,$pageSize,"*","time desc");
        $result = UtilService::getParam([
            "id",
            "money",
            "bank",
            'name',
            'code',
            "is_type",
            ["time","time","date('Y-m-d H:i:s',$1)"]
        ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }

}