<?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.yami.shop.distribution.comment.dao.DistributionProdMapper">
  <resultMap id="BaseResultMap" type="com.yami.shop.distribution.comment.model.DistributionProd">
    <id column="distribution_prod_id" jdbcType="BIGINT" property="distributionProdId" />
    <result column="shop_id" jdbcType="BIGINT" property="shopId" />
    <result column="prod_id" jdbcType="BIGINT" property="prodId" />
    <result column="award_id" jdbcType="DOUBLE" property="awardId" />
    <result column="state" jdbcType="TINYINT" property="state" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="modifier" jdbcType="VARCHAR" property="modifier" />
    <result column="default_reward" jdbcType="TINYINT" property="defaultReward" />
  </resultMap>

  <resultMap id="DistributionProd_ProdCut_DistributionAward" type="com.yami.shop.distribution.comment.model.DistributionProd">
    <id column="distribution_prod_id" jdbcType="BIGINT" property="distributionProdId" />
    <result column="shop_id" jdbcType="BIGINT" property="shopId" />
    <result column="prod_id" jdbcType="BIGINT" property="prodId" />
    <result column="state" jdbcType="TINYINT" property="state" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="modifier" jdbcType="VARCHAR" property="modifier" />
    <result column="default_reward" jdbcType="TINYINT" property="defaultReward" />
    <result column="award_proportion" jdbcType="TINYINT" property="awardProportion" />
    <result column="award_number_set" jdbcType="TINYINT" property="awardNumberSet" />
    <result column="award_numbers" jdbcType="VARCHAR" property="awardNumbers" />
    <result column="parent_award_numbers" jdbcType="VARCHAR" property="parentAwardNumbers" />
    <result column="parent_award_set" jdbcType="TINYINT" property="parentAwardSet" />
    <association javaType="com.yami.shop.bean.model.Product" property="product">
      <id column="prod_id" jdbcType="BIGINT" property="prodId" />
      <result column="prod_name" jdbcType="VARCHAR" property="prodName" />
      <result column="pic" jdbcType="VARCHAR" property="pic" />
    </association>
  </resultMap>

    <select id="distributionProdsPage" resultMap="DistributionProd_ProdCut_DistributionAward">
        select
            p.prod_id,p.prod_name,p.pic,
            dp.distribution_prod_id,
            dp.shop_id,
            dp.award_id,
            dp.state,
            dp.update_time,
            dp.modifier,
            dp.default_reward,
            dp.award_proportion,
            dp.award_number_set,
            dp.award_numbers,
            dp.parent_award_numbers,
            dp.parent_award_set
        from
        tz_distribution_prod dp
        join tz_prod p on p.prod_id=dp.prod_id and p.`status` = 1
        <where>
          <if test="distributionProd.shopId != null ">
           and  dp.shop_id=#{distributionProd.shopId}
          </if>
          <if test="distributionProd.state != null ">
            and  dp.state=#{distributionProd.state}
          </if>
          <if test="prodName!= null and prodName!='' ">
          and prod_name LIKE CONCAT('%',#{prodName},'%')
          </if>
        </where>
        ORDER BY dp.update_time DESC
    </select>



    <!--分销商品接口-->
    <resultMap id="DistributionProdPO" type="com.yami.shop.distribution.comment.po.DistributionProdPO">
        <result column="shop_id" property="shopId"/>
        <result column="prod_name" property="prodName"/>
        <result column="prod_id" property="prodId"/>
        <result column="pic" property="pic"/>
        <result column="price" property="price"/>
        <result column="award_proportion" jdbcType="TINYINT" property="awardProportion" />
        <result column="award_number_set" jdbcType="TINYINT" property="awardNumberSet" />
        <result column="award_numbers" jdbcType="VARCHAR" property="awardNumbers" />
        <result column="parent_award_numbers" jdbcType="VARCHAR" property="parentAwardNumbers" />
        <result column="parent_award_set" jdbcType="TINYINT" property="parentAwardSet" />
    </resultMap>

    <select id="distributionProdPOPage" resultMap="DistributionProdPO">
            select
                p.prod_name,
                p.pic,
                p.price,
                dp.default_reward,
                dp.prod_id,
                dp.shop_id,
                dp.award_proportion,
                dp.award_number_set,
                dp.award_numbers,
                dp.award_id,
                dp.parent_award_numbers,
                dp.parent_award_set
            from
            tz_distribution_prod dp
            join tz_prod p on p.prod_id=dp.prod_id
            <where>
                dp.state = 1 and p.`status` = 1
                <if test="prodName != null and prodName!=''">
                    and p.prod_name like concat('%',#{prodName} ,'%')
                </if>
                <if test="sort == 1">
                    order by p.putaway_time
                </if>
                <if test="sort == 2">
                    order by p.sold_num
                </if>
                <if test="sort == 3">
                    order by p.price
                </if>
                <if test="orderBy == 0">
                    asc
                </if>
                <if test="orderBy == 1">
                    desc
                </if>
            </where>

    </select>

    <select id="getDistributionProdPOByProdId" resultMap="DistributionProdPO">
        select
            dp.default_reward,
            dp.prod_id,
            dp.shop_id,
            dp.award_proportion,
            dp.award_number_set,
            dp.award_numbers,
            dp.award_id,
            dp.parent_award_numbers,
            dp.parent_award_set
        from
        tz_distribution_prod dp
        where dp.prod_id=#{prodId}
    </select>

    <update id="updateState">
        UPDATE tz_distribution_prod dp SET dp.`state` = #{state} WHERE dp.`distribution_prod_id`= #{distributionProdId}
    </update>
    <select id="getDistributionProdLogPage"
            resultType="com.yami.shop.distribution.comment.model.DistributionProdLog">
        SELECT dui.income_id,dui.income_amount,dui.order_number,dui.state,oi.prod_name,oi.pic,oi.shop_id,du.nick_name,du.user_mobile
          <if test="distributionProdLog.shopId == null">
              ,sd.shop_name
          </if>
        FROM  tz_distribution_user_income dui
        JOIN tz_order_item oi ON dui.order_item_id = oi.order_item_id
        JOIN tz_distribution_user du ON du.distribution_user_id = dui.distribution_user_id
        <if test="distributionProdLog.shopId == null">
            JOIN tz_shop_detail sd ON sd.shop_id = oi.shop_id
        </if>
        WHERE dui.income_type IN (1,2)
        <if test="distributionProdLog.shopId != null">
            AND oi.shop_id = #{distributionProdLog.shopId}
        </if>
        <if test="distributionProdLog.state != null">
            AND dui.state = #{distributionProdLog.state}
        </if>
        <if test="distributionProdLog.prodName != null and distributionProdLog.prodName != ''">
            AND oi.prod_name LIKE CONCAT('%',#{distributionProdLog.prodName},'%')
        </if>
        <if test="distributionProdLog.shopName != null and distributionProdLog.shopName != ''">
            AND sd.shop_name LIKE CONCAT('%',#{distributionProdLog.shopName},'%')
        </if>
        <if test="distributionProdLog.nickName != null and distributionProdLog.nickName != ''">
            AND du.nick_name LIKE CONCAT('%',#{distributionProdLog.nickName},'%')
        </if>
        <if test="distributionProdLog.userMobile != null and distributionProdLog.userMobile != ''">
            AND du.user_mobile LIKE CONCAT('%',#{distributionProdLog.userMobile},'%')
        </if>
        ORDER BY dui.create_time DESC, dui.order_number
    </select>

</mapper>
