<?php
/*
 * @Descripttion:
 * @version:
 * @Author: kangkang
 * @Date: 2020-10-11 11:22:56
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-12-07 17:30:59
 */

namespace app\order\controller;

use app\manager\model\Manager;
use alipay\Alipay;
use app\order\model\CashOut as CashMoedl;
use app\order\model\User as UserModel;
use app\order\model\UserScore as ScoreModel;
use fastphp\base\Controller;
use app\agent\model\AgentScore as AgentScoreModel;
use app\agent\model\AgentUser;

class CashOutAdmin extends Controller
{
    //获取记录
    public function getData()
    {
        $cash = new CashMoedl;
        $manager_model = new Manager;

        $manager = $manager_model->getOne(['id'=>$this->userinfo['OperaterID']]);
        $where = [];
        if($manager['IsRoot'] != 1){
            $where['status'] = 0;
        }

        $list = $cash->getListPage($where);
        //提现状态
        $status = ['待处理', '同意', '拒绝'];
        foreach ($list as &$v) {
            $v['status'] = $status[$v['status']];
            $v['alipay_account'] = substr($v['alipay_account'], 0, 3) . '***' . substr($v['alipay_account'], -3);
            
        }
        $count = $cash->getCount($where);
        $data = [
            'Code' => 10000,
            'count' => $count['count'],
            'data' => $list,
        ];
        echo json_encode($data);
    }

    //获取指定页码数据
    public function pageList()
    {
        if (!$_GET) {
            die;
        }
        
        $where = [];
        $page = ($_GET['page'] - 1) * 50;
        $cash = new CashMoedl;
        $manager_model = new Manager;

        $manager = $manager_model->getOne(['id'=>$this->userinfo['OperaterID']]);
        $where = [];
        if($manager['IsRoot'] != 1){
            $where['status'] = 0;
        }
        $list = $cash->getListPage($where, '*', 'id desc', "$page,50");
        //提现状态
        $status = ['待处理', '同意', '拒绝'];
        foreach ($list as &$v) {
            $v['status'] = $status[$v['status']];
            $v['alipay_account'] = substr($v['alipay_account'], 0, 3) . '***' . substr($v['alipay_account'], -3);
            $v['username'] = substr($v['username'], 0, 3) . '***' . substr($v['username'], -3);
            
        }
        $data = [
            'Code' => 10000,
            'data' => $list,
        ];
        echo json_encode($data);
    }

    /**
     * @description: 处理订单
     * @param {type}
     * @return {type}
     */
    public function handle()
    {
        $data = json_decode(file_get_contents("php://input"), true)['info'];
        $id = $data['id'];
        unset($data['update_time']);
        unset($data['create_time']);
        unset($data['id']);
        unset($data['username']);

        $user = $this->userinfo;
        $cash = new CashMoedl;
        $cash_data = $cash->getOne(['id' => $id]);
        if ($data['status'] == '1') {

            // $param = [
            //     'out_biz_no' => '2020' . time() . rand(10000, 99999),
            //     'trans_amount' => $cash_data['money'],
            //     'product_code' => 'TRANS_ACCOUNT_NO_PWD',
            //     'biz_scene' => 'DIRECT_TRANSFER',
            //     'payee_info' => [
            //         'identity' => $cash_data['alipay_account'],
            //         'identity_type' => 'ALIPAY_LOGON_ID',
            //         'name' => $cash_data['real_name'],
            //     ],
            //     'remark' => '聚IP提现-会员' . $cash_data['username'] . '-' . $cash_data['money'] . '元',
            // ];
            // $result = Alipay::transfer($param);
            // if ($result) {
            //     $data['cash_no'] = $result->alipay_fund_trans_uni_transfer_response->out_biz_no;
            //     $data['alipay_no'] = $result->alipay_fund_trans_uni_transfer_response->order_id;
            //     $data['status'] = 1;
            // } else {
                $data['status'] = 1;
            // }
        } else if ($data['status'] == '2') {
            if ($cash_data['is_agent'] != 1){
                $user_model = new UserModel;
                $user_rest = $user_model->getOne(['id' => $data['userid']], 'RestAmount')['RestAmount'];
                $rest = $user_rest + $cash_data['money'];
                $user_model->where(['id' => $data['userid']])->update(['RestAmount' => $rest]);
                $score = new ScoreModel;
                $score_data = [
                    'UserId' => $data['userid'],
                    'ScoreType' => 9,
                    'ScoreTypeName' => '提现被拒退还',
                    'ScoreValue' => $cash_data['money'],
                    'UserName' => $cash_data['username'],
                    'OperateUserName' => $user['LoginName'],
                    'RestAmount1' => $user_rest,
                    'RestAmount2' => $rest,
                    'CreateTime' => date('Y-m-d H:i:s', time()),
                    'UpdateTime' => date('Y-m-d H:i:s', time()),
                    'TenantId' => 0,
                    'DeleteTag' => 0,
                ];
                $score->add($score_data);
            } else {
                $agent_user_model = new AgentUser;
                $agent_info = $agent_user_model->getOne(['id'=>$cash_data['userid']]);
                $user_rest = $agent_info['account'];
                $rest = $user_rest + $cash_data['money'];
                $agent_user_model->where(['id'=>$cash_data['userid']])->update(['account' => $rest]);
                $score_model = new AgentScoreModel;
                $score['agent_id'] = $cash_data['userid'];
                $score['order_id'] = 0;
                $score['score_type'] = 3;
                $score['score_value'] = $cash_data['money'];
                $score['agent_name'] = $agent_info['realname'];
                $score['op_user'] = $this->userinfo['LoginName'];
                $score['rest_amount1'] = $agent_info['account'];
                $score['rest_amount2'] = $rest;
                $score['remark'] = '提现被拒退还';
                $score_model->add($score);
            }
        }
        $data['op_user'] = $user['LoginName'];
        $status = ['0', '1', '2'];
        if (!in_array($data['status'], $status)) {
            unset($data['status']);
        }

        $up_status = $cash->where(['id' => $id])->update($data);
        $return_info = [
            'Code' => 10000,
        ];
        echo json_encode($return_info);
    }

