<?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.ylx.massage.mapper.AlarmMapper">

    <sql id="AlarmColumns">
        id, order_no, merchant_id, merchant_name, merchant_nick_name, merchant_sex, merchant_avatar,
        merchant_phone, address, alarm_status, note, create_by, create_time, update_by, update_time, is_delete
    </sql>

    <resultMap type="com.ylx.massage.domain.Alarm" id="YlxAlarmMap">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="orderNo" column="order_no" jdbcType="VARCHAR"/>
        <result property="merchantId" column="merchant_id" jdbcType="VARCHAR"/>
        <result property="merchantName" column="merchant_name" jdbcType="VARCHAR"/>
        <result property="merchantNickName" column="merchant_nick_name" jdbcType="VARCHAR"/>
        <result property="merchantSex" column="merchant_sex" jdbcType="INTEGER"/>
        <result property="merchantPhone" column="merchant_phone" jdbcType="VARCHAR"/>
        <result property="address" column="address" jdbcType="VARCHAR"/>
        <result property="alarmStatus" column="alarm_status" jdbcType="INTEGER"/>
        <result property="note" column="note" jdbcType="VARCHAR"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="isDelete" column="is_delete" jdbcType="INTEGER"/>
    </resultMap>

    <select id="selectAlarmPage" resultType="com.ylx.massage.domain.vo.AlarmListVO">
        SELECT
            a.id AS id,
            a.order_no AS orderNo,
            b.te_name AS teName,
            b.te_nick_name AS teNickName,
            b.te_sex AS teSex,
            b.avatar AS avatar,
            b.te_phone AS tePhone,
            a.address AS address,
            a.create_time AS createTime
        FROM t_alarm a
        JOIN ma_technician b ON a.merchant_id = b.id
        <where>
            a.is_delete = 0
            AND b.is_delete = 0
            <!--商户姓名-->
            <if test="alarm.merchantName != null and alarm.merchantName != ''">
                AND b.te_name LIKE concat('%', #{alarm.merchantName}, '%')
            </if>
            <!--商户昵称-->
            <if test="alarm.merchantNickName != null and alarm.merchantNickName != ''">
                AND b.te_nick_name LIKE concat('%', #{alarm.merchantNickName}, '%')
            </if>
            <!--商户性别-->
            <if test="alarm.merchantSex != null">
                AND b.te_sex = #{alarm.merchantSex}
            </if>
            <!--商户电话-->
            <if test="alarm.merchantPhone != null and alarm.merchantPhone != ''">
                AND b.te_phone LIKE concat('%', #{alarm.merchantPhone}, '%')
            </if>
            <!--告警状态-->
            <if test="alarm.alarmStatus != null">
                AND a.alarm_status = #{alarm.alarmStatus}
            </if>
            <!--申请开始时间-->
            <if test="alarm.beginTime != null and alarm.beginTime != ''">
                AND a.create_time &gt;= #{alarm.beginTime}
            </if>
            <!--申请结束时间-->
            <if test="alarm.endTime != null and alarm.endTime != ''">
                AND a.create_time &lt;= #{alarm.endTime}
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>

    <!--通过主键ID查询详情-->
    <select id="queryById" resultType="com.ylx.massage.domain.vo.AlarmListVO">
        SELECT
        a.id AS id,
        a.order_no AS orderNo,
        b.te_name AS teName,
        b.te_nick_name AS teNickName,
        b.te_sex AS teSex,
        b.avatar AS avatar,
        b.te_phone AS tePhone,
        a.address AS address,
        a.note AS note,
        a.create_time AS createTime
        FROM t_alarm a
        JOIN ma_technician b ON a.merchant_id = b.id
        <where>
            a.is_delete = 0
            AND b.is_delete = 0
            and a.id = #{id}
        </where>
        ORDER BY a.create_time DESC
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="YlxAlarmMap">
        select <include refid="AlarmColumns"/>
        from t_alarm
        <where>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
            <if test="orderNo != null and orderNo != ''">
                and order_no = #{orderNo}
            </if>
            <if test="merchantId != null and merchantId != ''">
                and merchant_id = #{merchantId}
            </if>
            <if test="merchantName != null and merchantName != ''">
                and merchant_name = #{merchantName}
            </if>
            <if test="merchantNickName != null and merchantNickName != ''">
                and merchant_nick_name = #{merchantNickName}
            </if>
            <if test="merchantSex != null">
                and merchant_sex = #{merchantSex}
            </if>
            <if test="merchantPhone != null and merchantPhone != ''">
                and merchant_phone = #{merchantPhone}
            </if>
            <if test="address != null and address != ''">
                and address = #{address}
            </if>
            <if test="alarmStatus != null">
                and alarm_status = #{alarmStatus}
            </if>
            <if test="isDelete != null">
                and is_delete = #{isDelete}
            </if>
        </where>
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from t_alarm
        <where>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
            <if test="orderNo != null and orderNo != ''">
                and order_no = #{orderNo}
            </if>
            <if test="merchantId != null and merchantId != ''">
                and merchant_id = #{merchantId}
            </if>
            <if test="merchantName != null and merchantName != ''">
                and merchant_name = #{merchantName}
            </if>
            <if test="merchantNickName != null and merchantNickName != ''">
                and merchant_nick_name = #{merchantNickName}
            </if>
            <if test="merchantSex != null">
                and merchant_sex = #{merchantSex}
            </if>
            <if test="merchantPhone != null and merchantPhone != ''">
                and merchant_phone = #{merchantPhone}
            </if>
            <if test="address != null and address != ''">
                and address = #{address}
            </if>
            <if test="alarmStatus != null">
                and alarm_status = #{alarmStatus}
            </if>
            <if test="isDelete != null">
                and is_delete = #{isDelete}
            </if>
        </where>
    </select>

    <!--批量新增-->
    <insert id="insertBatch">
        insert into t_alarm
        (<include refid="AlarmColumns"/>)
        values
        <foreach collection="entities" item="entity" separator=",">
            (
            #{entity.id}, #{entity.orderNo}, #{entity.merchantId}, #{entity.merchantName},
            #{entity.merchantNickName}, #{entity.merchantSex}, #{entity.merchantAvatar},
            #{entity.merchantPhone}, #{entity.address}, #{entity.alarmStatus}, #{entity.note},
            #{entity.createBy}, #{entity.createTime}, #{entity.updateBy}, #{entity.updateTime}, #{entity.isDelete}
            )
        </foreach>
    </insert>

    <!--批量新增或按主键更新-->
    <insert id="insertOrUpdateBatch">
        insert into t_alarm
        (<include refid="AlarmColumns"/>)
        values
        <foreach collection="entities" item="entity" separator=",">
            (
            #{entity.id}, #{entity.orderNo}, #{entity.merchantId}, #{entity.merchantName},
            #{entity.merchantNickName}, #{entity.merchantSex}, #{entity.merchantAvatar},
            #{entity.merchantPhone}, #{entity.address}, #{entity.alarmStatus}, #{entity.note},
            #{entity.createBy}, #{entity.createTime}, #{entity.updateBy}, #{entity.updateTime}, #{entity.isDelete}
            )
        </foreach>
        on duplicate key update
            order_no = values(order_no),
            merchant_id = values(merchant_id),
            merchant_name = values(merchant_name),
            merchant_nick_name = values(merchant_nick_name),
            merchant_sex = values(merchant_sex),
            merchant_avatar = values(merchant_avatar),
            merchant_phone = values(merchant_phone),
            address = values(address),
            alarm_status = values(alarm_status),
            note = values(note),
            create_by = values(create_by),
            create_time = values(create_time),
            update_by = values(update_by),
            update_time = values(update_time),
            is_delete = values(is_delete)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update t_alarm
        <set>
            <if test="orderNo != null and orderNo != ''">
                order_no = #{orderNo},
            </if>
            <if test="merchantId != null and merchantId != ''">
                merchant_id = #{merchantId},
            </if>
            <if test="merchantName != null and merchantName != ''">
                merchant_name = #{merchantName},
            </if>
            <if test="merchantNickName != null and merchantNickName != ''">
                merchant_nick_name = #{merchantNickName},
            </if>
            <if test="merchantSex != null">
                merchant_sex = #{merchantSex},
            </if>
            <if test="merchantAvatar != null and merchantAvatar != ''">
                merchant_avatar = #{merchantAvatar},
            </if>
            <if test="merchantPhone != null and merchantPhone != ''">
                merchant_phone = #{merchantPhone},
            </if>
            <if test="address != null and address != ''">
                address = #{address},
            </if>
            <if test="alarmStatus != null">
                alarm_status = #{alarmStatus},
            </if>
            <if test="note != null and note != ''">
                note = #{note},
            </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>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from t_alarm
        where id = #{id}
    </delete>

</mapper>
