<?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.chaozhanggui.dao.system.dao.TbPlussUserInfoMapper">
  <resultMap id="BaseResultMap" type="com.chaozhanggui.dao.system.entity.TbPlussUserInfo">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="loginName" jdbcType="VARCHAR" property="loginname" />
    <result column="trueName" jdbcType="VARCHAR" property="truename" />
    <result column="provinceCode" jdbcType="VARCHAR" property="provincecode" />
    <result column="cityCode" jdbcType="VARCHAR" property="citycode" />
    <result column="sex" jdbcType="VARCHAR" property="sex" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="creator" jdbcType="VARCHAR" property="creator" />
    <result column="createTime" jdbcType="TIMESTAMP" property="createtime" />
    <result column="updator" jdbcType="VARCHAR" property="updator" />
    <result column="updateTime" jdbcType="TIMESTAMP" property="updatetime" />
    <result column="version" jdbcType="VARCHAR" property="version" />
    <result column="parentId" jdbcType="BIGINT" property="parentid" />
    <result column="areaId" jdbcType="INTEGER" property="areaid" />
    <result column="areaCode" jdbcType="VARCHAR" property="areacode" />
    <result column="source" jdbcType="VARCHAR" property="source" />
    <result column="merchantCode" jdbcType="VARCHAR" property="merchantcode" />
    <result column="storeId" jdbcType="VARCHAR" property="storeid" />
    <result column="payPassword" jdbcType="VARCHAR" property="paypassword" />
    <result column="faceCert" jdbcType="VARCHAR" property="facecert" />
    <result column="faceCompare" jdbcType="VARCHAR" property="facecompare" />
    <result column="openId" jdbcType="VARCHAR" property="openid" />
    <result column="channelCode" jdbcType="VARCHAR" property="channelcode" />
  </resultMap>
  <sql id="Base_Column_List">
    id, loginName, trueName, provinceCode, cityCode, sex, phone, email, status, password, 
    creator, createTime, updator, updateTime, version, parentId, areaId, areaCode, source, 
    merchantCode, storeId, payPassword, faceCert, faceCompare, openId, channelCode
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from tb_pluss_user_info
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from tb_pluss_user_info
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="com.chaozhanggui.dao.system.entity.TbPlussUserInfo" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
    insert into tb_pluss_user_info (id, loginName, trueName, 
      provinceCode, cityCode, sex, 
      phone, email, status, 
      password, creator, createTime, 
      updator, updateTime, version, 
      parentId, areaId, areaCode, 
      source, merchantCode, storeId, 
      payPassword, faceCert, faceCompare, 
      openId, channelCode)
    values (#{id,jdbcType=BIGINT}, #{loginname,jdbcType=VARCHAR}, #{truename,jdbcType=VARCHAR}, 
      #{provincecode,jdbcType=VARCHAR}, #{citycode,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, 
      #{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, 
      #{password,jdbcType=VARCHAR}, #{creator,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, 
      #{updator,jdbcType=VARCHAR}, #{updatetime,jdbcType=TIMESTAMP}, #{version,jdbcType=VARCHAR}, 
      #{parentid,jdbcType=BIGINT}, #{areaid,jdbcType=INTEGER}, #{areacode,jdbcType=VARCHAR}, 
      #{source,jdbcType=VARCHAR}, #{merchantcode,jdbcType=VARCHAR}, #{storeid,jdbcType=VARCHAR}, 
      #{paypassword,jdbcType=VARCHAR}, #{facecert,jdbcType=VARCHAR}, #{facecompare,jdbcType=VARCHAR}, 
      #{openid,jdbcType=VARCHAR}, #{channelcode,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.chaozhanggui.dao.system.entity.TbPlussUserInfo">
    insert into tb_pluss_user_info
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="loginname != null">
        loginName,
      </if>
      <if test="truename != null">
        trueName,
      </if>
      <if test="provincecode != null">
        provinceCode,
      </if>
      <if test="citycode != null">
        cityCode,
      </if>
      <if test="sex != null">
        sex,
      </if>
      <if test="phone != null">
        phone,
      </if>
      <if test="email != null">
        email,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="creator != null">
        creator,
      </if>
      <if test="createtime != null">
        createTime,
      </if>
      <if test="updator != null">
        updator,
      </if>
      <if test="updatetime != null">
        updateTime,
      </if>
      <if test="version != null">
        version,
      </if>
      <if test="parentid != null">
        parentId,
      </if>
      <if test="areaid != null">
        areaId,
      </if>
      <if test="areacode != null">
        areaCode,
      </if>
      <if test="source != null">
        source,
      </if>
      <if test="merchantcode != null">
        merchantCode,
      </if>
      <if test="storeid != null">
        storeId,
      </if>
      <if test="paypassword != null">
        payPassword,
      </if>
      <if test="facecert != null">
        faceCert,
      </if>
      <if test="facecompare != null">
        faceCompare,
      </if>
      <if test="openid != null">
        openId,
      </if>
      <if test="channelcode != null">
        channelCode,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="loginname != null">
        #{loginname,jdbcType=VARCHAR},
      </if>
      <if test="truename != null">
        #{truename,jdbcType=VARCHAR},
      </if>
      <if test="provincecode != null">
        #{provincecode,jdbcType=VARCHAR},
      </if>
      <if test="citycode != null">
        #{citycode,jdbcType=VARCHAR},
      </if>
      <if test="sex != null">
        #{sex,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="creator != null">
        #{creator,jdbcType=VARCHAR},
      </if>
      <if test="createtime != null">
        #{createtime,jdbcType=TIMESTAMP},
      </if>
      <if test="updator != null">
        #{updator,jdbcType=VARCHAR},
      </if>
      <if test="updatetime != null">
        #{updatetime,jdbcType=TIMESTAMP},
      </if>
      <if test="version != null">
        #{version,jdbcType=VARCHAR},
      </if>
      <if test="parentid != null">
        #{parentid,jdbcType=BIGINT},
      </if>
      <if test="areaid != null">
        #{areaid,jdbcType=INTEGER},
      </if>
      <if test="areacode != null">
        #{areacode,jdbcType=VARCHAR},
      </if>
      <if test="source != null">
        #{source,jdbcType=VARCHAR},
      </if>
      <if test="merchantcode != null">
        #{merchantcode,jdbcType=VARCHAR},
      </if>
      <if test="storeid != null">
        #{storeid,jdbcType=VARCHAR},
      </if>
      <if test="paypassword != null">
        #{paypassword,jdbcType=VARCHAR},
      </if>
      <if test="facecert != null">
        #{facecert,jdbcType=VARCHAR},
      </if>
      <if test="facecompare != null">
        #{facecompare,jdbcType=VARCHAR},
      </if>
      <if test="openid != null">
        #{openid,jdbcType=VARCHAR},
      </if>
      <if test="channelcode != null">
        #{channelcode,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.chaozhanggui.dao.system.entity.TbPlussUserInfo">
    update tb_pluss_user_info
    <set>
      <if test="loginname != null">
        loginName = #{loginname,jdbcType=VARCHAR},
      </if>
      <if test="truename != null">
        trueName = #{truename,jdbcType=VARCHAR},
      </if>
      <if test="provincecode != null">
        provinceCode = #{provincecode,jdbcType=VARCHAR},
      </if>
      <if test="citycode != null">
        cityCode = #{citycode,jdbcType=VARCHAR},
      </if>
      <if test="sex != null">
        sex = #{sex,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=INTEGER},
      </if>
      <if test="password != null">
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="creator != null">
        creator = #{creator,jdbcType=VARCHAR},
      </if>
      <if test="createtime != null">
        createTime = #{createtime,jdbcType=TIMESTAMP},
      </if>
      <if test="updator != null">
        updator = #{updator,jdbcType=VARCHAR},
      </if>
      <if test="updatetime != null">
        updateTime = #{updatetime,jdbcType=TIMESTAMP},
      </if>
      <if test="version != null">
        version = #{version,jdbcType=VARCHAR},
      </if>
      <if test="parentid != null">
        parentId = #{parentid,jdbcType=BIGINT},
      </if>
      <if test="areaid != null">
        areaId = #{areaid,jdbcType=INTEGER},
      </if>
      <if test="areacode != null">
        areaCode = #{areacode,jdbcType=VARCHAR},
      </if>
      <if test="source != null">
        source = #{source,jdbcType=VARCHAR},
      </if>
      <if test="merchantcode != null">
        merchantCode = #{merchantcode,jdbcType=VARCHAR},
      </if>
      <if test="storeid != null">
        storeId = #{storeid,jdbcType=VARCHAR},
      </if>
      <if test="paypassword != null">
        payPassword = #{paypassword,jdbcType=VARCHAR},
      </if>
      <if test="facecert != null">
        faceCert = #{facecert,jdbcType=VARCHAR},
      </if>
      <if test="facecompare != null">
        faceCompare = #{facecompare,jdbcType=VARCHAR},
      </if>
      <if test="openid != null">
        openId = #{openid,jdbcType=VARCHAR},
      </if>
      <if test="channelcode != null">
        channelCode = #{channelcode,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.chaozhanggui.dao.system.entity.TbPlussUserInfo">
    update tb_pluss_user_info
    set loginName = #{loginname,jdbcType=VARCHAR},
      trueName = #{truename,jdbcType=VARCHAR},
      provinceCode = #{provincecode,jdbcType=VARCHAR},
      cityCode = #{citycode,jdbcType=VARCHAR},
      sex = #{sex,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      status = #{status,jdbcType=INTEGER},
      password = #{password,jdbcType=VARCHAR},
      creator = #{creator,jdbcType=VARCHAR},
      createTime = #{createtime,jdbcType=TIMESTAMP},
      updator = #{updator,jdbcType=VARCHAR},
      updateTime = #{updatetime,jdbcType=TIMESTAMP},
      version = #{version,jdbcType=VARCHAR},
      parentId = #{parentid,jdbcType=BIGINT},
      areaId = #{areaid,jdbcType=INTEGER},
      areaCode = #{areacode,jdbcType=VARCHAR},
      source = #{source,jdbcType=VARCHAR},
      merchantCode = #{merchantcode,jdbcType=VARCHAR},
      storeId = #{storeid,jdbcType=VARCHAR},
      payPassword = #{paypassword,jdbcType=VARCHAR},
      faceCert = #{facecert,jdbcType=VARCHAR},
      faceCompare = #{facecompare,jdbcType=VARCHAR},
      openId = #{openid,jdbcType=VARCHAR},
      channelCode = #{channelcode,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="selectByLoginName" resultMap="BaseResultMap">
    select * from tb_pluss_user_info where loginName=#{loginName}
    </select>

  <select id="selectIndexData" resultType="java.util.Map">

    SELECT
      t.sumConsumeFee,
      m.yestedayConsumeFee,
      p.sumfansShareMoney,
      p.yestedayShareMoney,
      p.current_fee AS currentFee
    FROM
      (
        SELECT
          sum( o.consumeFee ) AS sumConsumeFee,
          101 AS id
        FROM
          (
            SELECT
              p.user_id,
              p.current_fee,
              p.type_code,
              p.parent_user_id,
              p.is_extend
            FROM
              (
                SELECT
                  @ids AS _ids,
                  ( SELECT @ids := GROUP_CONCAT( user_id ) FROM tb_pluss_user_promotion WHERE FIND_IN_SET( parent_user_id, @ids ) ) AS cids,
              @l := @l + 1 AS LEVEL
            FROM
              tb_pluss_user_promotion,
              ( SELECT @ids := #{userId}, @l := 0 ) b
            WHERE
              @ids IS NOT NULL
          ) ID,
          tb_pluss_user_promotion p
        WHERE
          FIND_IN_SET( p.user_id, ID._ids )
        ORDER BY
          user_id
      ) d
        LEFT JOIN view_base_order o ON d.user_id = o.userId
      ) t
	LEFT JOIN (
	SELECT
		sum( IFNULL( o.consumeFee, 0 ) ) AS yestedayConsumeFee,
		101 AS id
	FROM
		(
		SELECT
			p.user_id,
			p.current_fee,
			p.type_code,
			p.parent_user_id,
			p.is_extend
		FROM
			(
			SELECT
				@ids AS _ids,
				( SELECT @ids := GROUP_CONCAT( user_id ) FROM tb_pluss_user_promotion WHERE FIND_IN_SET( parent_user_id, @ids ) ) AS cids,
				@l := @l + 1 AS LEVEL
			FROM
				tb_pluss_user_promotion,
				( SELECT @ids := #{userId}, @l := 0 ) b
			WHERE
				@ids IS NOT NULL
			) ID,
			tb_pluss_user_promotion p
		WHERE
			FIND_IN_SET( p.user_id, ID._ids )
		ORDER BY
			user_id
		) d
		LEFT JOIN view_base_order o ON d.user_id = o.userId
	WHERE
	date_format( `o`.`createDt`, '%Y-%m-%d' ) = date_format(( curdate() - INTERVAL 0 DAY ), '%Y-%m-%d' )) m ON t.id = m.id
      JOIN ( SELECT * FROM view_merchant_profit_sum WHERE userId = #{userId} ) p
  </select>

  <select id="selectOutFlow" resultType="java.util.Map">
    SELECT
      v.id,
      v.userId,
      v.userName,
      v.merchantCode,
      v.accountNo,
      v.accountName,
      p.type_code as typeCode,
      v.cashAmt,
      v.`status`,
      v.createDt,
    v.cashStatus

    FROM
      (
        SELECT
          @ids AS _ids,
          ( SELECT @ids := GROUP_CONCAT( user_id ) FROM tb_pluss_user_promotion WHERE FIND_IN_SET( parent_user_id, @ids ) ) AS cids,
      @l := @l + 1 AS LEVEL
    FROM
      tb_pluss_user_promotion,
      ( SELECT @ids := #{userId}, @l := 0 ) b
    WHERE
      @ids IS NOT NULL
      ) ID,
      tb_pluss_user_promotion p  LEFT JOIN tb_pluss_cash v on p.user_id=v.userId
    WHERE
      FIND_IN_SET( p.user_id, ID._ids )
      and v.userId!=#{userId}
      and v.id is not null
    <if test="userName != null and userName!=''">
      and v.userName=#{userName}
    </if>

    <if test="merchantCode != null and merchantCode!=''">
      and v.merchantCode=#{merchantCode}
    </if>

    <if test="status != null and status!=''">
      and v.status=#{status}
    </if>
    order by v.id desc

    </select>


  <select id="selectProfitByDay" resultType="java.util.Map">

    SELECT
      d.a AS times,
      IFNULL( k.price, 0 ) AS price
    FROM
      (
        SELECT
          date_sub( curdate( ), INTERVAL 7 DAY ) AS a UNION
        SELECT
          date_sub( curdate( ), INTERVAL 6 DAY ) AS a UNION
        SELECT
          date_sub( curdate( ), INTERVAL 5 DAY ) AS a UNION
        SELECT
          date_sub( curdate( ), INTERVAL 4 DAY ) AS a UNION
        SELECT
          date_sub( curdate( ), INTERVAL 3 DAY ) AS a UNION
        SELECT
          date_sub( curdate( ), INTERVAL 2 DAY ) AS a UNION
        SELECT
          date_sub( curdate( ), INTERVAL 1 DAY ) AS a
        FROM
          DUAL
      ) d
        LEFT JOIN (
        SELECT
          o.recordDate,
          sum( o.price ) AS price
        FROM
          tb_pluss_merchant_profit o
        WHERE
            o.userId IN (
            SELECT
              p.user_id
            FROM
              (
                SELECT
                  @ids AS _ids,
                  ( SELECT @ids := GROUP_CONCAT( user_id ) FROM tb_pluss_user_promotion WHERE FIND_IN_SET( parent_user_id, @ids ) ) AS cids,
              @l := @l + 1 AS LEVEL
        FROM
          tb_pluss_user_promotion,
          ( SELECT @ids := #{userId}, @l := 0 ) b
        WHERE
          @ids IS NOT NULL
      ) ID,
      tb_pluss_user_promotion p
    WHERE
      FIND_IN_SET( p.user_id, ID._ids )
    ORDER BY
      user_id
      )
    GROUP BY
      o.recordDate
      ) k ON d.a = k.recordDate
  </select>

  <select id="selectProfitByMonth" resultType="java.util.Map">
    SELECT
      f.mymonth,
      IFNULL( d.price, 0 ) AS price
    FROM
        ( SELECT CONCAT( #{year}, '-', `code` ) AS mymonth FROM tb_pluss_month ) f
          LEFT JOIN (
        SELECT
          DATE_FORMAT( o.recordDate, '%Y-%m' ) AS recordDate,
          sum( o.price ) AS price
        FROM
          tb_pluss_merchant_profit o
        WHERE
            o.userId IN (
            SELECT
              p.user_id
            FROM
              (
                SELECT
                  @ids AS _ids,
                  ( SELECT @ids := GROUP_CONCAT( user_id ) FROM tb_pluss_user_promotion WHERE FIND_IN_SET( parent_user_id, @ids ) ) AS cids,
              @l := @l + 1 AS LEVEL
        FROM
          tb_pluss_user_promotion,
          ( SELECT @ids := #{userId}, @l := 0 ) b
        WHERE
          @ids IS NOT NULL
      ) ID,
        tb_pluss_user_promotion p
    WHERE
      FIND_IN_SET( p.user_id, ID._ids )
    ORDER BY
      user_id
      )
		AND YEAR ( o.recordDate )= #{year}
    GROUP BY
      DATE_FORMAT( o.recordDate, '%Y-%m' )) d ON f.mymonth = d.recordDate
    ORDER BY
      f.mymonth ASC
  </select>

  <select id="selectOrderByMonth" resultType="java.util.Map">


    SELECT
      f.mymonth,
      IFNULL(d.consumeFee,0)  as consumeFee
    FROM
        ( SELECT CONCAT( #{year}, '-', `code` ) AS mymonth FROM tb_pluss_month ) f
          LEFT JOIN (
        SELECT
          DATE_FORMAT( o.createDate, '%Y-%m' ) AS createDate,
          sum( o.consumeFee ) AS consumeFee
        FROM
          view_base_order o
        WHERE
            o.userId IN (
            SELECT
              p.user_id
            FROM
              (
                SELECT
                  @ids AS _ids,
                  ( SELECT @ids := GROUP_CONCAT( user_id ) FROM tb_pluss_user_promotion WHERE FIND_IN_SET( parent_user_id, @ids ) ) AS cids,
              @l := @l + 1 AS LEVEL
        FROM
          tb_pluss_user_promotion,
          ( SELECT @ids := #{userId}, @l := 0 ) b
        WHERE
          @ids IS NOT NULL
      ) ID,
        tb_pluss_user_promotion p
    WHERE
      FIND_IN_SET( p.user_id, ID._ids )
    ORDER BY
      user_id
      )
		AND YEAR ( o.createDate )= #{year}
    GROUP BY
      DATE_FORMAT( o.createDate, '%Y-%m' )) d ON f.mymonth = d.createDate
    order by f.mymonth asc
  </select>

  <select id="selectOrderByDay" resultType="java.util.Map">
    SELECT
      d.a AS times,
      IFNULL( k.consumeFee, 0 ) AS consumeFee
    FROM
      (
        SELECT
          date_sub( curdate( ), INTERVAL 7 DAY ) AS a UNION
        SELECT
          date_sub( curdate( ), INTERVAL 6 DAY ) AS a UNION
        SELECT
          date_sub( curdate( ), INTERVAL 5 DAY ) AS a UNION
        SELECT
          date_sub( curdate( ), INTERVAL 4 DAY ) AS a UNION
        SELECT
          date_sub( curdate( ), INTERVAL 3 DAY ) AS a UNION
        SELECT
          date_sub( curdate( ), INTERVAL 2 DAY ) AS a UNION
        SELECT
          date_sub( curdate( ), INTERVAL 1 DAY ) AS a
        FROM
          DUAL
      ) d
        LEFT JOIN (
        SELECT
          o.createDate,
          sum( o.consumeFee ) AS consumeFee
        FROM
          tb_pluss_merchant_order o
            LEFT JOIN tb_pluss_merchant_base_info b ON o.merchantCode = b.merchantCode
        WHERE
          o.`status` = 1
          AND b.userId IN (
          SELECT
            p.user_id
          FROM
            (
              SELECT
                @ids AS _ids,
                ( SELECT @ids := GROUP_CONCAT( user_id ) FROM tb_pluss_user_promotion WHERE FIND_IN_SET( parent_user_id, @ids ) ) AS cids,
            @l := @l + 1 AS LEVEL
        FROM
          tb_pluss_user_promotion,
          ( SELECT @ids := #{userId}, @l := 0 ) b
        WHERE
          @ids IS NOT NULL
      ) ID,
      tb_pluss_user_promotion p
    WHERE
      FIND_IN_SET( p.user_id, ID._ids )
    ORDER BY
      user_id
      )
    GROUP BY
      o.createDate
      ) k ON d.a = k.createDate

  </select>
</mapper>