<?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.xingxi.mq.server.product.mapper.ProdPriceMqMapper">

    <resultMap type="ProdPrice" id="ProdPriceMqResult">
        <result property="prodPriceId"    column="prodPriceId"    />
        <result property="prodId"    column="prodId"    />
        <result property="pprId"    column="pprId"    />
        <result property="buyerId"    column="buyerId"    />
        <result property="buyerRoleKey"    column="buyerRoleKey"    />
        <result property="salesmanId"    column="salesmanId"    />
        <result property="salesmanRoleKey"    column="salesmanRoleKey"    />
        <result property="costItemKey"    column="costItemKey"    />
        <result property="surfaceQty"    column="surfaceQty"    />
        <result property="price"    column="price"    />
        <result property="delFlag"    column="delFlag"    />
        <result property="createUser"    column="createUser"    />
        <result property="createTime"    column="createTime"    />
        <result property="updateUser"    column="updateUser"    />
        <result property="updateTime"    column="updateTime"    />
    </resultMap>

    <sql id="selectProdPriceMq">
        select
            p.prodPriceId,
            p.prodId,
            p.pprId,
            p.buyerId,
            p.buyerRoleKey,
            p.salesmanId,
            p.salesmanRoleKey,
            p.costItemKey,
            p.surfaceQty,
            p.price,
            p.delFlag,
            p.createUser,
            p.createTime,
            p.updateUser,
            p.updateTime
        from m_prod_price p
            inner join m_cost_item c on p.costItemKey = c.costItemKey
    </sql>

    <select id="getProdPriceByUnionId" parameterType="ProdPrice" resultType="BigDecimal">
        SELECT sum(p.price) price
        FROM m_prod_price p
            inner join m_cost_item c on p.costItemKey = c.costItemKey
        WHERE p.delFlag = '0'
            and p.prodId = #{prodId}
            and p.pprId = #{pprId}
            and p.buyerId = #{buyerId}
            and p.salesmanId = #{salesmanId}
            and (case when c.singleFlag = '0' then p.surfaceQty = #{surfaceQty} else 1 end)
    </select>
</mapper>