<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Filter\SupplierAccountFilter;
use App\Http\Services\SupplierAccountService;
use App\Http\Transformers\SupplierLogTransformer;
use App\Http\Validators\SupplierAccountValidator;
use App\Model\LogModel;
use App\Model\RedisModel;
use App\Model\SupplierAccountModel;
use App\Model\SupplierChannelModel;
use App\Model\SupplierLogModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

//通用API,比如获取品牌列表,分类列表等
class SupplierAccountApiController extends Controller
{
    public function Entrance(Request $request, $id)
    {
        $this->$id($request, $id);
    }

    //获取供应商信息变更记录
    public function GetSupplierAccountList($request)
    {
        $service = new SupplierAccountService();
        $list = $service->getSupplierAccountList($request);
        $this->response(0, 'ok', $list['data'], $list['total']);
    }

    //添加
    public function AddSupplierAccount($request)
    {
        $data = $request->only([
            'supplier_code',
            'mobile',
            'password_raw',
            'a_type',
        ]);
        $validator = new SupplierAccountValidator();
        $check = $validator->checkSave($request);
        if ($check !== true) {
            $this->response(-1, $check);
        }
        $supplierModel = new SupplierChannelModel();
        $supplierId = $supplierModel->where('supplier_code', $data['supplier_code'])->value('supplier_id');
        $data['supplier_id'] = $supplierId;
        $data['a_type'] = empty($data['a_type']) ? 0 : 1;
        $data['create_time'] = time();
        $data['create_uid'] = $request->user->userId;
        $data['password'] = Hash::make($data['password_raw']);
        $model = new SupplierAccountModel();
        $id = $model->insertGetId($data);
        if ($id) {
            //还要插入一个redis
//            $redis = new RedisModel();
            $redis = Redis::connection('user');
            $redis->hset('yunxin_api_user_mobile', $data['mobile'], $id);
            $accountService = new SupplierAccountService();
            $accountService->pushToInitData($data['supplier_code']);
            $this->response(0, '添加成功');
        }
        $this->response(-1, '添加失败');
    }

    //修改
    public function UpdateSupplierAccount($request)
    {
        $data = $request->only([
            'id',
            'supplier_code',
            'mobile',
            'password_raw',
            'a_type',
        ]);
        $validator = new SupplierAccountValidator();
        $check = $validator->checkSave($request);
        if ($check !== true) {
            $this->response(-1, $check);
        }
        $supplierModel = new SupplierChannelModel();
        $supplierId = $supplierModel->where('supplier_code', $data['supplier_code'])->value('supplier_id');
        $data['supplier_id'] = $supplierId;
        $data['a_type'] = empty($data['a_type']) ? 0 : 1;
        $data['update_time'] = time();
        $data['create_uid'] = $request->user->userId;
        $data['password'] = Hash::make($data['password_raw']);
        $model = new SupplierAccountModel();
        $result = $model->where('id', $data['id'])->update($data);
        if ($result) {
            //还要插入一个redis
            $redis = Redis::connection('user');
            $redis->hset('yunxin_api_user_mobile', $data['mobile'], $data['id']);
            $accountService = new SupplierAccountService();
            $accountService->pushToInitData($data['supplier_code']);
            $this->response(0, '修改成功');
        }
        $this->response(-1, '修改失败', $result);
    }

    //启用
    public function EnableSupplierAccount($request)
    {
        $id = $request->get('id');
        $model = new SupplierAccountModel();
        $data['update_time'] = date('Y-m-d H:i:s');
        $data['a_status'] = 1;
        $result = $model->where('id', $id)->update($data);
        if ($result) {
            $this->response(0, '启用成功');
        }
        $this->response(-1, '启用失败', $result);
    }

    //禁用
    public function DisableSupplierAccount($request)
    {
        $id = $request->get('id');
        $model = new SupplierAccountModel();
        $data['update_time'] = date('Y-m-d H:i:s');
        $data['a_status'] = 0;
        $result = $model->where('id', $id)->update($data);
        if ($result) {
            $this->response(0, '禁用成功');
        }
        $this->response(-1, '禁用失败', $result);
    }
}