<?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="org.xyzh.workcase.mapper.TbCustomerServiceMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.api.workcase.dto.TbCustomerServiceDTO">
        <id column="user_id" property="userId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <result column="user_code" property="userCode" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="VARCHAR"/>
        <result column="skill_tags" property="skillTags" jdbcType="ARRAY" typeHandler="org.xyzh.common.jdbc.handler.StringArrayTypeHandler"/>
        <result column="max_concurrent" property="maxConcurrent" jdbcType="INTEGER"/>
        <result column="current_workload" property="currentWorkload" jdbcType="INTEGER"/>
        <result column="total_served" property="totalServed" jdbcType="INTEGER"/>
        <result column="avg_response_time" property="avgResponseTime" jdbcType="INTEGER"/>
        <result column="satisfaction_score" property="satisfactionScore" jdbcType="NUMERIC"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <resultMap id="VOResultMap" type="org.xyzh.api.workcase.vo.CustomerServiceVO">
        <id column="user_id" property="userId" jdbcType="VARCHAR"/>
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <result column="user_code" property="userCode" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="VARCHAR"/>
        <result column="skill_tags" property="skillTags" jdbcType="ARRAY" typeHandler="org.xyzh.common.jdbc.handler.StringArrayTypeHandler"/>
        <result column="max_concurrent" property="maxConcurrent" jdbcType="INTEGER"/>
        <result column="current_workload" property="currentWorkload" jdbcType="INTEGER"/>
        <result column="total_served" property="totalServed" jdbcType="INTEGER"/>
        <result column="avg_response_time" property="avgResponseTime" jdbcType="INTEGER"/>
        <result column="satisfaction_score" property="satisfactionScore" jdbcType="NUMERIC"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <sql id="Base_Column_List">
        user_id, optsn, username, user_code, status, skill_tags, max_concurrent, current_workload,
        total_served, avg_response_time, satisfaction_score, creator, create_time, update_time,
        delete_time, deleted
    </sql>

    <insert id="insertCustomerService" parameterType="org.xyzh.api.workcase.dto.TbCustomerServiceDTO">
        INSERT INTO workcase.tb_customer_service (
            optsn, user_id, username, creator
            <if test="userCode != null">, user_code</if>
            <if test="status != null">, status</if>
            <if test="skillTags != null">, skill_tags</if>
            <if test="maxConcurrent != null">, max_concurrent</if>
            <if test="currentWorkload != null">, current_workload</if>
            <if test="totalServed != null">, total_served</if>
            <if test="avgResponseTime != null">, avg_response_time</if>
            <if test="satisfactionScore != null">, satisfaction_score</if>
        ) VALUES (
            #{optsn}, #{userId}, #{username}, #{creator}
            <if test="userCode != null">, #{userCode}</if>
            <if test="status != null">, #{status}</if>
            <if test="skillTags != null">, #{skillTags, typeHandler=org.xyzh.common.jdbc.handler.StringArrayTypeHandler}</if>
            <if test="maxConcurrent != null">, #{maxConcurrent}</if>
            <if test="currentWorkload != null">, #{currentWorkload}</if>
            <if test="totalServed != null">, #{totalServed}</if>
            <if test="avgResponseTime != null">, #{avgResponseTime}</if>
            <if test="satisfactionScore != null">, #{satisfactionScore}</if>
        )
    </insert>

    <update id="updateCustomerService" parameterType="org.xyzh.api.workcase.dto.TbCustomerServiceDTO">
        UPDATE workcase.tb_customer_service
        <set>
            <if test="username != null and username != ''">username = #{username},</if>
            <if test="userCode != null">user_code = #{userCode},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="skillTags != null">skill_tags = #{skillTags, typeHandler=org.xyzh.common.jdbc.handler.StringArrayTypeHandler},</if>
            <if test="maxConcurrent != null">max_concurrent = #{maxConcurrent},</if>
            <if test="currentWorkload != null">current_workload = #{currentWorkload},</if>
            <if test="totalServed != null">total_served = #{totalServed},</if>
            <if test="avgResponseTime != null">avg_response_time = #{avgResponseTime},</if>
            <if test="satisfactionScore != null">satisfaction_score = #{satisfactionScore},</if>
            update_time = now()
        </set>
        WHERE user_id = #{userId} AND deleted = false
    </update>

    <update id="deleteCustomerService" parameterType="org.xyzh.api.workcase.dto.TbCustomerServiceDTO">
        UPDATE workcase.tb_customer_service
        SET deleted = true, delete_time = now()
        WHERE user_id = #{userId} AND deleted = false
    </update>

    <select id="selectCustomerServiceById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_customer_service
        WHERE user_id = #{userId} AND deleted = false
    </select>

    <select id="selectCustomerServiceList" resultMap="VOResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_customer_service
        <where>
            <if test="filter.userId != null and filter.userId != ''">AND user_id = #{filter.userId}</if>
            <if test="filter.username != null and filter.username != ''">AND username LIKE CONCAT('%', #{filter.username}, '%')</if>
            <if test="filter.userCode != null and filter.userCode != ''">AND user_code = #{filter.userCode}</if>
            <if test="filter.status != null and filter.status != ''">AND status = #{filter.status}</if>
            AND deleted = false
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="selectCustomerServicePage" resultMap="VOResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM workcase.tb_customer_service
        <where>
            <if test="filter.userId != null and filter.userId != ''">AND user_id = #{filter.userId}</if>
            <if test="filter.username != null and filter.username != ''">AND username LIKE CONCAT('%', #{filter.username}, '%')</if>
            <if test="filter.userCode != null and filter.userCode != ''">AND user_code = #{filter.userCode}</if>
            <if test="filter.status != null and filter.status != ''">AND status = #{filter.status}</if>
            AND deleted = false
        </where>
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countCustomerServices" resultType="long">
        SELECT COUNT(*)
        FROM workcase.tb_customer_service
        <where>
            <if test="filter.userId != null and filter.userId != ''">AND user_id = #{filter.userId}</if>
            <if test="filter.username != null and filter.username != ''">AND username LIKE CONCAT('%', #{filter.username}, '%')</if>
            <if test="filter.userCode != null and filter.userCode != ''">AND user_code = #{filter.userCode}</if>
            <if test="filter.status != null and filter.status != ''">AND status = #{filter.status}</if>
            AND deleted = false
        </where>
    </select>

</mapper>
