<?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.sckw.order.dao.KwoTradeOrderArchiveMapper">
  <resultMap id="BaseResultMap" type="com.sckw.order.model.KwoTradeOrderArchive">
    <!--@mbg.generated-->
    <!--@Table kwo_trade_order_archive-->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="trade_order_id" jdbcType="BIGINT" property="tradeOrderId" />
    <result column="goods_id" jdbcType="BIGINT" property="goodsId" />
    <result column="product_name" jdbcType="VARCHAR" property="productName" />
    <result column="product_spec" jdbcType="VARCHAR" property="productSpec" />
    <result column="unit_price" jdbcType="DECIMAL" property="unitPrice" />
    <result column="amount" jdbcType="DECIMAL" property="amount" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, trade_order_id, goods_id, product_name, product_spec, unit_price, amount
  </sql>

  <select id="summary" resultType="com.sckw.order.model.vo.KwoTradeOrderArchiveVo">
    SELECT
      product_name,
      product_spec,
      MAX(unit_price) AS maxPrice,
      MIN(unit_price) AS minPrice,
      SUM(amount) as amount,
      ROUND(SUM(unit_price * amount) / SUM(amount), 2) AS avgPrice,
      ROUND(SUM(unit_price * amount) / 10000, 2) AS salePrice,
      ROUND(100 * SUM(unit_price * amount) / total_sales, 2) AS percent
    FROM
      kwo_trade_order_archive
        CROSS JOIN
      (SELECT SUM(unit_price * amount) AS total_sales FROM kwo_trade_order_archive) AS t
    GROUP BY
      product_name, product_spec
    ORDER BY
      amount DESC;
    </select>
</mapper>
