谷神后端$vs.proc.invoke.stock.loadMap

发布于:2024-10-13 ⋅ 阅读:(117) ⋅ 点赞:(0)
loadMap
// 调用
@loadMap($src, $field, $strTableName, $key, $target, $other, $systemId)



/**
 * loadMap
 * 加载map。
 *
 * @param $src:list:列表。
 * @param $field:string:参数域。
 * @param $strTableName:string:表名。
 * @param $key:string:键。
 * @param $target:string:落地属性。
 * @param $other:map:其它条件。
 * @param $systemId:string:系统id。
 *
 * @return list:加载数据后的列表。
 *
 */
#function loadMap($src, $field, $strTableName, $key, $target, $other, $systemId)
	// 参数处理
	#set($where = $vs.util.newMap())
	#if ($vs.util.isMap($other))
		$where.putAll($other)
	#end

	// 数据收集
	#set($fields = @collectSet($src, $field))
	// 查询条件
	$where.put($key, $fields)

	// 列表数据
	#set($list = $vs.util.newList())
	#if ($vs.util.isString($systemId))
		#set($list = @remoteList($systemId, $strTableName, '*', $where))
	#else
		#set($list = $vs.dbTools.select($strTableName, $where))
	#end

	// 列表分组
	#set($map = @list2map($list, $key, null))
	// 数据组装
	@listPut($src, $map, $field, $target)

	// 初始化默认值
	#foreach ($row in $src)
		#if (!$vs.util.isMap($row.get($target)))
			$row.put($target, $vs.util.newMap())
		#end
	#end

	// 返回
	return $src
#end



/**
 * collectSet
 * 集合数据收集list实现:判空、去重。
 *
 * @param $list:list:列表。
 * @param $field:string:字段。
 *
 * @return list
 *
 */
#function collectSet($list, $field)
	#if ($vs.util.isList($list))
		#set($result = $vs.util.newList())
		#set($set = $vs.util.newList())
		#foreach($row in $list)
			#set($value = $row)
			#if ($vs.util.isNotNull($field))
				#set($value = $row.get($field))
			#end
			#if ($vs.util.isNotNull($value) and !$set.contains($value))
				$result.add($value)
				$set.add($value)
			#end
		#end
		return $result
	#end
	return null
#end

/**
 * remoteList
 * 远程列表。
 *
 * @param $systemId:string:系统id。
 * @param $strTableName:string:表。
 * @param $strColumns:string:字段。
 * @param $where:map:条件。
 *
 * @return list:列表。
 *
 */
#function remoteList($systemId, $strTableName, $strColumns, $where)
	#if ($vs.util.isBlankOne($strTableName, $systemId))
		return null
	#end
	#if ($vs.util.isNull($strColumns))
		#set($strColumns = ' * ')
	#end
	#set($strSql = `
		select
			${$strColumns}
		from
			${strTableName}
		where 1 = 1
	`)
	#set($strSql = $strSql + @andWhere($where))
	return $vs.dbTools.remoteList($systemId, $strSql ,$where)
#end

/**
 * andWhere
 * 条件。
 * 
 * @param $where:map:条件
 *
 * @return string:sql。
 *
 */
#function andWhere($where)
	#set($sql = '')
	#if ($vs.util.isMap($where))
		#foreach ($key in $vs.util.getMapKeys($where))
			#set($value = $where.get($key))
			#if ($vs.util.isList($value))
				#set($sql = $sql + " and " + $vs.sqlHelper.listIn($value, $key))
			#else
				#set($sql = $sql + $vs.sqlHelper.and($where, $key))
			#end
		#end
	#end
	return $sql
#end

/**
 * list2map
 * list转map:支持全量映射、单字段映射。
 * 
 * @param $list:list:列表。
 * @param $key:string:键字段域。
 * @param $field:string:值字段域。
 *
 * @return map
 *
 */ 
#function list2map($list, $key, $field)
	#if ($vs.util.isList($list))
		#set($map = $vs.util.newMap())
		#foreach($row in $list)
			#set($value = $row)
			#if (!$vs.util.isNull($field))
				#set($value = $row.get($field))
			#end
			$map.put($row.get($key), $value)
		#end
		return $map
	#end
	return null
#end

/**
 * listPut
 * 数据组装。
 *
 * @param $list:list:列表。
 * @param $map:map:映射。
 * @param $key:string:键。
 * @param $field:string:域。
 *
 * @return null
 *
 */ 
#function listPut($list, $map, $key, $field)
	#if ($vs.util.isList($list) and $vs.util.isMap($map) and $vs.util.isString($key))
		#if ($vs.util.isNull($field))
			#set($field = 'data')
		#end
		#foreach($item in $list)
			#set($itemKey = $item.get($key))
			#set($data = $map.get($itemKey))
			$item.put($field, $data)
		#end
	#end
	return null
#end

网站公告

今日签到

点亮在社区的每一天
去签到