<?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.dsk.cscec.mapper.AdvisoryBodyMapper">
    <select id="selectPageAdvisoryBodyList" resultType="com.dsk.cscec.domain.vo.AdvisoryBodySearchVo">
        select
        <include refid="allColumn"></include>
        from advisory_body ab ${ew.getCustomSqlSegment}
    </select>

    <select id="queryListByBo" resultType="com.dsk.cscec.domain.vo.AdvisoryBodyVo">
        select abp.advisory_body_id,abp.advisory_body_cid,abp.project_key,abp.project_leader,abp.project_leader_major,
        abp.project_leader_phone,
        ab.advisory_body_name,
        dp.project_name,dp.project_code,dp.province_name,dp.city_name,dp.isinvestproject,dp.project_type1,
        dp.project_type2,dp.contract_value,dp.owner_name,dp.contract_org_name,dp.contract_sign_date
        from advisory_body_project abp
        left join advisory_body ab on ab.advisory_body_id = abp.advisory_body_id
        left join d_project dp on dp.project_key = abp.project_key
        <where>
            <if test="bo.advisoryBodyId != null and bo.advisoryBodyId != ''">
                and ab.advisory_body_id = #{bo.advisoryBodyId}
            </if>
            <if test="bo.advisoryBodyCid != null and bo.advisoryBodyCid != ''">
                and ab.advisory_body_cid = #{bo.advisoryBodyCid}
            </if>
            <if test="bo.advisoryBodyName != null and bo.advisoryBodyName != ''">
                and ab.advisory_body_name = #{bo.advisoryBodyName}
            </if>
            <if test="bo.contractOrgId != null and bo.contractOrgId != ''">
                and dp.contract_org_id = #{bo.contractOrgId}
            </if>
            <if test="bo.contractOrgName != null and bo.contractOrgName != ''">
                and dp.contract_org_name = #{bo.contractOrgName}
            </if>
            <if test="bo.area != null and bo.area.size > 0 or (bo.province != null and bo.province.size > 0) or (bo.city != null and bo.city.size > 0)">
                and
                <trim prefix="(" suffix=")" prefixOverrides="or">
                    <if test="bo.area != null and bo.area.size > 0 ">
                        or dp.area_name in
                        <foreach collection="bo.area" item="areaName" open="(" close=")" separator=",">
                            #{areaName}
                        </foreach>
                    </if>
                    <if test="bo.province != null and bo.province.size > 0 ">
                        or dp.province_name in
                        <foreach collection="bo.province" item="provinceName" open="(" close=")" separator=",">
                            #{provinceName}
                        </foreach>
                    </if>
                    <if test="bo.city != null and bo.city.size > 0 ">
                        or dp.city_name in
                        <foreach collection="bo.city" item="cityName" open="(" close=")" separator=",">
                            #{cityName}
                        </foreach>
                    </if>
                </trim>
            </if>
            <if test="bo.isinvestproject != null and bo.isinvestproject != ''">
                and dp.isinvestproject = #{bo.isinvestproject}
            </if>
            <if test="bo.projectType1 != null and bo.projectType1 != ''">
                and dp.project_type1 = #{bo.projectType1}
            </if>
            <if test="bo.projectType2 != null and bo.projectType2 != ''">
                and dp.project_type2 = #{bo.projectType2}
            </if>
        </where>
        order by dp.contract_sign_date desc
    </select>

    <select id="countByBo" resultType="int">
        select count(1)
        from advisory_body_project abp
        left join advisory_body ab on ab.advisory_body_id = abp.advisory_body_id
        left join d_project dp on dp.project_key = abp.project_key
        <where>
            <if test="bo.advisoryBodyId != null and bo.advisoryBodyId != ''">
                and ab.advisory_body_id = #{bo.advisoryBodyId}
            </if>
            <if test="bo.advisoryBodyCid != null and bo.advisoryBodyCid != ''">
                and ab.advisory_body_cid = #{bo.advisoryBodyCid}
            </if>
            <if test="bo.advisoryBodyName != null and bo.advisoryBodyName != ''">
                and ab.advisory_body_name = #{bo.advisoryBodyName}
            </if>
            <if test="bo.contractOrgId != null and bo.contractOrgId != ''">
                and dp.contract_org_id = #{bo.contractOrgId}
            </if>
            <if test="bo.contractOrgName != null and bo.contractOrgName != ''">
                and dp.contract_org_name = #{bo.contractOrgName}
            </if>
        </where>
    </select>

    <select id="selectAdvisoryBodyCidsByName" resultType="java.lang.Long">
        select
        ab.advisory_body_cid
        from advisory_body ab
        <where>
            <if test="advisoryBodyName != null">
                advisory_body_name like concat('%',#{advisoryBodyName},'%')
            </if>
        </where>
    </select>

    <select id="queryAdvisoryBodyByProjectKey" resultType="com.dsk.cscec.domain.AdvisoryBody">
        select
        <include refid="allColumn"></include>
        from advisory_body ab
        left join advisory_body_project abp
        on abp.advisory_body_cid = ab.advisory_body_cid
        where abp.project_key = ${projectKey};
    </select>

    <sql id="allColumn">
        ab.advisory_body_id,
       ab.advisory_body_cid,
       ab.advisory_body_name,
       ab.business_scope,
       ab.create_by,
       ab.create_time,
       ab.update_by,
       ab.update_time
    </sql>
</mapper>