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.
 
 

313 lines
9.3 KiB

<?php
declare (strict_types=1);
namespace app\api\model;
use app\Request;
use think\facade\Config;
use think\facade\Db;
use think\Model;
/**
* @mixin think\Model
*/
class Field extends Common
{
protected $table = 'bmz_field';
protected $_data_table = 'field';
protected $cms_table = 'bmz_cms';
/**
* 管理员列表数据模型
* @return array|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function index()
{
$this->search_arr = ['name','field'];
$module_id = $this->request->param('module_id', 0);
$_op = $this->field('*');
if($module_id){
$_op = $this->where('module_id',$module_id);
}
//执行通用查询
return parent::parentLists($_op);
}
/**
* 读取后
* @param Model $model
* @return bool|void
*/
public static function onAfterRead(Model $model)
{
if (in_array(request()->action(), [ 'delete', 'status'])) {
return true;
}
$m_id = $model->getAttr('module_id');
$type = $model->getAttr('type');
$m_model = new Module();
if($m_id>0){
$module_data = $m_model->find($m_id)->toArray();
if($module_data){
$model->setAttr('module_name',$module_data['module_name']);
}else{
$model->setAttr('module_name','');
}
}else{
$model->setAttr('module_name','');
}
$cms_field_rtoes = $lists = config('cms.field_types');
if(isset($cms_field_rtoes[$type])){
$model->setAttr('type_name',$cms_field_rtoes[$type]);
}else{
$model->setAttr('type_name','');
}
}
public function delPost($ids){
if (strpos($ids, ',') !== false) {
return -1;
}
$site_id = SITE_ID;
//获取module信息
$m_model = new Module();
$field_info = $this->find($ids)->toArray();
$field = $field_info['field'];
if(!$field_info){
return -1;
}
//获取模型信息
$module_info = $m_model->find($field_info['module_id'])->toArray();
if(!$module_info){
return -1;
}
$table_name = $this->cms_table.'_'.$module_info['table_name'].'_'.$site_id;
$stste = $this->parentDel($ids);
if($stste){
//实际查询表中是否有该字段
if ($this->_iset_field($table_name,$field )) {
Db::execute("ALTER TABLE `{$table_name}` DROP `$field`");
}
}
return $stste;
}
/**
* 新增或修改前
* @param Model $model
* @return mixed|void
*/
// public static function onBeforeWrite(Model $model)
// {
// if (!in_array(request()->action(), ['add', 'edit','dataEdit','dataAdd'])) {
// return;
// }
//
//
// }
public function addPost(){
$_param = $this->request->post();
if(!$_param){
return 0;
}
$site_id = SITE_ID;
//获取module信息
$m_model = new Module();
$module_info = $m_model->find($_param['module_id'])->toArray();
if(!$module_info){
return -1;
}
$table_name = $this->cms_table.'_'.$module_info['table_name'].'_'.$site_id;
//数据库没有表先建立表
$tables = Db::getTables();
if(!in_array($table_name,$tables)){
$t_sqlStr = "`id` int(11) unsigned NOT NULL AUTO_INCREMENT, " .
"`common_id` int(11) NOT NULL DEFAULT '0', ";
$t_sql = "CREATE TABLE `{$table_name}` (
{$t_sqlStr}
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='".$module_info['table_comment']."'";
Db::execute($t_sql);
}
// 查询字段是否已录入
$hasIn = $this->where('field', $_param['field'])->where('module_id', $_param['module_id'])->count();
if ($hasIn) {
return -2;
}
// 查询字段是否已在表中存在
if ($this->_iset_field($table_name, $_param['field'])) {
return -2;
} else {
// 获取字段sql
$addfieldsql = $this->get_tablesql($_param, 'add');
}
$field = $this->parentAdd($_param);
if ($field) {
// 数据库已存在字段时不再执行字段操作
if (isset($addfieldsql) && !empty($addfieldsql)) {
if (is_array($addfieldsql)) {
foreach ($addfieldsql as $sql) {
Db::execute($sql);
}
} else {
Db::execute($addfieldsql);
}
}
}
return $field;
}
public function editPost($id){
$_param = $this->request->post();
if(!$_param){
return 0;
}
$site_id = SITE_ID;
//获取module信息
$m_model = new Module();
$module_info = $m_model->find($_param['module_id'])->toArray();
if(!$module_info){
return -1;
}
$table_name = $this->cms_table.'_'.$module_info['table_name'].'_'.$site_id;
//数据库没有表先建立表
$tables = Db::getTables();
if(!in_array($table_name,$tables)){
$t_sqlStr = "`id` int(11) unsigned NOT NULL AUTO_INCREMENT, " .
"`common_id` int(11) NOT NULL DEFAULT '0', ";
$t_sql = "CREATE TABLE `{$table_name}` (
{$t_sqlStr}
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='".$module_info['table_comment']."'";
Db::execute($t_sql);
}
// 查询字段是否已在表中存在
if (!$this->_iset_field($table_name, $_param['field'])) {
return -2;
} else {
// 获取字段sql
$editfieldsql = $this->get_tablesql($_param, 'edit');
}
$field = $this->parentEdit($_param,$id);
if ($field) {
// 数据库已存在字段时不再执行字段操作
if (is_array($editfieldsql)) {
foreach ($editfieldsql as $sql) {
Db::execute($sql);
}
} else {
Db::execute($editfieldsql);
}
}
return $field;
}
/**
* 判断表中是否存在所选字段
* @param $table 表全称
* @param $field 字段名称
* @return mixed
*/
protected function _iset_field($table, $field)
{
$fields = Db::getTableFields($table);
if (array_search($field, $fields) === false) {
return false;
} else {
return true;
}
}
/**
* 获取要执行的sql
* @param array $info 字段信息
* @param string $do 操作类型[add/edit]
* @return array|string
*/
protected function get_tablesql(array $info, string $do = 'add')
{
$sql = '';
$comment = $info['name'];
$type = $info['type'];
$cms_field_set_ups = $lists = config('cms.field_set_ups');
$set_ups = $cms_field_set_ups[$type];
$fieldtype = $set_ups?$set_ups['fieldtype']:'varchar';
$default = $set_ups?$set_ups['default']:'varchar';
$field = $info['field'];
$site_id = SITE_ID;
//获取module信息
$m_model = new Module();
$module_info = $m_model->find($info['module_id'])->toArray();
$tablename = $this->cms_table.'_'.$module_info['table_name'].'_'.$site_id;
$pk = 'id';
$maxlength = isset($info['maxlength'])?intval($info['maxlength']):0;
$minlength = isset($info['minlength'])?intval($info['minlength']):0;
$numbertype = '1'; // 是否包含负数
if ($do == 'add') {
$do = ' ADD ';
} else {
$do = " CHANGE `$field` ";
}
// int varchart text longtext
switch ($fieldtype){
case 'int':
if (!$maxlength)
$maxlength = 10;
$sql = "ALTER TABLE `$tablename` $do `$field` $fieldtype( $maxlength ) "." NOT NULL DEFAULT '$default' COMMENT '$comment'";
break;
case 'varchar':
if (!$maxlength) {
$maxlength = 255;
}
$maxlength = min($maxlength, 500);
$sql = "ALTER TABLE `$tablename` $do `$field` $fieldtype( $maxlength ) NOT NULL DEFAULT '$default' COMMENT '$comment'";
break;
case 'text':
$sql = "ALTER TABLE `$tablename` $do `$field` TEXT NULL COMMENT '$comment'";
break;
case 'longtext':
$sql = "ALTER TABLE `$tablename` $do `$field` LONGTEXT NULL COMMENT '$comment'";
break;
default:
if (!$maxlength) {
$maxlength = 255;
}
$maxlength = min($maxlength, 255);
$sql = "ALTER TABLE `$tablename` $do `$field` $fieldtype( $maxlength ) NOT NULL DEFAULT '$default' COMMENT '$comment'";
break;
}
return $sql;
}
}