<?php

namespace app\admin\controller;

use app\BaseController;
use app\admin\model\Currency as CurrencyModel;
use app\admin\validate\CurrencyValidate;
use Exception;
use think\facade\Db;

class Currency extends BaseController
{

    /**
     * 删除
     */
    function delete()
    {
        Db::startTrans();
        try {
            $id = $this->request->param('id');
            $count = CurrencyModel::where('id', $id)->delete();
            if ($count < 1) throw new Exception('操作失败');
            Db::commit();
        } catch (Exception $e) {
            Db::rollBack();
            return $this->error($e->getMessage());
        }
        return $this->success('删除成功');
    }

    /**
     * 更新
     */
    public function update()
    {
        $errors = [];
        Db::startTrans();
        try {
            $params = (new CurrencyValidate)->post()->goCheck('edit');
            if (!empty($params['id'])) {
                if (CurrencyModel::where('id', "<>", $params['id'])
                    ->where('currency', $params['currency'])->value('id')) {
                    $errors = ['currency' => $params['currency']];
                    throw new Exception('已存在');
                }
                $chain = CurrencyModel::where('id', $params['id'])->find();
                if (!$chain) {
                    $errors = ['id' => $params['id']];
                    throw new Exception('不存在');
                }
                unset($params['id']);
                $chain->update($params);
            } else {
                if (CurrencyModel::where('currency', $params['currency'])->value('id')) {
                    $errors = [
                        'currency' => $params['currency'],
                    ];
                    throw new Exception('已存在');
                }
                unset($params['id']);
                CurrencyModel::create($params);
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollBack();
            return $this->error($e->getMessage(), $errors);
        }
        return $this->success();
    }

    /**
     * 列表
     */
    function list()
    {
        try {
            $params = $this->request->param();
            $page = $params['page'] ?? 1;
            $limit = $params['limit'] ?? 15;
            $query = new CurrencyModel();
            if (!empty($params['currency'])) {
                $query->where('currency', 'like', "%{$params['currency']}%");
            }
            if (!empty($params['short_currency'])) {
                $query->where('short_currency', 'like', "%{$params['short_currency']}%");
            }
            if (!empty($params['id'])) {
                $query->where('id', $params['id']);
            }
            $count = $query->count();
            $list = $query
                ->limit($limit)
                ->page($page)
                ->order('currency', 'asc')
                ->select()->toArray();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success(['count' => $count, 'list' => $list]);


    }
}