<?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.task.mapper.AppointmentMapper">

    <resultMap type="com.ruoyi.ibs.task.domain.entity.AppointmentInfo" id="AppointmentInfoResult">
        <result property="id"    column="id"    />
        <result property="custIdc"    column="cust_idc"    />
        <result property="custName"    column="cust_name"    />
        <result property="belongUserId"    column="belong_user_id"    />
        <result property="startTime"    column="start_time"    />
        <result property="completeTime"    column="complete_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="prodCode"    column="prod_code"    />
        <result property="prodName"    column="prod_name"    />
        <result property="userName"    column="user_name"    />
        <result property="nickName"    column="nick_name"    />
    </resultMap>

    <sql id="selectAppointmentInfoVo">
        select id, cust_idc, cust_name, belong_user_id, start_time, complete_time, create_by, create_time, belong_user_id, prod_code, prod_name, user_name, nick_name, cust_type from appointment_info
    </sql>

    <select id="selectAppointmentByCustIdc" parameterType="AppointmentInfo" resultMap="AppointmentInfoResult">
        <include refid="selectAppointmentInfoVo"/>
        <where>
            <if test="custIdc != null and custIdc != ''">
                and cust_idc = #{custIdc}
            </if>
            and NOW() BETWEEN start_time AND complete_time
        </where>
	</select>

    <select id="selectUnClaimListByCustIdc" parameterType="AppointmentInfo" resultMap="AppointmentInfoResult">
        <include refid="selectAppointmentInfoVo"/>
        <where>
            <if test="custIdc != null and custIdc != ''">
                and cust_idc = #{custIdc}
            </if>
            and (belong_user_id is null or belong_user_id = '')
            and (prod_code is not null and prod_code != '')
            and (prod_name is not null and prod_name != '')
        </where>
    </select>

    <select id="selectAppointmentListByUserName" parameterType="AppointmentInfo" resultMap="AppointmentInfoResult">
        <include refid="selectAppointmentInfoVo"/>
        <where>
            <if test="userRole == 'branch' ">
                and dept_id = #{deptId}
            </if>
            <if test="userRole == 'outlet' ">
                and outlets_id = #{outletId}
            </if>
            <if test="userRole == 'manager' ">
                and user_name = #{userName}
            </if>
            <if test="userRole == 'public' ">
                and cust_type = '2'
            </if>
            <if test="userRole == 'private' ">
                and cust_type != '2'
            </if>
            and (prod_code is null or prod_code = '')
            and (prod_name is null or prod_name = '')
        </where>
        order by create_time
    </select>

    <select id="selectClaimListByUserName" parameterType="AppointmentInfo" resultMap="AppointmentInfoResult">
        select  cust_idc, cust_name, cust_type,
        complete_time, user_name, nick_name
        from appointment_info
        <where>
            <if test="userRole == 'branch' ">
                and dept_id = #{deptId}
            </if>
            <if test="userRole == 'outlet' ">
                and outlets_id = #{outletId}
            </if>
            <if test="userRole == 'manager' ">
                and user_name = #{userName}
            </if>
            <if test="userRole == 'public' ">
                and cust_type = '2'
            </if>
            <if test="userRole == 'private' ">
                and cust_type != '2'
            </if>
            and (user_name is not null and user_name != '')
            and (prod_code is not null and prod_code != '')
            and (prod_name is not null and prod_name != '')
        </where>
        group by cust_idc, cust_name, cust_type,complete_time, user_name, nick_name
        order by complete_time
    </select>

    <select id="selectDictValueByDictLabel" parameterType="String" resultType="String">
        select
        CASE
        WHEN count(1) &gt; 0
        then max(dict_value)
        ELSE '7'
        END AS dict_value
        FROM sys_dict_data
        <where>
            <if test="dictLabel != null and dictLabel != ''">
                and dict_label = #{dictLabel}
            </if>
            and dict_type = 'appointment_duration'
        </where>
    </select>

    <select id="selectAllProds" resultType="com.ruoyi.ibs.task.domain.vo.ProductInfoVO">
        select
            CONCAT(code_no_level1, '_', code_no_level2) AS prodCode,
            code_value_level2 AS prodName
        from
            cmpm_code
    </select>

    <select id="selectCustProdDetail" resultType="com.ruoyi.ibs.task.domain.vo.ProductInfoVO">
        select
            prod_code,
            prod_name
        from
            appointment_info
        <where>
            <if test="custIdc != null and custIdc != ''">
                and cust_idc = #{custIdc}
            </if>
            and (prod_code is not null and prod_code != '')
            and (prod_name is not null and prod_name != '')
        </where>
    </select>

    <select id="selectClaimCustDetail" resultType="com.ruoyi.ibs.task.domain.vo.ProductInfoVO">
        select
            prod_code,
            prod_name
        from
            appointment_info
        <where>
            <if test="userName != null and userName != ''">
                and user_name = #{userName}
            </if>
            <if test="custIdc != null and custIdc != ''">
                and cust_idc = #{custIdc}
            </if>
            and (prod_code is not null and prod_code != '')
            and (prod_name is not null and prod_name != '')
        </where>
    </select>

    <insert id="insertAppointment" parameterType="AppointmentInfo">
        insert into appointment_info(
        <if test="id != null and id != 0">id,</if>
        <if test="custIdc != null and custIdc != ''">cust_idc,</if>
        <if test="custName != null and custName != ''">cust_name,</if>
        <if test="custType != null and custType != ''">cust_type,</if>
        <if test="belongUserId != null and belongUserId != ''">belong_user_id,</if>
        <if test="startTime != null">start_time,</if>
        <if test="completeTime != null">complete_time,</if>
        <if test="prodCode != null">prod_code,</if>
        <if test="prodName != null">prod_name,</if>
        <if test="userName != null">user_name,</if>
        <if test="nickName != null">nick_name,</if>
        <if test="createBy != null">create_by,</if>
        <if test="deptId != null">dept_id,</if>
        <if test="outletId != null">outlets_id,</if>
        create_time
        )values(
        <if test="id != null and id != 0">#{id},</if>
        <if test="custIdc != null and custIdc != ''">#{custIdc},</if>
        <if test="custName != null and custName != ''">#{custName},</if>
        <if test="custType != null and custType != ''">#{custType},</if>
        <if test="belongUserId != null and belongUserId != ''">#{belongUserId},</if>
        <if test="startTime != null">#{startTime},</if>
        <if test="completeTime != null">#{completeTime},</if>
        <if test="prodCode != null">#{prodCode},</if>
        <if test="prodName != null">#{prodName},</if>
        <if test="userName != null">#{userName},</if>
        <if test="nickName != null">#{nickName},</if>
        <if test="createBy != null">#{createBy},</if>
        <if test="deptId != null">#{deptId},</if>
        <if test="outletId != null">#{outletId},</if>
        sysdate()
        )
    </insert>

    <update id="updateAppointment" parameterType="AppointmentInfo">
        update appointment_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="belongUserId != null and belongUserId != ''">belong_user_id = #{belongUserId},</if>
            <if test="outletId != null and outletId != ''">outlets_id = #{outletId},</if>
            <if test="deptId != null and deptId != ''">dept_id = #{deptId},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>
            complete_time = sysdate(),
        </trim>
        where cust_idc = #{custIdc}
        and (user_name is null or user_name = '')
    </update>

    <insert id="secondVisit">
        insert into second_visit(
        <if test="custId != null and custId != ''">cust_id,</if>
        <if test="custName != null and custName != ''">cust_name,</if>
        <if test="custType != null and custType != ''">cust_type,</if>
        <if test="fromUserName != null">from_user_name,</if>
        <if test="fromNickName != null">from_nick_name,</if>
        <if test="toUserName != null">to_user_name,</if>
        <if test="toNickName != null">to_nick_name,</if>
        <if test="secondVisitTime != null">second_visit_time,</if>
        <if test="applyRemark != null">apply_remark,</if>
        <if test="approvalRemark != null">approval_remark,</if>
        <if test="createBy != null">create_by,</if>
        create_time
        )values(
        <if test="custId != null and custId != ''">#{custId},</if>
        <if test="custName != null and custName != ''">#{custName},</if>
        <if test="custType != null and custType != ''">#{custType},</if>
        <if test="fromUserName != null">#{fromUserName},</if>
        <if test="fromNickName != null">#{fromNickName},</if>
        <if test="toUserName != null">#{toUserName},</if>
        <if test="toNickName != null">#{toNickName},</if>
        <if test="secondVisitTime != null">#{secondVisitTime},</if>
        <if test="applyRemark != null">#{applyRemark},</if>
        <if test="approvalRemark != null">#{approvalRemark},</if>
        <if test="createBy != null">#{createBy},</if>
        sysdate()
        )
    </insert>

    <update id="updateSecondVisitReadTime">
        update second_visit
        set read_time = sysdate()
        where from_user_name = #{userName} and second_visit_time is not null
    </update>

    <select id="selectTodaySecondVisits" resultType="SecondVisitVO">
        select id, cust_id, cust_name, cust_type, from_user_name, from_nick_name, second_visit_time, read_time
        from second_visit
        where second_visit_time is not null
        and from_user_name = #{userName}
        and second_visit_time = #{today}
    </select>

    <update id="updateResourceAssistReadTime">
        update second_visit
        set read_time = sysdate()
        where to_user_name = #{userName} and second_visit_time is null
    </update>

    <select id="selectResourceAssistList" resultType="SecondVisitVO">
        select id, cust_id, cust_name, cust_type, from_user_name, from_nick_name, to_user_name, to_nick_name, apply_remark, approval_remark, status, read_time, create_by
        from second_visit
        where second_visit_time is null
          and (from_user_name = #{userName} or to_user_name = #{userName})
    </select>

    <update id="resourceAssistFeedback">
        update second_visit
        set status = #{status},
            approval_remark = #{approvalRemark},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where id = #{id}
    </update>

</mapper>