<?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.DSubcontractMapper">

    <select id="selectEnterpriseCountByCustomerId" resultType="java.lang.Integer">
        select count(num) from (
            select count(sign_org_id) num from d_subcontract where customer_id = #{customerId} group by sign_org_id
        ) a

    </select>

    <select id="selectProjectCountByCustomerId" resultType="java.lang.Integer">
         select count(num) from (
            select count(project_id) num from d_subcontract where customer_id = #{customerId} group by project_id
        ) a
    </select>

	<select id="selectStatisticByCustomerId" resultType="com.dsk.cscec.domain.vo.DCustomerListVo">
        select
            count(DISTINCT org.sign_org_id) enterpriseCooperationCount,
           	count(DISTINCT project.project_id) projectCooperationCount
        from d_customer dc
        left join  d_subcontract org on org.customer_id = dc.customer_id
        left join  d_subcontract project on project.customer_id = dc.customer_id
        where dc.customer_id  = #{customerId}
    </select>

 <select id="selectCooperationList" resultType="com.dsk.cscec.domain.vo.CustomerCooperationVo">
        select ds.subcontract_key,ds.subcontract_id,ds.project_name,ds.sign_org_name,ds.sign_date,ds.subcontract_value,ds.settle_value,ds.job_scope,
            dp.area_name,dp.province_name,dp.city_name,dp.project_manager_name,dp.project_manager_phone,dp.project_type2
        from d_subcontract ds
        left join d_project dp on dp.project_id = ds.project_id
        <where>
            <if test="bo.signOrgId != null and bo.signOrgId != ''">
                and ds.sign_org_id =#{bo.signOrgId}
            </if>
            <if test="bo.signOrgName != null and bo.signOrgName != ''">
                and ds.sign_org_name =#{bo.signOrgName}
            </if>
            <if test="bo.customerId != null and bo.customerId != ''">
                and ds.customer_id =#{bo.customerId}
            </if>
            <if test="bo.customerName != null and bo.customerName != ''">
                and ds.customer_name =#{bo.customerName}
            </if>
            <if test="bo.areaName != null and bo.areaName.size > 0 or (bo.provinceName != null and bo.provinceName.size > 0) or (bo.cityName != null and bo.cityName.size > 0)">
                and
                <trim prefix="(" suffix=")" prefixOverrides="or">
                    <if test="bo.areaName != null and bo.areaName.size > 0 ">
                        or dp.area_name in
                        <foreach collection="bo.areaName" item="areaName" open="(" close=")" separator=",">
                            #{areaName}
                        </foreach>
                    </if>
                    <if test="bo.provinceName != null and bo.provinceName.size > 0 ">
                        or dp.province_name in
                        <foreach collection="bo.provinceName" item="provinceName" open="(" close=")" separator=",">
                            #{provinceName}
                        </foreach>
                    </if>
                    <if test="bo.cityName != null and bo.cityName.size > 0 ">
                        or dp.city_name in
                        <foreach collection="bo.cityName" item="cityName" open="(" close=")" separator=",">
                            #{cityName}
                        </foreach>
                    </if>
                </trim>
            </if>
        </where>
        order by ds.sign_date desc
    </select>

    <select id="countByBo" resultType="int">
        select count (1)
        from d_subcontract ds
        left join d_project dp on dp.project_id = ds.project_id
        <where>
            <if test="bo.signOrgId != null and bo.signOrgId != ''">
                and ds.sign_org_id =#{bo.signOrgId}
            </if>
            <if test="bo.signOrgName != null and bo.signOrgName != ''">
                and ds.sign_org_name =#{bo.signOrgName}
            </if>
            <if test="bo.customerId != null and bo.customerId != ''">
                and ds.customer_id =#{bo.customerId}
            </if>
            <if test="bo.customerName != null and bo.customerName != ''">
                and ds.customer_name =#{bo.customerName}
            </if>
        </where>
    </select>
</mapper>