<?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.brilliance.isc.mda.dao.BopcodMapper">

    <resultMap id="BaseResultMap" type="com.brilliance.isc.bo.Bopcod">
        <result property="inr" column="inr" jdbcType="VARCHAR"/>
        <result property="ver" column="ver" jdbcType="VARCHAR"/>
        <result property="cod" column="cod" jdbcType="VARCHAR"/>
        <result property="dir" column="dir" jdbcType="VARCHAR"/>
        <result property="typ" column="typ" jdbcType="VARCHAR"/>
        <result property="txt" column="txt" jdbcType="VARCHAR"/>
        <result property="sta" column="sta" jdbcType="VARCHAR"/>
        <result property="ghutyp" column="ghutyp" jdbcType="VARCHAR"/>
        <result property="waitjm" column="waitjm" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        inr,ver,cod,dir,typ,txt,sta,ghutyp,waitjm
    </sql>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from bopcod
        where inr = #{inr,jdbcType=VARCHAR}
    </select>

    <select id="selectByCod" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from bopcod
        where cod = #{cod}
        and dir = #{dir}
    </select>

    <select id="selectByCodShenBao" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from bopcod
        where cod = #{cod}
    </select>

    <delete id="deleteByPrimaryKey">
        delete from bopcod
        where inr = #{inr,jdbcType=VARCHAR}
    </delete>

    <insert id="insert">
        insert into bopcod (<include refid="Base_Column_List"/>)
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{inr,jdbcType=VARCHAR},
            #{ver,jdbcType=VARCHAR},
            #{cod,jdbcType=VARCHAR},
            #{dir,jdbcType=VARCHAR},
            #{typ,jdbcType=VARCHAR},
            #{txt,jdbcType=VARCHAR},
            #{sta,jdbcType=VARCHAR},
        </trim>
    </insert>

    <insert id="insertSelective">
        insert into bopcod
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="inr != null">inr,</if>
            <if test="ver != null">ver,</if>
            <if test="cod != null">cod,</if>
            <if test="dir != null">dir,</if>
            <if test="typ != null">typ,</if>
            <if test="txt != null">txt,</if>
            <if test="sta != null">sta,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="inr != null">#{inr,jdbcType=VARCHAR},</if>
            <if test="ver != null">#{ver,jdbcType=VARCHAR},</if>
            <if test="cod != null">#{cod,jdbcType=VARCHAR},</if>
            <if test="dir != null">#{dir,jdbcType=VARCHAR},</if>
            <if test="typ != null">#{typ,jdbcType=VARCHAR},</if>
            <if test="txt != null">#{txt,jdbcType=VARCHAR},</if>
            <if test="sta != null">#{sta,jdbcType=VARCHAR},</if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective">
        update bopcod
        <set>
            <if test="ver != null">ver = #{ver,jdbcType=VARCHAR},</if>
            <if test="cod != null">cod = #{cod,jdbcType=VARCHAR},</if>
            <if test="dir != null">dir = #{dir,jdbcType=VARCHAR},</if>
            <if test="typ != null">typ = #{typ,jdbcType=VARCHAR},</if>
            <if test="txt != null">txt = #{txt,jdbcType=VARCHAR},</if>
            <if test="sta != null">sta = #{sta,jdbcType=VARCHAR},</if>
        </set>
        where inr = #{inr,jdbcType=VARCHAR}
    </update>

    <update id="updateByPrimaryKey">
        update bopcod
        <set>
            ver = #{ver,jdbcType=VARCHAR},
            cod = #{cod,jdbcType=VARCHAR},
            dir = #{dir,jdbcType=VARCHAR},
            typ = #{typ,jdbcType=VARCHAR},
            txt = #{txt,jdbcType=VARCHAR},
            sta = #{sta,jdbcType=VARCHAR},
        </set>
        where inr = #{inr,jdbcType=VARCHAR}
    </update>


    <select id="queryAllByLimit" resultMap="BaseResultMap">
        select
        INR,VER,COD,DIR,TYP,TXT,STA from BOPCOD
        <where>
            <if test="inr != null and inr != ''">
                AND INR like concat(concat ('%',#{inr}),'%')
            </if>
            <if test="ver != null and ver != ''">
                and VER like concat(concat ('%',#{ver}),'%')
            </if>
            <if test="cod != null and cod != ''">
                and COD like concat(concat ('%',#{cod}),'%')
            </if>
            <if test="dir != null and dir != ''">
                and DIR like concat(concat ('%',#{dir}),'%')
            </if>
            <if test="typ != null and typ != ''">
                and TYP like concat(concat ('%',#{typ}),'%')
            </if>
            <if test="txt != null and txt != ''">
                and TXT like concat(concat ('%',#{txt}),'%')
            </if>
            <if test="sta != null and sta != ''">
                and STA like concat(concat ('%',#{sta}),'%')
            </if>
        </where>
        order by length(INR) desc, INR DESC
    </select>

    <select id="queryBopcodList" resultMap="BaseResultMap">
        select
        INR,VER,COD,DIR,TYP,TXT,STA,ghutyp,waitjm from BOPCOD
        <where>
            <if test="inr != null and inr != ''">
                AND INR like concat(concat ('%',#{inr}),'%')
            </if>
            <if test="ver != null and ver != ''">
                and VER like concat(concat ('%',#{ver}),'%')
            </if>
            <if test="cod != null and cod != ''">
                and COD like concat(concat ('%',#{cod}),'%')
            </if>
            <if test="dir != null and dir != ''">
                and DIR like concat(concat ('%',#{dir}),'%')
            </if>
            <if test="typ != null and typ != ''">
                and TYP like concat(concat ('%',#{typ}),'%')
            </if>
            <if test="txt != null and txt != ''">
                and TXT like concat(concat ('%',#{txt}),'%')
            </if>
            <if test="sta != null and sta != ''">
                and STA like concat(concat ('%',#{sta}),'%')
            </if>
        </where>
        order by cod asc
    </select>

    <select id="queryById" resultMap="BaseResultMap">
        select
        INR,VER,COD,DIR,TYP,TXT,STA from BOPCOD
        where INR = #{inr}
    </select>

    <update id="update">
        update BOPCOD
        <set>
            <if test="typ != null and typ != ''">
                TYP = #{typ},
            </if>
            <if test="txt != null and txt != ''">
                TXT = #{txt},
            </if>
        </set>
        where INR = #{inr}
    </update>

    <update id="deleteById">
        update BOPCOD
        <set>
            <if test="sta != null and sta != ''">
                STA = #{sta},
            </if>
        </set>
        where INR = #{inr}
    </update>

    <select id="checkadd" resultType="java.lang.Integer">
        select count(*) from BOPCOD
        <where>
            <if test="cod != null and cod != ''">
                AND COD = #{cod}
            </if>
            <if test="dir != null and dir != ''">
                and DIR = #{dir}
            </if>
        </where>
    </select>

    <select id="listFuzzyByRecord" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from bopcod
        <where>
            <!-- <choose>
                 <when test='(bop != null and (bop == "I" or bop == "")) or bop == null'>
                     <if test="dir != null and dir != ''">
                         and DIR = #{dir}
                     </if>
                     <if test="typ != null and typ != ''">
                         and TYP = #{typ}
                     </if>
                 </when>
                 <otherwise>
                     and DIR = #{bop} and TYP = #{bop}
                 </otherwise>
             </choose>-->
            <choose>
                <when test='bop != null and bop == "I"'>
                    <choose>
                        <when test="dir != null and dir != ''">
                            and DIR = #{dir}
                        </when>
                        <otherwise>
                            and DIR in ('I','O')
                        </otherwise>
                    </choose>
                    <choose>
                        <when test="typ != null and typ != ''">
                            and TYP = #{typ}
                        </when>
                        <otherwise>
                            and TYP in ('T','N','C')
                        </otherwise>
                    </choose>
                </when>
                <when test='bop == null or bop == ""'>

                </when>
                <otherwise>
                    and DIR = #{bop} and TYP = #{bop}
                </otherwise>
            </choose>
            <if test="cod != null and cod != ''">
                and COD like concat('%',#{cod},'%')
            </if>
            <if test="txt != null and txt != ''">
                and TXT like concat('%',#{txt},'%')
            </if>
            <if test="sta != null and sta != ''">
                and STA = #{sta}
            </if>
        </where>
        order by sta asc
    </select>

    <select id="listByUniqueKey" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from bopcod
        where DIR = #{dir} and TYP = #{typ} and COD = #{cod}
    </select>

    <select id="getBopcodByCondition" resultMap="BaseResultMap">
        select
        INR,VER,COD,DIR,TYP,TXT,STA from BOPCOD
        <where>
            <if test="notCod != null and notCod != ''">
                and COD not like concat(concat ('%',#{notCod}),'%')
            </if>
            <if test="cod != null and cod != ''">
                and COD like concat(concat ('%',#{cod}),'%')
            </if>
            <if test="dir != null and dir != ''">
                and DIR = #{dir}
            </if>
            <if test="sta != null and sta != ''">
                and STA = #{sta}
            </if>
        </where>
        order by COD ASC
    </select>

    <select id="listByGhutyp" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from bopcod
        where DIR = #{dir} and STA = #{sta} and ghutyp like concat(concat ('%',#{ghutyp}),'%')
    </select>

    <select id="listByWaitjm" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from bopcod
        where DIR = #{dir} and STA = #{sta} and waitjm like concat(concat ('%',#{waitjm}),'%') order by COD ASC
    </select>

    <select id="getBopcodType" resultType="java.lang.String">
        select TYP from BOPCOD
        <where>
            STA = 'A'
            <if test="cod != null and cod != ''">
                and COD = #{cod}
            </if>
            <if test="dir != null and dir != ''">
                and DIR = #{dir}
            </if>
        </where>
    </select>

    <select id="getBopcod" resultMap="BaseResultMap">
        select
        INR,VER,COD,DIR,TYP,TXT,STA from BOPCOD
        <where>
            (substr(cod,4,3)='000' or substr(cod,1,1)='7')
            <if test="dir != null and dir != ''">
                and DIR = #{dir}
            </if>
            and STA = 'A'
            <if test="notCod != null and notCod != ''">
                and COD not like concat(concat ('%',#{notCod}),'%')
            </if>
            <if test="cod != null and cod != ''">
                and COD like concat(concat ('%',#{cod}),'%')
            </if>
        </where>
        order by COD ASC
    </select>

    <select id="getBopcodByCodNoZmq" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
         FROM BOPCOD WHERE DIR='O' AND STA='A' AND COD NOT LIKE 'D%' ORDER BY COD
    </select>

    <select id="getBopcodByCodAndZmq" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
         FROM BOPCOD WHERE DIR='O' AND STA='A' AND COD NOT LIKE '9%' ORDER BY COD
    </select>
</mapper>