<?php
/*
 * @Author: taowj 965042433@qq.com
 * @Date: 2023-07-14 14:58:11
 * @LastEditors: taowj 965042433@qq.com
 * @LastEditTime: 2023-07-28 13:48:54
 * @FilePath: \zhupu\application\api\controller\Order.php
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2023/7/3
 * Time: 14:04
 */

namespace app\api\controller;

use app\api\model\RechargeModel;
use app\api\model\OrderModel;
use app\common\controller\Api;
use \app\admin\model\order\Order as OrderItem;
use app\admin\model\worship\Worshipgoodsmodel;

class Order extends Api
{

    protected $noNeedLogin = [''];

    /**
     * 充值（认证）订单列表
     */
    public function getRechargeList()
    {
        $page = $this->request->post('page', 1);
        $status = $this->request->post('status', '');//all,created,paid,expired
        $where = [];
        if ($status) {
            $where['ro.status'] = $status;
        }
        $where['pl.order_type'] = 'recharge';
        $where['ro.user_id'] = $this->auth->id;
        $list = RechargeModel::orderlist($where,$page,$this->pageSize);
        if(!$list){
            $this->error('暂无充值订单');
        }
        foreach ($list as $key => $val){
                switch ($val['status']){
                    case 'created':
                        $val['status'] = '待支付';
                        break;
                    case 'paid':
                        $val['status'] = '已支付';
                        break;
                    case 'expired':
                        $val['status'] = '已失效';
                        break;
                }
        }
        $this->success('获取充值订单成功',['list' =>$list]);
    }

    /**
     * 充值（认证）订单详情
     */
    public function getRechargeDetail()
    {
        $order_id = $this->request->post('order_id');
        $where = [];
        if ($order_id) {
            $where['ro.id'] = $order_id;
        }
        $where['pl.order_type'] = 'recharge';
        $detail = RechargeModel::orderdetail($where);
        if(!$detail){
            $this->error('该充值订单不存在');
        }
        switch ($detail['status']){
            case 'created':
                $detail['status'] = '待支付';
                break;
            case 'paid':
                $detail['status'] = '已支付';
                break;
            case 'expired':
                $detail['status'] = '已失效';
                break;
        }
        switch ($detail['pay_way']){
            case 'money':
                $detail['pay_way'] = '余额支付';
                break;
            case 'weixin':
                $detail['pay_way'] = '微信支付';
                break;
            case 'alipay':
                $detail['pay_way'] = '支付宝支付';
                break;
        }
        $this->success('获取充值订单详情',['detail' =>$detail]);
    }

    /**
     * 查询普通贡品订单详情
     */
    public function worshipOrderDetail()
    {
        $order_id = $this->request->post('order_id');
        $where['o.id']=$order_id;
        $order_detail = OrderModel::orderDetail($where); //查出订单详情
        if (!$order_detail) {
            $this->error('暂无订单');
        }
        $this->success('获取订单详情成功', ['order_detail' => $order_detail]);
    }

    /**
     * 查询个人所有贡品订单
     */
    public function worshipOrderList()
    {
        //订单支付状态:0=全部,1=未支付,2=已支付,3=已退款
        $status = $this->request->post('status', 0);
        //页数
        $page = $this->request->post('page', 1);
        //订单所有者
        $where['o.user_id'] = $this->auth->id;
        if ($status == 0) {
            $where['pl.status'] = ['in', [1,2,3]];
        } else {
            $where['pl.status'] = $status;
        }
        $order_list = OrderModel::orderList($where,$page,$this->pageSize);
        if (!$order_list) {
            $this->error('暂无订单');
        }
        $this->success('获取订单列表成功', ['order_list' => $order_list]);
    }
}
