<?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.tmerclub.cloud.supplier.mapper.SupplierUserMapper">
  <resultMap id="BaseResultMap" type="com.tmerclub.cloud.supplier.model.SupplierUser">
    <!--@mbg.generated-->
    <!--@Table supplier_user-->
    <id column="supplier_user_id" property="supplierUserId" />
    <result column="create_time" property="createTime" />
    <result column="update_time" property="updateTime" />
    <result column="supplier_id" property="supplierId" />
    <result column="nick_name" property="nickName" />
    <result column="code" property="code" />
    <result column="phone_num" property="phoneNum" />
    <result column="has_account" property="hasAccount"/>
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    supplier_user_id, create_time, update_time, supplier_id, nick_name,`code`,phone_num,has_account
  </sql>

  <select id="getByUserId" resultType="com.tmerclub.cloud.supplier.vo.SupplierUserVO">
    select supplier_user_id,nick_name,`code`,phone_num,has_account,supplier_id  from supplier_user where supplier_user_id = #{userId}
  </select>
  <select id="listBySupplierId" resultType="com.tmerclub.cloud.supplier.vo.SupplierUserVO">
    select supplier_user_id,nick_name,`code`,phone_num,has_account from supplier_user
    <where>
      supplier_id = #{supplierUser.supplierId}
      <if test="supplierUser.nickName != null and supplierUser.nickName != ''">
        and nick_name like concat('%', #{supplierUser.nickName}, '%')
      </if>
      <if test="supplierUser.phoneNum != null and supplierUser.phoneNum != ''">
        and phone_num like concat('%', #{supplierUser.phoneNum}, '%')
      </if>
    </where>
    order by supplier_user_id
  </select>
  <insert id="save">
    insert into `supplier_user` ( `supplier_user_id`, `supplier_id`, `nick_name`, `code`, `phone_num`,has_account)
    values (#{supplierUser.supplierUserId},#{supplierUser.supplierId},#{supplierUser.nickName},#{supplierUser.code},#{supplierUser.phoneNum}, #{supplierUser.hasAccount});
  </insert>
  <update id="update">
    update supplier_user
    <set>
      <if test="supplierUser.nickName != null">
        nick_name = #{supplierUser.nickName},
      </if>
      <if test="supplierUser.code != null">
        code = #{supplierUser.code},
      </if>
      <if test="supplierUser.phoneNum != null">
        phone_num = #{supplierUser.phoneNum},
      </if>
      <if test="supplierUser.hasAccount != null">
        has_account = #{supplierUser.hasAccount},
      </if>
    </set>
    where supplier_user_id = #{supplierUser.supplierUserId} and supplier_id = #{supplierUser.supplierId}
  </update>

  <update id="updateSupplierIdBySupplierUserId">
    UPDATE supplier_user SET supplier_id = #{supplierId} WHERE supplier_user_id = #{supplierUserId} LIMIT 1
  </update>

    <delete id="deleteById">
    delete  from supplier_user where supplier_user_id = #{supplierUserId}
  </delete>

  <select id="getUserIdBySupplierId" resultType="java.lang.Long">
    SELECT supplier_user_id FROM supplier_user WHERE supplier_id = #{supplierId} HAVING MAX(create_time) ORDER BY supplier_user_id ASC
  </select>

  <select id="countAccoutByMobile" resultType="int">
    select ifnull(count(*),0) from `supplier_user` where phone_num = #{mobile} and has_account = 1
  </select>
</mapper>
