<?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.PmPolicyGroupMapper">


    <sql id = "BaseSql">
        PPG.POLICY_GROUP_ID,
        PPG.POLICY_GROUP_CODE,
        PPG.POLICY_GROUP_DESC,
        PPG.DELIVY_DATE_FROM,
        PPG.DELIVY_DATE_TO,
        PPG.OVERCAST_QTY,
        PPG.OVERCAST_DELIVY_DATE_TO,
        PPG.DESIGN_LENGTH_MAX,
        PPG.BETTER_TO_WORSE_FLAG,
        PPG.YIELD_LOWER,
        PPG.INVERTED_FLAG,
        PPG.CONTRACT_NUMS,
        PPG.TENANT_ID,
        PPG.ATTRIBUTE1,
        PPG.ATTRIBUTE2,
        PPG.ATTRIBUTE3,
        PPG.ATTRIBUTE4,
        PPG.ATTRIBUTE5,
        PPG.creation_date,        PPG.created_by,        PPG.last_updated_by,        PPG.last_update_date,        PPG.object_version_number
    </sql>

    <select id = "selectList" resultType = "com.xisc.pm.domain.entity.PmPolicyGroup">
        select
        <include refid = "BaseSql"/>
        from PM_POLICY_GROUP PPG
        <where>
            <if test="policyGroupId !=null">
                and PPG.POLICY_GROUP_ID = #{policyGroupId,jdbcType = INTEGER}
            </if>
            <if test="policyGroupCode !=null">
                and PPG.POLICY_GROUP_CODE = #{policyGroupCode,jdbcType = VARCHAR}
            </if>
            <if test="policyGroupDesc !=null">
                and PPG.POLICY_GROUP_DESC = #{policyGroupDesc,jdbcType = VARCHAR}
            </if>
            <if test="delivyDateFrom !=null">
                and PPG.DELIVY_DATE_FROM = #{delivyDateFrom,jdbcType = OTHER}
            </if>
            <if test="delivyDateTo !=null">
                and PPG.DELIVY_DATE_TO = #{delivyDateTo,jdbcType = OTHER}
            </if>
            <if test="overcastQty !=null">
                and PPG.OVERCAST_QTY = #{overcastQty,jdbcType = INTEGER}
            </if>
            <if test="overcastDelivyDateTo !=null">
                and PPG.OVERCAST_DELIVY_DATE_TO = #{overcastDelivyDateTo,jdbcType = OTHER}
            </if>
            <if test="designLengthMax !=null">
                and PPG.DESIGN_LENGTH_MAX = #{designLengthMax,jdbcType = INTEGER}
            </if>
            <if test="betterToWorseFlag !=null">
                and PPG.BETTER_TO_WORSE_FLAG = #{betterToWorseFlag,jdbcType = INTEGER}
            </if>
            <if test="yieldLower !=null">
                and PPG.YIELD_LOWER = #{yieldLower,jdbcType = INTEGER}
            </if>
            <if test="invertedFlag !=null">
                and PPG.INVERTED_FLAG = #{invertedFlag,jdbcType = INTEGER}
            </if>
            <if test="tenantId !=null">
                and PPG.TENANT_ID = #{tenantId,jdbcType = INTEGER}
            </if>
            <if test="attribute1 !=null">
                and PPG.ATTRIBUTE1 = #{attribute1,jdbcType = VARCHAR}
            </if>
            <if test="attribute2 !=null">
                and PPG.ATTRIBUTE2 = #{attribute2,jdbcType = VARCHAR}
            </if>
            <if test="attribute3 !=null">
                and PPG.ATTRIBUTE3 = #{attribute3,jdbcType = VARCHAR}
            </if>
            <if test="attribute4 !=null">
                and PPG.ATTRIBUTE4 = #{attribute4,jdbcType = VARCHAR}
            </if>
            <if test="attribute5 !=null">
                and PPG.ATTRIBUTE5 = #{attribute5,jdbcType = VARCHAR}
            </if>
        </where>
    </select>

    <select id="queryList" resultType="com.xisc.pm.domain.entity.PmPolicyGroup">
        SELECT DISTINCT
            ppg.POLICY_GROUP_ID,
            ppg.POLICY_GROUP_CODE,
            ppg.POLICY_GROUP_DESC,
            ppg.DELIVY_DATE_FROM,
            ppg.DELIVY_DATE_TO,
            ppg.OVERCAST_QTY,
            ppg.OVERCAST_DELIVY_DATE_TO,
            ppg.DESIGN_LENGTH_MAX,
            ppg.BETTER_TO_WORSE_FLAG,
            ppg.YIELD_LOWER,
            ppg.INVERTED_FLAG,
            PPG.CONTRACT_NUMS,
            ppg.TENANT_ID,
            ppg.creation_date,
            ppg.created_by,
            ppg.last_updated_by,
            ppg.last_update_date,
            ppg.object_version_number
        FROM
            PM_POLICY_GROUP ppg
                left join PM_POLICY_GROUP_REL ppgr on ppg.POLICY_GROUP_ID = ppgr.POLICY_GROUP_ID
                left join PM_POLICY_STEEL_TYPE ppst on ppst.POLICY_ID = ppgr.POLICY_ID
        <where>
            <if test="tenantId !=null">
                and ppg.TENANT_ID = #{tenantId}
            </if>
            <if test="dto.policyGroupCode !=null">
                <bind name="policyGroupCodeLike" value="'%' + dto.policyGroupCode + '%'"/>
                and ppg.POLICY_GROUP_CODE like #{policyGroupCodeLike}
            </if>
            <if test="dto.stNo !=null">
                and ppst.ST_NO = #{dto.stNo}
            </if>
        	<if test="policyIdList != null and policyIdList.size() > 0">
				and ppgr.POLICY_ID in
				<foreach collection="policyIdList" item="item" open="(" close=")" separator="," >
					#{item}
				</foreach>
			</if>
        </where>
    </select>

    <select id="getProdLineCodeByGroupIdList" resultType="com.xisc.pm.domain.entity.PmPolicyGroup">
        SELECT
            ppgr.POLICY_GROUP_ID,
            ppl.PROD_LINE_CODE AS prodLineCodeValue
        FROM
            PM_POLICY_GROUP_REL ppgr
                JOIN PM_POLICY pp ON ppgr.POLICY_ID = pp.POLICY_ID
                JOIN PM_PRODUCTION_LINE ppl ON pp.PROD_LINE_ID = ppl.PROD_LINE_ID
        <where>
            <if test="tenantId !=null">
                and ppgr.TENANT_ID = #{tenantId}
            </if>
            <if test="policyGroupIdList != null and policyGroupIdList.size() > 0">
                and ppgr.POLICY_GROUP_ID in
                <foreach collection="policyGroupIdList" item="policyGroupId" open="(" close=")" separator="," >
                    #{policyGroupId}
                </foreach>
            </if>
        </where>
    </select>
    
    <select id="selectContGroupConditionByGroupId" resultType="com.xisc.pm.app.service.autoboard.dto.PmOrderGroupDemension">
    	select * from PM_POLICY_GR_ORDER where POLICY_GROUP_ID = #{policyGroupId} AND TENANT_ID = #{tenantId} order by PRIORITY asc
    </select>
    
</mapper>

