<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2023/6/6
 * Time: 11:11
 */
namespace app\api\controller\shopro;

use app\admin\model\shopro\order\Rechargeorder;
use app\api\model\PaymentLogModel;
use app\common\controller\Api;
use think\Db;
use think\Exception;

class Recharge extends Api{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 充值生成订单
     */
    public function addRechargeOrder(){
        $amount = $this->request->post('amount', 0);//充值金额
        $paytype = $this->request->post('pay_way', 'weixin');//支付方式:weixin=微信,money=钱包,alipay=支付宝
        try {
            Db::startTrans();
            $data = [
                'user_id' => $this->auth->id,
                'order_sn' => getOrdersn('recharge'),
                'amount' => $amount,
                'payamount' => $amount,
                'paytype' => $paytype,
                'paytime' => time(),
                'ip' => getIp(),
                'createtime' => time(),
                'updatetime' => time(),
                'status' => 'created',
            ];
            if($this->auth->pid){
                $data['share_id'] = $this->auth->share_id;
            }
            $order = Rechargeorder::create($data);
            $pay_arr = [
                'user_id' => $this->auth->id,
                'order_sn' =>$order->order_sn,
                'order_id' => $order->id,
                'order_type' => 'recharge',
                'need_pay' =>$amount,
                'pay_way' =>$paytype,
                'status' => 1,
                'createtime' => time(),
            ];
            $payment_log = PaymentLogModel::create($pay_arr);
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            $this->error('提交订单失败', $e->getMessage());
        }
        $this->success('提交订单成功', ['order_id' => $order->id, 'log' => $payment_log]);

    }

    /**
     * 充值订单记录
     */
    public function rechargeOrder(){
        $page = $this->request->param("page", 1);
        $map = ['r.user_id' =>$this->auth->id];
        $list = Rechargeorder::alias('r')
            ->join('users u','u.id = r.user_id','left')
            ->where($map)
            ->field('r.*,FROM_UNIXTIME(r.createtime,"%Y-%m-%d %H:%i") as createtime,FROM_UNIXTIME(r.paytime,"%Y-%m-%d %H:%i") as paytime,u.nickname,u.realname,u.avatar')
            ->page($page)
            ->limit($this->pageSize)
            ->order('r.createtime desc')
            ->select();

        if(!$list){
            $this->error('订单已不存在');
        }
        foreach ($list as $key => $val){
            switch ($val['status']){
                case 'created':
                    $val['status_remark'] = '待充值';
                    break;
                case 'paid':
                    $val['status_remark'] = '已支付';
                    break;
                case 'expired':
                    $val['status_remark'] = '已过期';
                    break;
            }
        }
        $this->success('获取成功',['list' =>$list]);

    }

}