Commit ba69e79b authored by chentianyu's avatar chentianyu

初始化结构

parent 7ab3e0e0
.idea
/vendor
composer.lock
{
"name": "meibuyu/common",
"description": "美不语微服务公共库",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Rain",
"email": "823773695@qq.com"
}
],
"require": {
"php": ">=7.2"
},
"autoload": {
"psr-4": {
"Meibuyu\\Rpc\\": "src/"
}
},
"extra": {
"hyperf": {
"config": "Meibuyu\\Rpc\\ConfigProvider"
}
}
}
<?php
/**
* Created by PhpStorm.
* User: 王源
* Date: 2020/1/9
* Time: 15:08
*/
namespace Meibuyu\Micro\Service;
use Exception;
use Hyperf\DbConnection\Model\Model;
use Meibuyu\Micro\Helper;
class BaseService
{
/**
* @var Model
*/
protected $model;
/**
* 查找一个数据
* @param $id
* @return Model | array
*/
protected function find($id)
{
$model = $this->model->find($id);
return $model;
}
public function all(array $columns = ['*'], array $relations = []): array
{
return $this->model->with($relations)->get($columns)->toArray();
}
/**
* 获取一条数据
* @param int $id
* @param array $columns
* @param array $relations
* @return mixed
*/
public function get(int $id, array $columns = ['*'], array $relations = [])
{
return $this->model->with($relations)->find($id, $columns);
}
/**
* 插入一条数据
* @param array $params
* @return array
*/
public function insert($params)
{
try {
$res = $this->model->insert($params);
return Helper::success($res);
} catch (Exception $e) {
return Helper::fail('', $e->getMessage());
}
}
/**
* 新增一条数据
* @param array $params
* @return array
*/
public function create($params)
{
try {
$model = $this->model->newInstance($params);
$model->save();
return Helper::success($model);
} catch (Exception $e) {
return Helper::fail('', $e->getMessage());
}
}
/**
* 更新数据
* @param $id
* @param array $params
* @return array
*/
public function update($id, $params)
{
try {
$model = $this->find($id);
$model->fill($params);
$model->save();
return Helper::success($model);
} catch (Exception $e) {
return Helper::fail('', $e->getMessage());
}
}
/**
* 删除数据
* @param $id
* @return array
*/
public function delete($id)
{
try {
$model = $this->find($id);
$res = $model->delete();
if ($res) {
return Helper::success($res, '删除成功');
} else {
return Helper::fail($res, '删除失败');
}
} catch (Exception $e) {
return Helper::fail('', $e->getMessage());
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment