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

    <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, sign_flag, record_id from appointment_info
    </sql>

    <select id="selectAppointmentByCustIdc" parameterType="AppointmentInfo" resultType="AppointmentInfo">
        <include refid="selectAppointmentInfoVo"/>
        <where>
            <if test="custIdc != null and custIdc != ''">
                and cust_idc = #{custIdc}
            </if>
                and NOW() &lt; complete_time
        </where>
	</select>

    <select id="selectUnClaimListByCustIdc" parameterType="AppointmentInfo" resultType="AppointmentInfo">
        <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" resultType="AppointmentInfo">
        <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" resultType="AppointmentInfo">
        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="appointmentCustListByUserNameFinish" parameterType="AppointmentInfo" resultType="AppointmentInfo">
        <include refid="selectAppointmentInfoVo"/>
        <where>
            <if test="userName != null and userName != '' ">
                and user_name = #{userName}
            </if>
            and (prod_code is null or prod_code = '')
            and (prod_name is null or prod_name = '')
            <if test="finishFlag == 'true' ">
                and record_id is not null
            </if>
            <if test="finishFlag == 'false' ">
                and NOW() &lt; complete_time
                and (record_id is null or record_id = '')
            </if>
        </where>
        order by create_time
    </select>

    <select id="appointmentCustListByUnCompleted" parameterType="AppointmentInfo" resultType="AppointmentInfo">
        <include refid="selectAppointmentInfoVo"/>
        <where>
            <if test="userName != null and userName != '' ">
                and user_name = #{userName}
            </if>
            and (prod_code is null or prod_code = '')
            and (prod_name is null or prod_name = '')
            and NOW() &lt; complete_time
        </where>
        order by create_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
        <set>
            <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>
            <if test="signFlag != null and signFlag != ''">sign_flag = #{signFlag},</if>
            <if test="recordId != null and recordId != ''">record_id = #{recordId},</if>
            <if test="completeTime != null">complete_time = #{completeTime},</if>
        </set>
        <where>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
            <if test="custIdc != null and custIdc != ''">
                and cust_idc = #{custIdc}
            </if>
        </where>
    </update>


</mapper>