<?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.tofly.gpsboot.mapper.plan.PlanMapper">

    <resultMap id="planMap" type="com.tofly.pojo.plan.Plan">
        <id property="id" column="ID"/>
        <result property="planType" column="PLAN_TYPE"/>
        <result property="mouthId" column="MOUTH_ID"/>
        <result property="periodId" column="PERIOD_ID"/>
        <result property="planBegindate" column="PLAN_BEGINDATE"/>
        <result property="planEnddate" column="PLAN_ENDDATE"/>
        <result property="regionId" column="REGION_ID"/>
        <result property="regionName" column="REGION_NAME"/>
        <result property="inspectType" column="INSPECT_TYPE"/>
        <result property="inspectContents" column="INSPECT_CONTENTS"/>
        <result property="isread" column="ISREAD"/>
        <result property="isover" column="ISOVER"/>
        <result property="planState" column="PLAN_STATE"/>
        <result property="intervalDay" column="INTERVAL_DAY"/>
        <result property="planPercent" column="PLAN_PERCENT"/>
        <result property="percent" column="PERCENT"/>
        <result property="totalLength" column="TOTAL_LENGTH"/>
        <result property="totalCount" column="TOTAL_COUNT"/>
        <result property="description" column="DESCRIPTION"/>
        <result property="lastTime" column="LAST_TIME"/>
        <result property="lastUser" column="LAST_USER"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="isDispatching" column="IS_DISPATCHING"/>
        <result property="timeoutRemindFlag" column="TIMEOUT_REMIND_FLAG"/>
        <association property="createUserName" column="CREATE_USER" select="com.tofly.gpsboot.mapper.plan.HydrantrecordMapper.getRealName"/>
        <association property="departmentName" column="CREATE_USER" select="getDeptName"/>
        <association property="departmentId" column="CREATE_USER" select="getDeptId"/>
    </resultMap>

    <select id="getDeptName" resultType="string">
        select NAME as deptName
        from TF_SMPN_DEPARTMENT_X
        where id =
              (select DEPARTMENT_ID
               from TF_SMPN_USER_X
               where id = #{createUser})
    </select>
    <select id="getDeptId" resultType="java.lang.Long">
       select DEPARTMENT_ID
               from TF_SMPN_USER_X
               where id = #{createUser}
    </select>

    <select id="getPlanPage" resultMap="planMap" databaseId="oracle">
        SELECT T1.ID,T1.PLAN_TYPE,T1.MOUTH_ID,T1.PERIOD_ID,T1.PLAN_BEGINDATE,T1.PLAN_ENDDATE,T1.REGION_ID,T1.REGION_NAME,T1.INSPECT_TYPE,T1.INSPECT_CONTENTS,
        T1.ISREAD,T1.PLAN_STATE,T1.INTERVAL_DAY,T1.PLAN_PERCENT,T1.PERCENT,T1.TOTAL_LENGTH,T1.TOTAL_COUNT,T1.DESCRIPTION,T1.LAST_TIME,T1.LAST_USER,T1.CREATE_USER,
        T1.CREATE_TIME,T1.IS_DISPATCHING,T1.TIMEOUT_REMIND_FLAG
        FROM TF_YWPN_PLAN_W t1
        where 1=1
        <if test="dto.userIdList != null and dto.userIdList.size>0  ">
        and t1.CREATE_USER in
            <foreach collection="dto.userIdList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.isDispatching != null and dto.isDispatching != '' ">
            and t1.IS_DISPATCHING = #{dto.isDispatching}
        </if>
        <if test="dto.planState != null and dto.planState != '' ">
            and t1.PLAN_STATE in
            <foreach collection="dto.planState.split(',')" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.inspectType != null and dto.inspectType != '' ">
            and t1.INSPECT_TYPE in
            <foreach collection="dto.inspectType.split(',')" item="item" open="(" separator="," close=")" >
               #{item}
            </foreach>
        </if>
        <if test="dto.planBegindate != null and dto.planEnddate != null ">
            and (t1.PLAN_BEGINDATE >= #{dto.planBegindate}
            and t1.PLAN_ENDDATE &lt;= #{dto.planEnddate}
            or
            t1.PLAN_BEGINDATE &lt;= #{dto.planBegindate}
            and t1.PLAN_ENDDATE >= #{dto.planEnddate})
        </if>
        <if test="dto.createUser != null  ">
            and t1.CREATE_USER = #{createUser}
        </if>
        <if test="dto.inspectUsers != null and dto.inspectUsers != '' ">
            and t1.id in (select plan_id from TF_YWPN_PLAN_INSPECT_USER_W where user_Id in
            <foreach collection="dto.inspectUsers.split(',')" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>)
        </if>
        <if test="dto.mouthId != null and dto.mouthId != null ">
            and t1.MOUTH_ID = #{dto.mouthId}
        </if>
        <if test="dto.selType != null and dto.selType != '' ">
            <if test='dto.selType == "0"  '>
                and t1.ID in (select plan_Id from TF_YWPN_PLANSTOP_W
                where 1=1
                <if test="dto.selTypeAuditResult != null and dto.selTypeAuditResult != '' ">
                    and AUDIT_RESULT in
                    <foreach collection="dto.selTypeAuditResult.split(',')" item="item" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="dto.selTypeAuditUserId != null and dto.selTypeAuditUserId != '' ">
                    and AUDIT_USER = #{dto.selTypeAuditUserId}
                </if>
                )
            </if>
            <if test='dto.selType == "1"  '>
                and t1.ID in (select plan_Id from TF_YWPN_PLANCANCLE_W
                where 1=1
                <if test="dto.selTypeAuditResult != null and dto.selTypeAuditResult != '' ">
                    and AUDIT_RESULT in
                    <foreach collection="dto.selTypeAuditResult.split(',')" item="item" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="dto.selTypeAuditUserId != null and dto.selTypeAuditUserId != '' ">
                    and AUDIT_USER = #{dto.selTypeAuditUserId}
                </if>
                )
            </if>
        </if>
        ORDER BY T1.CREATE_TIME desc
    </select>

    <select id="getUserPlanNum" resultType="java.lang.Long">
        select  count(DISTINCT(t2.id))
        from  TF_YWPN_PLAN_W t1 left join TF_YWPN_PLANCHILD_W t2
        on t1.id = t2.plan_id
        where t1.plan_Begindate between #{startDate} and  #{endDate}
        and t1.plan_Enddate between #{startDate} and  #{endDate}
        and t2.resp_User = #{userId}
    </select>

    <select id="getReportPlanYear" resultType="com.tofly.pojo.plan.vo.PlanYearReportVo">
        select total_length,
               total_count,
               total_finish,
               (SELECT count(1) --隐患总数
                FROM TF_YWPN_TROUBLE_W t
                where to_char(t.submit_time, 'yyyy') =#{year}) as total_Trouble,
               (select count(distinct user_id) as total_user_count
                from TF_YWPN_PLAN_INSPECT_USER_W planuser
                         inner join TF_YWPN_PLAN_W plan
                                    on plan.id = planuser.plan_id
                where to_char(plan.plan_begindate, 'yyyy') = #{year}
                  and plan.inspect_type = '11') as total_user_count, --巡查人员数量
               (select count(1) as total_gd_count
                from TF_YWPN_GDINFO_W gd
                where to_char(gd.djsj, 'yyyy') = #{year}) as total_gd_count,
               (select count(1) as total_gd_count
                from TF_YWPN_GDINFO_W gd
                where to_char(gd.djsj, 'yyyy') = #{year} and finish_time is not null) as finish_gd_count
        from (select sum(plan.total_length) total_length --巡检总公里数量
                      ,
                     count(1) as total_count, --巡查次数
                     sum(case when plan.plan_state='4' then 1 else 0 end) as total_finish
              from TF_YWPN_PLAN_W plan
              where to_char(plan.plan_begindate, 'yyyy') = #{year}
                and plan.inspect_type = '11'
             ) tt

    </select>

    <select id="getReportPlanMonth" resultType="com.tofly.pojo.plan.vo.PlanYearReportVo">
        select total_length,
               total_count,
               total_finish,
               (SELECT count(1) --隐患总数
                FROM TF_YWPN_TROUBLE_W t
                where to_char(t.submit_time, 'yyyyMM') =to_char(sysdate,'yyyyMM') ) as total_Trouble,
               (select count(distinct user_id) as total_user_count
                from TF_YWPN_PLAN_INSPECT_USER_W planuser
                         inner join TF_YWPN_PLAN_W plan
                                    on plan.id = planuser.plan_id
                where to_char(plan.plan_begindate, 'yyyyMM') = to_char(sysdate,'yyyyMM')
                  and plan.inspect_type = '11') as total_user_count, --巡查人员数量
               (select count(1) as total_gd_count
                from TF_YWPN_GDINFO_W gd
                where to_char(gd.djsj, 'yyyyMM') = to_char(sysdate,'yyyyMM')) as total_gd_count,
               (select count(1) as total_gd_count
                from TF_YWPN_GDINFO_W gd
                where to_char(gd.djsj, 'yyyyMM') = to_char(sysdate,'yyyyMM') and finish_time is not null ) as finish_gd_count
        from (select sum(plan.total_length) total_length --巡检总公里数量
                      ,
                     count(1) as total_count, --巡查次数
                     sum(case when plan.plan_state='4' then 1 else 0 end) as total_finish
              from TF_YWPN_PLAN_W plan
              where to_char(plan.plan_begindate, 'yyyyMM') = to_char(sysdate,'yyyyMM')
                and plan.inspect_type = '11'
             ) tt
    </select>

    <select id="getUserAll" resultType="com.tofly.pojo.User">
        SELECT ID, REAL_NAME, DEPARTMENT_ID
        FROM TF_SMPN_USER_X
        WHERE STATUS_FLAG = '1'
    </select>
</mapper>
