<?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.xunmei.core.resumption.mapper.ResumptionRecordMapper">
    <select id="selectListByCondition" resultType="com.xunmei.core.resumption.vo.resumptionRecord.ResumptionRecordPageVo">
        select r.id as resumptionId,r.exception_count as abnormalDataNums,
       <!-- p.plan_name as time,
        p.plan_name as planName,-->
        r.plan_id as planId,
        r.ymd_date as ymdDate,
        r.status as status,
        r.role_id as roleId,
        <!-- o.name as orgName ,
        ro.name as roleName,-->
        r.submit_time as submitTime,
        <!--u.name as submitter,-->
        r.modified_name as submitter,
        r.plan_start_time as executeTime,
        r.plan_start_time as planStartTime,
        r.plan_end_time as planEndTime,
        r.org_id as orgId
        from core_resumption r
        <!--inner join t_app_plan p on r.plan_id = p.id
        inner join sys_org o on r.org_id = o.id and o.deleted = 0
        inner join t_role ro on ro.id = r.role_id
        left join t_user u on u.id=r.modified_id-->
        <where>
            <choose>
                <when test="pageDto.executeCycle  ==0 ">
                    r.type in (0,6)
                </when>
                <otherwise>
                    r.type = #{pageDto.executeCycle}
                </otherwise>
            </choose>
            <if test="isTime == true">
                <choose>
                    <when test="pageDto.executeCycle==0||pageDto.executeCycle==6">
                        and r.ymd_year between #{hashMap.startYear} and #{hashMap.endYear}
                        and r.ymd_date between #{hashMap.startDate} and #{hashMap.endDate}
                        and r.ymd_quarter in
                        <foreach collection="ymdQuarters" item="item" index="index" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </when>
                    <when test="pageDto.executeCycle==1">
                        and r.ymd_year between #{hashMap.startYear} and #{hashMap.endYear}
                        and r.ymd_week = #{hashMap.startWeek}
                        and r.ymd_quarter in
                        <foreach collection="ymdQuarters" item="item" index="index" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </when>
                    <when test="pageDto.executeCycle==2">
                        and r.ymd_year between #{hashMap.startYear} and #{hashMap.endYear}
                        <!--and r.ymd_month between #{hashMap.startMonth} and #{hashMap.endMonth}-->
                        and r.ymd_date between #{hashMap.startDate} and #{hashMap.endDate}
                        and r.ymd_quarter in
                        <foreach collection="ymdQuarters" item="item" index="index" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </when>
                    <when test="pageDto.executeCycle==3">
                        and r.ymd_year between #{hashMap.startYear} and #{hashMap.endYear}
                        <!--and r.ymd_quarter between #{hashMap.startQuarter} and #{hashMap.endQuarter}-->
                        and r.ymd_quarter in
                        <foreach collection="ymdQuarters" item="item" index="index" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </when>
                    <when test="pageDto.executeCycle==4">
                        and r.ymd_year between #{hashMap.startYear} and #{hashMap.endYear}
                        and r.ymd_halfyear between #{hashMap.startHalfYear} and #{hashMap.endHalfYear}
                        and r.ymd_quarter in
                        <foreach collection="ymdQuarters" item="item" index="index" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </when>
                    <when test="pageDto.executeCycle==5">
                        and r.ymd_year between #{hashMap.startYear} and #{hashMap.endYear}
                        and r.ymd_quarter=1
                    </when>
                </choose>
            </if>
            <if test="pageDto.status !=null ">
                and r.status = #{pageDto.status}
            </if>
            <if test="pageDto.orgId !=null ">
                and r.org_id = #{pageDto.orgId}
            </if>
            <if test="pageDto.executeRole !=null ">
                and r.role_id = #{pageDto.executeRole}
            </if>
            <if test="pageDto.planId !=null ">
                and r.plan_id = #{pageDto.planId}
            </if>
            <!--<if test="pageDto.checkSub == true">
                and o.path like concat(#{pageDto.orgPath},'%')
            </if>-->
            <if test="pageDto.orgIdList!=null and pageDto.orgIdList.size()>0">
                and r.org_id in
                <foreach collection="pageDto.orgIdList" item="orgId" open="(" close=")" separator=",">
                    #{orgId}
                </foreach>
            </if>
        </where>
        order by r.plan_start_time desc ,r.plan_end_time desc
    </select>
    <select id="selectPageByCondition" resultType="com.xunmei.core.resumption.vo.resumptionRecord.ResumptionRecordPageVo">
        select r.id as resumptionId,r.exception_count as abnormalDataNums,
        <!--p.plan_name as time,
        p.plan_name as planName,-->
        r.plan_id as planId,
        <!--        p.plan_exec as planExec,-->
        r.ymd_date as ymdDate,
        r.status as status,
        r.role_id as roleId,
        <!--o.name as orgName ,-->
        <!--ro.name as roleName,-->
        r.submit_time as submitTime,
        r.modified_name as submitter,
        <!--u.name as submitter,-->
        r.org_id as orgId,
        r.plan_start_time as planStartTime,
        r.plan_end_time as planEndTime
        from core_resumption r
        <!--inner join t_app_plan p on r.plan_id = p.id
        inner join sys_org o on r.org_id = o.id and o.deleted = 0
        inner join t_role ro on ro.id = r.role_id
        eft join t_user u on u.id=r.modified_id-->
        <where>
            <choose>
                <when test="pageDto.executeCycle  ==1">
                    r.type in (1,2)
                </when>
                <otherwise>
                    r.type = #{pageDto.executeCycle}
                </otherwise>
            </choose>

            <if test="isTime==true">
                <if test="pageDto.executeCycle==1||pageDto.executeCycle==2">
                    and r.ymd_year between #{hashMap.startYear} and #{hashMap.endYear}
                    and r.ymd_date between #{hashMap.startDate} and #{hashMap.endDate}
                    and r.ymd_quarter in
                    <foreach collection="ymdQuarters" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>

                <if test="pageDto.executeCycle==3">
                    and r.ymd_year between #{hashMap.startYear} and #{hashMap.endYear}
                    and r.ymd_date between #{hashMap.startDate} and #{hashMap.endDate}
                    and r.ymd_week = #{hashMap.startWeek}
                    and r.ymd_quarter in
                    <foreach collection="ymdQuarters" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="pageDto.executeCycle==4">
                    and r.ymd_year between #{hashMap.startYear} and #{hashMap.endYear}
                    and r.ymd_date between #{hashMap.startDate} and #{hashMap.endDate}
                   <!-- and r.ymd_month between #{hashMap.startMonth} and #{hashMap.endMonth}-->
                    and r.ymd_date between #{hashMap.startMonth} and #{hashMap.endMonth}
                    and r.ymd_quarter in
                    <foreach collection="ymdQuarters" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="pageDto.executeCycle==5">
                    and r.ymd_year between #{hashMap.startYear} and #{hashMap.endYear}
                    and r.ymd_date between #{hashMap.startDate} and #{hashMap.endDate}
                    <!--and r.ymd_quarter between #{hashMap.startQuarter} and #{hashMap.endQuarter}-->
                    and r.ymd_quarter in
                    <foreach collection="ymdQuarters" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="pageDto.executeCycle==6">
                    and r.ymd_year between #{hashMap.startYear} and #{hashMap.endYear}
                    and r.ymd_date between #{hashMap.startDate} and #{hashMap.endDate}
                    and r.ymd_halfyear between #{hashMap.startHalfYear} and #{hashMap.endHalfYear}
                    and r.ymd_quarter in
                    <foreach collection="ymdQuarters" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="pageDto.executeCycle==7">
                    and r.ymd_year between #{hashMap.startYear} and #{hashMap.endYear}
                    and r.ymd_date between #{hashMap.startDate} and #{hashMap.endDate}
                    and r.ymd_quarter=1
                </if>
            </if>
            <if test="pageDto.status !=null ">
                and r.status = #{pageDto.status}
            </if>
            <if test="pageDto.orgId !=null ">
                and r.org_id = #{pageDto.orgId}
            </if>
            <if test="pageDto.executeRole !=null ">
                and r.role_id = #{pageDto.executeRole}
            </if>
            <if test="pageDto.planId !=null ">
                and r.plan_id = #{pageDto.planId}
            </if>
<!--            <if test="pageDto.checkSub == true">-->
<!--                and o.path like concat(#{pageDto.orgPath},'%')-->
<!--            </if>-->
          <if test="pageDto.orgIdList!=null and pageDto.orgIdList.size()>0">
              and r.org_id in
              <foreach collection="pageDto.orgIdList" item="orgId" open="(" close=")" separator=",">
                  #{orgId}
              </foreach>
          </if>
        </where>
        order by r.plan_start_time desc ,r.plan_end_time desc
    </select>
    <select id="selectResumptionDataList" resultType="com.xunmei.core.resumption.vo.resumptionRecord.AppResumptionDataInfoVo">
        select p.point_num  as rulePointNum,
               p.point_name as rulePointName,
               p.safe_type  as safeType,
               a.name       as checkAreaName,
               d.res_value  as executeResult,
               d.res_time   as executeTime,
               d.res_remark as resRemark,
               d.res_status as executeStatus,
               d.id         as dataId,
               p.data_type  as dataTypeId
        from core_resumption_data d
                 inner join core_resumption r on d.resumption_id = r.id
                 inner join core_resumption_data_rule_point p on d.item_id = p.id
                 inner join sys_area a on a.id = p.area_id
        where d.resumption_id = #{resumptionId}
    </select>

    <resultMap id="infoResultMap" type="com.xunmei.core.resumption.vo.resumptionRecord.ResumptionRecordInfoVo">
        <id column="id" jdbcType="BIGINT" javaType="java.lang.Long" property="itemId"/>
        <result column="item_name" jdbcType="VARCHAR" javaType="java.lang.String" property="itemName"/>
<!--        <result column="item_desc" jdbcType="VARCHAR" javaType="java.lang.String" property="itemDesc"/>-->
        <collection property="dataInfoList"
                    ofType="com.xunmei.core.resumption.vo.resumptionRecord.AppResumptionDataInfoVo"
                    select="selectResumptionDataList"
                    column="{resumptionId=resumptionId}"/>
    </resultMap>


    <select id="selectInfo" parameterType="java.lang.String" resultMap="infoResultMap">
        select distinct #{resumptionId} as resumptionId,
                        i.id,
                        i.item_name
        from core_resumption_rule_item i
        where i.id in (select item_id
                       from core_resumption_plan_to_item pti
                       where pti.plan_id in (select plan_id from core_resumption where id = #{resumptionId}))
    </select>

    <select id="selectResumptionRole" resultType="com.xunmei.core.resumption.vo.resumptionRecord.ResumptionRoleVo">
        select distinct tr.id as id, tr.name as name
        from core_resumption r
        inner join sys_role tr on r.role_id = tr.id
        inner join sys_org o on r.org_id = o.id and o.deleted=0
        where o.path like concat(#{roleDto.orgPath},'%')
        <if test="roleDto.type != null">
            and r.type = #{roleDto.type}
        </if>
    </select>

    <select id="selectNFCRecord" resultType="com.xunmei.core.resumption.vo.resumptionRecord.NFCRecordVo">
        select distinct a.name as areaName,
        b.collection_area_name as collectionAreaName,
        n.submit_time as executeTime,
        d.submit_name as executor,
        n.scan_method as scanMethod,
        n.img
        from core_resumption_data_nfc n
        inner join sys_nfc_bind b on n.nfc_id = b.check_id and b.org_id = #{dto.orgId}
        inner join sys_area a on a.id = b.area_id
        <!--INNER JOIN core_resumption_data d on d.resumption_id = n.resumption_id and d.item_id = n.point_id-->
        INNER JOIN core_resumption_data d on d.resumption_id = n.resumption_id and d.area_id = a.id

        <!--where d.resumption_id = #{dto.resumptionId}-->
        where n.resumption_id = #{dto.resumptionId}
    </select>

<!--    <resultMap id="secMap" type="com.isp.dict.domain.SecBookDtInst">-->
<!--        <id column="ID" jdbcType="BIGINT" javaType="java.lang.Long" property="id"/>-->
<!--        <result column="DATA_NAME" jdbcType="VARCHAR" javaType="java.lang.String" property="dataName"/>-->
<!--        <result column="DATA_TYPE" jdbcType="VARCHAR" javaType="java.lang.String" property="dataType"/>-->
<!--        <result column="DELETE_STATUS" jdbcType="VARCHAR" javaType="java.lang.String" property="deleteStatus"/>-->
<!--        <result column="IS_VALIE" jdbcType="VARCHAR" javaType="java.lang.String" property="isValie"/>-->
<!--        <collection property="dataItemList" ofType="com.isp.dict.domain.SecBookDtiInst"-->
<!--                    select="selectSecBookDtiInstList"-->
<!--                    column="{secBookDtInstId=ID}">-->
<!--        </collection>-->
<!--    </resultMap>-->

<!--    <select id="selectSecBookDtInstList" resultMap="secMap">-->
<!--        select *-->
<!--        from sec_book_dt_inst-->
<!--    </select>-->

<!--    <select id="selectSecBookDtiInstList" resultType="com.isp.dict.domain.SecBookDtiInst">-->
<!--        select ID                  as id,-->
<!--               SEC_BOOK_DT_INST_ID as secBookDtInesId,-->
<!--               DATA_ITEM_NUMB      as dataItemNumb,-->
<!--               DATA_ITEM_NAME      as dataItemName,-->
<!--               DATA_ITEM_VALUE     as dataItemValue-->
<!--        from sec_book_dti_inst-->
<!--        where SEC_BOOK_DT_INST_ID = #{secBookDtInstId}-->
<!--    </select>-->

    <select id="selectDataInfo" resultType="com.xunmei.core.resumption.vo.resumptionRecord.AppResumptionDataInfoVo">
        SELECT  p.point_name  AS pointName,
               a.`name`      AS areaName,
               d.res_value   AS executeResult,
               d.submit_time AS executeTime,
               d.res_remark  AS resRemark,
               d.res_status  AS executeStatus,
               d.id          AS dataId,
               p.data_type   AS dataTypeId
        FROM core_resumption_data d
                 INNER JOIN  core_resumption_rule_point p ON d.item_id = p.id
                 INNER JOIN sys_area a ON a.id = p.area_id
        WHERE d.resumption_id = #{resumptionId}
          and p.item_id = #{ruleItemId}
    </select>

    <select id="selectRuleItemIdList" resultType="java.lang.Long">
        select DISTINCT p.item_id
        from  core_resumption_rule_point p
        where id in (select d.item_id from core_resumption_data d where resumption_id = #{resumptionId})

    </select>
    <select id="selectRuleItemIdListByResumptionIdList" resultType="java.lang.Long">
        select DISTINCT p.item_id
        from  core_resumption_rule_point p
        where id in (select d.item_id from core_resumption_data d where resumption_id IN
        <foreach collection="resumptionIdList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        )
        and p.deleted = 0
    </select>
    <select id="selectRuleItemInfo" resultType="com.xunmei.core.resumption.vo.resumptionRecord.ResumptionRecordInfoVo">
        select i.item_num as itemNum, i.item_name as itemName, i.item_desc as itemDesc, i.id as itemId
        from  core_resumption_rule_item i
        where id = #{ruleItemId}
    </select>

    <select id="selectResumptionPlan" resultType="com.xunmei.core.resumption.vo.resumptionRecord.ResumptionRoleVo">
        select distinct p.id as id, p.plan_name as name, p.plan_status as status
        from  core_resumption_plan p INNER JOIN core_resumption r on r.plan_id = p.id and p.plan_type = 0
        inner join  core_resumption_plan_to_role pr on p.id = pr.plan_id
        INNER JOIN sys_org o on o.id=r.org_id
        where o.path like concat(#{planDto.orgPath},'%')
        <choose>
            <when test="planDto.type != null and planDto.type ==0">
                and r.type in (0,6)
            </when>
            <otherwise>
                and r.type = #{planDto.type}
            </otherwise>
        </choose>
        <if test="planDto.roleId != null">
            and pr.role_id = #{planDto.roleId}
        </if>
    </select>

<!--    <select id="selectData" resultType="com.xunmei.core.resumption.vo.registerBook.RegisterBookItemInfoVo">-->
<!--        select distinct o.name as orgName, r.start_time as date, #{id} as resumptionId-->
<!--        from core_resumption r-->
<!--                 inner join sys_org o on r.org_id = o.id-->
<!--                 inner join core_resumption_data d on r.id = d.resumption_id-->
<!--        where d.resumption_id = #{id}-->
<!--    </select>-->

    <select id="selectResumptionDataPointId" resultType="java.lang.Long">
        select item_id
        from core_resumption_data
        where resumption_id = #{id}
    </select>

    <select id="selectAbnormalData" resultType="com.xunmei.core.resumption.vo.resumptionRecord.ResumptionRecordTempVo">
        SELECT d.resumption_id as resumptionId,count(d.res_value = '1' or null) as nums,count(d.id) as nums2
        FROM core_resumption_data d
        INNER JOIN  core_resumption_rule_point p ON d.item_id = p.id
        where d.resumption_id in
        <foreach collection="resumptionIdList" item="resumptionId" open="(" separator="," close=")">
            #{resumptionId}
        </foreach>
        and p.item_id in
        <foreach collection="ruleItemIdList" item="itemId" open="(" separator="," close=")">
            #{itemId}
        </foreach>
        GROUP BY d.resumption_id
    </select>
    <select id="selectItemIdList" resultType="java.lang.Long">
        select d.item_id from core_resumption_data d where resumption_id = #{resumptionId}
    </select>
    <select id="selectRuleItemIdListNew" resultType="java.lang.Long">
        select DISTINCT p.item_id
        from  core_resumption_rule_point p
        where id in
        <foreach collection="ruleItemIdList" item="rulePointId" open="(" separator="," close=")">
            #{rulePointId}
        </foreach>
    </select>

    <select id="selectResumptionRoles" resultType="com.xunmei.common.core.vo.IdNameVo">
        select distinctrow r.id,r.name
        from core_resumption_plan p
        inner join core_resumption_plan_to_role pr on p.id=pr.plan_id
        inner join sys_role r on pr.role_id=r.id
        where p.plan_status = 0
<!--        <if test="roleDto.type != null">-->
<!--            and plan_cycle=#{roleDto.type}-->
<!--        </if>-->

    </select>
</mapper>
