<?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.supervision.grassrootsregistration.mapper.BdglEquipMapper">

    <resultMap type="BdglEquip" id="BdglEquipResult">
        <result property="id"    column="id"    />
        <result property="type"    column="type"    />
        <result property="title"    column="title"    />
        <result property="unitId"    column="unit_id"    />
        <result property="granttime"    column="granttime"    />
        <result property="grantnum"    column="grantnum"    />
        <result property="ownnum"    column="ownnum"    />
        <result property="stocknum"    column="stocknum"    />
        <result property="intime"    column="intime"    />
        <result property="innum"    column="innum"    />
        <result property="peopleId"    column="people_id"    />
        <result property="outtime"    column="outtime"    />
        <result property="outnum"    column="outnum"    />
        <result property="peopleId1"    column="people_id1"    />
        <result property="quality"    column="quality"    />
        <result property="info"    column="info"    />
        <result property="unitId1"    column="unit_id1"    />
        <result property="createtime"    column="createtime"    />
        <result property="adminId"    column="admin_id"    />
        <result property="unitName"    column="unit_name"    />
        <result property="peopleName"    column="people_name"    />
        <result property="people1Name"    column="people1_name"    />
        <result property="unitName1"    column="unit_name1"    />
    </resultMap>

    <sql id="selectBdglEquipVo">
        select id, type, title, unit_id, granttime, grantnum, ownnum, stocknum, intime, innum, people_id, outtime, outnum, people_id1, quality, info, unit_id1, createtime, admin_id, unit_name, people_name, people1_name, unit_name1 from bdgl_equip
    </sql>

    <select id="selectBdglEquipList" parameterType="BdglEquip" resultMap="BdglEquipResult">
        select id, e.type, title, unit_id, granttime, grantnum, ownnum, stocknum, intime, innum, people_id, outtime, outnum, people_id1, quality, info, unit_id1, createtime, admin_id, unit_name, people_name, people1_name, unit_name1
        from bdgl_equip e left join sys_dept t
        on e.unit_id=t.dept_id
        <where>
            <if test="unitId1 != null ">
                and (e.unit_id1=#{unitId1} or find_in_set(#{unitId1}, t.ancestors))
            </if>
            <if test="type != null  and type != ''"> and e.type = #{type}</if>
            <if test="title != null  and title != ''"> and e.title = #{title}</if>
            <if test="unitId != null "> and e.unit_id = #{unitId}</if>
            <if test="granttime != null  and granttime != ''"> and e.granttime = #{granttime}</if>
            <if test="grantnum != null "> and e.grantnum = #{grantnum}</if>
            <if test="ownnum != null "> and e.ownnum = #{ownnum}</if>
            <if test="stocknum != null "> and e.stocknum = #{stocknum}</if>
            <if test="intime != null  and intime != ''"> and e.intime = #{intime}</if>
            <if test="innum != null "> and e.innum = #{innum}</if>
            <if test="peopleId != null "> and e.people_id = #{peopleId}</if>
            <if test="outtime != null  and outtime != ''"> and e.outtime = #{outtime}</if>
            <if test="outnum != null "> and e.outnum = #{outnum}</if>
            <if test="peopleId1 != null  and peopleId1 != ''"> and e.people_id1 = #{peopleId1}</if>
            <if test="quality != null  and quality != ''"> and e.quality = #{quality}</if>
            <if test="info != null  and info != ''"> and e.info = #{info}</if>
            <if test="createtime != null  and createtime != ''"> and e.createtime = #{createtime}</if>
            <if test="adminId != null "> and e.admin_id = #{adminId}</if>
            <if test="unitName != null  and unitName != ''"> and e.unit_name like concat('%', #{unitName}, '%')</if>
            <if test="peopleName != null  and peopleName != ''"> and e.people_name like concat('%', #{peopleName}, '%')</if>
            <if test="people1Name != null  and people1Name != ''"> and e.people1_name like concat('%', #{people1Name}, '%')</if>
            <if test="unitName1 != null  and unitName1 != ''"> and e.unit_name1 = #{unitName1}</if>
        </where>
        order by e.id desc
    </select>

    <select id="selectBdglEquipById" parameterType="Long" resultMap="BdglEquipResult">
        <include refid="selectBdglEquipVo"/>
        where id = #{id}
    </select>

    <select id="selectBdglByunitid" parameterType="Integer" resultMap="BdglEquipResult">
        <include refid="selectBdglEquipVo"/>
        where unit_id = #{id} and YEAR(createtime)=YEAR(NOW())
        order by type,id desc
    </select>
    <select id="selectlEquipCountByunitId" resultType="java.lang.Integer">
        select count(*) from bdgl_equip where unit_id = #{unitId} and YEAR(createtime)=#{year}
    </select>
    <select id="selectEquIpByunitId" resultMap="BdglEquipResult">
        <include refid="selectBdglEquipVo"/>
        <where>
            <if test="unitId != null"> and unit_id = #{unitId}</if>
            <if test="year != null and year != ''"> and YEAR(createtime)= #{year}</if>
        </where>
        order by type,id desc
    </select>

    <insert id="insertBdglEquip" parameterType="BdglEquip" useGeneratedKeys="true" keyProperty="id">
        insert into bdgl_equip
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="type != null">type,</if>
            <if test="title != null">title,</if>
            <if test="unitId != null">unit_id,</if>
            <if test="granttime != null">granttime,</if>
            <if test="grantnum != null">grantnum,</if>
            <if test="ownnum != null">ownnum,</if>
            <if test="stocknum != null">stocknum,</if>
            <if test="intime != null">intime,</if>
            <if test="innum != null">innum,</if>
            <if test="peopleId != null">people_id,</if>
            <if test="outtime != null">outtime,</if>
            <if test="outnum != null">outnum,</if>
            <if test="peopleId1 != null">people_id1,</if>
            <if test="quality != null">quality,</if>
            <if test="info != null">info,</if>
            <if test="unitId1 != null">unit_id1,</if>
            <if test="createtime != null">createtime,</if>
            <if test="adminId != null">admin_id,</if>
            <if test="unitName != null">unit_name,</if>
            <if test="peopleName != null">people_name,</if>
            <if test="people1Name != null">people1_name,</if>
            <if test="unitName1 != null">unit_name1,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="type != null">#{type},</if>
            <if test="title != null">#{title},</if>
            <if test="unitId != null">#{unitId},</if>
            <if test="granttime != null">#{granttime},</if>
            <if test="grantnum != null">#{grantnum},</if>
            <if test="ownnum != null">#{ownnum},</if>
            <if test="stocknum != null">#{stocknum},</if>
            <if test="intime != null">#{intime},</if>
            <if test="innum != null">#{innum},</if>
            <if test="peopleId != null">#{peopleId},</if>
            <if test="outtime != null">#{outtime},</if>
            <if test="outnum != null">#{outnum},</if>
            <if test="peopleId1 != null">#{peopleId1},</if>
            <if test="quality != null">#{quality},</if>
            <if test="info != null">#{info},</if>
            <if test="unitId1 != null">#{unitId1},</if>
            <if test="createtime != null">#{createtime},</if>
            <if test="adminId != null">#{adminId},</if>
            <if test="unitName != null">#{unitName},</if>
            <if test="peopleName != null">#{peopleName},</if>
            <if test="people1Name != null">#{people1Name},</if>
            <if test="unitName1 != null">#{unitName1},</if>
        </trim>
    </insert>

    <update id="updateBdglEquip" parameterType="BdglEquip">
        update bdgl_equip
        <trim prefix="SET" suffixOverrides=",">
            <if test="type != null">type = #{type},</if>
            <if test="title != null">title = #{title},</if>
            <if test="unitId != null">unit_id = #{unitId},</if>
            <if test="granttime != null">granttime = #{granttime},</if>
            <if test="grantnum != null">grantnum = #{grantnum},</if>
            <if test="ownnum != null">ownnum = #{ownnum},</if>
            <if test="stocknum != null">stocknum = #{stocknum},</if>
            <if test="intime != null">intime = #{intime},</if>
            <if test="innum != null">innum = #{innum},</if>
            <if test="peopleId != null">people_id = #{peopleId},</if>
            <if test="outtime != null">outtime = #{outtime},</if>
            <if test="outnum != null">outnum = #{outnum},</if>
            <if test="peopleId1 != null">people_id1 = #{peopleId1},</if>
            <if test="quality != null">quality = #{quality},</if>
            <if test="info != null">info = #{info},</if>
            <if test="unitId1 != null">unit_id1 = #{unitId1},</if>
            <if test="createtime != null">createtime = #{createtime},</if>
            <if test="adminId != null">admin_id = #{adminId},</if>
            <if test="unitName != null">unit_name = #{unitName},</if>
            <if test="peopleName != null">people_name = #{peopleName},</if>
            <if test="people1Name != null">people1_name = #{people1Name},</if>
            <if test="unitName1 != null">unit_name1 = #{unitName1},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBdglEquipById" parameterType="Long">
        delete from bdgl_equip where id = #{id}
    </delete>

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