<?php
/*
 * @Author: your name
 * @Date: 2020-10-10 13:58:14
 * @LastEditTime: 2023-07-29 18:05:41
 * @LastEditors: “wanyongkang” “937888580@qq.com”
 * @Description: In User Settings Edit
 * @FilePath: /phptest/app/order/controller/Index.php
 */

namespace app\order\controller;

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 extend\alisms\Sms;
use app\manager\model\Manager;

class CashOutIndex extends Controller
{
    //接收提现请求
    public function index()
    {
        $post = json_decode(file_get_contents("php://input"), true);
        $data = [];
        $data['userid'] = $this->userinfo['UserId'];
        $data['username'] = $this->userinfo['LoginName'];
        $data['money'] = $post['cash_out_money'];
        $data['apply_reason'] = $post['reason'];
        $data['alipay_account'] = $post['alipay_account'];
        $data['real_name'] = $post['real_name'];
        $user = new UserModel;
        $user_one_info = $user->getOne(['id'=>$data['userid']],'RestAmount,ManagerId');
        $user_rest = $user_one_info['RestAmount'];
        $rest = $user_rest - abs($post['cash_out_money']);

        if($data['money']<=0){
            die;
        }
        if ($rest < 0){
            echo  json_encode(['Code'=>-10000,]);
            die;
        } else {
            $user->where(['id'=>$data['userid']])->update(['RestAmount'=>$rest]);
            $cash = new CashMoedl;
            $status = $cash->add($data);
            $score = new ScoreModel;
            $score_data = [
                'UserId' => $data['userid'],
                'ScoreType' => 8,
                'ScoreTypeName' => '用户提现扣除',
                'ScoreValue' => $post['cash_out_money'],
                'UserName' => $data['username'],
                'OperateUserName' => $data['username'],
                '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);
        }
        echo  json_encode(['Code'=>10000,]);
        $h = date('H');
        if ($h>8 && $h<23) {
            $alisms = new Sms;
            $manager_model = new Manager;
            $manager_info = $manager_model->getOne(['id' => $user_one_info['ManagerId']]);
            if ($manager_info['RoleId'] == 100 && $manager_info['TenantId'] == 1) {
                $alisms::sendToCashout($manager_info['Phone'], ['name'=>$manager_info['RealName'],'phone'=>$data['username']]);
            }
        }
    }

    //获取记录
    public function getData()
    {
        $cash = new CashMoedl;
        $list = $cash->getListPage(['userid'=>$this->userinfo['UserId']],'username,money,remark,apply_reason,alipay_account,status,create_time');
        //提现状态
        $status = ['待处理', '提现成功', '提现未成功'];
        foreach ($list as &$v) {
            $v['status'] = $status[$v['status']];
            $v['alipay_account'] = substr($v['alipay_account'], 0, 3) . '***' . substr($v['alipay_account'], -5);
        }
        $count = $cash->getCount();
        $data = [
            'Code' => 10000,
            'count' => $count['count'],
            'data' => $list,
        ];
        echo json_encode($data);
    }

}
