<?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.dianmi.dao.CustProjectDao">
    <resultMap id="BaseResultMap" type="com.dianmi.domain.entity.CustProject">
        <id column="id" property="id"/>
        <result column="customer_id" property="customerId"/>
        <result column="parent_id" property="parentId"/>
        <result column="wo_id" property="woId"/>
        <result column="project_name" property="projectName"/>
        <result column="project_code" property="projectCode"/>
        <result column="business_type_name" property="businessTypeName"/>
        <result column="business_type_code" property="businessTypeCode"/>
        <result column="sign_company_id" property="signCompanyId"/>
        <result column="sign_company_name" property="signCompanyName"/>
        <result column="is_exclusive" property="isExclusive"/>
        <result column="exclusive_description" property="exclusiveDescription"/>
        <result column="nc_code" property="ncCode"/>
        <result column="interface_man" property="interfaceMan"/>
        <result column="interface_man_territory" property="interfaceManTerritory"/>
        <result column="interface_man_phone" property="interfaceManPhone"/>
        <result column="interface_man_email" property="interfaceManEmail"/>
        <result column="remark" property="remark"/>
        <result column="if_need_recruitment" property="ifNeedRecruitment"/>
        <result column="recruiter" property="recruiter"/>
        <result column="service_status" property="serviceStatus"/>
        <result column="ejy_wage_auto_card" property="ejyWageAutoCard"/>
        <result column="data_source" property="dataSource"/>
        <result column="service_mode" property="serviceMode"/>
        <result column="service_province" property="serviceProvince"/>
        <result column="service_city" property="serviceCity"/>
        <result column="tax_rate" property="taxRate"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,
        customer_id,
        parent_id,
        wo_id,
        project_name,
        project_code,
        business_type_name,
        business_type_code,
        sign_company_id,
        sign_company_name,
        is_exclusive,
        exclusive_description,
        nc_code,
        interface_man,
        interface_man_territory,
        interface_man_phone,
        interface_man_email,
        remark,
        if_need_recruitment,
        recruiter,
        service_status,
        ejy_wage_auto_card,
        data_source,
        service_mode,
        service_province,
        service_city,
        tax_rate,
        create_time,
        update_time
    </sql>

    <select id="selectProject" resultType="com.dianmi.dto.proj.CustProjectDto">
        select
        cp.id,
        cp.project_name,
        cp.project_code,
        cp.customer_id,
        cp.cust_sale_process_id as custSaleProcessId,
        c.customer_name,
        c.customer_code,
        (SELECT id FROM cust_project WHERE parent_id = cp.id order by id LIMIT 1) firstProjectId,
        (SELECT GROUP_CONCAT(DISTINCT user_name) FROM proj_member WHERE project_id in (SELECT id FROM cust_project WHERE
        parent_id = cp.id and `type` = '销售')) sellerName,
        cp.business_type_name,
        cp.sign_company_name,
        cp.service_status
        from
        cust_project cp
        left join customer c on c.id = cp.customer_id
        where
        parent_id = 0 and ((cp.area_code != 'AR0003' AND cp.area_code != 'AR0001') or cp.area_code is null)
        <if test="pojo!=null">
            and cp.id in (
            SELECT parent_id FROM cust_project WHERE id in
            ( select project_id from proj_member where user_account in
            <foreach item="item" index="index" open="(" separator="," close=")" collection="pojo.memberSet">
                #{item}
            </foreach>
            )
            )
            <if test="pojo.searchType==1">
                <if test="pojo.searchContent!=null">
                    and (c.customer_name like #{pojo.searchContent} or c.customer_code like #{pojo.searchContent})
                </if>
            </if>
            <if test="pojo.searchType==2">
                <if test="pojo.searchContent!=null">
                    and (cp.project_name like #{pojo.searchContent} or cp.project_code like #{pojo.searchContent})
                </if>
            </if>
            <if test="pojo.searchType==3">
                <if test="pojo.searchContent!=null">
                    and cp.id in
                    (
                    select parent_id from cust_project
                    where (project_name like #{pojo.searchContent} or project_code like #{pojo.searchContent})
                    )
                </if>
            </if>
            <if test="pojo.sellerAccounts!=null">
                and cp.id in (
                select project_id from proj_member where `type` = '销售' and user_account in
                <foreach item="item" index="index" open="(" separator="," close=")"
                         collection="pojo.sellerAccounts">
                    #{item}
                </foreach>
                UNION
                SELECT project_id FROM proj_contract
                where
                zwzy_acct in
                <foreach item="item" index="index" open="(" separator="," close=")"
                         collection="pojo.sellerAccounts">
                    #{item}
                </foreach>
                or kf_followup_account in
                <foreach item="item" index="index" open="(" separator="," close=")"
                         collection="pojo.sellerAccounts">
                    #{item}
                </foreach>
                )
            </if>
        </if>
        <if test="projectIds != null and projectIds.size()>0">
            and cp.id in
            <foreach item="item" index="index" open="(" separator="," close=")" collection="projectIds">
                #{item}
            </foreach>
        </if>
        order by cp.id desc
    </select>

    <!--财务角色,看到全部数据,筛选条件 -->
    <select id="selectProjectByCaiWu" resultType="com.dianmi.dto.proj.CustProjectDto">
        select
        cp.id,
        cp.project_name,
        cp.project_code,
        cp.customer_id,
        c.customer_name,
        c.customer_code,
        cp.cust_sale_process_id as custSaleProcessId,
        (SELECT id FROM cust_project WHERE parent_id = cp.id order by id LIMIT 1) firstProjectId,
        (SELECT GROUP_CONCAT(DISTINCT user_name) FROM proj_member WHERE project_id in (SELECT id FROM cust_project WHERE
        parent_id = cp.id and `type` = '销售')) sellerName,
        cp.business_type_name,
        cp.sign_company_name,
        cp.service_status
        from
        cust_project cp
        left join customer c on c.id = cp.customer_id
        where
        parent_id = 0 and ((cp.area_code != 'AR0003' AND cp.area_code != 'AR0001') or cp.area_code is null)
        <if test="pojo!=null">
            <if test="pojo.searchType==1">
                <if test="pojo.searchContent!=null">
                    and (c.customer_name like #{pojo.searchContent} or c.customer_code like #{pojo.searchContent})
                </if>
            </if>
            <if test="pojo.searchType==2">
                <if test="pojo.searchContent!=null">
                    and (cp.project_name like #{pojo.searchContent} or cp.project_code like #{pojo.searchContent})
                </if>
            </if>
            <if test="pojo.searchType==3">
                <if test="pojo.searchContent!=null">
                    and cp.id in
                    (
                    select parent_id from cust_project
                    where (project_name like #{pojo.searchContent} or project_code like #{pojo.searchContent})
                    )
                </if>
            </if>
            <if test="pojo.sellerAccounts!=null">
                and cp.id in (
                select project_id from proj_member where `type` = '销售' and user_account in
                <foreach item="item" index="index" open="(" separator="," close=")"
                         collection="pojo.sellerAccounts">
                    #{item}
                </foreach>
                UNION
                SELECT project_id FROM proj_contract
                where
                zwzy_acct in
                <foreach item="item" index="index" open="(" separator="," close=")"
                         collection="pojo.sellerAccounts">
                    #{item}
                </foreach>
                or kf_followup_account in
                <foreach item="item" index="index" open="(" separator="," close=")"
                         collection="pojo.sellerAccounts">
                    #{item}
                </foreach>
                )
            </if>
        </if>
        <if test="projectIds != null and projectIds.size()>0">
            and cp.id in
            <foreach item="item" index="index" open="(" separator="," close=")" collection="projectIds">
                #{item}
            </foreach>
        </if>
        order by cp.id desc
    </select>

    <select id="selectChildList" resultType="com.dianmi.dto.proj.CustProjectDto">
        select
        cp.id,
        cp.project_name,
        cp.project_code,
        cp.customer_id,
        c.customer_name,
        c.customer_code,
        (select GROUP_CONCAT(user_name) from proj_member where project_id = cp.id and `type` = '销售') seller_name,
        cp.business_type_name,
        cp.sign_company_name,
        cp.service_status
        from
        cust_project cp
        left join customer c on c.id = cp.customer_id
        where cp.parent_id in
        <foreach item="item" index="index" open="(" separator="," close=")" collection="projectIdList">
            #{item}
        </foreach>
        order by cp.id desc
    </select>

    <select id="selectByAccounts" resultType="com.dianmi.domain.entity.CustProject">
        select
        <include refid="Base_Column_List"/>
        from cust_project
        where
        parent_id != 0
        and service_status = 1
        and id in (
        select project_id from proj_member where user_account in
        <foreach item="item" index="index" open="(" separator="," close=")" collection="accountList">
            #{item}
        </foreach>
        and `type` = '销售'
        )and parent_id in (
        select project_id from proj_contract
        )
        and id not in (
        select project_id from company_sign where sign_type = 2
        )
        order by id desc
    </select>
    <select id="selectNotInvalidCount" resultType="java.lang.Integer">
        select count(*) from cust_project where service_status = 1 and customer_id = #{customerId}
    </select>

    <select id="listLigongCustProjects" resultType="com.dianmi.dto.ligong.CustProjectLigongDto">
        SELECT
        foo.project_code as parentProjectCode,
        cp.*,
        pc.id as crmContactId
        FROM
        cust_project cp
        JOIN customer c on c.id = cp.customer_id
        join business_type bt on bt.composite_code=cp.business_type_code
        left join cust_project foo on foo.id = cp.parent_id
        left JOIN proj_contract pc on pc.project_id = foo.id
        WHERE cp.customer_id = #{customerId}
        and cp.parent_id != 0 and cp.project_code!=''
        <if test="signCompanyId != null and signCompanyId != ''">
            and cp.sign_company_id =#{signCompanyId}
        </if>
        and bt.composite_code='YG0302'
        and bt.area_code='AR0002'
        order by cp.id desc
    </select>

    <!--根据客户id查询父项目-->
    <select id="getFooProjectsByCustomerId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        cust_project
        WHERE
        parent_id = 0
        AND customer_id = #{customerId}
    </select>

    <select id="selectByProjectCode" resultType="com.dianmi.domain.entity.CustProject">
        SELECT
        <include refid="Base_Column_List"/>
        from cust_project
        where project_code = #{projectCode}
    </select>

    <select id="selectChildByProjectCode" resultType="com.dianmi.domain.entity.CustProject">
        SELECT
        <include refid="Base_Column_List"/>
        from cust_project
        where parent_id in (
        SELECT
        id
        from cust_project
        where project_code = #{projectCode}
        )
    </select>

    <select id="selectFirstProjectId" resultType="java.lang.String">
        SELECT id FROM cust_project WHERE parent_id = #{parentProjectId} order by id LIMIT 1
    </select>
    <select id="selectAreaChild" resultType="com.dianmi.crm.api.dto.CustProjectInfoDto">
        select
        cp.id id,
        cp.project_name projectName,
        cp.project_code projectCode,
        cp.customer_id customerId,
        CAST(cp.tax_rate AS decimal(14,2)) taxRate,
        c.customer_name customerName,
        c.customer_code customerCode,
        c.area_name areaName,
        cp.business_type_name businessTypeName,
        cp.sign_company_name signCompanyName,
        cp.service_status serviceStatus,
        cp.parent_id parentId
        from
        cust_project cp
        left join customer c on c.id = cp.customer_id
        where
        c.area_code = #{areaCode}
        and cp.parent_id != 0
    </select>

    <select id="selectSubProjectsBySubProjectCodes" resultType="com.dianmi.dto.proj.CustProjectDto">
        select
        <include refid="Base_Column_List"/>
        from cust_project
        where project_code in
        <foreach item="item" index="index" open="(" separator="," close=")" collection="subProjectCodes">
            #{item}
        </foreach>
        and parent_id !=0
    </select>

    <select id="getProjectByprojectIds" resultType="com.dianmi.crm.api.dto.CustProjectInfoDto">
        select
        cp.id id,
        cp.project_name projectName,
        cp.project_code projectCode,
        cp.customer_id customerId,
        CAST(cp.tax_rate AS decimal(14,2)) taxRate,
        c.customer_name customerName,
        c.customer_code customerCode,
        c.area_name areaName,
        cp.business_type_name businessTypeName,
        cp.sign_company_name signCompanyName,
        cp.service_status serviceStatus,
        cp.parent_id parentId
        from
        cust_project cp
        left join customer c on c.id = cp.customer_id
        where cp.id in
        <foreach item="item" index="index" open="(" separator="," close=")" collection="projectIds">
            #{item}
        </foreach>
        and cp.parent_id != 0
    </select>

    <select id="selectChildByCode" resultType="com.dianmi.crm.api.dto.CustProjectInfoDto">
        select
        cp.id id,
        cp.project_name projectName,
        cp.project_code projectCode,
        cp.customer_id customerId,
        CAST(cp.tax_rate AS decimal(14,2)) taxRate,
        c.customer_name customerName,
        c.customer_code customerCode,
        c.area_name areaName,
        c.area_code areaCode,
        cp.business_type_name businessTypeName,
        cp.business_type_code businessTypeCode,
        cp.sign_company_name signCompanyName,
        cp.service_status serviceStatus,
        cp.parent_id parentId
        from
        cust_project cp
        left join customer c on c.id = cp.customer_id
        where
        cp.project_code in
        <foreach item="item" index="index" open="(" separator="," close=")" collection="subProjectCodeList">
            #{item}
        </foreach>
        and cp.parent_id != 0
    </select>

</mapper>