<?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.TInspectReportMapper">
    
    <resultMap type="TInspectReport" id="TInspectReportResult">
        <result property="reportId"    column="report_id"    />
        <result property="annual"    column="annual"    />
        <result property="securityCodeIn"    column="security_code_in"    />
        <result property="securityCode"    column="security_code"    />
        <result property="userNo"    column="user_no"    />
        <result property="userName"    column="user_name"    />
        <result property="indoorTime"    column="indoor_time"    />
        <result property="indoorStatus"    column="indoor_status"    />
        <result property="region"    column="region"    />
        <result property="village"    column="village"    />
        <result property="houseNumber"    column="house_number"    />
        <result property="phoneNumber"    column="phone_number"    />
        <result property="deptCode"    column="dept_code"    />
        <result property="companyInfoId"    column="company_info_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="userOwnId"    column="user_own_id"    />
    </resultMap>

    <sql id="selectTInspectReportVo">
        select report_id, annual, security_code_in, security_code, user_no, user_name, indoor_time, indoor_status, region, village,
         (SELECT f_village_name FROM t_user_manage_village WHERE  f_village_id = village) AS villageName,
         house_number, phone_number, dept_code, company_info_id, create_time, user_own_id from t_inspect_report r
    </sql>

    <select id="selectTInspectReportList" parameterType="TInspectReport" resultMap="TInspectReportResult">
        <include refid="selectTInspectReportVo"/>
        <where>  
            <if test="annual != null "> and annual = #{annual}</if>
            <if test="companyInfoId != null "> and company_info_id = #{companyInfoId}</if>
            <if test="village != null "> and village = #{village}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="indoorStatus != null  and indoorStatus != ''"> and indoor_status = #{indoorStatus}</if>
        </where>
    </select>

    <select id="selectTInspectSummaryList" parameterType="TInspectReport" resultMap="TInspectReportResult">
        SELECT r.annual, e.`enterprise_name` AS enterpriseName,r.security_code ,r.company_info_id,r.village,
        v.f_village_name AS villageName,
        SUM(IF(indoor_status = 'DONE', 1, 0)) AS doneNum,
        SUM(IF(indoor_status = 'REJ', 1, 0)) AS rejNum,
        SUM(IF(indoor_status = 'NONE', 1, 0)) AS noneNum,
        MAX(indoor_time) AS lastTime
        FROM t_inspect_report r
        LEFT JOIN t_enterprise_info e ON e.`enterprise_id` = r.`company_info_id`
        LEFT JOIN t_user_manage_village v ON v.f_village_id = r.`village`
        <where>
            <if test="annual != null "> and r.annual = #{annual}</if>
            <if test="companyInfoId != null"> and r.company_info_id = #{companyInfoId}</if>
        </where>
        GROUP BY r.annual ,r.company_info_id , r.village
    </select>
    
    <select id="selectTInspectReportById" parameterType="Long" resultMap="TInspectReportResult">
        <include refid="selectTInspectReportVo"/>
        where report_id = #{reportId}
    </select>
        
    <insert id="insertTInspectReport" parameterType="TInspectReport" useGeneratedKeys="true" keyProperty="reportId">
        insert into t_inspect_report
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="annual != null">annual,</if>
            <if test="securityCodeIn != null">security_code_in,</if>
            <if test="securityCode != null">security_code,</if>
            <if test="userNo != null">user_no,</if>
            <if test="userName != null">user_name,</if>
            <if test="indoorTime != null">indoor_time,</if>
            <if test="indoorStatus != null">indoor_status,</if>
            <if test="region != null">region,</if>
            <if test="village != null">village,</if>
            <if test="houseNumber != null">house_number,</if>
            <if test="phoneNumber != null">phone_number,</if>
            <if test="deptCode != null">dept_code,</if>
            <if test="companyInfoId != null">company_info_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="userOwnId != null">user_own_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="annual != null">#{annual},</if>
            <if test="securityCodeIn != null">#{securityCodeIn},</if>
            <if test="securityCode != null">#{securityCode},</if>
            <if test="userNo != null">#{userNo},</if>
            <if test="userName != null">#{userName},</if>
            <if test="indoorTime != null">#{indoorTime},</if>
            <if test="indoorStatus != null">#{indoorStatus},</if>
            <if test="region != null">#{region},</if>
            <if test="village != null">#{village},</if>
            <if test="houseNumber != null">#{houseNumber},</if>
            <if test="phoneNumber != null">#{phoneNumber},</if>
            <if test="deptCode != null">#{deptCode},</if>
            <if test="companyInfoId != null">#{companyInfoId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="userOwnId != null">#{userOwnId},</if>
         </trim>
    </insert>

    <insert id="inserts" parameterType="TInspectReport">
    insert into t_inspect_report
        <trim prefix="(" suffix=")" suffixOverrides=",">
            annual,
            security_code_in,
            security_code,
            user_no,
            user_name,
            indoor_time,
            indoor_status,
            village,
            house_number,
            phone_number,
            company_info_id,
            user_own_id
        </trim>
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.annual},
            #{item.securityCodeIn},
            #{item.securityCode},
            #{item.userNo},
            #{item.userName},
            #{item.indoorTime},
            #{item.indoorStatus},
            #{item.village},
            #{item.houseNumber},
            #{item.phoneNumber},
            #{item.companyInfoId},
            #{item.userOwnId})
        </foreach>
    </insert>

    <update id="updateTInspectReport" parameterType="TInspectReport">
        update t_inspect_report
        <trim prefix="SET" suffixOverrides=",">
            <if test="annual != null">annual = #{annual},</if>
            <if test="securityCodeIn != null">security_code_in = #{securityCodeIn},</if>
            <if test="securityCode != null">security_code = #{securityCode},</if>
            <if test="userNo != null">user_no = #{userNo},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="indoorTime != null">indoor_time = #{indoorTime},</if>
            <if test="indoorStatus != null">indoor_status = #{indoorStatus},</if>
            <if test="region != null">region = #{region},</if>
            <if test="village != null">village = #{village},</if>
            <if test="houseNumber != null">house_number = #{houseNumber},</if>
            <if test="phoneNumber != null">phone_number = #{phoneNumber},</if>
            <if test="deptCode != null">dept_code = #{deptCode},</if>
            <if test="companyInfoId != null">company_info_id = #{companyInfoId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="userOwnId != null">user_own_id = #{userOwnId},</if>
        </trim>
        where report_id = #{reportId}
    </update>

    <delete id="deleteTInspectReportById" parameterType="Long">
        delete from t_inspect_report where report_id = #{reportId}
    </delete>

    <delete id="deleteTInspectReportByIds" parameterType="String">
        delete from t_inspect_report where report_id in 
        <foreach item="reportId" collection="array" open="(" separator="," close=")">
            #{reportId}
        </foreach>
    </delete>

    <select id="selectIndoorStatistic" resultType="hashmap" parameterType="TInspectReport">
      SELECT
        IFNULL(SUM(IF(u.user_type=1,1,0)),0) AS jmnum,
        IFNULL(SUM(IF(u.user_type=2,1,0)),0) AS synum,
        IFNULL(SUM(IF(u.user_type=3,1,0)),0) AS gynum,
        IFNULL(SUM(IF(u.user_type=1 AND t.indoor_status ='DONE',1,0)),0) AS jmdone,
        IFNULL(SUM(IF(u.user_type=1 AND t.indoor_status ='REJ',1,0)),0) AS jmrej,
        IFNULL(SUM(IF(u.user_type=1 AND t.indoor_status ='NONE',1,0)),0) AS jmnone,

        IFNULL(SUM(IF(u.user_type=2 AND t.indoor_status ='DONE',1,0)),0) AS sydone,
        IFNULL(SUM(IF(u.user_type=2 AND t.indoor_status ='REJ',1,0)),0) AS syrej,
        IFNULL(SUM(IF(u.user_type=2 AND t.indoor_status ='NONE',1,0)),0) AS synone,

        IFNULL(SUM(IF(u.user_type=3 AND t.indoor_status ='DONE',1,0)),0) AS gydone,
        IFNULL(SUM(IF(u.user_type=3 AND t.indoor_status ='REJ',1,0)),0) AS gyrej,
        IFNULL(SUM(IF(u.user_type=3 AND t.indoor_status ='NONE',1,0)),0) AS gynone

        FROM t_inspect_report t
        JOIN (
        SELECT `user_no`, MAX(indoor_time) AS max_time
        FROM t_inspect_report
        WHERE `annual` = #{annual}
        <if test="companyInfoId != null and companyInfoId != ''">AND company_info_id = #{companyInfoId}</if>
        GROUP BY `user_no`
        ) AS sub_t
        ON t.`user_no` = sub_t.`user_no`
        AND t.indoor_time = sub_t.max_time
        LEFT JOIN t_detector_user u ON u.user_id = t.user_no
    </select>
    <select id="selectIndoorSummery" resultType="hashmap" parameterType="TInspectReport">
        SELECT
         #{annual} as yearNum,
        (select count(*) from t_detector_user where create_time &lt; concat(#{annual},"-12-31 23:59:59")) AS allNum,
        IFNULL(SUM(IF(t.indoor_status ='DONE',1,0)),0) AS doneNum,
        IFNULL(SUM(IF(t.indoor_status ='REJ',1,0)),0) AS rejNum,
        IFNULL(SUM(IF(t.indoor_status ='NONE',1,0)),0) AS noneNum
        FROM t_inspect_report t
        JOIN (
        SELECT `user_no`, MAX(indoor_time) AS max_time
        FROM t_inspect_report
        WHERE `annual` = #{annual}
        GROUP BY `user_no`
        ) AS sub_t
        ON t.`user_no` = sub_t.`user_no`
        AND t.indoor_time = sub_t.max_time
        LEFT JOIN t_detector_user u ON u.user_id = t.user_no
    </select>
</mapper>