<?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.zehong.system.mapper.TSafeEquipmentStandingBookMapper">

    <resultMap type="TSafeEquipmentStandingBook" id="TSafeEquipmentStandingBookResult">
        <result property="safeEquipmentId"    column="safe_equipment_id"    />
        <result property="userName"    column="user_name"    />
        <result property="userNo"    column="user_no"    />
        <result property="userAddress"    column="user_address"    />
        <result property="idCard"    column="id_card"    />
        <result property="linkMobile"    column="link_mobile"    />
        <result property="equipmentType"    column="equipment_type"    />
        <result property="installTime"    column="install_time"    />
        <result property="brandName"    column="brand_name"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="remarks"    column="remarks"    />
        <result property="userType"    column="user_type"    />
        <result property="enterpriseId"    column="enterprise_id"    />
        <result property="enterpriseName"    column="enterpriseName"    />
    </resultMap>

    <sql id="selectTSafeEquipmentStandingBookVo">
        select book.safe_equipment_id, book.user_name, book.user_no, book.user_address, book.id_card, book.link_mobile,
               book.equipment_type, book.install_time, book.brand_name, book.create_by, book.create_time, book.update_by,
               book.update_time, book.is_del, book.remarks,book.user_type,book.enterprise_id,info.enterprise_name as enterpriseName
        from t_safe_equipment_standing_book book left join t_enterprise_info info on book.enterprise_id = info.enterprise_id
    </sql>

    <select id="selectTSafeEquipmentStandingBookList" parameterType="TSafeEquipmentStandingBookForm" resultMap="TSafeEquipmentStandingBookResult">
        <include refid="selectTSafeEquipmentStandingBookVo"/>
        <where>  book.is_del = '0'
            <if test="userName != null  and userName != ''"> and book.user_name like concat('%', #{userName}, '%')</if>
            <if test="linkMobile != null  and linkMobile != ''"> and book.link_mobile like concat('%', #{linkMobile}, '%')</if>
            <if test="equipmentType != null  and equipmentType != ''"> and book.equipment_type like concat('%', #{equipmentType}, '%')</if>
            <if test="installTimeStart != null "> and book.install_time &gt;= #{installTimeStart}</if>
            <if test="installTimeEnd != null "> and book.install_time &lt;= #{installTimeEnd}</if>
            <if test="enterpriseId != null and enterpriseId != '' "> and book.enterprise_id = #{enterpriseId}</if>
        </where>
        order by book.create_time desc
    </select>

    <select id="selectTSafeEquipmentStandingBookById" parameterType="Long" resultMap="TSafeEquipmentStandingBookResult">
        <include refid="selectTSafeEquipmentStandingBookVo"/>
        where safe_equipment_id = #{safeEquipmentId}
    </select>

    <insert id="insertTSafeEquipmentStandingBook" parameterType="TSafeEquipmentStandingBook" useGeneratedKeys="true" keyProperty="safeEquipmentId">
        insert into t_safe_equipment_standing_book
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userName != null">user_name,</if>
            <if test="userNo != null">user_no,</if>
            <if test="userAddress != null">user_address,</if>
            <if test="idCard != null">id_card,</if>
            <if test="linkMobile != null">link_mobile,</if>
            <if test="installTime != null">install_time,</if>
            <if test="equipmentType != null">equipment_type,</if>
            <if test="brandName != null">brand_name,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="remarks != null">remarks,</if>
            <if test="userType != null">user_type,</if>
            <if test="enterpriseId != null">enterprise_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userName != null">#{userName},</if>
            <if test="userNo != null">#{userNo},</if>
            <if test="userAddress != null">#{userAddress},</if>
            <if test="idCard != null">#{idCard},</if>
            <if test="linkMobile != null">#{linkMobile},</if>
            <if test="installTime != null">#{installTime},</if>
            <if test="equipmentType != null">#{equipmentType},</if>
            <if test="brandName != null">#{brandName},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="userType != null">#{userType},</if>
            <if test="enterpriseId != null">#{enterpriseId},</if>
         </trim>
    </insert>

    <update id="updateTSafeEquipmentStandingBook" parameterType="TSafeEquipmentStandingBook">
        update t_safe_equipment_standing_book
        <trim prefix="SET" suffixOverrides=",">
            <if test="userName != null">user_name = #{userName},</if>
            <if test="userNo != null">user_no = #{userNo},</if>
            <if test="userAddress != null">user_address = #{userAddress},</if>
            <if test="idCard != null">id_card = #{idCard},</if>
            <if test="linkMobile != null">link_mobile = #{linkMobile},</if>
            <if test="installTime != null">install_time = #{installTime},</if>
            <if test="equipmentType != null">equipment_type = #{equipmentType},</if>
            <if test="brandName != null">brand_name = #{brandName},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="enterpriseId != null">enterprise_id = #{enterpriseId},</if>
        </trim>
        where safe_equipment_id = #{safeEquipmentId}
    </update>

    <delete id="deleteTSafeEquipmentStandingBookById" parameterType="Long">
        delete from t_safe_equipment_standing_book where safe_equipment_id = #{safeEquipmentId}
    </delete>

    <delete id="deleteTSafeEquipmentStandingBookByIds" parameterType="String">
        delete from t_safe_equipment_standing_book where safe_equipment_id in
        <foreach item="safeEquipmentId" collection="array" open="(" separator="," close=")">
            #{safeEquipmentId}
        </foreach>
    </delete>
    <select id="selectCountByYears" resultType="java.util.HashMap">
        SELECT d.`years`, COUNT(w.`safe_equipment_id`) AS `count`
            FROM(
              SELECT DATE_FORMAT((CURDATE()), '%Y') AS `years`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 1 YEAR), '%Y') AS `years`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 2 YEAR), '%Y') AS `years`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 3 YEAR), '%Y') AS `years`
                UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 4 YEAR), '%Y') AS `years`
                )d
                LEFT JOIN t_safe_equipment_standing_book w
                ON DATE_FORMAT(w.create_time, '%Y')=d.`years` AND w.`is_del` =0
                GROUP BY years
    </select>
    <select id="selectCountByUserType" resultType="java.util.HashMap">
        SELECT  IFNULL(SUM(IF(user_type=1,1,0)),0) AS cheng,
            IFNULL(SUM(IF(user_type=2,1,0)),0) AS zhen,
            IFNULL(SUM(IF(user_type=3,1,0)),0) AS danwei
        FROM t_safe_equipment_standing_book
        WHERE is_del = 0
    </select>
</mapper>
