<?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.iamberry.wechat.service.mapper.AgentInfoMapper">

    <sql id="agentConfig">
        config_id configId,
        agent_id agentId,
        color_id colorId,
        config_amount configAmount,
        config_status configStatus,
        config_create_time configCreateTime
    </sql>
    <sql id="agentInfo">
      agent_id agentId,
      agent_name agentName,
      agent_addr agentAddr,
      agent_tel agentTel,
      agent_role_id agentRoleId,
      agent_create_date agentCreateDate,
      agent_status agentStatus,
      agent_openid agentOpenid
    </sql>

    <!-- 查询代理商列表 -->
    <select id="listAgentTooth" parameterType="AgentTooth" resultType="AgentTooth">
      select <include refid="agentInfo"/> from tb_iamberry_agent_agentinfo where agent_status = #{agentStatus}
    </select>

    <!-- 查询代理商配置金额集合 -->
    <select id="listAgentConfig" parameterType="AgentConfig" resultType="AgentConfig">
      select
        a.config_id configId,
        a.agent_id agentId,
        a.color_id colorId,
        a.config_amount configAmount,
        a.config_status configStatus,
        a.config_create_time configCreateTime,
        c.color_name colorName,
        p.product_name productName,
        i.agent_name agentName,
        i.agent_tel agentTel
      from
        tb_iamberry_agent_config a
        LEFT JOIN tb_iamberry_agent_agentinfo i ON a.agent_id = i.agent_id
        LEFT JOIN tb_iamberry_product_color c ON a.color_id = c.color_id
        LEFT JOIN tb_iamberry_product_info p ON c.color_product_id = p.product_id
        <where>
            <if test="configId != null and configId != ''">
                a.config_id = #{configId}
            </if>
            <if test="agentName != null and agentName != ''">
                and i.agent_name like CONCAT('%',#{agentName},'%')
            </if>
            <if test="agentTel != null and agentTel != ''">
                and i.agent_tel like CONCAT('%',#{agentTel},'%')
            </if>
            <if test="productName != null and productName != ''">
                and p.product_name like CONCAT('%',#{productName},'%')
            </if>
            <if test="colorName != null and colorName != ''">
                and c.color_name like CONCAT('%',#{colorName},'%')
            </if>
            <if test="agentId != null and agentId != ''">
                and a.agent_id = #{agentId}
            </if>
            <if test="configStatus != null">
                AND a.config_status = #{configStatus}
            </if>
            <if test="colorId != null and colorId != ''">
                and a.color_id = #{colorId}
            </if>
        </where>
        ORDER BY a.config_create_time DESC
        <if test="page!=null and page.pageSize>0 ">
            LIMIT ${page.recordBegin},${page.pageSize}
        </if>
    </select>

    <!-- 查询代理商配置金额数量 -->
    <select id="listAgentConfigCount" parameterType="AgentConfig" resultType="Integer">
        select
          count(a.config_id)
        from
        tb_iamberry_agent_config a
        LEFT JOIN tb_iamberry_agent_agentinfo i ON a.agent_id = i.agent_id
        LEFT JOIN tb_iamberry_product_color c ON a.color_id = c.color_id
        LEFT JOIN tb_iamberry_product_info p ON c.color_product_id = p.product_id
        <where>
            <if test="configId != null and configId != ''">
                a.config_id = #{configId}
            </if>
            <if test="agentName != null and agentName != ''">
                and i.agent_name like CONCAT('%',#{agentName},'%')
            </if>
            <if test="agentTel != null and agentTel != ''">
                and i.agent_tel like CONCAT('%',#{agentTel},'%')
            </if>
            <if test="productName != null and productName != ''">
                and p.product_name like CONCAT('%',#{productName},'%')
            </if>
            <if test="colorName != null and colorName != ''">
                and c.color_name like CONCAT('%',#{colorName},'%')
            </if>
            <if test="agentId != null and agentId != ''">
                and a.agent_id = #{agentId}
            </if>
            <if test="configStatus != null">
                AND a.config_status = #{configStatus}
            </if>
            <if test="colorId != null and colorId != ''">
                and a.color_id = #{colorId}
            </if>
        </where>
        ORDER BY a.config_create_time DESC
    </select>

    <!-- 批量新增代理商配置信息 -->
    <insert id="addAgentConfigList" parameterType="ArrayList">
        <selectKey resultType="int" keyProperty="configId" order="AFTER">
            SELECT
            LAST_INSERT_ID()
        </selectKey>
        INSERT INTO tb_iamberry_agent_config
        (
            agent_id,color_id,config_amount,
            config_status,config_create_time
        )
        VALUES
        <foreach collection="list" item="item" index="index" separator="," >
            (
                #{item.agentId},#{item.colorId},#{item.configAmount},
                #{item.configStatus},NOW()
            )
        </foreach>
    </insert>

    <!-- 修改代理商优惠价配置信息 -->
    <update id="updateAgentConfig" parameterType="AgentConfig">
        update
          tb_iamberry_agent_config
          <set>
              <if test="agentId != null and agentId != ''">
                  agent_id = #{agentId},
              </if>
              <if test="colorId != null and colorId != ''">
                  color_id = #{colorId},
              </if>
              <if test="configAmount != null and configAmount != ''">
                  config_amount = #{configAmount},
              </if>
              <if test="configStatus != null and configStatus != ''">
                  config_status = #{configStatus}
              </if>
          </set>
          <where>
              config_id = #{configId}
          </where>
    </update>

    <!-- 查询代理商配置金额 -->
    <select id="getAgentConfig" parameterType="AgentConfig" resultType="AgentConfig">
        select
        <include refid="agentConfig"/>
        from
        tb_iamberry_agent_config
        <where>
            <if test="agentId !=null and agentId !=''">
                agent_id = #{agentId}
            </if>
            <if test="colorId !=null and colorId !=''">
                AND color_id = #{colorId}
            </if>
            <if test="configStatus != null">
                AND config_status = #{configStatus}
            </if>
            <if test="configId !=null and configId !=''">
                AND config_id = #{configId}
            </if>
        </where>
    </select>

    <!-- 查询代理商配置金额 -->
    <select id="getAgentTooth" parameterType="AgentTooth" resultType="AgentTooth">
        SELECT
          <include refid="agentInfo"/>,
          user_head agentHead
        FROM
          tb_iamberry_agent_agentinfo
        LEFT JOIN tb_iamberry_user_userinfo ON agent_openid = user_openid
        <where>
            <if test="agentOpenid !=null and agentOpenid !=''">
                agent_openid = #{agentOpenid}
            </if>
            <if test="agentId !=null and agentId !=''">
                AND  agent_id = #{agentId}
            </if>
            <if test="agentTel != null and agentTel != ''">
                and agent_tel = #{agentTel}
            </if>
            <if test="agentStatus != null and agentStatus != ''">
                and agent_status = #{agentStatus}
            </if>
        </where>
    </select>

    <!-- 修改代理商信息 -->
    <update id="updateAgentTooth" parameterType="AgentTooth">
      update
        tb_iamberry_agent_agentinfo
      <set>
          <if test="agentName != null and agentName != ''">
              agent_name = #{agentName},
          </if>
          <if test="agentAddr != null and agentAddr != ''">
              agent_addr = #{agentAddr},
          </if>
          <if test="agentTel != null and agentTel != ''">
              agent_tel = #{agentTel},
          </if>
          <if test="agentRoleId != null and agentRoleId != ''">
              agent_role_id = #{agentRoleId},
          </if>
          <if test="agentStatus != null and agentStatus != ''">
              agent_status = #{agentStatus},
          </if>
          <if test="agentStatus != null and agentStatus != ''">
              agent_openid = #{agentOpenid}
          </if>
      </set>
      <where>
          <if test="agentId != null and agentId != ''">
              agent_id = #{agentId}
          </if>
          <if test="agentTel != null and agentTel != ''">
              and agent_tel = #{agentTel}
          </if>
      </where>
    </update>

    <!-- 激活代理商 -->
    <update id="updateAgentOpenId" parameterType="AgentTooth">
        update
          tb_iamberry_agent_agentinfo
        <set>
            <if test="agentOpenid != null and agentOpenid != ''">
                agent_openid = #{agentOpenid}
            </if>
        </set>
        <where>
              agent_tel = #{agentTel}
            <if test="agentStatus != null and agentStatus != ''">
              and agent_status = #{agentStatus}
            </if>
        </where>
    </update>


    <!--查询所有代理商 -->
    <select id="selectAllAgentinfo" parameterType="AgentTooth" resultType="AgentTooth">
        SELECT
        <include refid="agentInfo" />
        FROM
        tb_iamberry_agent_agentinfo
        <where>
            <if test="agentName != null">
                and agent_name LIKE CONCAT('%',#{agentName},'%')
            </if>
            <if test="agentTel!=null and agentTel!=''">
                and agent_tel LIKE CONCAT('%',#{agentTel},'%')
            </if>
            <if test="agentRoleId!=null and agentRoleId!=''">
                and agent_role_id = #{agentRoleId}
            </if>
            <if test="agentStatus!=null and agentStatus!=''">
                and agent_status = #{agentStatus}
            </if>
            <if test="agentOpenid!=null and agentOpenid!=''">
                and agent_openid = #{agentOpenid}
            </if>
        </where>
        order by agent_create_date desc
        <if test="page!=null and page.pageSize>0 ">
            LIMIT ${page.recordBegin},${page.pageSize}
        </if>
    </select>
    <!-- 查询所有物流信息（代理商赠送机器记录）总数 -->
    <select id="selectAllAgentinfoCount" parameterType="AgentTooth" resultType="Integer">
        SELECT
          count(*)
        FROM
        tb_iamberry_agent_agentinfo
        <where>
            <if test="agentName != null">
                and agent_name = #{agentName}
            </if>
            <if test="agentTel!=null and agentTel!=''">
                and agent_tel = #{agentTel}
            </if>
            <if test="agentRoleId!=null and agentRoleId!=''">
                and agent_role_id = #{agentRoleId}
            </if>
            <if test="agentStatus!=null and agentStatus!=''">
                and agent_status = #{agentStatus}
            </if>
        </where>
    </select>

    <!--新增代理商 -->
    <insert id="addAgentinfo" parameterType="AgentTooth" >
        INSERT INTO tb_iamberry_agent_agentinfo(
          agent_name,
          agent_addr,
          agent_tel,
          agent_role_id,
          agent_create_date,
          agent_status,
          agent_openid
        )
        VALUES
        (
          #{agentName} ,
          #{agentAddr},
          #{agentTel},
          #{agentRoleId},
          NOW(),
          #{agentStatus},
          #{agentOpenid}
        )
    </insert>
</mapper>