<?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.github.wxiaoqi.security.admin.mapper.BaseOrderAcceptDetailedMapper">


    <select id="selectList" parameterType="com.github.wxiaoqi.security.admin.dto.BaseOrderAcceptDetailedDTO" resultType="com.github.wxiaoqi.security.admin.vo.BaseOrderAcceptDetailedVO">
        SELECT
        r.*,
        SUM(r.zDivisionAmount)-SUM(r.fDivisionAmount) as divisionAmount
        FROM
        (

        SELECT mch_id,order_no,order_id,user_id,division_type,entry_type,order_amount,(division_amount-fee) as zDivisionAmount, 0 as fDivisionAmount,fee,status_time FROM base_order_accept_detailed
        WHERE type=1  AND `status`=1
        <if test="orderNo != null and orderNo != ''">
            AND order_no=#{orderNo}
        </if>
        <if test="acceptId != null and acceptId > 0">
            AND id=#{acceptId}
        </if>
        <if test="divisionTypes != null and divisionTypes.size() > 0">
            and division_type in
            <foreach collection="divisionTypes" item="divisionType" open="(" separator="," close=")">
                #{divisionType}
            </foreach>
        </if>

        UNION ALL

        SELECT mch_id,order_no,order_id,user_id,division_type,entry_type,order_amount,0 as zDivisionAmount, (division_amount-fee) as fDivisionAmount,fee,status_time FROM base_order_accept_detailed
        WHERE type=2  AND `status`=1
        <if test="orderNo != null and orderNo != ''">
            AND order_no=#{orderNo}
        </if>
        <if test="acceptId != null and acceptId > 0">
            AND id=#{acceptId}
        </if>
        <if test="divisionTypes != null and divisionTypes.size() > 0">
            and division_type in
            <foreach collection="divisionTypes" item="divisionType" open="(" separator="," close=")">
                #{divisionType}
            </foreach>
        </if>
        ) r
        GROUP BY r.division_type

    </select>

    <select id="getListByOrderNo" resultType="com.github.wxiaoqi.security.admin.entity.BaseOrderRevenue" parameterType="com.github.wxiaoqi.security.admin.dto.OrderWaterDTO">
        SELECT order_no as orderNo,mch_id as companyId,user_id as userId,SUM(division_amount-fee) as amount,GROUP_CONCAT(division_type) as type FROM base_order_accept_detailed
          WHERE type=1  AND `status`=0  and division_type in (3,5,6,7,8)AND order_no=#{orderNo}
        GROUP BY order_no,mch_id
    </select>


</mapper>