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

    <resultMap type="TStaningBook" id="TStaningBookResult">
        <result property="bookId"    column="book_id"    />
        <result property="troubleName"    column="trouble_name"    />
        <result property="troubleType"    column="trouble_type"    />
        <result property="troubleLevel"    column="trouble_level"    />
        <result property="measures"    column="measures"    />
        <result property="troubleHarm"    column="trouble_harm"    />
        <result property="escalation"    column="escalation"    />
        <result property="escalationName"    column="escalationName"    />
        <result property="findTime"    column="find_time"    />
        <result property="picture"    column="picture"    />
        <result property="enclosure"    column="enclosure"    />
        <result property="proposal"    column="proposal"    />
        <result property="rectificationTerm"    column="rectification_term"    />
        <result property="investmentCost"    column="investment_cost"    />
        <result property="investmentMaterial"    column="investment_material"    />
        <result property="rectification"    column="rectification"    />
        <result property="rectificationName"    column="rectificationName"    />
        <result property="rectificationPictrue"    column="rectification_pictrue"    />
        <result property="rectificationText"    column="rectification_text"    />
        <result property="rectificationTime"    column="rectification_time"    />
        <result property="remarks"    column="remarks"    />
        <result property="finishDay"    column="finish_day"    />
        <result property="inspectPictrue"    column="inspect_pictrue"    />
        <result property="inspectTime"    column="inspect_time"    />
        <result property="inspectResult"    column="inspect_result"    />
        <result property="personLiable"    column="person_liable"    />
        <result property="personLiableName"    column="personLiableName"    />
        <result property="state"    column="state"    />
        <result property="deptId"    column="dept_id"    />
        <result property="workId"    column="work_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="isDel"    column="is_del"    />
        <result property="deptName"    column="deptName"    />
    </resultMap>

    <sql id="selectTStaningBookVo">
        select book_id, trouble_name, trouble_type, trouble_level, measures, trouble_harm, escalation, find_time, picture, enclosure, proposal,rectification_term, investment_cost, investment_material, rectification, rectification_pictrue, rectification_text, rectification_time, remarks, finish_day, inspect_pictrue, inspect_time, inspect_result, person_liable, state, dept_id, work_id, create_time, create_by, is_del from t_staning_book
    </sql>

    <select id="selectTStaningBookList" parameterType="TStaningBook" resultMap="TStaningBookResult">
        SELECT b.*,s1.staff_name AS escalationName,
        s2.`staff_name` AS rectificationName,
        s3.`staff_name` AS personLiableName ,
        d.`dept_name` AS deptName
        FROM t_staning_book b
        LEFT JOIN sys_user s1 ON b.`escalation` = s1.`user_id`
        LEFT JOIN sys_user s2 ON b.`rectification` = s2.`user_id`
        LEFT JOIN sys_user s3 ON b.`person_liable` = s3.`user_id`
        LEFT JOIN sys_dept d ON d.`dept_id` = b.`dept_id`
        <where>

            <foreach collection="deptList" item="deptId" open="AND b.dept_id in (" separator="," close=")">
                #{deptId}
            </foreach>
            <if test="troubleName != null  and troubleName != ''"> and b.trouble_name like concat('%', #{troubleName}, '%')</if>
            <if test="findTime != null "> and b.find_time &gt; #{findTime}</if>
            <if test="rectificationTime != null "> and b.find_time &lt; #{rectificationTime}</if>
            <if test="state != null "> and b.state = #{state}</if>
        </where>
    </select>

    <select id="selectTStaningBookById" parameterType="Long" resultMap="TStaningBookResult">
        <include refid="selectTStaningBookVo"/>
        where book_id = #{bookId}
    </select>

    <insert id="insertTStaningBook" parameterType="TStaningBook" useGeneratedKeys="true" keyProperty="bookId">
        insert into t_staning_book
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="troubleName != null">trouble_name,</if>
            <if test="troubleType != null">trouble_type,</if>
            <if test="troubleLevel != null">trouble_level,</if>
            <if test="measures != null">measures,</if>
            <if test="troubleHarm != null">trouble_harm,</if>
            <if test="escalation != null">escalation,</if>
            <if test="findTime != null">find_time,</if>
            <if test="picture != null">picture,</if>
            <if test="enclosure != null">enclosure,</if>
            <if test="proposal != null">proposal,</if>
            <if test="rectificationTerm != null">rectification_term,</if>
            <if test="investmentCost != null">investment_cost,</if>
            <if test="investmentMaterial != null">investment_material,</if>
            <if test="rectification != null">rectification,</if>
            <if test="rectificationPictrue != null">rectification_pictrue,</if>
            <if test="rectificationText != null">rectification_text,</if>
            <if test="rectificationTime != null">rectification_time,</if>
            <if test="remarks != null">remarks,</if>
            <if test="finishDay != null">finish_day,</if>
            <if test="inspectPictrue != null">inspect_pictrue,</if>
            <if test="inspectTime != null">inspect_time,</if>
            <if test="inspectResult != null">inspect_result,</if>
            <if test="personLiable != null">person_liable,</if>
            <if test="state != null">state,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="workId != null">work_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="isDel != null">is_del,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="troubleName != null">#{troubleName},</if>
            <if test="troubleType != null">#{troubleType},</if>
            <if test="troubleLevel != null">#{troubleLevel},</if>
            <if test="measures != null">#{measures},</if>
            <if test="troubleHarm != null">#{troubleHarm},</if>
            <if test="escalation != null">#{escalation},</if>
            <if test="findTime != null">#{findTime},</if>
            <if test="picture != null">#{picture},</if>
            <if test="enclosure != null">#{enclosure},</if>
            <if test="proposal != null">#{proposal},</if>
            <if test="rectificationTerm != null">#{rectificationTerm},</if>
            <if test="investmentCost != null">#{investmentCost},</if>
            <if test="investmentMaterial != null">#{investmentMaterial},</if>
            <if test="rectification != null">#{rectification},</if>
            <if test="rectificationPictrue != null">#{rectificationPictrue},</if>
            <if test="rectificationText != null">#{rectificationText},</if>
            <if test="rectificationTime != null">#{rectificationTime},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="finishDay != null">#{finishDay},</if>
            <if test="inspectPictrue != null">#{inspectPictrue},</if>
            <if test="inspectTime != null">#{inspectTime},</if>
            <if test="inspectResult != null">#{inspectResult},</if>
            <if test="personLiable != null">#{personLiable},</if>
            <if test="state != null">#{state},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="workId != null">#{workId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="isDel != null">#{isDel},</if>
         </trim>
    </insert>

    <update id="updateTStaningBook" parameterType="TStaningBook">
        update t_staning_book
        <trim prefix="SET" suffixOverrides=",">
            <if test="troubleName != null">trouble_name = #{troubleName},</if>
            <if test="troubleType != null">trouble_type = #{troubleType},</if>
            <if test="troubleLevel != null">trouble_level = #{troubleLevel},</if>
            <if test="measures != null">measures = #{measures},</if>
            <if test="troubleHarm != null">trouble_harm = #{troubleHarm},</if>
            <if test="escalation != null">escalation = #{escalation},</if>
            <if test="findTime != null">find_time = #{findTime},</if>
            <if test="picture != null">picture = #{picture},</if>
            <if test="enclosure != null">enclosure = #{enclosure},</if>
            <if test="proposal != null">proposal = #{proposal},</if>
            <if test="rectificationTerm != null">rectification_term = #{rectificationTerm},</if>
            <if test="investmentCost != null">investment_cost = #{investmentCost},</if>
            <if test="investmentMaterial != null">investment_material = #{investmentMaterial},</if>
            <if test="rectification != null">rectification = #{rectification},</if>
            <if test="rectificationPictrue != null">rectification_pictrue = #{rectificationPictrue},</if>
            <if test="rectificationText != null">rectification_text = #{rectificationText},</if>
            <if test="rectificationTime != null">rectification_time = #{rectificationTime},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="finishDay != null">finish_day = #{finishDay},</if>
            <if test="inspectPictrue != null">inspect_pictrue = #{inspectPictrue},</if>
            <if test="inspectTime != null">inspect_time = #{inspectTime},</if>
            <if test="inspectResult != null">inspect_result = #{inspectResult},</if>
            <if test="personLiable != null">person_liable = #{personLiable},</if>
            <if test="state != null">state = #{state},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="workId != null">work_id = #{workId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where book_id = #{bookId}
    </update>

    <delete id="deleteTStaningBookById" parameterType="Long">
        delete from t_staning_book where book_id = #{bookId}
    </delete>

    <delete id="deleteTStaningBookByIds" parameterType="String">
        delete from t_staning_book where book_id in
        <foreach item="bookId" collection="array" open="(" separator="," close=")">
            #{bookId}
        </foreach>
    </delete>
    <select id="statics" parameterType="String" resultType="Map">
        select count(1) faxian,sum(case state when '2' then 1 else 0 end )as zhenggai from t_staning_book where is_del=0
        <if test="type=='day'">
            and DATE_SUB(now(), INTERVAL 1 DAY) &lt; date(find_time)
        </if>
        <if test="type=='month'">
            and DATE_SUB(now(), INTERVAL 30 DAY) &lt; date(find_time)
        </if>
        <if test="type=='year'">
            and DATE_SUB(now(), INTERVAL 365 DAY) &lt; date(find_time)
        </if>
    </select>

    <select id="hiddenBarStat" resultType="HiddenStatVO">
        SELECT
            IFNULL(DATE_FORMAT(find_time,'%Y-%m'),0) AS month,
            IFNULL(count(1),0) hiddenCount,
            IFNULL(sum(CASE state WHEN '2' THEN 1 ELSE 0 END),0) AS rectifiedCount
        FROM
            t_staning_book
        WHERE
            is_del = 0
        <if test="beginTime != null and endTime != null">
            AND find_time BETWEEN #{ beginTime } AND #{ endTime }
        </if>
        <if test="troubleLevel != null and troubleLevel != ''">
            AND trouble_level = #{troubleLevel}
        </if>
        GROUP BY DATE_FORMAT(find_time,'%Y-%m')
    </select>

    <select id="hiddenPieStat" resultType="java.util.Map">
        SELECT
          IFNULL(sum(CASE state WHEN '2' THEN 1 ELSE 0 END),0) AS rectifiedCount,
          IFNULL(sum(IF(IFNULL(state,'-1') != '2',1,0)),0) AS stayRectifiedCount
        FROM
            t_staning_book
        WHERE
            is_del = 0
        <if test="beginTime != null and endTime != null">
            AND find_time BETWEEN #{ beginTime } AND #{ endTime }
        </if>
        <if test="troubleLevel != null and troubleLevel != ''">
            AND trouble_level = #{troubleLevel}
        </if>
    </select>
</mapper>