<?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="cn.com.xingxi.store.data.mapper.WxaStoreGroupMapper">


    <resultMap id="selectYdtGroupByGroupIdResult" type="YdtGroupDTO">
        <result property="groupId"    column="groupId"    />
        <result property="groupTitle"    column="groupTitle"    />
        <result property="groupContent"    column="groupContent"    />
        <result property="groupUserId"    column="groupUserId"    />
        <result property="groupRoleKey"    column="groupRoleKey"    />
        <result property="groupStatus"    column="groupStatus"    />
        <result property="startTime"    column="startTime"    />
        <result property="successTime"    column="successTime"    />
        <result property="endTime"    column="endTime"    />
        <result property="minQty"    column="minQty"    />
        <result property="sameAddrFlag"    column="sameAddrFlag"    />
        <result property="sameAddrProvName"    column="sameAddrProvName"    />
        <result property="sameAddrCityName"    column="sameAddrCityName"    />
        <result property="sameAddrAreaName"    column="sameAddrAreaName"    />
        <result property="sameAddrAddress"    column="sameAddrAddress"    />
        <result property="buildingFlag"    column="buildingFlag"    />
        <result property="roomFlag"    column="roomFlag"    />
        <result property="remark"    column="remark"    />
        <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"    />
        <collection property="ydtGroupDetailDTOS" ofType="YdtGroupDetailDTO">
            <result property="groupDetailId"    column="groupDetailId"    />
            <result property="groupId"    column="groupId"    />
            <result property="prodId"    column="prodId"    />
            <result property="pprId"    column="pprId"    />
            <result property="salesPrice"    column="salesPrice"    />
            <result property="linePrice"    column="linePrice"    />
            <result property="stockQty"    column="stockQty"    />
            <result property="soldQty"    column="soldQty"    />
            <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"    />
        </collection>
    </resultMap>



    <resultMap id="selectFlowGroupOrderListResult" type="OrderDTO" >
        <result property="orderId"    column="orderId"    />
        <result property="orderNo"    column="orderNo"    />
        <result property="orderTime"    column="orderTime"    />
        <result property="orderStatus"    column="orderStatus"    />
        <result property="orderType"    column="orderType"    />
        <result property="groupId"    column="groupId"    />
        <result property="buildingNo"    column="buildingNo"    />
        <result property="roomNo"    column="roomNo"    />
        <result property="groupNo"    column="groupNo"    />
        <result property="orderAmount"    column="orderAmount"    />
        <result property="payAmount"    column="payAmount"    />
        <result property="prodQuantity"    column="prodQuantity"    />
        <result property="buyerId"    column="buyerId"    />
        <result property="buyerName" column="buyerName"/>
        <result property="buyerRole"    column="buyerRole"    />
        <result property="sellerId"    column="sellerId"    />
        <result property="sellerRole"    column="sellerRole"    />
        <result property="payeeId"    column="payeeId"    />
        <result property="payeeRole"    column="payeeRole"    />
        <result property="referrerId"    column="referrerId"    />
        <result property="referrerRole"    column="referrerRole"    />
        <result property="referrerRate"    column="referrerRate"    />
        <result property="serviceRate"    column="serviceRate"    />
        <result property="cancelTime"    column="cancelTime"    />
        <result property="finishTime"    column="finishTime"    />
        <result property="recProvId"    column="recProvId"    />
        <result property="recProvName"    column="recProvName"    />
        <result property="recCityId"    column="recCityId"    />
        <result property="recCityName"    column="recCityName"    />
        <result property="recAreaId"    column="recAreaId"    />
        <result property="recAreaName"    column="recAreaName"    />
        <result property="recAddress"    column="recAddress"    />
        <result property="recName"    column="recName"    />
        <result property="recPhone"    column="recPhone"    />
        <result property="remark"    column="remark"    />
        <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"    />
        <collection property="orderDetailDTOS" ofType="OrderDetailDTO">
            <result property="orderDetailId"    column="orderDetailId"    />
            <result property="orderId"    column="orderId"    />
            <result property="suiteId"    column="suiteId"    />
            <result property="prodId"    column="prodId"    />
            <result property="prodName"    column="prodName"    />
            <result property="pprId"    column="pprId"    />
            <result property="pprName"    column="pprName"    />
            <result property="upId"    column="upId"    />
            <result property="orgProdId"    column="orgProdId"    />
            <result property="prodType"    column="prodType"    />
            <result property="mateId"    column="mateId"    />
            <result property="mateName"    column="mateName"    />
            <result property="mateType"    column="mateType"    />
            <result property="vendorId"    column="vendorId"    />
            <result property="surfaceQty"    column="surfaceQty"    />
            <result property="unitQty"    column="unitQty"    />
            <result property="buyQty"    column="buyQty"    />
            <result property="afterQty"    column="afterQty"    />
            <result property="returnQty"    column="returnQty"    />
            <result property="buyPrice"    column="buyPrice"    />
            <result property="salePrice"    column="salePrice"    />
            <result property="confirmTime"    column="confirmTime"    />
            <result property="finishTime"    column="finishTime"    />
            <result property="orderDetailStatus"    column="orderDetailStatus"    />
            <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"    />

        </collection>
    </resultMap>




    <resultMap id="selectYdtGroupDetailByGroupIdResult" type="YdtGroupDTO">
        <result property="groupId"    column="groupId"    />
        <result property="groupTitle"    column="groupTitle"    />
        <result property="groupContent"    column="groupContent"    />
        <result property="groupUserId"    column="groupUserId"    />
        <result property="groupRoleKey"    column="groupRoleKey"    />
        <result property="groupStatus"    column="groupStatus"    />
        <result property="startTime"    column="startTime"    />
        <result property="successTime"    column="successTime"    />
        <result property="endTime"    column="endTime"    />
        <result property="minQty"    column="minQty"    />
        <result property="sameAddrFlag"    column="sameAddrFlag"    />
        <result property="sameAddrProvName"    column="sameAddrProvName"    />
        <result property="sameAddrCityName"    column="sameAddrCityName"    />
        <result property="sameAddrAreaName"    column="sameAddrAreaName"    />
        <result property="sameAddrAddress"    column="sameAddrAddress"    />
        <result property="buildingFlag"    column="buildingFlag"    />
        <result property="roomFlag"    column="roomFlag"    />
        <result property="remark"    column="remark"    />
        <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"    />
        <collection property="ydtGroupDetailDTOS" select="selectYdgGroupDetailListByGroupId" column="groupId"/>
    </resultMap>


    <resultMap id="selectYdgGroupDetailListByGroupIdResult" type="YdtGroupDetailDTO">
        <result property="groupDetailId"    column="groupDetailId"    />
        <result property="groupId"    column="groupId"    />
        <result property="prodId"    column="prodId"    />
        <result property="prodName" column="prodName"/>
        <result property="subTitle" column="subTitle"/>
        <result property="description" column="prodContent"/>
        <result property="flowGroupCount" column="flowGroupCount"/>
        <result property="pprId"    column="pprId"    />
        <result property="salesPrice"    column="salesPrice"    />
        <result property="linePrice"    column="linePrice"    />
        <result property="stockQty"    column="stockQty"    />
        <result property="soldQty"    column="soldQty"    />
        <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"    />
        <collection property="imageDTOS" select="selectProductImagesByProductId" column="prodId"/>
    </resultMap>

    <resultMap id="selectSkusByProductIdResult" type="ProductSkuDTO">
        <result property="id"    column="pprId"/>
        <result property="name"    column="pprName"/>
        <result property="attrs"    column="propIds"/>
    </resultMap>


    <resultMap id="selectAttrByProductIdResult" type="ProductAttrDTO">
        <id property="id"    column="propId"/>
        <result property="label"    column="propName"/>
        <collection property="values" javaType="java.util.List" ofType="ProductAttrDTO">
            <result property="id"    column="ppvId"/>
            <result property="label"    column="prodPropValueName"/>
        </collection>
    </resultMap>

    <resultMap id="ImagesResult" type="ImageDTO">
        <id property="imageId"    column="fileId"/>
        <result property="imageUrl"    column="fileUrl"/>
    </resultMap>



    <select id="selectYdtGroupListByUserId" parameterType="Long" resultType="YdtGroupDTO">
        SELECT t_group.*,
               (SELECT COUNT(1)
                   FROM t_order
                   WHERE t_order.orderStatus = 'PAYED' AND t_order.groupId = t_group.groupId) AS flowGroupOrderCnt,
               (SELECT COUNT(1)
                   FROM t_order
                   WHERE t_order.orderStatus = 'CANCELLED' AND t_order.groupId = t_group.groupId) AS cancelFlowGroupOrderCnt,
               (SELECT IFNULL(SUM(t_order.prodQuantity), 0)
                   FROM t_order
                   WHERE t_order.orderStatus = 'PAYED' AND t_order.groupId = t_group.groupId) AS flowGroupProductCnt
        FROM t_group
        WHERE t_group.delFlag = '0' AND t_group.groupUserId = #{userId}
    </select>

    <select id="selectYdtGroupByGroupId" parameterType="Long" resultMap="selectYdtGroupByGroupIdResult">
        SELECT t_group.*,t_group_detail.*
        FROM t_group
        INNER JOIN t_group_detail ON t_group.groupId = t_group_detail.groupId
        WHERE t_group.delFlag = '0'
          AND  t_group.groupId = #{groupId}
    </select>


    <select id="selectGroupDetailByPprId" resultType="YdtGroupDetailDTO">
        SELECT t_group_detail.*
        FROM t_group_detail
        WHERE t_group_detail.delFlag = '0'
          AND t_group_detail.groupId = #{groupId}
          AND t_group_detail.pprId = #{pprId}
    </select>


    <select id="selectFlowGroupOrderListByGroupId" parameterType="Long" resultMap="selectFlowGroupOrderListResult">
        SELECT t_order.orderTime, t_order.buyerId, t_order_detail.*, (
            SELECT sys_user.user_name
            FROM sys_user
            WHERE sys_user.user_id = t_order.buyerId) AS buyerName
        FROM t_order
        INNER JOIN t_order_detail ON t_order.orderId = t_order_detail.orderId
        WHERE t_order.orderStatus = 'PAYED'
            AND t_order.groupId = #{groupId}
    </select>


    <select id="selectCountPublishGroupTimesByUserId" parameterType="Long" resultType="int">
        SELECT COUNT(1)
        FROM t_group
        WHERE t_group.groupStatus = 'DEPLOY'
          AND t_group.groupUserId = #{userId}
    </select>


    <select id="selectSkusByProductId" resultMap="selectSkusByProductIdResult">
        SELECT t_group_detail.prodId,
               m_prod_prop_relation.propIds,m_prod_prop_relation.pprId,
               m_prod_prop_relation.pprName
        FROM t_group_detail
        INNER JOIN m_prod_prop_relation ON t_group_detail.pprId = m_prod_prop_relation.pprId
        WHERE m_prod_prop_relation.delFlag = '0'
          AND t_group_detail.groupId = #{groupId}
          AND t_group_detail.prodId = #{productId}
    </select>




    <select id="selectAttrByProductId" resultMap="selectAttrByProductIdResult">
        SELECT m_prod_prop.propId,m_prod_prop.propName,
               m_prod_prop_value.ppvId, m_prod_prop_value.prodPropValueName
        FROM t_group_detail
        INNER JOIN m_prod_prop ON t_group_detail.prodId = m_prod_prop.prodId
        INNER JOIN m_prod_prop_value ON m_prod_prop.prodId = m_prod_prop_value.prodId
        AND m_prod_prop_value.prodPropId = m_prod_prop.propId
        WHERE  m_prod_prop.delFlag = '0'
          AND m_prod_prop_value.delFlag = '0'
          AND t_group_detail.groupId = #{groupId}
          AND t_group_detail.prodId = #{productId}
        ORDER BY m_prod_prop.sort, m_prod_prop_value.sort
    </select>


    <select id="selectYdtGroupDetailByGroupId" resultMap="selectYdtGroupDetailByGroupIdResult">
        SELECT t_group.*
        FROM t_group
        WHERE t_group.delFlag = '0'
          AND  t_group.groupId = #{groupId}
    </select>

    <select id="selectYdgGroupDetailListByGroupId" resultMap="selectYdgGroupDetailListByGroupIdResult">
        SELECT t_group_detail.*, m_prod.prodName, m_prod.subTitle,m_prod.prodContent,
               (SELECT IFNULL(SUM(t_order_detail.buyQty), 0)
                   FROM t_order INNER JOIN t_order_detail ON t_order.orderId = t_order_detail.orderId
                   WHERE t_order.orderStatus = 'PAYED'
                     AND t_order.groupId = t_group_detail.groupId
                     AND t_order_detail.prodId = t_group_detail.prodId) AS flowGroupCount
        FROM t_group_detail
        INNER JOIN m_prod ON m_prod.prodId = t_group_detail.prodId
        WHERE t_group_detail.delFlag = '0'
          AND t_group_detail.groupId = #{groupId}
        GROUP BY t_group_detail.prodId
    </select>

    <select id="selectProductImagesByProductId" parameterType="Long" resultMap="ImagesResult">
        SELECT m_prod_picture.picImgId, t_attch_file.fileId, t_attch_file.fileUrl
        FROM m_prod_picture
        INNER JOIN t_attch_file ON m_prod_picture.picImgId = t_attch_file.fileId
        WHERE m_prod_picture.prodId = #{prodId}
        ORDER BY m_prod_picture.sort
    </select>

</mapper>
