<#ibiztemplate>
TARGET=PSDATAENTITY
</#ibiztemplate>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper	namespace="${pub.getPKGCodeName()}.${item.getPSSystemModule().codeName?lower_case}.mapper.${item.codeName}Mapper">

	<#comment>暴露明细数据查询mapper给主关系调用</#comment>
	<#if item.getMinorPSDERs?? && item.getMinorPSDERs()??>
		<#list item.getMinorPSDERs() as MinorPSDER>
			<#assign MajorEntity = MinorPSDER.getMajorPSDataEntity()>
			<#assign MajorkeyField = MajorEntity.getKeyPSDEField()>
			<#assign MajorField = MinorPSDER.getPSPickupDEField()>
			<#list item.getAllPSDEDataQueries() as singleQuery>
				<#list singleQuery.getAllPSDEDataQueryCodes() as dedqcode>
	<select id="selectBy${MajorField.getName()?lower_case}"  resultMap="${de.codeName}ResultMap">
		<![CDATA[	select m1.* from(	select t1.* from (
		${srfjavasqlcode('${dedqcode.getQueryCode()}')}
		)t1   ) m1  where  ${MajorField.getName()?lower_case}=<#noparse>#{</#noparse>${MajorkeyField.codeName?lower_case}<#noparse>}</#noparse>
		]]>
	</select>
				</#list>
				<#break>
			</#list>
		</#list>
	</#if>

		<#comment>实体数据查询</#comment>
		<#list item.getAllPSDEDataQueries() as singleQuery>
			<#list singleQuery.getAllPSDEDataQueryCodes() as dedqcode>
	<select id="search${singleQuery.getCodeName()}"  resultMap="${de.codeName}ResultMap">
        <![CDATA[select t1.* from (
		${srfjavasqlcode('${dedqcode.getQueryCode()}')}
        )t1]]>
        <where><if test="ew!=null and ew.sqlSegment!=null and !ew.emptyOfWhere"><#noparse>${ew.sqlSegment}</#noparse></if></where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere"><#noparse>${ew.sqlSegment}</#noparse></if>
    </select>
			</#list>
		</#list>

	<#comment>mybatis返回结果映射</#comment>
	<resultMap id="${de.codeName}ResultMap" type="${pub.getPKGCodeName()}.${item.getPSSystemModule().codeName?lower_case}.domain.${item.codeName}" autoMapping="true">
		<id property="${item.getKeyPSDEField().codeName?lower_case}" column="${item.getKeyPSDEField().getName()?lower_case}" /><!--主键字段映射-->
		<#if item.getMinorPSDERs?? && item.getMinorPSDERs()??>
			<#list item.getMinorPSDERs() as MinorPSDER>
				<#assign MajorField = MinorPSDER.getPSPickupDEField()>
		<result property="${MajorField.codeName?lower_case}" column="${MajorField.getName()?lower_case}" /><!--关系字段映射-->
			</#list>
		</#if>
		<#comment>1：N关系中，在子实体中创建父实体的实例对象</#comment>
		<#if item.getMinorPSDERs?? && item.getMinorPSDERs()??>
			<#list item.getMinorPSDERs() as MinorPSDER>
				<#assign MajorEntity = MinorPSDER.getMajorPSDataEntity()>
		<association property="${MinorPSDER.getCodeName()?lower_case}" javaType="${pub.getPKGCodeName()}.${MajorEntity.getPSSystemModule().codeName?lower_case}.domain.${MajorEntity.codeName}" column="${MajorField.getName()?lower_case}" select="${pub.getPKGCodeName()}.${MajorEntity.getPSSystemModule().codeName?lower_case}.mapper.${MajorEntity.codeName}Mapper.selectById" fetchType="lazy"></association>
			</#list>
		</#if>
		<#comment>1：N关系中，在父实体中创建子实体的List集合</#comment>
		<#if item.getMajorPSDERs?? && item.getMajorPSDERs()??>
			<#list item.getMajorPSDERs() as MajorPSDER>
				<#assign MinorEntity = MajorPSDER.getMinorPSDataEntity()>
				<#assign MajorDerField = MajorPSDER.getPSPickupDEField()>
		<collection property="${MajorPSDER.getMinorCodeName()?lower_case}" ofType="${pub.getPKGCodeName()}.${MinorEntity.getPSSystemModule().codeName?lower_case}.domain.${MinorEntity.codeName}" column="${item.getKeyPSDEField().getName()?lower_case}" select="${pub.getPKGCodeName()}.${MinorEntity.getPSSystemModule().codeName?lower_case}.mapper.${MinorEntity.codeName}Mapper.selectBy${MajorDerField.getName()?lower_case}" fetchType="lazy"></collection>
			</#list>
		</#if> 
	</resultMap>

</mapper>
