<?php

namespace app\admin\controller;

use app\BaseController;
use app\admin\model\DepositAddress;
use app\admin\validate\DepositAddressValidate;
use Exception;
use think\facade\Db;

class DepositAddresses extends BaseController
{

    /**
     * 区块链地址删除
     */
    function delete()
    {
        Db::startTrans();
        try {
            $params = $this->request->param();
            $count = DepositAddress::where('id', $params['id'])->delete();
            if ($count < 1) throw new Exception('操作失败');
            Db::commit();
        } catch (Exception $e) {
            Db::rollBack();
            return $this->error($e->getMessage());
        }
        return $this->success();
    }

    /**
     *  /blockChain/update 区块链地址更新
     */
    public function update()
    {
        $errors = [];
        Db::startTrans();
        try {
            $params = (new DepositAddressValidate)->post()->goCheck('edit');
            $params['operator_id'] = $this->admin_id;
            $params['security_code'] = $params['security_code'] ?? '';
            $params['withdraw_fee'] = $params['withdraw_fee'] ?? 0;
            if (!empty($params['id'])) {
                if (DepositAddress::where('id', "<>", $params['id'])
                    ->where('currency', $params['currency'])
                    ->where('network_type', $params['network_type'])->value('id')) {
                    $errors = ['currency' => $params['currency'], 'network_type' => $params['network_type']];
                    throw new Exception('区块链地址已存在');
                }
                $chain = DepositAddress::findOrFail($params['id']);
                unset($params['id']);
                $chain->update($params);
            } else {
                if (DepositAddress::where('currency', $params['currency'])
                    ->where('network_type', $params['network_type'])->value('id')) {
                    $errors = [
                        'currency' => $params['currency'],
                        'network_type' => $params['network_type']
                    ];
                    throw new Exception('区块链地址已存在');
                }
                unset($params['id']);
                DepositAddress::create($params);
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollBack();
            return $this->error($e->getMessage(), $errors);
        }
        return $this->success();
    }

    /**
     * 区块链地址列表
     */
    function index()
    {
        try {
            $page = $this->request->param('page', 1);
            $limit = $this->request->param('limit', 15);
            $query = new DepositAddress();
            $count = $query->count();
            $list = $query
                ->limit($limit)
                ->page($page)
                ->order('currency','asc')
                ->selete()->toArray();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success(['count' => $count, 'list' => $list]);


    }
}