[更新]优化数据服务组件

This commit is contained in:
Anyon 2017-06-20 12:13:55 +08:00
parent 0c012e2056
commit dd05c100bb

View File

@ -15,6 +15,7 @@
namespace service; namespace service;
use think\Db; use think\Db;
use think\Request;
/** /**
* 基础数据服务 * 基础数据服务
@ -45,14 +46,16 @@ class DataService {
public static function createSequence($length = 10, $type = 'SYSTEM') { public static function createSequence($length = 10, $type = 'SYSTEM') {
$times = 0; $times = 0;
while ($times++ < 10) { while ($times++ < 10) {
$sequence = '';
$i = 0; $i = 0;
$sequence = '';
while ($i++ < $length) { while ($i++ < $length) {
$sequence .= ($i <= 1 ? rand(1, 9) : rand(0, 9)); $sequence .= ($i <= 1 ? rand(1, 9) : rand(0, 9));
} }
$data = ['sequence' => $sequence, 'type' => strtoupper($type)]; $data = ['sequence' => $sequence, 'type' => strtoupper($type)];
if (Db::name('SystemSequence')->where($data)->count() < 1 && Db::name('SystemSequence')->insert($data)) { if (Db::name('SystemSequence')->where($data)->count() < 1) {
return $sequence; if (Db::name('SystemSequence')->insert($data) !== false) {
return $sequence;
}
} }
} }
return null; return null;
@ -62,40 +65,17 @@ class DataService {
* 数据增量保存 * 数据增量保存
* @param \think\db\Query|string $dbQuery 数据查询对象 * @param \think\db\Query|string $dbQuery 数据查询对象
* @param array $data 需要保存或更新的数据 * @param array $data 需要保存或更新的数据
* @param string $upkey 条件主键限制 * @param string $key 条件主键限制
* @param array $where 其它的where条件 * @param array $where 其它的where条件
* @return bool * @return bool
*/ */
public static function save($dbQuery, $data, $upkey = 'id', $where = []) { public static function save($dbQuery, $data, $key = 'id', $where = []) {
$db = is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery; $db = is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery;
$fields = $db->getTableFields(['table' => $db->getTable()]); $where[$key] = isset($data[$key]) ? $data[$key] : '';
$_data = []; if ($db->where($where)->count() > 0) {
foreach ($data as $k => $v) { return $db->where($where)->update($data) !== false;
in_array($k, $fields) && ($_data[$k] = $v);
} }
if (self::_apply_save_where($db, $data, $upkey, $where)->count() > 0) { return $db->insert($data) !== false;
return self::_apply_save_where($db, $data, $upkey, $where)->update($_data) !== FALSE;
}
return self::_apply_save_where($db, $data, $upkey, $where)->insert($_data) !== FALSE;
}
/**
* 应用 where 条件
* @param \think\db\Query|string $db 数据查询对象
* @param array $data 需要保存或更新的数据
* @param string $upkey 条件主键限制
* @param array $where 其它的where条件
* @return \think\db\Query
*/
protected static function _apply_save_where(&$db, $data, $upkey, $where) {
foreach (is_string($upkey) ? explode(',', $upkey) : $upkey as $v) {
if (is_string($v) && array_key_exists($v, $data)) {
$db->where($v, $data[$v]);
} elseif (is_string($v)) {
$db->where("{$v} IS NULL");
}
}
return $db->where($where);
} }
/** /**
@ -105,23 +85,24 @@ class DataService {
* @return bool|null * @return bool|null
*/ */
public static function update(&$dbQuery, $where = []) { public static function update(&$dbQuery, $where = []) {
$request = Request::instance();
$db = is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery; $db = is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery;
$ids = explode(',', input("post.id", '')); $ids = explode(',', $request->post('id', ''));
$field = input('post.field', ''); $field = $request->post('field', '');
$value = input('post.value', ''); $value = $request->post('value', '');
$pk = $db->getPk(['table' => $db->getTable()]); $pk = $db->getPk(['table' => $db->getTable()]);
$db->where(empty($pk) ? 'id' : $pk, 'in', $ids); $where[empty($pk) ? 'id' : $pk] = ['in', $ids];
!empty($where) && $db->where($where); // 删除模式,如果存在 is_deleted 字段使用软删除
// 删除模式
if ($field === 'delete') { if ($field === 'delete') {
$fields = $db->getTableFields(['table' => $db->getTable()]); if (method_exists($db, 'getTableFields')) {
if (in_array('is_deleted', $fields)) { if (in_array('is_deleted', $db->getTableFields(['table' => $db->getTable()]))) {
return false !== $db->update(['is_deleted' => 1]); return false !== $db->where($where)->update(['is_deleted' => 1]);
}
} }
return false !== $db->delete(); return false !== $db->where($where)->delete();
} }
// 更新模式 // 更新模式,更新指定字段内容
return false !== $db->update([$field => $value]); return false !== $db->where($where)->update([$field => $value]);
} }
} }