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

namespace app\system\controller\v1;


use app\BaseController;
use app\model\system\SiteTotal;
use app\Request;
use library\services\UtilService;
use library\utils\alipay;

class Tx extends BaseController {


    public function init(){
        //获取全部提现
        $allTx = (new \app\model\system\Tx)->count();
        $noTx = (new \app\model\system\Tx)->where('is_type',0)->count();
        $yeTx = (new \app\model\system\Tx)->where('is_type',1)->count();
        $allTxPrice =  (new \app\model\system\Tx)->sum('money');
        return app('json')->success([
            'all_tx'    => $allTx,
            'no_tx'     => $noTx,
            'yes_tx'     => $yeTx,
            'all_tx_price' => $allTxPrice
        ]);


    }

    /**
     * 添加
     * @param Request $request
     */
    public function add(Request $request) {
        $post = UtilService::getMore(
            [
                ['id','','empty','参数错误'],
                ['mono'],
                ['type',0]
            ],$request);
       $txData =  (new \app\model\system\Tx)->where('id',$post['id'])->find();
        if(empty($txData)) {
            return app('json')->fail('数据不存在');
        }
        if(!empty($txData['is_type'])) {
            return app('json')->fail('已经确认打款了');
        }
        if($post['type'] == 1) {
            $alipaly = new alipay();
            $result = $alipaly->accountTransfer($txData['code'],$txData['money'],$txData['name']);
            if($result['code'] == 1000){
                $mono = '后台确认，操作员:'
                                . $request->adminInfo['username']
                                . (empty($post['mono'] ) ? '' : ('，备注理由:' .$post['mono'] )) ;
                (new \app\model\system\Tx)
                    ->where('id',$post['id'])
                    ->save(['is_type'=>1,
                            'mono'=>$mono,
                            'check_time'=>time(),
                            'transfer_type' => 2,
                            'pay_fund_order_id' => $result['pay_fund_order_id'],
                            'out_biz_no' => $result['out_biz_no'],
                            'order_id'  => $result['order_id']
                    ]);
            } else {
                return app('json')->fail($result['msg']);
            }
        } else {
            $mono = '后台确认，操作员:'
                . $request->adminInfo['username']
                . (empty($post['mono'] ) ? '' : ('，备注理由:' .$post['mono'] )) ;
            (new \app\model\system\Tx)
                ->where('id',$post['id'])
                ->save(['is_type'=>1,
                        'mono'=>$mono,
                        'check_time'=>time(),
                        'transfer_type' => 1,
                        'pay_fund_order_id' => '',
                        'out_biz_no' => '',
                        'order_id'  => ''
                ]);
        }
        return app('json')->success('操作成功');
    }


    /**
     * 获取订单数据
     * @param Request $request
     */
    public function list(Request $request){
        $pageSize = 50;
        $post =  UtilService::getMore(
            [
                ['page',1],
                ['orderType',''],
                ['mobile',''],
                ['type',''],
                ['uid',''],
                ['sassids','']
            ],$request
        );
        $where = $post;
        list($pageCount,$data) = (new \app\model\system\Tx)->getList($post['page'],$where,$pageSize);

        $result = UtilService::getParam([
            "id",
            "member_mobile",
            "site_name",
            "money",
            "bank",
            "name",
            "code",
            ["time","time","date('Y-m-d H:i',$1)"],
            ["check_time","check_time","date('Y-m-d H:i',$1)"],
            "is_type",
            "transfer_type",
            "pay_fund_order_id",
            "warehouse_name",
            "type",
            "mono"
        ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }

}