<?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.dsk.system.mapper.BusinessUserMapper">

    <resultMap type="com.dsk.common.core.domain.entity.BusinessUser" id="BusinessUserResult">
        <result property="id" column="id"/>
        <result property="businessId" column="business_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="userId" column="user_id"/>
        <result property="isFounder" column="is_founder"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectBusinessUserVo">
        select id, business_id, dept_id, user_id, is_founder, create_time, update_time
        from business_user
    </sql>

    <select id="selectBusinessUserList" parameterType="com.dsk.common.core.domain.entity.BusinessUser" resultMap="BusinessUserResult">
        <include refid="selectBusinessUserVo"/>
        <where>
            <if test="businessId != null ">and business_id = #{businessId}</if>
            <if test="deptId != null ">and company_id = #{deptId}</if>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="isFounder != null ">and is_founder = #{isFounder}</if>
        </where>
    </select>

    <select id="selectBusinessUserById" parameterType="Long" resultMap="BusinessUserResult">
        <include refid="selectBusinessUserVo"/>
        where id = #{id}
    </select>
    <select id="selectCreatorByBusinessId" resultType="java.lang.String">
        select u.nick_name
        from business_user bu
        left join business_info i on i.id = bu.business_id
        left join sys_user u on u.user_id = bu.user_id
        where bu.is_founder = 1
        and i.id = #{BusinessId}
    </select>
    <select id="selectFounder" resultType="java.lang.Integer">
        select is_founder
        from business_user
        where business_id = #{businessId}
        and user_id = #{userId}
    </select>

    <insert id="insertBusinessUser" parameterType="com.dsk.common.core.domain.entity.BusinessUser" useGeneratedKeys="true" keyProperty="id">
        insert into business_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="businessId != null">business_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="isFounder != null">is_founder,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="businessId != null">#{businessId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="isFounder != null">#{isFounder},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateBusinessUser" parameterType="com.dsk.common.core.domain.entity.BusinessUser">
        update business_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="businessId != null">business_id = #{businessId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="isFounder != null">is_founder = #{isFounder},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBusinessUserById" parameterType="Long">
        delete
        from business_user
        where id = #{id}
    </delete>

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