You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.4 KiB

12 months ago
<?php
namespace App\Repositories;
use App\Models\BaseForm;
use App\Models\CustomForm;
use App\Models\CustomFormField;
use App\Models\CustomFormRelation;
use App\Models\ParameterDetail;
use App\Models\DynamicModel;
use App\Models\Upload;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Prettus\Repository\Eloquent\BaseRepository;
class BaseFormRepository extends BaseRepository
{
/**
* 定义model
* @return string
*/
function model()
{
return BaseForm::class;
}
/**
* 实例化
*/
static function instance()
{
return app(static::class);
}
/**
* json数据一对多关联获取关联数据
*/
public function getJsonData($model)
{
$customFormFields = request('customFormFields');
if (empty($customFormFields)) return $model;
$customFormFields = $customFormFields->whereIn('edit_input', CustomFormField::getJsonFieldType());
foreach ($customFormFields as $item) {
if ($item->select_item) {
continue;
}
$field = $item->field;
$newField = $field . '_details';
if ($item->edit_input == 'files') {
$model->$newField = Upload::whereIn('id', (array)$model->$field)->get();
} elseif (isset($item->link_table_name)) {
$dynamicModel = tableNameToModel($item->link_table_name);
$model->$newField = $dynamicModel->whereIn('id', (array)$model->$field)->get();
}
}
return $model;
}
/**
* 构建with
*/
public function buildWith($showRelation = [])
{
$allWith = $this->model->allRelationFields();
if (!empty($showRelation)) {
$allWith = array_intersect($allWith, $showRelation);
}
$this->model = $this->model->with($allWith);
return $this;
}
/**
* 搜索构建
*/
public function buildSeacher($conditions)
{
foreach ($conditions as $condition) {
$key = $condition['key'] ?? null;
$op = $condition['op'] ?? null;
$value = $condition['value'] ?? null;
if (!isset($key) || !isset($op) || !isset($value)) {
continue;
}
// 等于
if ($op == 'eq') {
$this->model = $this->model->where($key, $value);
}
// 不等于
if ($op == 'neq') {
$this->model = $this->model->where($key, '!=', $value);
}
// 模糊搜索
if ($op == 'like') {
$this->model = $this->model->where($key, 'like', '%' . $value . '%');
}
// 否定模糊搜索
if ($op == 'notlike') {
$this->model = $this->model->where($key, 'not like', '%' . $value . '%');
}
// 否定模糊搜索
if ($op == 'json_contains') {
$this->model = $this->model->whereJsonContains($key, (int)$value);
}
// 范围搜索
if ($op == 'range') {
list($from, $to) = explode(',', $value);
if (empty($from) || empty($to)) {
continue;
}
$this->model = $this->model->whereBetween($key, [$from, $to]);
}
}
return $this;
}
/**
* 更新关联关系
*/
public function updateRelation($all, $model)
{
$allRelation = $this->model->allRelationFields(true);
foreach ($allRelation as $item) {
if (!isset($all[$item->link_with_name])) {
continue;
}
foreach ($all[$item->link_with_name] as $v) {
if (isset($v['id'])) {
$linkModel = $model->{$item->link_with_name}()->find($v['id']);
$linkModel->fill($v);
$linkModel->save();
} else {
// 新增
$fillData = $this->filterRequestColumns($v, $item->link_table_name);
$fillData[$item->foreign_key] = $model->id;
$model->{$item->link_with_name}()->insert($fillData);
}
}
}
return true;
}
/**
* filter request columns by fields
* @param $request
* @param $linkTableName
* @return array
*/
public function filterRequestColumns($request, $linkTableName)
{
$columns = (new CustomFormField())->rowTableFieldsByType($linkTableName);
$return = [];
foreach ($request as $k => $v) {
if (!in_array($k, array_keys($columns))) {
continue;
}
if ($k === "password") {
if (!$v) {
continue;
}
$v = Hash::make($v);
}
switch ($columns[$k]) {
case "json":
$v = json_encode($v, JSON_UNESCAPED_UNICODE);
break;
default:
if (is_array($v)) {
if (count($v) == count($v, 1)) {
$v = implode(',', $v);
} else {
$v = json_encode($v, JSON_UNESCAPED_UNICODE);
}
}
}
$return[$k] = $v;
}
return $return;
}
}