<?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.dgtly.system.mapper.AlertUpvoteLogMapper">
    
    <resultMap type="AlertUpvoteLog" id="AlertUpvoteLogResult">
        <result property="alertUpvoteId"    column="alert_upvote_id"    />
        <result property="alertId"    column="alert_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDelete"    column="is_delete"    />
        <result property="userId"    column="user_id"    />
        <result property="status"    column="status"    />
        <result property="loginName"    column="login_name"    />
        <result property="userName"    column="user_name"    />
        <result property="sapEmployeeId"    column="alert_title"    />
        <result property="postName"    column="post_name"    />
        <result property="alertContent"    column="alert_content"    />
        <result property="photoUrl"    column="photo_url"    />
        <result property="statusName"    column="statusName"    />
        <result property="alertTitle"    column="alert_title"    />
        <result property="companyName"    column="company_name"    />
        <result property="officeName"    column="office_name"    />
        <result property="subOfficeName"    column="sub_office_name"    />

    </resultMap>

    <sql id="selectAlertUpvoteLogVo">
        select alert_upvote_id, alert_id, create_by, create_time, update_by, update_time, is_delete, user_id, status,login_name,user_name,alert_content,photo_url from alert_upvote_log
    </sql>

    <select id="selectAlertUpvoteLogList"  resultType="com.dgtly.system.domain.AlertUpvoteLog">
        select alert_upvote_id as alertUpvoteId,
               alert_id as alertId,
               create_time as createTime,
               DATE_FORMAT(update_time, '%Y-%m-%d %H:%i:%s') as updateTimeStr,
               user_id as userId,
               status as status,
               login_name as loginName,
               user_name as userName,
               sap_employee_id as sapEmployeeId,
               post_name as postName,
               alert_content as alertContent,
               photo_url as photoUrl,
               alert_title as alertTitle,
               company_name as companyName,
               office_name as officeName,
               sub_office_name as subOfficeName,
               CASE
                WHEN status = '0' THEN '无'
                WHEN status = '1' THEN '赞'
                WHEN status = '2' THEN '踩'
                END statusName
        from alert_upvote_log
         where status != '0'
            <if test="userName != null and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="loginName != null and loginName != '' "> and login_name like concat('%', #{loginName}, '%')</if>
            <if test="alertContent != null and alertContent != '' "> and alert_content like concat('%', #{alertContent}, '%')</if>
            <if test="status != null"> and status = #{status}</if>
            <if test="startTime != null "> and create_time &gt;= #{startTime}</if>
            <if test="endTime != null "> and create_time &lt; #{endTime}</if>
        order by create_time desc
    </select>
    
    <select id="selectAlertUpvoteLogById" parameterType="Long" resultMap="AlertUpvoteLogResult">
        <include refid="selectAlertUpvoteLogVo"/>
        where alert_upvote_id = #{alertUpvoteId}
    </select>
        
    <insert id="insertAlertUpvoteLog" parameterType="AlertUpvoteLog" useGeneratedKeys="true" keyProperty="alertUpvoteId">
        insert into alert_upvote_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="alertId != null ">alert_id,</if>
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="updateTime != null ">update_time,</if>
            <if test="isDelete != null ">is_delete,</if>
            <if test="userId != null ">user_id,</if>
            <if test="status != null ">status,</if>
            <if test="alertTitle != null  and alertTitle != ''">alert_title,</if>
            <if test="alertContent != null ">alert_content,</if>
            <if test="photoUrl != null ">photo_url,</if>
            <if test="loginName != null  and loginName != ''">login_name,</if>
            <if test="userName != null  and userName != ''">user_name,</if>
            <if test="postName != null  and postName != ''">post_name,</if>
            <if test="sapEmployeeId != null  and sapEmployeeId != ''">sap_employee_id,</if>
            <if test="salesLevel != null  and salesLevel != ''">sales_level,</if>
            <if test="companyName != null  and companyName != ''">company_name,</if>
            <if test="officeName != null  and officeName != ''">office_name,</if>
            <if test="subOfficeName != null  and subOfficeName != ''">sub_office_name,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="alertId != null ">#{alertId},</if>
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null ">#{updateTime},</if>
            <if test="isDelete != null ">#{isDelete},</if>
            <if test="userId != null ">#{userId},</if>
            <if test="status != null ">#{status},</if>
            <if test="alertTitle != null  and alertTitle != ''">#{alertTitle},</if>
            <if test="alertContent != null ">#{alertContent},</if>
            <if test="photoUrl != null ">#{photoUrl},</if>
            <if test="loginName != null  and loginName != ''">#{loginName},</if>
            <if test="userName != null  and userName != ''">#{userName},</if>
            <if test="postName != null  and postName != ''">#{postName},</if>
            <if test="sapEmployeeId != null  and sapEmployeeId != ''">#{sapEmployeeId},</if>
            <if test="salesLevel != null  and salesLevel != ''">#{salesLevel},</if>
            <if test="companyName != null  and companyName != ''">#{companyName},</if>
            <if test="officeName != null  and officeName != ''">#{officeName},</if>
            <if test="subOfficeName != null  and subOfficeName != ''">#{subOfficeName},</if>
         </trim>
    </insert>

    <update id="updateAlertUpvoteLog" parameterType="AlertUpvoteLog">
        update alert_upvote_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="alertId != null ">alert_id = #{alertId},</if>
            <if test="createBy != null  and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null ">update_time = #{updateTime},</if>
            <if test="isDelete != null ">is_delete = #{isDelete},</if>
            <if test="userId != null ">user_id = #{userId},</if>
            <if test="status != null ">status = #{status},</if>
            <if test="alertTitle != null  and alertTitle != ''">alert_title = #{alertTitle},</if>
            <if test="alertContent != null ">alert_content = #{alertContent},</if>
            <if test="photoUrl != null ">photo_url = #{photoUrl},</if>
            <if test="loginName != null  and loginName != ''">login_name = #{loginName},</if>
            <if test="userName != null  and userName != ''">user_name = #{userName},</if>
            <if test="postName != null  and postName != ''">post_name = #{postName},</if>
            <if test="sapEmployeeId != null  and sapEmployeeId != ''">sap_employee_id = #{sapEmployeeId},</if>
            <if test="salesLevel != null  and salesLevel != ''">sales_level = #{salesLevel},</if>
            <if test="companyName != null  and companyName != ''">company_name = #{companyName},</if>
            <if test="officeName != null  and officeName != ''">office_name = #{officeName},</if>
            <if test="subOfficeName != null  and subOfficeName != ''">sub_office_name = #{subOfficeName},</if>
        </trim>
        where alert_upvote_id = #{alertUpvoteId}
    </update>

    <delete id="deleteAlertUpvoteLogById" parameterType="Long">
        delete from alert_upvote_log where alert_upvote_id = #{alertUpvoteId}
    </delete>

    <delete id="deleteAlertUpvoteLogByIds" parameterType="String">
        delete from alert_upvote_log where alert_upvote_id in 
        <foreach item="alertUpvoteId" collection="array" open="(" separator="," close=")">
            #{alertUpvoteId}
        </foreach>
    </delete>

    <select id="selectUpvoteByUidAndAid" parameterType="Long" resultMap="AlertUpvoteLogResult">
        <include refid="selectAlertUpvoteLogVo"/>
        where user_id = #{userId} and alert_id = #{alertId}
    </select>

    <select id="selectMasterNameByCode" resultType="com.dgtly.system.domain.AlertUpvoteLog">
        select
            sales_company_name AS companyName,
            sales_office_name AS officeName,
            sales_group_name AS subOfficeName
        from data_master_customer where 1=1
        <if test="customerCode !=null and customerCode !='' ">and CustomerCode =#{customerCode}</if>
        <if test="salesOfficeCode !=null and salesOfficeCode !='' ">and sales_office =#{salesOfficeCode}</if>
        <if test="salesGroupCode !=null and salesGroupCode !='' ">and sales_group =#{salesGroupCode}</if>
        limit 1
    </select>
</mapper>