<?php

namespace app\ros\controller;

use app\ros\model\Buyer as BuyerModel;
use fastphp\base\Controller;

class Buyer extends Controller
{
    public function __construct()
    {
        parent::__construct('','');
        if (empty($this->userinfo['UserId'])) {
            die;
        }
    }

    //获取产品列表
    public function getBuyerInfo() {
        $buyer_model = new BuyerModel();

        $info = $buyer_model->getOne(['user_id'=>$this->userinfo['UserId']]);
        echo json_encode($info);
    }

    public function updateBuyerInfo() {
        $userinfo = json_decode(file_get_contents("php://input"),true)['user_info'];
        $userinfo['user_id'] = $this->userinfo['UserId'];

        if (empty($userinfo['name']) || empty($userinfo['phone']) || empty($userinfo['address'])) {
            echo json_encode(['code' => -1]);
            die;
        }

        $buyer_model = new BuyerModel();

        $info = $buyer_model->getOne(['user_id'=>$this->userinfo['UserId']]);

        $flag = false;
        if (empty($info)) {
            $flag = $buyer_model->add($userinfo);
        } else {
            $flag = $buyer_model->updateOne(['user_id'=>$this->userinfo['UserId']],$userinfo);
        }
        if ($flag) {
            echo json_encode(['code' => 1]);
        } else {
            echo json_encode(['code' => -1]);
        }
    }
}