添加地区筛选

This commit is contained in:
hant 2025-03-10 23:07:20 +08:00
parent d028613923
commit 3e780bf5c6
9 changed files with 116 additions and 126 deletions

View File

@ -79,17 +79,16 @@ class Worker extends Backend
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
$area_code = request()->get('area_code');
$build = $this->model
->with(['area'])
->where($where)
->order($sort, $order)
->paginate($limit);
foreach ($list as $row) {
->order($sort, $order);
if ($area_code){
$build->where('area_id','like',$this->getSelectAreaCode($area_code).'%');
}
$list = $build
->paginate($limit);
$result = array("total" => $list->total(), "rows" => $list->items());

View File

@ -16,5 +16,6 @@ return [
'Star' => '信用(5星制)',
'Create_time' => '创建时间',
'Update_time' => '更新时间',
'Deletetime' => '删除时间'
'Deletetime' => '删除时间',
'Area.short_merge_name' => '区域'
];

View File

@ -0,0 +1,57 @@
<?php
namespace app\admin\model;
use think\Db;
use think\Exception;
use think\Model;
class BaseModel extends Model
{
/**
* 批量插入数据到数据库
*
* @param string $table 数据表名
* @param array $data 待插入的数据
* @param int $batchSize 每次插入的数据量(默认 500
* @return int 成功插入的总行数
* @throws Exception 插入失败时抛出异常
*/
function batchInsert( array $data, int $batchSize = 500): int
{
if (empty($data)) {
throw new Exception('插入数据不能为空');
}
// 提取字段名(确保所有数据的字段一致)
$columns = array_keys($data[0]);
$columnList = implode(', ', $columns);
$placeholders = '(' . implode(', ', array_fill(0, count($columns), '?')) . ')';
$totalInserted = 0;
Db::startTrans();
try {
// 数据分批插入
foreach (array_chunk($data, $batchSize) as $chunk) {
$sql = "INSERT INTO {$this->getTable()} ({$columnList}) VALUES " . implode(', ', array_fill(0, count($chunk), $placeholders));
// 将数据展开填充
$values = [];
foreach ($chunk as $row) {
$values = array_merge($values, array_values($row));
}
Db::execute($sql,$values);
}
Db::commit();
return $totalInserted;
} catch (Exception $e) {
Db::rollback();
throw new Exception('批量插入失败:' . $e->getMessage());
}
}
}

View File

@ -2,13 +2,10 @@
namespace app\admin\model;
use Exception;
use think\Db;
use think\Model;
use traits\model\SoftDelete;
class Worker extends Model
class Worker extends BaseModel
{
use SoftDelete;
@ -50,59 +47,7 @@ class Worker extends Model
public function area()
{
return $this->belongsTo('Area', 'area_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
/**
* 批量插入数据到数据库
*
* @param string $table 数据表名
* @param array $data 待插入的数据
* @param int $batchSize 每次插入的数据量(默认 500
* @return int 成功插入的总行数
* @throws Exception 插入失败时抛出异常
*/
function batchInsert( array $data, int $batchSize = 500): int
{
if (empty($data)) {
throw new Exception('插入数据不能为空');
}
// 提取字段名(确保所有数据的字段一致)
$columns = array_keys($data[0]);
$columnList = implode(', ', $columns);
$placeholders = '(' . implode(', ', array_fill(0, count($columns), '?')) . ')';
$totalInserted = 0;
Db::startTrans();
try {
// 数据分批插入
foreach (array_chunk($data, $batchSize) as $chunk) {
$sql = "INSERT INTO {$this->getTable()} ({$columnList}) VALUES " . implode(', ', array_fill(0, count($chunk), $placeholders));
$stmt = $this->prepare($sql);
// 将数据展开填充
$values = [];
foreach ($chunk as $row) {
$values = array_merge($values, array_values($row));
}
if ($stmt->execute($values)) {
$totalInserted += $stmt->rowCount();
} else {
throw new Exception('批量插入失败');
}
}
Db::commit();
return $totalInserted;
} catch (Exception $e) {
Db::rollback();
throw new Exception('批量插入失败:' . $e->getMessage());
}
return $this->belongsTo('Area', 'area_id', 'area_code');
}
}

View File

@ -3,57 +3,11 @@
namespace app\admin\model;
use think\Db;
use think\Model;
use traits\model\SoftDelete;
class WorkerItem extends Model
class WorkerItem extends BaseModel
{
// 表名
protected $name = 'worker_item';
/**
* 批量插入数据到数据库
*
* @param string $table 数据表名
* @param array $data 待插入的数据
* @param int $batchSize 每次插入的数据量(默认 500
* @return int 成功插入的总行数
* @throws Exception 插入失败时抛出异常
*/
function batchInsert( array $data, int $batchSize = 500): int
{
if (empty($data)) {
throw new Exception('插入数据不能为空');
}
// 提取字段名(确保所有数据的字段一致)
$columns = array_keys($data[0]);
$columnList = implode(', ', $columns);
$placeholders = '(' . implode(', ', array_fill(0, count($columns), '?')) . ')';
$totalInserted = 0;
Db::startTrans();
try {
// 数据分批插入
foreach (array_chunk($data, $batchSize) as $chunk) {
$sql = "INSERT INTO {$this->getTable()} ({$columnList}) VALUES " . implode(', ', array_fill(0, count($chunk), $placeholders));
// 将数据展开填充
$values = [];
foreach ($chunk as $row) {
$values = array_merge($values, array_values($row));
}
Db::execute($sql,$values);
}
Db::commit();
return $totalInserted;
} catch (Exception $e) {
Db::rollback();
throw new Exception('批量插入失败:' . $e->getMessage());
}
}
}

View File

@ -21,15 +21,15 @@
<div class="radio">
{foreach name="statusList" item="vo"}
<label for="row[status]-{$key}"><input id="row[status]-{$key}" name="row[status]" type="radio"
value="{$key}" {in name="key" value="$row.status" }checked{/in}
/> {$vo}</label>
<label for="row[status]-{$key}">
<input id="row[status]-{$key}" name="row[status]" type="radio" value="{$key}" {in name="key" value="$row.status" }checked{/in}/> {$vo}</label>
{/foreach}
</div>
</div>
</div>
<label class="control-label col-xs-12 col-sm-3">{:__('Area_id')}:</label>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Area_id')}:</label>
<div class='col-xs-12 col-sm-8'>
<input id="c-city" data-rule="required" class="form-control" data-toggle="city-picker" value="{$row.area_name}"
type="text" name="row[address]"/>
@ -40,17 +40,16 @@
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Deposit_amount')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-deposit_amount" data-rule="required" class="form-control" step="0.01"
name="row[deposit_amount]" type="number" value="{$row.deposit_amount|htmlentities}">
<input id="c-deposit_amount" data-rule="required" class="form-control" step="0.01" name="row[deposit_amount]" type="number" value="{$row.deposit_amount|htmlentities}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Star')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-star" data-rule="required" class="form-control" step="0.1" name="row[star]" type="number"
value="{$row.star|htmlentities}">
<input id="c-star" data-rule="required" class="form-control" step="0.1" name="row[star]" type="number" value="{$row.star|htmlentities}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Items')}:</label>
<div class="col-xs-12 col-sm-8">
@ -68,6 +67,5 @@
</div>
</form>
<script>
var nodeData = {:json_encode($tree);}
;
var nodeData = {:json_encode($tree);};
</script>

View File

@ -20,7 +20,6 @@
<a href="javascript:;" class="btn btn-success btn-add {:$auth->check('workers/worker/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>
<a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('workers/worker/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>
<a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('workers/worker/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>
<div class="dropdown btn-group {:$auth->check('workers/worker/multi')?'':'hide'}">
<a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>
@ -32,6 +31,17 @@
</div>
<a class="btn btn-success btn-recyclebin btn-dialog {:$auth->check('workers/worker/recyclebin')?'':'hide'}" href="workers/worker/recyclebin" title="{:__('Recycle bin')}"><i class="fa fa-recycle"></i> {:__('Recycle bin')}</a>
<form id="select-form" class="form-inline" role="form" style="margin-top: 5px">
<div class='form-group' style="width: 100%">
<label style="padding-left: 0px;text-align: left">{:__('Area_id')}:</label>
<div style="width: 350px;display: inline-block">
<input id="c-city" style="width: 400px" class="form-control col-xs-12 col-sm-8" data-toggle="city-picker" name="row[address]" type="text" value="" />
</div>
<p id="reset" style="display: inline-block;margin-left: -20px" class="btn btn-default">重置</p>
</div>
</form>
</div>
<table id="table" class="table table-striped table-bordered table-hover table-nowrap"
data-operate-edit="{:$auth->check('workers/worker/edit')}"

View File

@ -611,4 +611,22 @@ class Backend extends Controller
//刷新Token
$this->request->token();
}
public function getSelectAreaCode($area_code){
if (strlen($area_code) != 6){
return $area_code;
}
$chunks = str_split($area_code, 2);
$res = $chunks[0];
if ($chunks[2] == '00'){
if ($chunks[1] != '00'){
$res .= $chunks[1];
}
}else{
$res .= $chunks[1] . $chunks[2];
}
return $res;
}
}

View File

@ -53,15 +53,23 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form','jstree'], function ($
{field: 'star', title: __('Star'), operate:'BETWEEN'},
{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
{field: 'delete_time', title: __('Delete_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
]
]
});
$("#c-city").on("cp:updated", function() {
var citypicker = $(this).data("citypicker");
var code = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province");
table.bootstrapTable('refresh',{query: {area_code: code}});
});
$("#reset").on("click", function() {
$("#c-city").citypicker('reset');
table.bootstrapTable('refresh');
});
// 为表格绑定事件
Table.api.bindevent(table);
Form.events.citypicker($("form"));
},
add: function () {
$("#c-city").on("cp:updated", function() {