<?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.ProdMateMqMapper">
    
    <resultMap type="ProdMateMq" id="ProdMateMqResult">
        <result property="prodMateId"    column="prodMateId"    />
        <result property="prodId"    column="prodId"    />
        <result property="pprId"    column="pprId"    />
        <result property="surfaceSeq"    column="surfaceSeq"    />
        <result property="designName"    column="designName"    />
        <result property="mateId"    column="mateId"    />
        <result property="mateQty"    column="mateQty"    />
        <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"    />

        <result property="mateName"    column="mateName"    />
    </resultMap>

    <sql id="selectProdMateMq">
        select
            p.prodMateId,
            p.prodId,
            p.pprId,
            p.surfaceSeq,
            p.designName,
            p.mateId,
            p.mateQty,
            p.delFlag,
            p.createUser,
            p.createTime,
            p.updateUser,
            p.updateTime
        from m_prod_mate p
    </sql>

    <select id="selectProdMateUnitQty" parameterType="ProdMateMq" resultMap="ProdMateMqResult">
        select
            p.prodId,
            p.pprId,
            p.mateId,
            max(m.mateName) mateName,
            sum(p.mateQty) mateQty
        from m_prod_mate p
            inner join m_material m on p.mateId = m.mateId
        where p.delFlag = '0'
            <if test="prodId != null "> and p.prodId = #{prodId}</if>
            <if test="pprId != null "> and p.pprId = #{pprId}</if>
        group by p.prodId, p.pprId, p.mateId
    </select>

    <select id="selectProdMateSurfaceQty" resultType="int">
        select count(distinct surfaceSeq)
        from m_prod_mate
        where delFlag = '0' and prodId = #{prodId} and pprId = #{pprId}
    </select>

</mapper>