    //获取记录
    public function search()
    {
        $data = json_decode(file_get_contents("php://input"), true)['data'];
        $where = [];
        $where_str = '';
        if (!empty($data)) {
            if (!empty($data['username'])) {
                $where['username'] = ['like', '%' . $data['username'] . '%'];
            }
            if (!empty($data['op_user'])) {
                $where['op_user'] = ['like', '%' . $data['op_user'] . '%'];
            }
            if (!empty($data['create_time']['st'])) {
                $date1 = date('Y-m-d', strtotime($data['create_time']['st']));
                $date2 = date('Y-m-d', strtotime($data['create_time']['et']));
                $where_str = " create_time>='" . $date1 . "' and create_time<='" . $date2 . "' ";
            }
            if ($data['status'] != null) {
                $where['status'] = $data['status'];
            }
            if ($data['is_agent'] != null){
                $where ['is_agent'] = $data['is_agent'];
            }
        }
        $cash = new CashMoedl;
        $list = $cash->search($where, '*', 'id desc', '100', $where_str);
        //提现状态
        $status = ['待处理', '同意', '拒绝'];
        foreach ($list as &$v) {
            $v['status'] = $status[$v['status']];
            $v['alipay_account'] = substr($v['alipay_account'], 0, 3) . '***' . substr($v['alipay_account'], -3);
        }
        $data = [
            'Code' => 10000,
            'count' => $count['count'] ?? 0,
            'data' => $list,
        ];
        echo json_encode($data);
    }

    /**
     * @description: 获取一条记录
     * @param {*}
     * @return {*}
     */
    public function getOneInfo()
    {
        $data = json_decode(file_get_contents("php://input"), true);
        $id = $data['id'];
        $cash = new CashMoedl;
        $info = $cash->getOne(['id' => $id]);
        \result($info,'',30000);
    }

    /**
     * @description: 导出数据
     * @param {*}
     * @return {*}
     */
    public function export()
    {
        $data = json_decode(file_get_contents("php://input"), true)['data'];
        $where = [];
        $where_str = '';
        if (!empty($data)) {
            if (!empty($data['username'])) {
                $where['username'] = ['like', '%' . $data['username'] . '%'];
            }
            if (!empty($data['op_user'])) {
                $where['op_user'] = ['like', '%' . $data['op_user'] . '%'];
            }
            if (!empty($data['create_time']['st'])) {
                $date1 = date('Y-m-d', strtotime($data['create_time']['st']));
                $date2 = date('Y-m-d', strtotime($data['create_time']['et']));
                $where_str = " create_time>='" . $date1 . "' and create_time<='" . $date2 . "' ";
            }
            if ($data['status'] != null) {
                $where['status'] = $data['status'];
            }
        }
        $cash = new CashMoedl;
        $list = $cash->search($where, '*', 'id desc', '10000', $where_str);
        //提现状态
        $status = ['待处理', '同意', '拒绝'];
        foreach ($list as &$v) {
            $v['status'] = $status[$v['status']];
            $v['alipay_account'] = substr($v['alipay_account'], 0, 3) . '***' . substr($v['alipay_account'], -3);
        }
        \exportToCsv('test.csv',['id','用户id','用户名','真实姓名','账单号','支付宝订单号','状态','提现金额','申请理由','客户支付宝张号','处理人','处理备注','提现时间','处理时间'],$list);
    }

}
