<?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.zehong.system.mapper.TDoubleBookMapper">
    
    <resultMap type="TDoubleBook" id="TDoubleBookResult">
        <result property="bookId"    column="book_id"    />
        <result property="beyondEnterpriseId"    column="beyond_enterprise_id"    />
        <result property="username"    column="username"    />
        <result property="phone"    column="phone"    />
        <result property="type"    column="type"    />
        <result property="finishTime"    column="finish_time"    />
        <result property="dosage"    column="dosage"    />
        <result property="status"    column="status"    />
        <result property="township"    column="township"    />
        <result property="housesAddress"    column="houses_address"    />
        <result property="housesInfo"    column="houses_info"    />
        <result property="createTime"    column="create_time"    />
        <result property="isDel"    column="is_del"    />
<!--        导出使用-->
        <result property="townshipName"    column="townshipName"    />
    </resultMap>

    <sql id="selectTDoubleBookVo">
        SELECT b.book_id, b.beyond_enterprise_id, b.username, b.phone, b.type, b.finish_time,
	    b.dosage, b.status, b.township, b.houses_address, b.houses_info, b.create_time, b.is_del ,
        e.`enterprise_name` AS beyondEnterpriseName
        FROM t_double_book b
        LEFT JOIN t_enterprise_info e ON b.`beyond_enterprise_id` = e.`enterprise_id`
    </sql>

    <sql id="exportTDoubleBookVo">
        SELECT b.book_id, b.beyond_enterprise_id, b.username, b.phone, b.type, b.finish_time,
               b.dosage, b.status, r.f_name as townshipName, b.houses_address, b.houses_info, b.create_time, b.is_del ,
               e.`enterprise_name` AS beyondEnterpriseName
        FROM t_double_book b
                 LEFT JOIN t_enterprise_info e ON b.`beyond_enterprise_id` = e.`enterprise_id`
                 LEFT JOIN t_county_level_region r ON b.`township` = r.`f_id`
    </sql>

    <select id="selectTDoubleBookList" parameterType="TDoubleBook" resultMap="TDoubleBookResult">
        <include refid="selectTDoubleBookVo"/>
        <where>  
            <if test="beyondEnterpriseId != null "> and b.beyond_enterprise_id = #{beyondEnterpriseId}</if>
            <if test="username != null  and username != ''"> and b.username like concat('%', #{username}, '%')</if>
            <if test="phone != null  and phone != ''"> and b.phone like concat('%', #{phone}, '%')</if>
            <if test="type != null  and type != ''"> and b.type = #{type}</if>
        </where>
        order by b.create_time desc
    </select>

    <select id="exportTDoubleBookList" parameterType="TDoubleBook" resultMap="TDoubleBookResult">
        <include refid="exportTDoubleBookVo"/>
        <where>
            <if test="beyondEnterpriseId != null "> and b.beyond_enterprise_id = #{beyondEnterpriseId}</if>
            <if test="username != null  and username != ''"> and b.username like concat('%', #{username}, '%')</if>
            <if test="phone != null  and phone != ''"> and b.phone like concat('%', #{phone}, '%')</if>
            <if test="type != null  and type != ''"> and b.type = #{type}</if>
        </where>
        order by b.create_time desc
    </select>

    <select id="selectTDoubleBookById" parameterType="Long" resultMap="TDoubleBookResult">
        <include refid="selectTDoubleBookVo"/>
        where book_id = #{bookId}
    </select>
        
    <insert id="insertTDoubleBook" parameterType="TDoubleBook" useGeneratedKeys="true" keyProperty="bookId">
        insert into t_double_book
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="beyondEnterpriseId != null">beyond_enterprise_id,</if>
            <if test="username != null">username,</if>
            <if test="phone != null">phone,</if>
            <if test="type != null">type,</if>
            <if test="finishTime != null">finish_time,</if>
            <if test="dosage != null">dosage,</if>
            <if test="status != null">status,</if>
            <if test="township != null">township,</if>
            <if test="housesAddress != null">houses_address,</if>
            <if test="housesInfo != null">houses_info,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isDel != null">is_del,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="beyondEnterpriseId != null">#{beyondEnterpriseId},</if>
            <if test="username != null">#{username},</if>
            <if test="phone != null">#{phone},</if>
            <if test="type != null">#{type},</if>
            <if test="finishTime != null">#{finishTime},</if>
            <if test="dosage != null">#{dosage},</if>
            <if test="status != null">#{status},</if>
            <if test="township != null">#{township},</if>
            <if test="housesAddress != null">#{housesAddress},</if>
            <if test="housesInfo != null">#{housesInfo},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isDel != null">#{isDel},</if>
         </trim>
    </insert>

    <update id="updateTDoubleBook" parameterType="TDoubleBook">
        update t_double_book
        <trim prefix="SET" suffixOverrides=",">
            <if test="beyondEnterpriseId != null">beyond_enterprise_id = #{beyondEnterpriseId},</if>
            <if test="username != null">username = #{username},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="type != null">type = #{type},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
            <if test="dosage != null">dosage = #{dosage},</if>
            <if test="status != null">status = #{status},</if>
            <if test="township != null">township = #{township},</if>
            <if test="housesAddress != null">houses_address = #{housesAddress},</if>
            <if test="housesInfo != null">houses_info = #{housesInfo},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where book_id = #{bookId}
    </update>

    <delete id="deleteTDoubleBookById" parameterType="Long">
        delete from t_double_book where book_id = #{bookId}
    </delete>

    <delete id="deleteTDoubleBookByIds" parameterType="String">
        delete from t_double_book where book_id in 
        <foreach item="bookId" collection="array" open="(" separator="," close=")">
            #{bookId}
        </foreach>
    </delete>
</mapper>