<?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.ruoyi.ibs.cmpm.mapper.VisitCustReachMapper">
    <select id="queryVisitCust" resultType="VisitCustReachVO">
        select gcr.cust_id, gcr.cust_name, gcr.cust_idn, gcr.mobile,
        vcr.id, vcr.tel_plan_date, vcr.tel_plan_remark, vcr.tel_summary_date, vcr.tel_summary_remark, vcr.visit_plan_date, vcr.visit_plan_remark, vcr.visit_summary_date,
        vcr.visit_summary_remark
        from grid_cmpm_retail gcr
        left join (select * from visit_cust_reach where submit_flag = '0' and create_by = #{userName} ) vcr on gcr.cust_id = vcr.cust_id
        where
        (
            gcr.user_name = #{userName}
            or
            (gcr.user_name is null and gcr.branch_id is null)
            <if test="branchId != null">
                or
                (gcr.user_name is null and gcr.branch_id = #{branchId})
            </if>
        )
        <if test="custName != null and custName != ''">
            and gcr.cust_name = #{custName}
        </if>
    </select>

    <select id="queryVisitCustReachList" resultType="VisitCustReachListVO">
        select vcr.*, concat(su.nick_name,'-', vcr.create_by) as nick_name, (vcr.create_by = #{editUserName} and submit_flag = 0) as edit_flag
        from visit_cust_reach vcr
        left join sys_user su on su.user_name = vcr.create_by
        where 1=1
        <if test="aliasUserName != null and aliasUserName != ''">
            and vcr.create_by = #{aliasUserName}
        </if>
        <if test="userName != null and userName != ''">
            and vcr.create_by like concat('%',#{userName},'%')
        </if>
        <if test="custName != null and custName != ''">
            and vcr.cust_name like concat('%',#{custName},'%')
        </if>
        <if test="deptId != null">
            and vcr.dept_id in (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors))
        </if>
        order by vcr.create_time desc
    </select>

    <select id="queryVisitCustReachListByCustId" resultType="VisitCustReachListVO">
        select vcr.*, concat(su.nick_name,'-', vcr.create_by) as nick_name
        from visit_cust_reach vcr
        left join sys_user su on su.user_name = vcr.create_by
        where vcr.cust_id = #{custId}
        order by vcr.create_time desc
    </select>

</mapper>