<?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.hzya.frame.plugin.lets.dao.impl.BdTaxitemsDaoImpl">

    <resultMap id="get-BdTaxitemsEntity-result" type="com.hzya.frame.plugin.lets.entity.BdTaxitemsEntity">
        <result property="dr" column="dr" jdbcType="INTEGER"/>
        <result property="pkTaxitems" column="pk_taxitems" jdbcType="VARCHAR"/>
        <result property="taxcode" column="taxcode" jdbcType="VARCHAR"/>
        <result property="taxname" column="taxname" jdbcType="VARCHAR"/>
        <result property="taxratio" column="taxratio" jdbcType="NUMERIC"/>
        <result property="ts" column="ts" jdbcType="VARCHAR"/>
        <result property="pk_invbasdoc" column="pk_invbasdoc" jdbcType="VARCHAR"/>
        <result property="invcode" column="invcode" jdbcType="VARCHAR"/>
    </resultMap>
    <!-- 查询的字段-->
    <sql id="BdTaxitemsEntity_Base_Column_List">
        dr
        ,pk_taxitems
        ,taxcode 
        ,taxname 
        ,taxratio 
        ,ts
    </sql>
    <!-- 查询 采用==查询 -->
    <select id="entity_list_base" resultMap="get-BdTaxitemsEntity-result"
            parameterType="com.hzya.frame.plugin.lets.entity.BdTaxitemsEntity">
        select
        <include refid="BdTaxitemsEntity_Base_Column_List"/>
        from bd_taxitems
        <trim prefix="where" prefixOverrides="and">
            <if test="dr != null">and dr = #{dr}</if>
            <if test="pkTaxitems != null and pkTaxitems != ''">and pk_taxitems = #{pkTaxitems}</if>
            <if test="taxcode != null and taxcode != ''">and taxcode = #{taxcode}</if>
            <if test="taxname != null and taxname != ''">and taxname = #{taxname}</if>
            <if test="taxratio != null">and taxratio = #{taxratio}</if>
            <if test="ts != null and ts != ''">and ts = #{ts}</if>
            and sts='Y'
        </trim>
        <if test=" sort == null or sort == ''.toString() ">order by sorts asc</if>
        <if test=" sort !='' and sort!=null and order !='' and order!=null ">order by ${sort} ${order}</if>
    </select>

    <!-- 查询符合条件的数量 -->
    <select id="entity_count" resultType="Integer" parameterType="com.hzya.frame.plugin.lets.entity.BdTaxitemsEntity">
        select count(1) from bd_taxitems
        <trim prefix="where" prefixOverrides="and">
            <if test="dr != null">and dr = #{dr}</if>
            <if test="pkTaxitems != null and pkTaxitems != ''">and pk_taxitems = #{pkTaxitems}</if>
            <if test="taxcode != null and taxcode != ''">and taxcode = #{taxcode}</if>
            <if test="taxname != null and taxname != ''">and taxname = #{taxname}</if>
            <if test="taxratio != null">and taxratio = #{taxratio}</if>
            <if test="ts != null and ts != ''">and ts = #{ts}</if>
            and sts='Y'
        </trim>
        <if test=" sort == null or sort == ''.toString() ">order by sorts asc</if>
        <if test=" sort !='' and sort!=null and order !='' and order!=null ">order by ${sort} ${order}</if>
    </select>

    <!-- 分页查询列表 采用like格式 -->
    <select id="entity_list_like" resultMap="get-BdTaxitemsEntity-result"
            parameterType="com.hzya.frame.plugin.lets.entity.BdTaxitemsEntity">
        select
        <include refid="BdTaxitemsEntity_Base_Column_List"/>
        from bd_taxitems
        <trim prefix="where" prefixOverrides="and">
            <if test="dr != null">and dr like concat('%',#{dr},'%')</if>
            <if test="pkTaxitems != null and pkTaxitems != ''">and pk_taxitems like concat('%',#{pkTaxitems},'%')</if>
            <if test="taxcode != null and taxcode != ''">and taxcode like concat('%',#{taxcode},'%')</if>
            <if test="taxname != null and taxname != ''">and taxname like concat('%',#{taxname},'%')</if>
            <if test="taxratio != null">and taxratio like concat('%',#{taxratio},'%')</if>
            <if test="ts != null and ts != ''">and ts like concat('%',#{ts},'%')</if>
            and sts='Y'
        </trim>
        <if test=" sort == null or sort == ''.toString() ">order by sorts asc</if>
        <if test=" sort !='' and sort!=null and order !='' and order!=null ">order by ${sort} ${order}</if>
    </select>

    <!-- 查询列表 字段采用or格式 -->
    <select id="BdTaxitemsentity_list_or" resultMap="get-BdTaxitemsEntity-result"
            parameterType="com.hzya.frame.plugin.lets.entity.BdTaxitemsEntity">
        select
        <include refid="BdTaxitemsEntity_Base_Column_List"/>
        from bd_taxitems
        <trim prefix="where" prefixOverrides="and">
            <if test="dr != null">or dr = #{dr}</if>
            <if test="pkTaxitems != null and pkTaxitems != ''">or pk_taxitems = #{pkTaxitems}</if>
            <if test="taxcode != null and taxcode != ''">or taxcode = #{taxcode}</if>
            <if test="taxname != null and taxname != ''">or taxname = #{taxname}</if>
            <if test="taxratio != null">or taxratio = #{taxratio}</if>
            <if test="ts != null and ts != ''">or ts = #{ts}</if>
            and sts='Y'
        </trim>
        <if test=" sort == null or sort == ''.toString()  ">order by sorts asc</if>
        <if test=" sort !='' and sort!=null and order !='' and order!=null ">order by ${sort} ${order}</if>
    </select>

    <!--新增所有列-->
    <insert id="entity_insert" parameterType="com.hzya.frame.plugin.lets.entity.BdTaxitemsEntity"
            keyProperty="pkTaxitems" useGeneratedKeys="true">
        insert into bd_taxitems(
        <trim suffix="" suffixOverrides=",">
            <if test="dr != null">dr ,</if>
            <if test="pkTaxitems != null and pkTaxitems != ''">pk_taxitems ,</if>
            <if test="taxcode != null and taxcode != ''">taxcode ,</if>
            <if test="taxname != null and taxname != ''">taxname ,</if>
            <if test="taxratio != null">taxratio ,</if>
            <if test="ts != null and ts != ''">ts ,</if>
            <if test="sorts == null ">sorts,</if>
            <if test="sts == null ">sts,</if>
        </trim>
        )values(
        <trim suffix="" suffixOverrides=",">
            <if test="dr != null">#{dr} ,</if>
            <if test="pkTaxitems != null and pkTaxitems != ''">#{pkTaxitems} ,</if>
            <if test="taxcode != null and taxcode != ''">#{taxcode} ,</if>
            <if test="taxname != null and taxname != ''">#{taxname} ,</if>
            <if test="taxratio != null">#{taxratio} ,</if>
            <if test="ts != null and ts != ''">#{ts} ,</if>
            <if test="sorts == null ">(select (max(IFNULL( a.sorts, 0 )) + 1) as sort from bd_taxitems a WHERE a.sts =
                'Y' ),
            </if>
            <if test="sts == null ">'Y',</if>
        </trim>
        )
    </insert>
    <!-- 批量新增 -->
    <insert id="entityInsertBatch" keyProperty="pkTaxitems" useGeneratedKeys="true">
        insert into bd_taxitems(dr, taxcode, taxname, taxratio, ts, sts)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.dr},#{entity.taxcode},#{entity.taxname},#{entity.taxratio},#{entity.ts}, 'Y')
        </foreach>
    </insert>
    <!-- 批量新增或者修改-->
    <insert id="entityInsertOrUpdateBatch" keyProperty="pkTaxitems" useGeneratedKeys="true">
        insert into bd_taxitems(dr, taxcode, taxname, taxratio, ts)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.dr},#{entity.taxcode},#{entity.taxname},#{entity.taxratio},#{entity.ts})
        </foreach>
        on duplicate key update
        dr = values(dr),
        taxcode = values(taxcode),
        taxname = values(taxname),
        taxratio = values(taxratio),
        ts = values(ts)
    </insert>
    <!--通过主键修改方法-->
    <update id="entity_update" parameterType="com.hzya.frame.plugin.lets.entity.BdTaxitemsEntity">
        update bd_taxitems set
        <trim suffix="" suffixOverrides=",">
            <if test="dr != null">dr = #{dr},</if>
            <if test="taxcode != null and taxcode != ''">taxcode = #{taxcode},</if>
            <if test="taxname != null and taxname != ''">taxname = #{taxname},</if>
            <if test="taxratio != null">taxratio = #{taxratio},</if>
            <if test="ts != null and ts != ''">ts = #{ts},</if>
        </trim>
        where pk_taxitems = #{pkTaxitems}
    </update>
    <!-- 逻辑删除 -->
    <update id="entity_logicDelete" parameterType="com.hzya.frame.plugin.lets.entity.BdTaxitemsEntity">
        update bd_taxitems
        set sts= 'N',
            modify_time = #{modify_time},
            modify_user_id = #{modify_user_id}
        where pk_taxitems = #{pkTaxitems}
    </update>
    <!-- 多条件逻辑删除 -->
    <update id="entity_logicDelete_Multi_Condition" parameterType="com.hzya.frame.plugin.lets.entity.BdTaxitemsEntity">
        update bd_taxitems set sts= 'N' ,modify_time = #{modify_time},modify_user_id = #{modify_user_id}
        <trim prefix="where" prefixOverrides="and">
            <if test="dr != null">and dr = #{dr}</if>
            <if test="pkTaxitems != null and pkTaxitems != ''">and pk_taxitems = #{pkTaxitems}</if>
            <if test="taxcode != null and taxcode != ''">and taxcode = #{taxcode}</if>
            <if test="taxname != null and taxname != ''">and taxname = #{taxname}</if>
            <if test="taxratio != null">and taxratio = #{taxratio}</if>
            <if test="ts != null and ts != ''">and ts = #{ts}</if>
            and sts='Y'
        </trim>
    </update>
    <!--通过主键删除-->
    <delete id="entity_delete">
        delete
        from bd_taxitems
        where pk_taxitems = #{pkTaxitems}
    </delete>

    <select id="queryBdInvbasdocByInvcode" parameterType="com.hzya.frame.plugin.lets.entity.BdTaxitemsEntity" resultMap="get-BdTaxitemsEntity-result">
        select <include refid="BdTaxitemsEntity_Base_Column_List"></include> from bd_taxitems where pk_taxitems in (
            select pk_taxitems from bd_invbasdoc where invcode = #{invcode} and dr =0
        ) and dr = 0
    </select>

    <!--查询存货对应的税率-->
    <select id="queryBdInvbasdocByInvcodeV2" parameterType="com.hzya.frame.plugin.lets.entity.BdTaxitemsEntity" resultMap="get-BdTaxitemsEntity-result">
        SELECT
            bd_invbasdoc.pk_invbasdoc,
            bd_invbasdoc.invcode,
            bd_taxitems.*
        FROM
            bd_invbasdoc
                LEFT JOIN bd_taxitems ON bd_invbasdoc.pk_taxitems = bd_taxitems.pk_taxitems
        WHERE
            bd_invbasdoc.dr = 0
          and bd_taxitems.dr = 0
          <if test="invcode!=null and invcode!=''">
            and bd_invbasdoc.invcode = #{invcode}
          </if>
          <if test="invcodes!=null and invcodes!=''">
              and bd_invbasdoc.invcode in (${invcodes})
          </if>
    </select>

</mapper>