<?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.gxwebsoft.credit.mapper.CreditMpCustomerMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*, u.nickname AS nickname, u.avatar AS avatar, u.phone AS phone, u.real_name AS realName
        FROM credit_mp_customer a
        LEFT JOIN gxwebsoft_core.sys_user u ON u.user_id = a.user_id
        <where>
            <if test="param.id != null">
                AND a.id = #{param.id}
            </if>
            <if test="param.toUser != null">
                AND a.to_user LIKE CONCAT('%', #{param.toUser}, '%')
            </if>
            <if test="param.price != null">
                AND a.price LIKE CONCAT('%', #{param.price}, '%')
            </if>
            <if test="param.years != null">
                AND a.years LIKE CONCAT('%', #{param.years}, '%')
            </if>
            <if test="param.url != null">
                AND a.url LIKE CONCAT('%', #{param.url}, '%')
            </if>
            <if test="param.statusTxt != null">
                AND a.status_txt LIKE CONCAT('%', #{param.statusTxt}, '%')
            </if>
            <if test="param.companyId != null">
                AND a.company_id = #{param.companyId}
            </if>
            <if test="param.province != null">
                AND a.province LIKE CONCAT('%', #{param.province}, '%')
            </if>
            <if test="param.city != null">
                AND a.city LIKE CONCAT('%', #{param.city}, '%')
            </if>
            <if test="param.region != null">
                AND a.region LIKE CONCAT('%', #{param.region}, '%')
            </if>
            <if test="param.files != null">
                AND a.files LIKE CONCAT('%', #{param.files}, '%')
            </if>
            <if test="param.hasData != null">
                AND a.has_data = #{param.hasData}
            </if>
            <if test="param.comments != null">
                AND a.comments LIKE CONCAT('%', #{param.comments}, '%')
            </if>
            <if test="param.recommend != null">
                AND a.recommend = #{param.recommend}
            </if>
            <if test="param.sortNumber != null">
                AND a.sort_number = #{param.sortNumber}
            </if>
            <if test="param.status != null">
                AND a.status = #{param.status}
            </if>
            <if test="param.deleted != null">
                AND a.deleted = #{param.deleted}
            </if>
            <if test="param.deleted == null">
                AND a.deleted = 0
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.step != null">
                AND a.step = #{param.step}
            </if>
            <if test="param.createTimeStart != null">
                AND a.create_time &gt;= #{param.createTimeStart}
            </if>
            <if test="param.createTimeEnd != null">
                AND a.create_time &lt;= #{param.createTimeEnd}
            </if>
            <if test="param.keywords != null">
                AND (a.comments LIKE CONCAT('%', #{param.keywords}, '%')
                  OR a.to_user LIKE CONCAT('%', #{param.keywords}, '%')
                )
              </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="selectPageRel" resultType="com.gxwebsoft.credit.entity.CreditMpCustomer">
        <include refid="selectSql"></include>
    </select>

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.credit.entity.CreditMpCustomer">
        <include refid="selectSql"></include>
    </select>

    <!-- 获取待审核的跟进步骤列表 -->
    <select id="selectPendingApprovalSteps" resultType="com.gxwebsoft.credit.vo.PendingApprovalStepVO">
        SELECT 
            c.id as customerId,
            c.to_user as customerName,
            1 as step,
            '案件受理' as stepTitle,
            c.follow_step1_submitted_at as submittedAt,
            u.real_name as submittedBy,
            c.comments as content
        FROM credit_mp_customer c
        LEFT JOIN sys_user u ON c.user_id = u.user_id
        WHERE c.follow_step1_submitted = 1 
        AND c.follow_step1_approved = 0 
        AND c.deleted = 0
        
        UNION ALL
        
        SELECT 
            c.id as customerId,
            c.to_user as customerName,
            2 as step,
            '材料准备' as stepTitle,
            c.follow_step2_submitted_at as submittedAt,
            u.real_name as submittedBy,
            c.comments as content
        FROM credit_mp_customer c
        LEFT JOIN sys_user u ON c.user_id = u.user_id
        WHERE c.follow_step2_submitted = 1 
        AND c.follow_step2_approved = 0 
        AND c.deleted = 0
        
        UNION ALL
        
        SELECT 
            c.id as customerId,
            c.to_user as customerName,
            3 as step,
            '案件办理' as stepTitle,
            c.follow_step3_submitted_at as submittedAt,
            u.real_name as submittedBy,
            c.comments as content
        FROM credit_mp_customer c
        LEFT JOIN sys_user u ON c.user_id = u.user_id
        WHERE c.follow_step3_submitted = 1 
        AND c.follow_step3_approved = 0 
        AND c.deleted = 0
        
        UNION ALL
        
        SELECT 
            c.id as customerId,
            c.to_user as customerName,
            4 as step,
            '送达签收' as stepTitle,
            c.follow_step4_submitted_at as submittedAt,
            u.real_name as submittedBy,
            c.comments as content
        FROM credit_mp_customer c
        LEFT JOIN sys_user u ON c.user_id = u.user_id
        WHERE c.follow_step4_submitted = 1 
        AND c.follow_step4_approved = 0 
        AND c.deleted = 0
        
        UNION ALL
        
        SELECT 
            c.id as customerId,
            c.to_user as customerName,
            5 as step,
            '合同签订' as stepTitle,
            c.follow_step5_submitted_at as submittedAt,
            u.real_name as submittedBy,
            c.follow_step5_contracts as content
        FROM credit_mp_customer c
        LEFT JOIN sys_user u ON c.user_id = u.user_id
        WHERE c.follow_step5_submitted = 1 
        AND c.follow_step5_approved = 0 
        AND c.deleted = 0
        
        UNION ALL
        
        SELECT 
            c.id as customerId,
            c.to_user as customerName,
            6 as step,
            '订单回款' as stepTitle,
            c.follow_step6_submitted_at as submittedAt,
            u.real_name as submittedBy,
            c.follow_step6_expected_payments as content
        FROM credit_mp_customer c
        LEFT JOIN sys_user u ON c.user_id = u.user_id
        WHERE c.follow_step6_submitted = 1 
        AND c.follow_step6_approved = 0 
        AND c.deleted = 0
        
        UNION ALL
        
        SELECT 
            c.id as customerId,
            c.to_user as customerName,
            7 as step,
            '电话回访' as stepTitle,
            c.follow_step7_submitted_at as submittedAt,
            u.real_name as submittedBy,
            c.follow_step7_visit_records as content
        FROM credit_mp_customer c
        LEFT JOIN sys_user u ON c.user_id = u.user_id
        WHERE c.follow_step7_submitted = 1 
        AND c.follow_step7_approved = 0 
        AND c.deleted = 0
        
        <if test="param.step != null">
            HAVING step = #{param.step}
        </if>
        <if test="param.customerId != null">
            HAVING customerId = #{param.customerId}
        </if>
        <if test="param.userId != null">
            HAVING customerId IN (SELECT id FROM credit_mp_customer WHERE user_id = #{param.userId})
        </if>
        ORDER BY submittedAt DESC
    </select>

</mapper>
