<?php

namespace app\admin\controller;

use think\Validate;
use think\Lang;

class Recharge extends AdminControl
{

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/member.lang.php');
    }

    /**
     * 充值列表
     * @return mixed
     */
    public function index()
    {
        $model_recharge = Model('Recharge');
        $title = input('post.title');
        $time = input('post.timeRang');
        if(!empty($time)){
            $gap = explode(' - ', $time);
            $begin = $gap[0];
            $end = $gap[1];
        }
        $condition = array();
        if($title){
            $condition['user_email|user_phone'] = $title;
        }
        if($time && $begin && $end){
            $condition['recharge_time'] = array('between', array($begin, $end));
        }
        $recharge_list = $model_recharge->getRechargeList($condition, '*', 10);
        foreach ($recharge_list as $key => $value) {
            if($value['recharge_type'] == 1){
                $recharge_list[$key]['recharge_type'] = '支付宝';
            }
            if($value['recharge_type'] == 2){
                $recharge_list[$key]['recharge_type'] = '微信';
            }
            if($value['recharge_type'] == 3){
                $recharge_list[$key]['recharge_type'] = '银行卡';
            }

            if($value['recharge_status'] == 1){
                $recharge_list[$key]['recharge_status'] = '已审核';
            }
            if($value['recharge_status'] == 2){
                $recharge_list[$key]['recharge_status'] = '未审核';
            }
            if($value['recharge_status'] === 0){
                $recharge_list[$key]['recharge_status'] = '无效';
            }
        }
        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        $this->assign('recharge_list', $recharge_list);
        $this->assign('show_page', $model_recharge->page_info->render());
        $this->setAdminCurItem('index');
        return $this->fetch();
    }
    public function examine()
    {
        $model_recharge = Model('Recharge');
        $recharge_list = $model_recharge->rechargeList(['recharge_status'=>2]);
        $this->assign('recharge_list', $recharge_list);
        $this->assign('show_page',  $recharge_list->render());
        return $this->fetch();
    }
    public function putExamine()
    {
        $money = input('post.money');
        $recharge_id = input('post.recharge_id');
        if ($money && $recharge_id) {
            $model_recharge = Model('Recharge');
            $userInfo = Model('userinfo');
            $userMessage = Model('userMessage');
            $capitalMovements = Model('CapitalMovements');
            $recharge = $model_recharge->findRecharge(['recharge_id'=>$recharge_id]);
            if ($recharge->recharge_status != 2) {
                return ["msg" => "错误：该记录已审核"];
            }
            $model_recharge->startTrans();
            $userInfo->startTrans();
            $userMessage->startTrans();
            $capitalMovements->startTrans();
            try {
                $balance = $recharge->userInfo_money + $money;
                $model_recharge->updateRecharge(['recharge_id'=>$recharge_id], ['recharge_money'=>$money,'user_money'=>$balance,'recharge_status'=>1]);
                $userInfo->updateUserInfo(['user_id'=>$recharge->user_id], ['userInfo_money'=>$balance]);
                $nowDate = date('Y-m-d H:i:s');
                $userMessageData = [
                    'user_id' => $recharge->user_id,
                    'userMessage_title' => '充值消息',
                    'userMessage_content' => '您的充值订单号：' . $recharge->recharge_identity . '经管理员审核通过。',
                    'userMessage_create' => $nowDate,
                    'userMessage_update' => $nowDate,
                ];
                $userMessage->addMessage($userMessageData);
                if ($recharge->type == 1) {
                    $type = '支付宝充值';
                } else if ($recharge->type == 2) {
                    $type = '微信充值';
                } else {
                    $type = '银行卡转账';
                }
                $cplMvtData = [
                    'user_id' => $recharge->user_id,
                    'capitalMovements_describe' => $type,
                    'capitalMovements_identity' => $recharge->recharge_identity,
                    'capitalMovements_create' => $nowDate,
                    'capitalMovements_update' => $nowDate,
                    'capitalMovements_money' => $money,
                    'capitalMovements_type' => 1,
                    'capitalMovements_cash' => $balance,
                    'capitalMovements_operator' => $recharge->user_id,
                ];
                $capitalMovements->add($cplMvtData);
                $model_recharge->commit();
                $userInfo->commit();
                $userMessage->commit();
                $capitalMovements->commit();
                return ["msg" => "审核成功"];
            } catch (\Exception $e) {
                // 回滚事务
                $model_recharge->rollBack();
                $userInfo->rollBack();
                $userMessage->rollBack();
                $capitalMovements->rollBack();
                return ["msg" => "操作失败"];
            }
        } else if($recharge_id) {
            $model_recharge = Model('Recharge');
            $userMessage = Model('userMessage');
            try {
                $recharge = $model_recharge->findRecharge(['recharge_id'=>$recharge_id]);
                $model_recharge->updateRecharge(['recharge_id'=>$recharge_id], ['recharge_status'=>0]);
                $nowDate = date('Y-m-d H:i:s');
                $userMessageData = [
                    'user_id' => $recharge->user_id,
                    'userMessage_title' => '充值消息',
                    'userMessage_content' => '您的充值订单号：' . $recharge->recharge_identity . '管理员审核为无效订单，如有疑问请联系客服。',
                    'userMessage_create' => $nowDate,
                    'userMessage_update' => $nowDate,
                ];
                $userMessage->addMessage($userMessageData);
                $model_recharge->commit();
                $userMessage->commit();
                return ["msg" => "作废成功"];
            } catch (\Exception $e) {
                // 回滚事务
                $model_recharge->rollBack();
                $userMessage->rollBack();
                return ["msg" => "操作失败"];
            }
        } else {
            return ["msg" => "错误：请正确填写充值金额"];
        }
    }

}
