<?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.ycwl.basic.mapper.ScenicAccountMapper">
    <insert id="add">
        insert into scenic_account(id, is_super, name, phone, account, password, is_active, create_time, update_time)
        values (#{id}, #{isSuper}, #{name}, #{phone}, #{account}, #{password}, #{isActive}, now(), now())
    </insert>
    <insert id="addAccountScenicRelation">
        insert into account_scenic(account_id, scenic_id, is_admin)
        values (#{accountId}, #{scenicId}, #{isAdmin})
    </insert>
    <update id="update">
        update scenic_account
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="phone != null and phone != ''">
                phone = #{phone},
            </if>
            <if test="account != null and account != ''">
                account = #{account},
            </if>
            <if test="password != null and password != ''">
                password = #{password},
            </if>
            <if test="isActive != null">
                is_active = #{isActive},
            </if>
            update_time = now()
        </set>
        where id = #{id}
    </update>
    <update id="updateStatus">
        update scenic_account
        set status = (CASE
                          WHEN status = 1 THEN
                              0
                          WHEN status = 0 THEN
                              1
                          ELSE null
                      END)
        where id = #{id}
    </update>
    <update id="updateActiveStatus">
        update scenic_account
        set is_active = (CASE
                          WHEN is_active = 1 THEN
                              0
                          WHEN is_active = 0 THEN
                              1
                          ELSE 1
                      END)
        where id = #{id}
    </update>
    <delete id="deleteById">
        delete from scenic_account where id = #{id}
    </delete>
    <delete id="deleteByScenicId">
        delete from scenic_account where scenic_id = #{scenicId}
    </delete>
    <delete id="deleteRelationByScenicId">
        delete from account_scenic where scenic_id = #{scenicId}
    </delete>
    <delete id="deleteRelationById">
        delete from account_scenic where account_id = #{accountId}
    </delete>
    <select id="getSuperAccountOfScenic"
            resultType="com.ycwl.basic.model.pc.scenic.entity.ScenicAccountEntity">
        select a.id, b.scenic_id, b.is_admin as isSuper, a.name, a.phone, a.account, a.password, a.status, a.is_active, a.create_time, a.update_time
        from scenic_account a left join account_scenic b on a.id = b.account_id
        where b.scenic_id = #{scenicId} and b.is_admin = 1
    </select>
    <select id="getByAccount" resultType="com.ycwl.basic.model.pc.scenic.entity.ScenicAccountEntity">
        select id, scenic_id, is_super, name, phone, account, password, status, is_active, create_time, update_time
        from scenic_account
        where account = #{account}
    </select>
    <select id="findAccountById" resultType="com.ycwl.basic.model.pc.scenic.entity.ScenicAccountEntity">
        select *
        from scenic_account
        where id = #{id}
    </select>
    <select id="pageQuery" resultType="com.ycwl.basic.model.pc.scenic.entity.ScenicAccountEntity">
        SELECT * FROM scenic_account
        <where>
            <if test="scenicId != null">
                AND id in (select account_id from account_scenic where scenic_id = #{scenicId})
            </if>
            <if test="account != null and account != ''">
                AND account LIKE CONCAT('%', #{account}, '%')
            </if>
            <if test="phone != null and phone != ''">
                AND phone LIKE CONCAT('%', #{phone}, '%')
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
            <if test="isSuper != null">
                AND is_super = #{isSuper}
            </if>
            <if test="startTime != null">
                AND create_time >= #{startTime}
            </if>
            <if test="endTime != null">
                AND create_time &lt;= #{endTime}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>
    <select id="getAccountRelations" resultType="java.lang.Long">
        SELECT scenic_id
        FROM account_scenic
        WHERE account_id = #{scenicId}
    </select>
</mapper>