<?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.pj.project4sp.admin.SpAdminMapper">


    <!-- 增 -->
    <insert id="add">
		insert into
		sp_admin(name, customer_id,avatar, phone, role_id, create_by_aid, create_time)
		values (#{name},#{customerId}, #{avatar}, #{phone}, #{roleId}, #{createByAid}, now())
	</insert>
    <update id="setRole">
		update sp_admin set role_id=#{roleId} where id=#{id}
	</update>
    <update id="updateCustomerAdmin">
		update sp_admin set status=#{status} where customer_id=#{customerId}
	</update>
    <update id="updateOpenid">
			update sp_admin set openid=#{openid} where id=#{id}
	</update>
    <update id="updateAdmin">
        update sp_admin set name=#{name},role_id=#{roleId},status=#{status} where id=#{id}
    </update>


    <!-- 删 -->
    <delete id="delete">
		delete from sp_admin where id = #{id} 
	</delete>


    <!-- 改 -->
    <delete id="update">
		update sp_admin set 
		name = #{name} 
		where id = #{id} 
	</delete>
    <delete id="removeByCustomerId">
		delete  from sp_admin where customer_id=#{customerId}
	</delete>

    <!-- 自己改自己 -->
    <!-- <delete id="updateBy">
        update sp_admin set
        name = #{name}
        where id = #{id}
    </delete> -->

    <!-- =================== 查询相关 =================== -->

    <!-- 通用映射 -->
    <resultMap id="model" type="com.pj.project4sp.admin.SpAdmin">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="avatar" column="avatar"/>
        <result property="password" column="password"/>
        <result property="pw" column="pw"/>
        <result property="phone" column="phone"/>
        <result property="roleId" column="role_id"/>
        <result property="status" column="status"/>
        <result property="createByAid" column="create_by_aid"/>
        <result property="createTime" column="create_time"/>
        <result property="loginTime" column="login_time"/>
        <result property="loginIp" column="login_ip"/>
        <result property="loginCount" column="login_count"/>
        <result property="roleName" column="role_name"/>
    </resultMap>

    <!-- 查询sql -->
    <sql id="select_sql">
		select *,
		(select name from sp_role where id = sp_admin.role_id) as role_name 
		from sp_admin 
	</sql>


    <!-- 查询，根据id -->
    <select id="getById" resultMap="model">
        <include refid="select_sql"></include>
        where id = #{id}
    </select>

    <!-- 查询，根据名称 -->
    <select id="getByName" resultMap="model">
        <include refid="select_sql"></include>
        where name = #{name}
    </select>

    <!-- 查询，根据名称 -->
    <select id="getByPhone" resultMap="model">
        <include refid="select_sql"></include>
        where phone = #{phone}
    </select>

    <!-- 查询，根据customerId -->
    <select id="getByCostomerId" resultMap="model">
        <include refid="select_sql"></include>
        where customer_id = #{customerId}
    </select>


    <!-- 查询 -->
    <select id="getList" resultMap="model">
        <include refid="select_sql"></include>
        where customer_id=#{customerId}
        <if test=' this.has("id")  '>and id = #{id}</if>
        <if test=' this.has("name")  '>and name like concat('%', #{name}, '%')</if>
        <if test=' this.has("roleId")  '>and role_id = #{roleId}</if>
        order by
        <choose>
            <when test='sort_type == 0'>id desc</when>
            <when test='sort_type == 1'>id asc</when>
            <when test='sort_type == 2'>login_time desc</when>
            <when test='sort_type == 3'>login_count desc</when>
            <otherwise>id desc</otherwise>
        </choose>
    </select>
    <select id="findByOpenid" resultType="com.pj.project4sp.admin.SpAdmin">
        <include refid="select_sql"></include>
        where openid = #{openid}
    </select>
    <select id="findByCustomerId" resultType="com.pj.project4sp.admin.SpAdmin">
        <include refid="select_sql"></include>
        where customer_id = #{customerId}
    </select>


</mapper>
