<?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.dxhy.core.dao.mailGather.EmailMaintainDao">

    <resultMap type="com.dxhy.core.model.mailGather.EmailMaintainVo" id="EmailMaintainMap">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="deptId" column="dept_id" jdbcType="VARCHAR"/>
        <result property="companyCode" column="company_code" jdbcType="VARCHAR"/>
        <result property="deptName" column="dept_name" jdbcType="VARCHAR"/>
        <result property="username" column="username" jdbcType="VARCHAR"/>
        <result property="userId" column="user_id" jdbcType="VARCHAR"/>
        <result property="emailAddress" column="email_address" jdbcType="VARCHAR"/>
        <result property="emailPassword" column="email_password" jdbcType="VARCHAR"/>
        <result property="emailUid" column="email_uid" jdbcType="VARCHAR"/>
        <result property="createUser" column="create_user" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="delStatus" column="del_status" jdbcType="VARCHAR"/>
        <result property="modifyTime" column="modify_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryAll" resultMap="EmailMaintainMap">
        select
          id, dept_id, company_code, dept_name, username, user_id, email_address, email_password, email_uid, create_time, del_status, modify_time
        from email_maintain
        where del_status = '0';
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="EmailMaintainMap" parameterType="java.util.Map">
        select
          id, dept_id, company_code, dept_name, username, user_id, email_address,email_password, create_time, del_status, modify_time
        from email_maintain
        <where>
            <if test="deptId != null and deptId != ''">
                and dept_id = #{deptId}
            </if>
            <if test="username != null and username != ''">
                and username = #{username}
            </if>
             <if test="userId != null and userId != ''">
                and user_id = #{userId}
            </if>
            <if test="createUser != null and createUser != ''">
                and create_user = #{createUser}
            </if>
        </where>
    </select>

    <!--统计总行数-->
    <select id="getEmailMaintainVo" resultMap="EmailMaintainMap">
        select count(1)
        from email_maintain
        <where>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
            <if test="deptId != null and deptId != ''">
                and dept_id = #{deptId}
            </if>
            <if test="companyCode != null and companyCode != ''">
                and company_code = #{companyCode}
            </if>
            <if test="deptName != null and deptName != ''">
                and dept_name = #{deptName}
            </if>
            <if test="username != null and username != ''">
                and username = #{username}
            </if>
            <if test="userId != null and userId != ''">
                and user_id = #{userId}
            </if>
            <if test="emailAddress != null and emailAddress != ''">
                and email_address = #{emailAddress}
            </if>
            <if test="emailPassword != null and emailPassword != ''">
                and email_password = #{emailPassword}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="delStatus != null and delStatus != ''">
                and del_status = #{delStatus}
            </if>
            <if test="modifyTime != null">
                and modify_time = #{modifyTime}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into email_maintain(id, dept_id, company_code, dept_name, username, user_id, email_address, email_password, create_user, create_time, del_status, modify_time)
        values (#{id}, #{deptId}, #{companyCode}, #{deptName}, #{username}, #{userId}, #{emailAddress}, #{emailPassword}, #{createUser}, #{createTime}, #{delStatus}, #{modifyTime})
    </insert>

    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into email_maintain(id, dept_id, company_code, dept_name, username, user_id, email_address, email_password,  create_user, create_time, del_status, modify_time)
        values
        <foreach collection="entities" item="entity" separator=",">
        (#{entity.id}, #{entity.deptId}, #{entity.companyCode}, #{entity.deptName}, #{entity.username}, #{entity.userId}, #{entity.emailAddress}, #{entity.emailPassword},#{entity.createUser}, #{entity.createTime}, #{entity.delStatus}, #{entity.modifyTime})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into email_maintain(dept_id, company_code, dept_name, username, user_id, email_address, email_password, create_time, del_status, modify_time)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.deptId}, #{entity.companyCode}, #{entity.deptName}, #{entity.username}, #{entity.userId}, #{entity.emailAddress}, #{entity.emailPassword}, #{entity.createTime}, #{entity.delStatus}, #{entity.modifyTime})
        </foreach>
        on duplicate key update
        dept_id = values(dept_id),
        company_code = values(company_code),
        dept_name = values(dept_name),
        username = values(username),
        user_id = values(user_id),
        email_address = values(email_address),
        email_password = values(email_password),
        create_time = values(create_time),
        del_status = values(del_status),
        modify_time = values(modify_time)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update email_maintain
        <set>
            <if test="deptId != null and deptId != ''">
                dept_id = #{deptId},
            </if>
             <if test="companyCode != null and companyCode != ''">
                 company_code = #{companyCode},
            </if>
            <if test="deptName != null and deptName != ''">
                dept_name = #{deptName},
            </if>
            <if test="username != null and username != ''">
                username = #{username},
            </if>
            <if test="userId != null and userId != ''">
                user_id = #{userId},
            </if>
            <if test="emailAddress != null and emailAddress != ''">
                email_address = #{emailAddress},
            </if>
            <if test="emailPassword != null and emailPassword != ''">
                email_password = #{emailPassword},
            </if>
            <if test="delStatus != null and delStatus != ''">
                del_status = #{delStatus},
            </if>
            <if test="emailUid != null and emailUid != ''">
                email_uid = #{emailUid},
            </if>
        </set>
        where id = #{id}
    </update>

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

    <select id="getUserName" resultType="java.lang.String">
        select username from sys_user where loginname = #{userCode}
    </select>

    <select id="getUserByUsername" resultType="java.lang.String">
        select code from sys_user where username = #{username}
    </select>

    <select id="selectByEmail" resultType="java.lang.Integer">
        select count(1) from email_maintain where email_address = #{email}
    </select>

</mapper>

