<?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="cn.com.countrygarden.bi.main.dao.extension.ExtensionDupBindingMapper">
    <resultMap id="DupResultMap" type="cn.com.countrygarden.bi.main.dto.extension.ExtensionDupDTO">
        <result column="id" property="id"/>
        <result column="organ_name" property="organName"/>
        <result column="project_name" property="projectName"/>
        <result column="proposalOrganName" property="proposalOrganName"/>
        <result column="project_nature_name" property="projectNature"/>
        <result column="is_new" property="isNew"/>
        <result column="corp_type_name" property="corpType"/>
        <result column="sign_time" property="signTime"/>
    </resultMap>

    <!-- 查询全部 签约已上会 -->
    <select id="selectAllByDTO" resultMap="DupResultMap">
        SELECT
        ps.id ,
        ps.organ_name,
        ps.project_name ,
        org.`name` AS 'proposalOrganName',
        ps.project_nature_name ,
        ps.is_new ,
        ps.corp_type_name ,
        DATE_FORMAT(ps.sing_time_str,'%Y-%m-%d') as sign_time
        FROM
        project_sign ps
        LEFT JOIN organ org ON org.organ_code = ps.proposal_organ_id
        LEFT JOIN (SELECT project_sign_id FROM project_sign_item GROUP BY project_sign_id ) psi ON ps.id = psi.project_sign_id
        <where>
            ps.is_delete = 0 and psi.project_sign_id IS NOT NULL
            <if test="dto.projectName != null and dto.projectName != ''"> AND ps.project_name LIKE CONCAT('%',#{dto.projectName},'%') </if>
            <if test="dto.organName != null and dto.organName != ''"> AND ps.organ_name LIKE CONCAT('%',#{dto.organName},'%')  </if>
            <if test="dto.proposalOrganName != null and dto.proposalOrganName != ''"> AND org.name = #{dto.proposalOrganName} </if>
            <if test="dto.signTime != null and dto.signTime != ''"> AND date_format(ps.sing_time_str,'%Y-%m-%d') &gt;= #{dto.signTime} </if>
            <if test="dto.signTimeEnd != null and dto.signTimeEnd != ''"> AND date_format(ps.sing_time_str,'%Y-%m-%d') &lt;=  #{dto.signTimeEnd} </if>
            <if test="dto.bindingIdentifier != null ">
                <if test="dto.bindingIdentifier == 0" >
                    AND NOT EXISTS (SELECT er.dup_id FROM extension_report er WHERE er.dup_id = ps.id AND er.binding_Identifier = 1 AND er.is_delete = 0)
                </if>
                <if test="dto.bindingIdentifier == 1" >
                    AND EXISTS (SELECT er.dup_id FROM extension_report er WHERE er.dup_id = ps.id AND er.binding_Identifier = 1 AND er.is_delete = 0)
                </if>
            </if>
            <if test="organName != null and organName != ''" >
                AND org.name = #{organName}
            </if>
        </where>
        ORDER BY org.name
    </select>

    <!-- 导出签约已上会 查询 -->
    <select id="selectExportByDto" resultMap="DupResultMap">
        SELECT
        ps.id ,
        ps.organ_name,
        ps.project_name ,
        org.`name` AS 'proposalOrganName',
        ps.project_nature_name ,
        ps.is_new ,
        ps.corp_type_name ,
        DATE_FORMAT(ps.sing_time_str,'%Y-%m-%d') as sign_time
        FROM
        project_sign ps
        LEFT JOIN organ org ON org.organ_code = ps.proposal_organ_id
        LEFT JOIN (SELECT project_sign_id FROM project_sign_item GROUP BY project_sign_id ) psi ON ps.id = psi.project_sign_id
        <where>
            ps.is_delete = 0 and psi.project_sign_id IS NOT NULL
            <if test="dto.projectName != null and dto.projectName != ''"> AND ps.project_name LIKE CONCAT('%',#{dto.projectName},'%') </if>
            <if test="dto.organName != null and dto.organName != ''"> AND ps.organ_name LIKE CONCAT('%',#{dto.organName},'%')  </if>
            <if test="dto.proposalOrganName != null and dto.proposalOrganName != ''"> AND org.name = #{dto.proposalOrganName} </if>
            <if test="dto.signTime != null and dto.signTime != ''"> AND date_format(ps.sing_time_str,'%Y-%m-%d') &gt;= #{dto.signTime} </if>
            <if test="dto.signTimeEnd != null and dto.signTimeEnd != ''"> AND date_format(ps.sing_time_str,'%Y-%m-%d') &lt;=  #{dto.signTimeEnd} </if>
            <if test="organName != null and organName != ''" >
                AND org.name = #{organName}
            </if>
        </where>
        ORDER BY org.name
    </select>

    <!-- 导出未绑定 查询 -->
    <select id="selectUnbindingExportByDto" resultMap="DupResultMap">
        SELECT
        ps.id ,
        ps.organ_name,
        ps.project_name ,
        org.`name` AS 'proposalOrganName',
        ps.project_nature_name ,
        ps.is_new ,
        ps.corp_type_name ,
        DATE_FORMAT(ps.sing_time_str,'%Y-%m-%d') as sign_time
        FROM
        project_sign ps
        LEFT JOIN organ org ON org.organ_code = ps.proposal_organ_id
        LEFT JOIN (SELECT project_sign_id FROM project_sign_item GROUP BY project_sign_id ) psi ON ps.id = psi.project_sign_id
        <where>
            ps.is_delete = 0 and psi.project_sign_id IS NOT NULL
            <if test="ids != null and ids.size > 0">
                AND ps.id NOT IN
                <foreach collection="ids" item="id" open="(" separator="," close=")" >
                    #{id}
                </foreach>
            </if>
            <if test="dto.projectName != null and dto.projectName != ''"> AND ps.project_name LIKE CONCAT('%',#{dto.projectName},'%') </if>
            <if test="dto.organName != null and dto.organName != ''"> AND ps.organ_name LIKE CONCAT('%',#{dto.organName},'%')  </if>
            <if test="dto.proposalOrganName != null and dto.proposalOrganName != ''"> AND org.name = #{dto.proposalOrganName} </if>
            <if test="dto.signTime != null and dto.signTime != ''"> AND date_format(ps.sing_time_str,'%Y-%m-%d') &gt;= #{dto.signTime} </if>
            <if test="dto.signTimeEnd != null and dto.signTimeEnd != ''"> AND date_format(ps.sing_time_str,'%Y-%m-%d') &lt;=  #{dto.signTimeEnd} </if>
            <if test="organName != null and organName != ''" >
                AND org.name = #{organName}
            </if>
        </where>
        ORDER BY org.name
    </select>



    <select id="selectAllCount" resultType="java.lang.Long" >
        SELECT COUNT(0) FROM project_sign ps
        LEFT JOIN organ org ON org.organ_code = ps.proposal_organ_id
        LEFT JOIN (SELECT project_sign_id FROM project_sign_item GROUP BY project_sign_id ) psi ON ps.id = psi.project_sign_id
        where
        ps.is_delete = 0 and psi.project_sign_id IS NOT NULL
        <if test="organName != null and organName != ''" >
            AND org.name = #{organName}
        </if>
    </select>

    <select id="selectUnbindingCount" resultType="java.lang.Long" >
        SELECT COUNT(0) FROM project_sign ps
        LEFT JOIN organ org ON org.organ_code = ps.proposal_organ_id
        LEFT JOIN (SELECT project_sign_id FROM project_sign_item GROUP BY project_sign_id ) psi ON ps.id = psi.project_sign_id
        where
        ps.is_delete = 0 and psi.project_sign_id IS NOT NULL
        <if test="ids != null and ids.size > 0" >
            AND ps.id NOT IN
            <foreach collection="ids" item="id" open="(" separator="," close=")" >
                #{id}
            </foreach>
        </if>
        <if test="organName != null and organName != ''" >
            AND org.name = #{organName}
        </if>
    </select>

</mapper>