<?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.highdatas.mdm.mapper.MaintainMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.highdatas.mdm.entity.Maintain">
        <id column="id" property="id" />
        <result column="operate" property="operate" />
        <result column="table_name" property="tableName" />
        <result column="flow_id" property="flowId" />
        <result column="charge_id" property="chargeId" />
        <result column="desp" property="desp" />
        <result column="version" property="version" />
        <result column="create_time" property="createTime" />
    </resultMap>

    <select id="selectVersionOperator"  resultType="java.util.Map">
        SELECT * from ${tableTempName} t
        LEFT  JOIN maintain_detail d on d.pre_merge_id = t.id
        where parent_id = #{maintaInId} and ${where}
   </select>
    <select id="selectVersionOperatorByPage"  resultType="java.util.Map">
        SELECT * from ${tableTempName} t
        LEFT  JOIN maintain_detail d on d.pre_merge_id = t.id
        where parent_id = #{maintaInId} and ${where}
        ${limit}
    </select>

    <select id="countVersionOperator"  resultType="java.lang.Long">
        SELECT count(1) from ${tableTempName} t
        LEFT  JOIN maintain_detail d on d.pre_merge_id = t.id
        where parent_id = #{maintaInId} and ${where}
    </select>


    <select id="selectVersionOperatorByType" resultType="java.util.Map" >
        SELECT * from ${tableTempName} t
        LEFT  JOIN maintain_detail d on d.pre_merge_id = t.id
        WHERE parent_id = #{maintaInId} and ${where}
        AND d.operate = #{operate}
    </select>

    <select id="selectVersionOperatorByTypeByPage" resultType="java.util.Map" >
        SELECT * from ${tableTempName} t
        LEFT  JOIN maintain_detail d on d.pre_merge_id = t.id
        WHERE parent_id = #{maintaInId}
        AND d.operate = #{operate} and ${where}
        ${limit}
    </select>

    <select id="selectInvalidVersionData" resultType="java.util.Map" >
        <![CDATA[
        SELECT ${tempHFields},status, operate,version  from (
                (SELECT h.*, status,operate,version,order_no
        FROM
            ${tableTempName} h
        LEFT JOIN maintain_detail d ON d.pre_merge_id = h.id
        LEFT JOIN maintain ON maintain.id = d.parent_id
        LEFT JOIN flows on flows.business_id = maintain.id
        WHERE flows.`status` <> 'open' and flows.`status` <> 'close'
         order BY  maintain.order_no desc)
        UNION
        (SELECT h.*, status,operate,version,order_no
        FROM
            ${tableTempName} h
        LEFT JOIN maintain_detail d ON d.pre_merge_id = h.id
        LEFT JOIN maintain ON maintain.id = d.parent_id
        LEFT JOIN flows on flows.business_id = maintain.id
        WHERE flows.id is null and maintain.id is not null
         order BY  maintain.order_no desc)
        ) a where ${where} ORDER BY order_no  desc
        ${limit}
         ]]>
    </select>

    <select id="countInvalidVersionData" resultType="java.lang.Long" >
        <![CDATA[
        SELECT count(1) from
        (
                SELECT h.*, status,operate
        FROM
            ${tableTempName} h
        LEFT JOIN maintain_detail d ON d.pre_merge_id = h.id
        LEFT JOIN maintain ON maintain.id = d.parent_id
        LEFT JOIN flows on flows.business_id = maintain.id
        WHERE flows.`status` <> 'open' and flows.`status` <> 'close'
        UNION
        SELECT h.*, status,operate
        FROM
            ${tableTempName} h
        LEFT JOIN maintain_detail d ON d.pre_merge_id = h.id
        LEFT JOIN maintain ON maintain.id = d.parent_id
        LEFT JOIN flows on flows.business_id = maintain.id
        WHERE flows.id is null and maintain.id is not null
        ) a where ${where}
        ]]>
    </select>


    <select id="countVersionOperatorByType" resultType="java.lang.Long">
        SELECT count(1) from ${tableTempName} t
        LEFT  JOIN maintain_detail d on d.pre_merge_id = t.id
        WHERE parent_id = #{maintaInId}
        AND d.operate = #{operate} and ${where}
    </select>

    <select id="countTempDataByVersionByFlow" resultType="java.lang.Long">
        SELECT count(1) from ${tableTempName} mt
        LEFT JOIN maintain_detail mad  on mad.pre_merge_id = mt.id
        LEFT JOIN flows on flows.business_id = mad.parent_id
        WHERE flows.id = #{flowId}
        AND  ${where}
    </select>

    <select id="tempDataByVersionByFlow" resultType="java.util.Map">
         SELECT ${fields}, mad.operate from ${tableTempName} mt
        LEFT JOIN maintain_detail mad  on mad.pre_merge_id = mt.id
        LEFT JOIN flows on flows.business_id = mad.parent_id
        WHERE flows.id = #{flowId}
        AND  ${where} ${limit}
    </select>



    <select id="countUnSubmitData" resultType="java.lang.Long">
        SELECT count(1) from ${tableTempName} mt
        LEFT JOIN maintain_detail mad  on mad.pre_merge_id = mt.id
        WHERE mad.parent_id = ${maintainId}
        AND  ${where}
    </select>

    <select id="selectUnSubmitData" resultType="java.util.Map">
         SELECT ${fields}, mad.operate from ${tableTempName} mt
        LEFT JOIN maintain_detail mad  on mad.pre_merge_id = mt.id
        WHERE mad.parent_id = ${maintainId}
        AND  ${where} ${limit}
    </select>
</mapper>