<?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.TInvoiceDeviceMapper">

    <resultMap type="TInvoiceDevice" id="TInvoiceDeviceResult">
        <result property="id"    column="id"    />
        <result property="devName"    column="dev_name"    />
        <result property="workArea"    column="work_area"    />
        <result property="regionalHead"    column="regional_head"    />
        <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="createrName" column="creater_name" />
    </resultMap>

    <sql id="selectTInvoiceDeviceVo">
        select d.id, d.dev_name, d.work_area, d.regional_head, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id, d.remarks ,s.dept_name,su.nick_name as creater_name from t_invoice_device d
      left join sys_dept s on s.dept_id = d.dept_id
        left join sys_user su on su.user_id=to_number(d.creater_code)
    </sql>

    <select id="selectTInvoiceDeviceList" parameterType="TInvoiceDevice" resultMap="TInvoiceDeviceResult">
        <include refid="selectTInvoiceDeviceVo"/>
        <where>
            <if test="devName != null  and devName != ''"> and dev_name like concat(concat('%', #{devName}), '%')</if>
            <if test="workArea != null  and workArea != ''"> and work_area = #{workArea}</if>
            <if test="regionalHead != null  and regionalHead != ''"> and regional_head = #{regionalHead}</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}
    </select>

    <select id="selectRegionalHeadByWorkArea" parameterType="String" resultMap="TInvoiceDeviceResult">
        select regional_head  from t_invoice_device d
        <where>
            <if test="workArea != null  and workArea != ''"> and work_area = #{workArea}</if>
            and d.del_flag = 0
        </where>
    </select>

    <select id="selectTInvoiceDeviceAllList"  resultType="java.lang.String">
        select d.work_area  from t_invoice_device d                                                                                                                                                   left join sys_dept s on s.dept_id = d.dept_id
        where d.del_flag = 0
    </select>

    <select id="selectTInvoiceDeviceById" parameterType="Long" resultMap="TInvoiceDeviceResult">
        <include refid="selectTInvoiceDeviceVo"/>
        where id = #{id}
    </select>


    <insert id="insertTInvoiceDevice" parameterType="TInvoiceDevice">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_invoice_device.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_invoice_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="devName != null">dev_name,</if>
            <if test="workArea != null">work_area,</if>
            <if test="regionalHead != null">regional_head,</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="devName != null">#{devName},</if>
            <if test="workArea != null">#{workArea},</if>
            <if test="regionalHead != null">#{regionalHead},</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="updateTInvoiceDevice" parameterType="TInvoiceDevice">
        update t_invoice_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="devName != null">dev_name = #{devName},</if>
            <if test="workArea != null">work_area = #{workArea},</if>
            <if test="regionalHead != null">regional_head = #{regionalHead},</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="deleteTInvoiceDeviceById" parameterType="Long">
        update t_invoice_device set del_flag = 2 where id = #{id}
    </update>

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

</mapper>
