<?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.fs.company.mapper.CompanyWxAccountMapper">

    <resultMap type="CompanyWxAccount" id="CompanyWxAccountResult">
        <result property="id"    column="id"    />
        <result property="wxNickName"    column="wx_nick_name"    />
        <result property="wxNo"    column="wx_no"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="createUser"    column="create_user"    />
        <result property="phone"    column="phone"    />
    </resultMap>

    <sql id="selectCompanyWxAccountVo">
        select * from company_wx_account
    </sql>

    <select id="selectCompanyWxAccountList" parameterType="CompanyWxAccount" resultType="CompanyWxAccount">
        select a.*,u.nick_name as companyUserName from company_wx_account a
        left join company_user u on a.company_user_id = u.user_id
        <where>
            <if test="wxNickName != null  and wxNickName != ''"> and a.wx_nick_name like concat( #{wxNickName}, '%')</if>
            <if test="phone != null  and phone != ''"> and a.phone like concat( #{phone}, '%')</if>
            <if test="wxNo != null  and wxNo != ''"> and a.wx_no = #{wxNo}</if>
            <if test="companyUserId != null "> and a.company_user_id = #{companyUserId}</if>
            <if test="createUser != null "> and a.create_user = #{createUser}</if>
        </where>
    </select>
    <select id="selectCompanyWxAccountListCompany" parameterType="CompanyWxAccount" resultMap="CompanyWxAccountResult">
        select a.*,u.nick_name as companyUserName from company_wx_account a
        inner join company_user u on a.company_user_id = u.user_id
        inner join company_dept d on u.dept_id = d.dept_id
        <where>
            <if test="wxNickName != null  and wxNickName != ''"> and a.wx_nick_name like concat( #{wxNickName}, '%')</if>
            <if test="phone != null  and phone != ''"> and a.phone like concat( #{phone}, '%')</if>
            <if test="wxNo != null  and wxNo != ''"> and a.wx_no = #{wxNo}</if>
            <if test="companyUserId != null "> and a.company_user_id = #{companyUserId}</if>
            <if test="createUser != null "> and a.create_user = #{createUser}</if>
            ${params.dataScope}
        </where>
    </select>

    <select id="selectCompanyWxAccountById" parameterType="Long" resultMap="CompanyWxAccountResult">
        <include refid="selectCompanyWxAccountVo"/>
        where id = #{id}
    </select>

    <insert id="insertCompanyWxAccount" parameterType="CompanyWxAccount" useGeneratedKeys="true" keyProperty="id">
        insert into company_wx_account
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="wxNickName != null">wx_nick_name,</if>
            <if test="phone != null">phone,</if>
            <if test="wxNo != null">wx_no,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createUser != null">create_user,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="wxNickName != null">#{wxNickName},</if>
            <if test="phone != null">#{phone},</if>
            <if test="wxNo != null">#{wxNo},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createUser != null">#{createUser},</if>
         </trim>
    </insert>

    <update id="updateCompanyWxAccount" parameterType="CompanyWxAccount">
        update company_wx_account
        <trim prefix="SET" suffixOverrides=",">
            <if test="wxNickName != null">wx_nick_name = #{wxNickName},</if>
            <if test="wxNo != null">wx_no = #{wxNo},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createUser != null">create_user = #{createUser},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCompanyWxAccountById" parameterType="Long">
        delete from company_wx_account where id = #{id}
    </delete>

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

    <select id="companyListAll" resultType="com.fs.company.domain.CompanyUser">
        select * from company_user where del_flag = 0 and status = 0
    </select>
    <select id="companyListAllCompany" resultType="com.fs.company.domain.CompanyUser">
        select u.* from company_user u
          inner join company_dept d on u.dept_id = d.dept_id
        where u.del_flag = 0 and u.status = 0
        ${params.dataScope}
    </select>
    <select id="selectByCompanyUserAndWxNo" resultType="com.fs.company.domain.CompanyWxAccount">
        select * from company_wx_account where company_user_id = #{userId} and wx_no = #{wxNo}
    </select>
</mapper>
