<?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.qianhe.zqbf.mapper.ZqbfXmzBfzjMapper">
    <!--  返回实体  -->
    <resultMap id="ZqbfXmzBfzjResult" type="ZqbfXmzBfzjVo">
        <result property="zjid" column="zjid"/>
        <result property="dwid" column="dwid"/>
        <result property="dwmc" column="dwmc"/>
        <result property="zjmc" column="zjmc"/>
        <result property="zjzw" column="zjzw"/>
        <result property="zjzj" column="zjzj"/>
        <result property="sjh" column="sjh"/>
        <result property="cjr" column="cjr"/>
        <result property="cjsj" column="cjsj"/>
        <result property="gxr" column="gxr"/>
        <result property="gxsj" column="gxsj"/>
        <result property="bz" column="bz"/>
        <result property="yl1" column="yl1"/>
        <result property="yl2" column="yl2"/>
        <result property="yl3" column="yl3"/>
        <result property="yl4" column="yl4"/>
        <result property="yl5" column="yl5"/>
    </resultMap>
    <!--  通用查询  -->
    <sql id="selectCommon">
        SELECT
            bfzj.zjid,
            bfzj.dwid,
            CONCAT(dept.dept_name, dept.create_by) AS dwmc,
            bfzj.zjmc,
            bfzj.zjzw,
            bfzj.zjzj,
            bfzj.sjh,
            bfzj.cjr,
            bfzj.cjsj,
            bfzj.gxr,
            bfzj.gxsj,
            bfzj.bz,
            bfzj.yl1,
            bfzj.yl2,
            bfzj.yl3,
            bfzj.yl4,
            bfzj.yl5
        FROM
            zqbf_xmz_bfzj bfzj
            LEFT JOIN sys_dept dept ON bfzj.dwid = dept.dept_id
    </sql>
    <!--  条件查询  -->
    <select id="list" parameterType="ZqbfXmzBfzjQuery" resultMap="ZqbfXmzBfzjResult">
        <include refid="selectCommon"></include>
        <where>
            <if test="zjid != null ">AND bfzj.zjid = #{zjid}</if>
            <if test="dwid != null ">AND (dept.dept_id = #{dwid} OR FIND_IN_SET(#{dwid}, dept.ancestors))</if>
            <if test="zjmc != null  and zjmc !='' ">AND bfzj.zjmc like concat('%', #{zjmc}, '%')</if>
            ${params.dataScope}
        </where>
        ORDER BY bfzj.zjzj, bfzj.gxsj DESC
    </select>
    <!--  查询详情  -->
    <select id="info" parameterType="long" resultMap="ZqbfXmzBfzjResult">
        <include refid="selectCommon"></include> WHERE bfzj.zjid = #{id}
    </select>
    <!--  新增  -->
    <insert id="add" parameterType="ZqbfXmzBfzj">
        INSERT INTO zqbf_xmz_bfzj
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dwid != null">dwid,</if>
            <if test="user_id != null">user_id,</if>
            <if test="zjmc != null  and zjmc != ''">zjmc,</if>
            <if test="zjzw != null  and zjzw != ''">zjzw,</if>
            <if test="zjzj != null  and zjzj != ''">zjzj,</if>
            <if test="sjh != null  and sjh != ''">sjh,</if>
            <if test="cjr != null  and cjr != ''">cjr,</if>
            <if test="cjsj != null ">cjsj,</if>
            <if test="gxr != null  and gxr != ''">gxr,</if>
            <if test="bz != null  and bz != ''">bz,</if>
            <if test="yl1 != null  and yl1 != ''">yl1,</if>
            <if test="yl2 != null  and yl2 != ''">yl2,</if>
            <if test="yl3 != null  and yl3 != ''">yl3,</if>
            <if test="yl4 != null  and yl4 != ''">yl4,</if>
            <if test="yl5 != null  and yl5 != ''">yl5,</if>
            gxsj
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dwid != null">#{dwid},</if>
            <if test="user_id != null">#{user_id},</if>
            <if test="zjmc != null and zjmc !=''">#{zjmc},</if>
            <if test="zjzw != null and zjzw !=''">#{zjzw},</if>
            <if test="zjzj != null and zjzj != ''">#{zjzj},</if>
            <if test="sjh != null and sjh !=''">#{sjh},</if>
            <if test="cjr != null and cjr !=''">#{cjr},</if>
            <if test="cjsj != null">#{cjsj},</if>
            <if test="gxr != null and gxr !=''">#{gxr},</if>
            <if test="gxsj != null">#{gxsj},</if>
            <if test="bz != null and bz !=''">#{bz},</if>
            <if test="yl1 != null and yl1 != ''">#{yl1},</if>
            <if test="yl2 != null and yl2 != ''">#{yl2},</if>
            <if test="yl3 != null and yl3 != ''">#{yl3},</if>
            <if test="yl4 != null and yl4 != ''">#{yl4},</if>
            <if test="yl5 != null and yl5 != ''">#{yl5},</if>
            now()
        </trim>
    </insert>
    <!--  修改  -->
    <update id="edit" parameterType="ZqbfXmzBfzj">
        UPDATE zqbf_xmz_bfzj
        <trim prefix="SET" suffixOverrides=",">
            <if test="zjid != null">zjid = #{zjid},</if>
            <if test="user_id != null">user_id = #{user_id},</if>
            <if test="dwid != null">dwid = #{dwid},</if>
            <if test="zjmc != null and zjmc != ''">zjmc = #{zjmc},</if>
            <if test="zjzw != null and zjzw != ''">zjzw = #{zjzw},</if>
            <if test="zjzj != null and zjzj != ''">zjzj = #{zjzj},</if>
            <if test="sjh != null and sjh != ''">sjh = #{sjh},</if>
            <if test="cjr != null">cjr = #{cjr},</if>
            <if test="cjsj != null">cjsj = #{cjsj},</if>
            <if test="gxr != null">gxr = #{gxr},</if>
            <if test="bz != null">bz = #{bz},</if>
            <if test="yl1 != null and yl1 != ''">yl1 = #{yl1},</if>
            <if test="yl2 != null and yl2 != ''">yl2 = #{yl2},</if>
            <if test="yl3 != null and yl3 != ''">yl3 = #{yl3},</if>
            <if test="yl4 != null and yl4 != ''">yl4 = #{yl4},</if>
            <if test="yl5 != null and yl5 != ''">yl5 = #{yl5},</if>
            gxsj = now()
        </trim>
        WHERE zjid = #{zjid}
    </update>
    <!--  删除  -->
    <delete id="delete" parameterType="long">
        DELETE FROM zqbf_xmz_bfzj WHERE zjid IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>