<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Exception;

class Recharge extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';


    //首页
    public function getList(){

        $where = [
            'company_id' => $this->auth->company_id,
            'status'     => 1,
        ];
        $field = 'id,price,giftprice';
        $list = Db::name('recharge_config')->field($field)->where($where)->order('id desc')->select();

        //追加赠送
        if(!empty($list)){
            $config_ids = array_column($list,'id');
            $g = 'gift';
            $c = 'coupons';
            $gift = Db::name('recharge_gift')->alias($g)
                ->field($g.'.config_id,'.$g.'.coupon_id,'.$g.'.number,coupons.name,coupons.info,coupons.days')
                ->join($c,$c.'.id = '.$g.'.coupon_id' ,'LEFT')
                ->where($g.'.config_id','IN',$config_ids)
                ->where($c.'.status',1)
                ->select();

            foreach($list as $key => &$val){
                $val['gift'] = [];
                foreach($gift as $k => $v){
                    if($val['id'] == $v['config_id']){
                        unset($v['config_id']);
                        $val['gift'][] = $v;
                    }
                }
            }
        }
        $this->success(1,$list);
    }

    /**
     * 充值
     * @return void
     */
    /*public function add()
    {
        Db::startTrans();
        try {
            //验证参数
            $id = $this->request->param('id',0);
            $amounts = $this->request->param('amounts',0.00);
            if (empty($id) && empty($amounts)) {
                throw new Exception('参数错误');
            }
            if (!empty($id)) {

            } else {
                $isInt = is_int($amounts);
                if (!$isInt) {
                    throw new Exception('请输入整数');
                }
                if ($amounts < 1) {
                    throw new Exception('充值金额有误');
                }
            }

            $userId = $this->auth->id;
            $time = time();
            $data = [
                'car_number' => $this->request->param('car_number', ''),
                'car_model'  => $this->request->param('car_model', ''),
            ];
            $data['user_id'] = $userId;
            $data['createtime'] = $time;
            $res = $this->model->insertGetId($data);
            if (!$res) {
                throw new Exception('操作失败');
            }
            Db::commit();
            $this->success('操作成功');
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }*/
}