<?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="com.xisc.pm.infra.mapper.PmPolicyMapper">


    <sql id = "BaseSql">
    PP.POLICY_ID,
    PP.POLICY_CODE,
    PP.POLICY_DESC,
    PP.PROD_LINE_ID,
    PP.A2_WID_DEVIATION,
    PP.CONSIGN_CUST,
    PP.ST_LOWER_ECONOMIC_LOTS,
    PP.CUST_LOWER_ECONOMIC_LOTS,
    PP.TENANT_ID,
    PP.ATTRIBUTE1,
    PP.ATTRIBUTE2,
    PP.ATTRIBUTE3,
    PP.ATTRIBUTE4,
    PP.ATTRIBUTE5,
    PP.creation_date,    PP.created_by,    PP.last_updated_by,    PP.last_update_date,    PP.object_version_number
    </sql>

    <select id = "selectList" resultType = "com.xisc.pm.domain.entity.PmPolicy">
        select  DISTINCT
        ppl.PROD_LINE_CODE,
        PP.POLICY_ID,
        PP.POLICY_CODE,
        PP.POLICY_DESC,
        PP.PROD_LINE_ID,
        PP.A2_WID_DEVIATION,
        PP.CONSIGN_CUST,
        PP.ST_LOWER_ECONOMIC_LOTS,
        PP.CUST_LOWER_ECONOMIC_LOTS,
        PP.TENANT_ID,
        PP.creation_date,
        PP.created_by,
        PP.last_updated_by,
        PP.last_update_date,
        PP.object_version_number
        from 	PM_POLICY PP
        LEFT JOIN PM_POLICY_STEEL_TYPE ppst ON PP.POLICY_ID = ppst.POLICY_ID
        LEFT JOIN PM_PRODUCTION_LINE ppl ON PP.PROD_LINE_ID = ppl.PROD_LINE_ID
        LEFT JOIN PM_POLICY_SECTION pps on  PP.POLICY_ID = pps.POLICY_ID
        <where>
            <if test="policyIdList != null and policyIdList.size() > 0">
                and  PP.POLICY_ID in
                <foreach collection="policyIdList" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="dto.policyCode !=null">
                <bind name="policyCodeLike" value="'%' + dto.policyCode + '%'"/>
                and PP.POLICY_CODE like #{policyCodeLike}
            </if>
            <if test="dto.invertedFlag !=null">
                and PP.INVERTED_FLAG = #{dto.invertedFlag}
            </if>
            <if test="tenantId !=null">
                and PP.TENANT_ID = #{tenantId}
            </if>
            <if test="dto.prodLineId !=null">
                and PP.PROD_LINE_ID = #{dto.prodLineId}
            </if>
            <if test="stNoList != null and stNoList.size() > 0">
				and ppst.ST_NO in
				<foreach collection="stNoList" item="stNo" open="(" close=")" separator="," >
					#{stNo}
				</foreach>
			</if>
        </where>
    </select>
    
    <select id="selectByPolicyGroupId" resultType = "com.xisc.pm.domain.entity.PmPolicy">
    	select 
    		ppl.PROD_LINE_CODE,
    		<include refid="BaseSql" />
    	 from PM_POLICY pp
		left JOIN PM_POLICY_GROUP_REL ppgr on pp.POLICY_ID  = ppgr.POLICY_ID
		LEFT JOIN PM_PRODUCTION_LINE ppl ON PP.PROD_LINE_ID = ppl.PROD_LINE_ID
		WHERE ppgr.POLICY_GROUP_ID = #{policyGroupId}
		and ppgr.TENANT_ID = #{tenantId}
		and pp.TENANT_ID = #{tenantId}
		ORDER BY ppgr.SEQ ASC
    </select>
    
    
</mapper>

