<?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.ruoyi.project.invoice.mapper.TInvoiceUserUnitMapper">

    <resultMap type="TInvoiceUserUnit" id="TInvoiceUserUnitResult">
        <result property="id"    column="id"    />
        <result property="userUnit"    column="user_unit"    />
        <result property="userIds"    column="user_ids"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createrCode"    column="creater_code"    />
        <result property="createdate"    column="createdate"    />
        <result property="updaterCode"    column="updater_code"    />
        <result property="updatedate"    column="updatedate"    />
        <result property="deptId"    column="dept_id"    />
        <result property="remarks"    column="remarks"    />
        <result property="deptName" column="dept_name" />
        <result property="nickName" column="nick_name" />
    </resultMap>

    <sql id="selectTInvoiceUserUnitVo">
        select d.id, d.user_unit, d.user_ids, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id, d.remarks ,s.dept_name,u.nick_name from t_invoice_user_unit d
      left join sys_dept s on s.dept_id = d.dept_id
      left join sys_user u on d.user_ids = u.user_id
    </sql>

    <select id="selectTInvoiceUserUnitList" parameterType="TInvoiceUserUnit" resultMap="TInvoiceUserUnitResult">
        <include refid="selectTInvoiceUserUnitVo"/>
        <where>
            <if test="userUnit != null  and userUnit != ''"> and user_unit = #{userUnit}</if>
            <if test="userIds != null  and userIds != ''"> and user_ids = #{userIds}</if>
            <if test="createrCode != null  and createrCode != ''"> and creater_code = #{createrCode}</if>
            <if test="createdate != null "> and createdate = #{createdate}</if>
            <if test="updaterCode != null "> and updater_code = #{updaterCode}</if>
            <if test="updatedate != null "> and updatedate = #{updatedate}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by user_unit
    </select>

    <select id="selectTInvoiceUserUnitListToExcel" parameterType="TInvoiceUserUnit" resultMap="TInvoiceUserUnitResult">
        select d.id, d.user_unit, d.user_ids, d.del_flag, su.nick_name creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id, d.remarks ,s.dept_name,u.nick_name from t_invoice_user_unit d
        left join sys_dept s on s.dept_id = d.dept_id
        left join sys_user u on d.user_ids = u.user_id
        left join sys_user su on d.creater_code = su.user_id

        <where>
            <if test="userUnit != null  and userUnit != ''"> and user_unit = #{userUnit}</if>
            <if test="userIds != null  and userIds != ''"> and user_ids = #{userIds}</if>
            <if test="createrCode != null  and createrCode != ''"> and creater_code = #{createrCode}</if>
            <if test="createdate != null "> and createdate = #{createdate}</if>
            <if test="updaterCode != null "> and updater_code = #{updaterCode}</if>
            <if test="updatedate != null "> and updatedate = #{updatedate}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by user_unit
    </select>

    <select id="selectAllTInvoiceUserUnit" parameterType="TInvoiceUserUnit" resultMap="TInvoiceUserUnitResult">
        select distinct (d.user_unit) from t_invoice_user_unit d
        left join SYS_DEPT s on s.dept_id=d.dept_id
        <where>
            and d.del_flag = 0
        </where>
        group by d.user_unit
    </select>

    <select id="selectTInvoiceUserUnitById" parameterType="Long" resultMap="TInvoiceUserUnitResult">
        <include refid="selectTInvoiceUserUnitVo"/>
        where id = #{id}
    </select>

    <select id="selectTInvoiceUserUnitByUnit" parameterType="String" resultMap="TInvoiceUserUnitResult">
        <include refid="selectTInvoiceUserUnitVo"/>
        where user_unit = #{userUnit} and d.del_flag=0
    </select>

    <insert id="insertTInvoiceUserUnit" parameterType="TInvoiceUserUnit">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_invoice_user_unit.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_invoice_user_unit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="userUnit != null">user_unit,</if>
            <if test="userIds != null">user_ids,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createrCode != null">creater_code,</if>
            <if test="createdate != null">createdate,</if>
            <if test="updaterCode != null">updater_code,</if>
            <if test="updatedate != null">updatedate,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="userUnit != null">#{userUnit},</if>
            <if test="userIds != null">#{userIds},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createrCode != null">#{createrCode},</if>
            <if test="createdate != null">#{createdate},</if>
            <if test="updaterCode != null">#{updaterCode},</if>
            <if test="updatedate != null">#{updatedate},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>

    <update id="updateTInvoiceUserUnit" parameterType="TInvoiceUserUnit">
        update t_invoice_user_unit
        <trim prefix="SET" suffixOverrides=",">
            <if test="userUnit != null">user_unit = #{userUnit},</if>
            <if test="userIds != null">user_ids = #{userIds},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createrCode != null">creater_code = #{createrCode},</if>
            <if test="createdate != null">createdate = #{createdate},</if>
            <if test="updaterCode != null">updater_code = #{updaterCode},</if>
            <if test="updatedate != null">updatedate = #{updatedate},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteTInvoiceUserUnitById" parameterType="Long">
        update t_invoice_user_unit set del_flag = 2 where id = #{id}
    </update>

    <update id="deleteTInvoiceUserUnitByIds" parameterType="String">
        update t_invoice_user_unit set del_flag = 2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
