2019-12-06 09:17:40 +08:00
|
|
|
<?php
|
2020-11-29 09:29:14 +08:00
|
|
|
declare(strict_types=1);
|
2019-12-06 09:17:40 +08:00
|
|
|
|
|
|
|
namespace catcher\traits\db;
|
|
|
|
|
2021-04-24 20:32:05 +08:00
|
|
|
use catcher\library\excel\reader\Reader;
|
2020-07-28 16:40:05 +08:00
|
|
|
use catcher\Utils;
|
|
|
|
|
2019-12-06 09:17:40 +08:00
|
|
|
trait BaseOptionsTrait
|
|
|
|
{
|
2020-04-28 22:02:23 +08:00
|
|
|
/**
|
|
|
|
* 查询列表
|
|
|
|
*
|
|
|
|
* @time 2020年04月28日
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getList()
|
|
|
|
{
|
2020-09-17 21:14:34 +08:00
|
|
|
// 不分页
|
|
|
|
if (property_exists($this, 'paginate') && $this->paginate === false) {
|
|
|
|
return $this->catchSearch()
|
|
|
|
->field('*')
|
|
|
|
->catchOrder()
|
|
|
|
->creator()
|
|
|
|
->select();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 分页列表
|
2020-04-28 22:02:23 +08:00
|
|
|
return $this->catchSearch()
|
2020-09-17 21:14:34 +08:00
|
|
|
->field('*')
|
|
|
|
->catchOrder()
|
|
|
|
->creator()
|
|
|
|
->paginate();
|
2020-04-28 22:02:23 +08:00
|
|
|
}
|
|
|
|
|
2019-12-06 09:17:40 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月03日
|
|
|
|
* @param array $data
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function storeBy(array $data)
|
2020-04-21 21:16:23 +08:00
|
|
|
{
|
2021-03-01 21:02:01 +08:00
|
|
|
if ($this->allowField($this->field)->save($this->filterData($data))) {
|
2020-04-21 21:16:23 +08:00
|
|
|
return $this->{$this->getPk()};
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用于循环插入
|
|
|
|
*
|
|
|
|
* @time 2020年04月21日
|
|
|
|
* @param array $data
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function createBy(array $data)
|
2019-12-06 09:17:40 +08:00
|
|
|
{
|
2021-05-23 20:34:50 +08:00
|
|
|
$model = static::create($data, $this->field, true);
|
2019-12-06 09:17:40 +08:00
|
|
|
|
2020-04-21 10:51:25 +08:00
|
|
|
return $model->{$this->getPk()};
|
2019-12-06 09:17:40 +08:00
|
|
|
}
|
2021-05-23 20:34:50 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月03日
|
|
|
|
* @param $id
|
|
|
|
* @param $data
|
|
|
|
* @param string $field
|
|
|
|
* @return bool
|
|
|
|
* @throws \think\db\exception\DbException
|
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
|
*/
|
|
|
|
public function updateBy($id, $data, string $field = ''): bool
|
2019-12-06 09:17:40 +08:00
|
|
|
{
|
2021-03-01 21:02:01 +08:00
|
|
|
if (static::update($this->filterData($data), [$field ? : $this->getPk() => $id], $this->field)) {
|
2021-04-26 10:37:18 +08:00
|
|
|
|
|
|
|
$this->updateChildren($id, $data);
|
|
|
|
|
2019-12-12 09:14:08 +08:00
|
|
|
return true;
|
2019-12-06 09:17:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月03日
|
|
|
|
* @param $id
|
|
|
|
* @param array $field
|
2019-12-07 17:31:38 +08:00
|
|
|
* @param bool $trash
|
2019-12-06 09:17:40 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2021-05-23 20:34:50 +08:00
|
|
|
public function findBy($id, array $field = ['*'], bool $trash = false)
|
2019-12-06 09:17:40 +08:00
|
|
|
{
|
2019-12-07 17:31:38 +08:00
|
|
|
if ($trash) {
|
|
|
|
return static::onlyTrashed()->find($id);
|
|
|
|
}
|
|
|
|
|
2019-12-26 09:03:19 +08:00
|
|
|
|
2019-12-07 17:31:38 +08:00
|
|
|
return static::where($this->getPk(), $id)->field($field)->find();
|
2019-12-06 09:17:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月03日
|
|
|
|
* @param $id
|
2021-05-23 20:34:50 +08:00
|
|
|
* @param bool $force
|
2019-12-07 17:31:38 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2021-05-23 20:34:50 +08:00
|
|
|
public function deleteBy($id, bool $force = false)
|
2019-12-07 17:31:38 +08:00
|
|
|
{
|
2020-09-17 21:14:34 +08:00
|
|
|
return static::destroy(is_array($id) ? $id : Utils::stringToArrayBy($id), $force);
|
2019-12-07 17:31:38 +08:00
|
|
|
}
|
|
|
|
|
2020-04-21 10:51:25 +08:00
|
|
|
/**
|
|
|
|
* 批量插入
|
|
|
|
*
|
|
|
|
* @time 2020年04月19日
|
|
|
|
* @param array $data
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function insertAllBy(array $data)
|
|
|
|
{
|
|
|
|
$newData = [];
|
|
|
|
foreach ($data as $item) {
|
|
|
|
foreach ($item as $field => $value) {
|
|
|
|
if (!in_array($field, $this->field)) {
|
|
|
|
unset($item[$field]);
|
|
|
|
}
|
|
|
|
|
2021-04-26 10:37:18 +08:00
|
|
|
if (in_array($this->createTime, $this->field)) {
|
|
|
|
$item[$this->createTime] = time();
|
2020-04-21 10:51:25 +08:00
|
|
|
}
|
|
|
|
|
2021-04-26 10:37:18 +08:00
|
|
|
if (in_array($this->updateTime, $this->field)) {
|
|
|
|
$item[$this->updateTime] = time();
|
2020-04-21 10:51:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$newData[] = $item;
|
|
|
|
}
|
|
|
|
return $this->insertAll($newData);
|
|
|
|
}
|
|
|
|
|
2019-12-07 17:31:38 +08:00
|
|
|
/**
|
|
|
|
* @time 2019年12月07日
|
|
|
|
* @param $id
|
2019-12-06 09:17:40 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-12-07 17:31:38 +08:00
|
|
|
public function recover($id)
|
2019-12-06 09:17:40 +08:00
|
|
|
{
|
2019-12-07 17:31:38 +08:00
|
|
|
return static::onlyTrashed()->find($id)->restore();
|
2019-12-06 09:17:40 +08:00
|
|
|
}
|
2020-01-13 21:24:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取删除字段
|
|
|
|
*
|
|
|
|
* @time 2020年01月13日
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getDeleteAtField()
|
|
|
|
{
|
|
|
|
return $this->deleteTime;
|
|
|
|
}
|
|
|
|
|
2021-04-26 10:37:18 +08:00
|
|
|
/**
|
2021-04-29 08:47:32 +08:00
|
|
|
* 更新下级
|
2021-04-26 10:37:18 +08:00
|
|
|
*
|
2021-04-29 08:47:32 +08:00
|
|
|
* @time 2021年04月28日
|
2021-04-26 10:37:18 +08:00
|
|
|
* @param $parentId
|
|
|
|
* @param $data
|
2021-04-29 08:47:32 +08:00
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
|
* @throws \think\db\exception\DbException
|
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
2021-04-26 10:37:18 +08:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function updateChildren($parentId, $data)
|
|
|
|
{
|
|
|
|
if (property_exists($this, 'updateChildrenFields')) {
|
|
|
|
$parentIdField = property_exists($this, 'parentId') ? $this->$parentId : 'parent_id';
|
|
|
|
|
|
|
|
if (!empty($this->updateChildrenFields)) {
|
|
|
|
if (is_array($this->updateChildrenFields)) {
|
|
|
|
foreach ($data as $field => $value) {
|
2021-05-24 21:16:35 +08:00
|
|
|
if (! in_array($field, $this->updateChildrenFields)) {
|
2021-04-26 10:37:18 +08:00
|
|
|
unset($data[$field]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->recursiveUpdate($parentId, $parentIdField, $data);
|
|
|
|
}
|
|
|
|
|
2021-04-29 08:47:32 +08:00
|
|
|
if (is_string($this->updateChildrenFields) && isset($data[$this->updateChildrenFields])) {
|
2021-04-26 10:37:18 +08:00
|
|
|
$this->recursiveUpdate($parentId, $parentIdField, [
|
|
|
|
$this->updateChildrenFields => $data[$this->updateChildrenFields]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 递归更新子级
|
|
|
|
*
|
|
|
|
* @time 2021年04月25日
|
|
|
|
* @param $parentId
|
|
|
|
* @param $parentIdField
|
|
|
|
* @param $updateData
|
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
|
* @throws \think\db\exception\DbException
|
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function recursiveUpdate($parentId, $parentIdField, $updateData)
|
|
|
|
{
|
|
|
|
$this->where($parentIdField, $parentId)->update($updateData);
|
|
|
|
|
|
|
|
$children = $this->where($parentIdField, $parentId)->select();
|
|
|
|
|
|
|
|
if ($children->count()) {
|
|
|
|
foreach ($children as $child) {
|
|
|
|
$this->recursiveUpdate($child->id, $parentIdField, $updateData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 21:24:12 +08:00
|
|
|
/**
|
|
|
|
* 别名
|
|
|
|
*
|
|
|
|
* @time 2020年01月13日
|
|
|
|
* @param $field
|
2021-05-26 18:37:14 +08:00
|
|
|
* @param string $table
|
2021-03-01 21:02:01 +08:00
|
|
|
* @return array|string
|
2020-01-13 21:24:12 +08:00
|
|
|
*/
|
2021-05-26 18:37:14 +08:00
|
|
|
public function aliasField($field, $table = '')
|
2020-01-13 21:24:12 +08:00
|
|
|
{
|
2021-05-26 18:37:14 +08:00
|
|
|
$table = $table ? Utils::tableWithPrefix($table) : $this->getTable();
|
|
|
|
|
2021-03-01 21:02:01 +08:00
|
|
|
if (is_string($field)) {
|
2021-05-26 18:37:14 +08:00
|
|
|
return sprintf('%s.%s', $table, $field);
|
2021-03-01 21:02:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($field)) {
|
|
|
|
foreach ($field as &$value) {
|
2021-05-26 18:37:14 +08:00
|
|
|
$value = sprintf('%s.%s', $table, $value);
|
2021-03-01 21:02:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $field;
|
2020-01-13 21:24:12 +08:00
|
|
|
}
|
2020-06-29 22:01:47 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 禁用/启用
|
|
|
|
*
|
|
|
|
* @time 2020年06月29日
|
|
|
|
* @param $id
|
|
|
|
* @param string $field
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2021-05-23 20:34:50 +08:00
|
|
|
public function disOrEnable($id, string $field='status')
|
2020-06-29 22:01:47 +08:00
|
|
|
{
|
|
|
|
$model = $this->findBy($id);
|
|
|
|
|
|
|
|
$status = $model->{$field} == self::DISABLE ? self::ENABLE : self::DISABLE;
|
|
|
|
|
|
|
|
$model->{$field} = $status;
|
|
|
|
|
|
|
|
return $model->save();
|
|
|
|
}
|
2021-03-01 21:02:01 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 过滤数据
|
|
|
|
*
|
|
|
|
* @time 2021年02月28日
|
2021-05-23 20:34:50 +08:00
|
|
|
* @param array $data
|
2021-03-01 21:02:01 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2021-05-23 20:34:50 +08:00
|
|
|
protected function filterData(array $data)
|
2021-03-01 21:02:01 +08:00
|
|
|
{
|
|
|
|
foreach ($data as $field => $value) {
|
|
|
|
if (is_null($value)) {
|
|
|
|
unset($data[$field]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($field == $this->getPk()) {
|
|
|
|
unset($data[$field]);
|
|
|
|
}
|
2021-11-18 11:29:43 +08:00
|
|
|
|
|
|
|
if (in_array($field, [$this->createTime, $this->updateTime, $this->deleteTime])) {
|
|
|
|
unset($data[$field]);
|
|
|
|
}
|
2021-03-01 21:02:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2021-04-24 20:32:05 +08:00
|
|
|
|
|
|
|
|
2021-04-29 08:47:32 +08:00
|
|
|
/**
|
|
|
|
* 模型导入
|
|
|
|
*
|
|
|
|
* @time 2021年04月28日
|
|
|
|
* @param $fields
|
|
|
|
* @param $file
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function import($fields, $file): bool
|
2021-04-24 20:32:05 +08:00
|
|
|
{
|
|
|
|
$excel = new class(array_column($fields, 'field')) extends Reader {
|
|
|
|
|
|
|
|
protected $fields;
|
|
|
|
|
|
|
|
public function __construct($fields)
|
|
|
|
{
|
|
|
|
$this->fields = $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function headers()
|
|
|
|
{
|
|
|
|
// TODO: Implement headers() method.
|
|
|
|
return $this->fields;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$options = [];
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
$p = [];
|
|
|
|
if (isset($field['options']) && count($field['options'])) {
|
|
|
|
foreach ($field['options'] as $op) {
|
|
|
|
$p[$op['label']] = $op['value'];
|
|
|
|
}
|
|
|
|
$options[$field['field']] = $p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$creatorId = request()->user()->id;
|
|
|
|
|
|
|
|
$excel->import($file)->remove(0)->then(function ($data) use ($options, $creatorId){
|
|
|
|
foreach ($data as &$d) {
|
|
|
|
foreach ($d as $field => &$v) {
|
|
|
|
if (isset($options[$field])) {
|
|
|
|
$v = $options[$field][$v];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$d['creator_id'] = $creatorId;
|
|
|
|
|
|
|
|
$this->createBy($d);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-06 09:17:40 +08:00
|
|
|
}
|