<?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.yunniu.farming.webadmin.dao.OrderMainDao">
    <resultMap id="BaseResultMap" type="com.yunniu.farming.webadmin.model.OrderMain">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="customerid" property="customerid" jdbcType="INTEGER"/>
        <result column="addressid" property="addressid" jdbcType="INTEGER"/>
        <result column="sorderno" property="sorderno" jdbcType="VARCHAR"/>
        <result column="scusaddr" property="scusaddr" jdbcType="VARCHAR"/>
        <result column="scuname" property="scuname" jdbcType="VARCHAR"/>
        <result column="scuphone" property="scuphone" jdbcType="VARCHAR"/>
        <result column="sprovince" property="sprovince" jdbcType="VARCHAR"/>
        <result column="scity" property="scity" jdbcType="VARCHAR"/>
        <result column="sregion" property="sregion" jdbcType="VARCHAR"/>
        <result column="sorderdate" property="sorderdate" jdbcType="CHAR"/>
        <result column="tordertime" property="tordertime" jdbcType="TIMESTAMP"/>
        <result column="tovertime" property="tovertime" jdbcType="TIMESTAMP"/>
        <result column="dplanmoney" property="dplanmoney" jdbcType="DECIMAL"/>
        <result column="dactmoney" property="dactmoney" jdbcType="DECIMAL"/>
        <result column="dactscore" property="dactscore" jdbcType="DECIMAL"/>
        <result column="ipaystatus" property="ipaystatus" jdbcType="INTEGER"/>
        <result column="group_order_id" property="groupOrderId" jdbcType="INTEGER"/>
        <result column="irefundstatus" property="irefundstatus" jdbcType="INTEGER"/>
        <result column="irefundmoney" property="irefundmoney" jdbcType="INTEGER"/>
        <result column="tpaytime" property="tpaytime" jdbcType="TIMESTAMP"/>
        <result column="trefundtime" property="trefundtime" jdbcType="TIMESTAMP"/>
        <result column="istatus" property="istatus" jdbcType="INTEGER"/>
        <result column="itype" property="itype" jdbcType="INTEGER"/>
        <result column="srefundreason" property="srefundreason" jdbcType="VARCHAR"/>
        <result column="srefundpic" property="srefundpic" jdbcType="VARCHAR"/>
        <result column="couriernumber" property="couriernumber" jdbcType="VARCHAR"/>
        <result column="productid" property="productid" jdbcType="INTEGER"/>
        <result column="ipaytype" property="ipaytype" jdbcType="INTEGER"/>
        <result column="sremark" property="sremark" jdbcType="VARCHAR"/>

        <result column="sdef1" property="sdef1" jdbcType="VARCHAR"/>
        <result column="sdef2" property="sdef2" jdbcType="VARCHAR"/>
        <result column="sdef3" property="sdef3" jdbcType="VARCHAR"/>
        <result column="sdef4" property="sdef4" jdbcType="VARCHAR"/>
        <result column="sdef5" property="sdef5" jdbcType="VARCHAR"/>
        <result column="idef6" property="idef6" jdbcType="INTEGER"/>
        <result column="idef7" property="idef7" jdbcType="INTEGER"/>
        <result column="ddef8" property="ddef8" jdbcType="DECIMAL"/>
        <result column="ddef9" property="ddef9" jdbcType="DECIMAL"/>
        <result column="ddef10" property="ddef10" jdbcType="TIMESTAMP"/>

        <association property="customer" column="customerid" select="selectCustomer"/>


        <!-- FRICE TODO 一对多 关联查询 -->
        <collection property="subs" ofType="com.yunniu.farming.webadmin.model.OrderSub" javaType="java.util.ArrayList">
            <!-- 以下为子表相关字段 -->
            <id column="zid" property="id" jdbcType="INTEGER"/>
            <result column="zmainid" property="mainid" jdbcType="INTEGER"/>
            <result column="zproductid" property="productid" jdbcType="INTEGER"/>
            <result column="zipronum" property="ipronum" jdbcType="INTEGER"/>
            <result column="zdprosum" property="dprosum" jdbcType="DECIMAL"/>
            <result column="zsgoodprice" property="sgoodprice" jdbcType="DECIMAL"/>

            <!-- 以下为商品相关字段 -->
            <result column="zsproductname" property="sproductname" jdbcType="VARCHAR"/>
            <result column="zspshortpic" property="spshortpic" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>


    <select id="selectCustomer" parameterType="Integer" resultType="com.yunniu.farming.webadmin.model.Customer">
        SELECT *
        FROM customer
        WHERE id = #{customerid}
    </select>


    <sql id="Base_Column_List">
        id,
        customerid,
        addressid,
        productid,
        sorderno,
        scusaddr,
        scuname,
        scuphone,
        sprovince,
        scity,
        sregion,
        itype,
        sorderdate,
        tordertime,
        tovertime,
        dplanmoney,
        dactmoney,
        dactscore,
        ipaystatus,
        irefundstatus,
        tpaytime,
        trefundtime,
        istatus,
        sdef1,
        sdef2,
        sdef3,
        sdef4,
        sdef5,
        idef6,
        idef7,
        ddef8,
        ddef9,
        ddef10
    </sql>
    <sql id="Full_Column_List">
        ordermain.id,
        ordermain.customerid,
        ordermain.addressid,
        ordermain.productid,
        ordermain.sorderno,
        ordermain.scusaddr,
        ordermain.scuname,
        ordermain.scuphone,
        ordermain.sprovince,
        ordermain.scity,
        ordermain.sregion,
        ordermain.itype,
        ordermain.sorderdate,
        ordermain.tordertime,
        ordermain.tovertime,
        ordermain.dplanmoney,
        ordermain.dactmoney,
        ordermain.dactscore,
        ordermain.ipaystatus,
        ordermain.irefundstatus,
        ordermain.irefundmoney,
        ordermain.srefundreason,
        ordermain.srefundpic,
        ordermain.couriernumber,
        ordermain.tpaytime,
        ordermain.trefundtime,
        ordermain.istatus,
        ordermain.ipaytype,
        ordermain.sdef1,
        ordermain.sdef2,
        ordermain.sdef3,
        ordermain.sdef4,
        ordermain.sdef5,
        ordermain.idef6,
        ordermain.idef7,
        ordermain.ddef8,
        ordermain.ddef9,
        ordermain.ddef10,
        ordermain.group_order_id
    </sql>


    <!-- 带参数常规查询 S -->
    <select id="selectByParam" resultMap="BaseResultMap" parameterType="com.yunniu.farming.webadmin.model.OrderMain">
        select
        <include refid="Full_Column_List"/>

        FROM order_main ordermain

        <where>
            <if test="id != null">
                AND ordermain.id = #{id,jdbcType=INTEGER}
            </if>
            <if test="customerid != null">
                AND ordermain.customerid = #{customerid,jdbcType=INTEGER}
            </if>
            <if test="istatus != null">
                AND ordermain.istatus = #{istatus,jdbcType=INTEGER}
            </if>
            <if test="itype != null">
                AND ordermain.itype = #{itype,jdbcType=INTEGER}
            </if>

            <if test="sorderno != null">
                AND ordermain.sorderno = #{sorderno,jdbcType=VARCHAR}
            </if>

            <!-- 日期处理,>= 用 <![CDATA[>=]]> 来写 -->
            <if test="beginDate != null">
                AND ordermain.sorderdate <![CDATA[ >= ]]> #{beginDate,jdbcType=VARCHAR}
            </if>
            <if test="endDate != null">
                AND ordermain.sorderdate <![CDATA[ <= ]]> #{endDate,jdbcType=VARCHAR}
            </if>
            <if test="ipaystatus != null">
                AND ordermain.ipaystatus = #{ipaystatus,jdbcType=INTEGER}
            </if>
        </where>
        <!-- 以发布时间倒序显示 -->
        ORDER BY tordertime DESC
    </select>

    <resultMap id="ResultMap" type="com.yunniu.farming.webadmin.model.OrderMain">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="customerid" property="customerid" jdbcType="INTEGER"/>
        <result column="addressid" property="addressid" jdbcType="INTEGER"/>
        <result column="sorderno" property="sorderno" jdbcType="VARCHAR"/>
        <result column="scusaddr" property="scusaddr" jdbcType="VARCHAR"/>
        <result column="scuname" property="scuname" jdbcType="VARCHAR"/>
        <result column="scuphone" property="scuphone" jdbcType="VARCHAR"/>
        <result column="sprovince" property="sprovince" jdbcType="VARCHAR"/>
        <result column="scity" property="scity" jdbcType="VARCHAR"/>
        <result column="sregion" property="sregion" jdbcType="VARCHAR"/>
        <result column="sorderdate" property="sorderdate" jdbcType="CHAR"/>
        <result column="tordertime" property="tordertime" jdbcType="TIMESTAMP"/>
        <result column="tovertime" property="tovertime" jdbcType="TIMESTAMP"/>
        <result column="dplanmoney" property="dplanmoney" jdbcType="DECIMAL"/>
        <result column="dactmoney" property="dactmoney" jdbcType="DECIMAL"/>
        <result column="dactscore" property="dactscore" jdbcType="DECIMAL"/>
        <result column="ipaystatus" property="ipaystatus" jdbcType="INTEGER"/>
        <result column="group_order_id" property="groupOrderId" jdbcType="INTEGER"/>
        <result column="irefundstatus" property="irefundstatus" jdbcType="INTEGER"/>
        <result column="tpaytime" property="tpaytime" jdbcType="TIMESTAMP"/>
        <result column="trefundtime" property="trefundtime" jdbcType="TIMESTAMP"/>
        <result column="istatus" property="istatus" jdbcType="INTEGER"/>
        <result column="itype" property="itype" jdbcType="INTEGER"/>
        <result column="srefundreason" property="srefundreason" jdbcType="VARCHAR"/>
        <result column="srefundpic" property="srefundpic" jdbcType="VARCHAR"/>
        <result column="couriernumber" property="couriernumber" jdbcType="VARCHAR"/>
        <result column="productid" property="productid" jdbcType="INTEGER"/>
        <result column="ipaytype" property="ipaytype" jdbcType="INTEGER"/>
        <result column="sremark" property="sremark" jdbcType="VARCHAR"/>
        <result column="sdef1" property="sdef1" jdbcType="VARCHAR"/>
        <result column="sdef2" property="sdef2" jdbcType="VARCHAR"/>
        <result column="sdef3" property="sdef3" jdbcType="VARCHAR"/>
        <result column="sdef4" property="sdef4" jdbcType="VARCHAR"/>
        <result column="sdef5" property="sdef5" jdbcType="VARCHAR"/>
        <result column="idef6" property="idef6" jdbcType="INTEGER"/>
        <result column="idef7" property="idef7" jdbcType="INTEGER"/>
        <result column="ddef8" property="ddef8" jdbcType="DECIMAL"/>
        <result column="ddef9" property="ddef9" jdbcType="DECIMAL"/>
        <result column="ddef10" property="ddef10" jdbcType="TIMESTAMP"/>
        <result column="groupBuyId" property="groupBuyId" jdbcType="BIGINT"/>
        <result column="groupBuyName" property="groupBuyName" jdbcType="VARCHAR"/>
        <association property="customer" column="customerid" select="selectCustomer"/>
        <!-- FRICE TODO 一对多 关联查询 -->
        <collection property="subs" ofType="com.yunniu.farming.webadmin.model.OrderSub" select="selectOrderSub" column="id"/>

    </resultMap>
    <resultMap id="subs" type="com.yunniu.farming.webadmin.model.OrderSub">
        <result column="zid" property="id" jdbcType="INTEGER"/>
        <result column="zmainid" property="mainid" jdbcType="INTEGER"/>
        <result column="zproductid" property="productid" jdbcType="INTEGER"/>
        <result column="zipronum" property="ipronum" jdbcType="INTEGER"/>
        <result column="zdprosum" property="dprosum" jdbcType="DECIMAL"/>
        <result column="zsgoodprice" property="sgoodprice" jdbcType="DECIMAL"/>
        <!-- 以下为商品相关字段 -->
        <result column="zsproductname" property="sproductname" jdbcType="VARCHAR"/>
        <result column="zspshortpic" property="spshortpic" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="selectOrderSub" resultMap="subs">
        select sub.id           AS zid,
               sub.mainid       AS zmainid,
               sub.ipronum      AS zipronum,
               sub.dprosum      AS zdprosum,
               sub.productid    AS zproductid,
               sub.sgoodprice   AS zsgoodprice,
               sub.sproductname AS zsproductname,
               sub.spshortpic   AS zspshortpic
        from order_sub sub
        where mainid = #{id}
    </select>

    <!-- 带参数分页查询 S -->
    <select id="selectByParamPageList" resultMap="ResultMap"
            parameterType="com.yunniu.farming.webadmin.model.OrderMain">
        SELECT
        <include refid="Full_Column_List"/>
        <!-- 关联子表信息 -->
        FROM order_main ordermain
        <where>
            <if test="appflag == null or appflag == ''">
                ordermain.itype = 10
            </if>
            <if test="id != null">
                AND ordermain.id = #{id,jdbcType=INTEGER}
            </if>

            <if test="customerid != null">
                AND ordermain.customerid = #{customerid,jdbcType=INTEGER}
            </if>

            <if test="istatus != null">
                AND ordermain.istatus = #{istatus,jdbcType=INTEGER}
            </if>
            <if test="irefundstatus != null">
                AND ordermain.irefundstatus = #{irefundstatus,jdbcType=INTEGER}
            </if>
            <if test="itype != null">
                AND ordermain.itype = #{itype,jdbcType=INTEGER}
            </if>
            <if test="ipaytype != null">
                AND ordermain.ipaytype = #{ipaytype,jdbcType=INTEGER}
            </if>
            <if test="sorderno != null and sorderno != ''">
                AND ordermain.sorderno = #{sorderno,jdbcType=VARCHAR}
            </if>
            <if test="beginDate != null">
                AND ordermain.sorderdate <![CDATA[ >= ]]> #{beginDate,jdbcType=VARCHAR}
            </if>
            <if test="endDate != null">
                AND ordermain.sorderdate <![CDATA[ <= ]]> #{endDate,jdbcType=VARCHAR}
            </if>
        </where>
        <!-- 以时间倒序显示 -->
        ORDER BY ordermain.tordertime DESC
    </select>
    <!-- 带参数分页查询 E -->


    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        SELECT
        <include refid="Full_Column_List"/>
        <!-- 关联子表信息 -->
        ,
            sub.id           AS zid,
            sub.ipronum      AS zipronum,
            sub.dprosum      AS zdprosum,
            sub.productid    AS zproductid,
            sub.sgoodprice   AS zsgoodprice,
            sub.sproductname AS zsproductname,
            sub.spshortpic   AS zspshortpic
        FROM order_main ordermain
                 LEFT JOIN order_sub sub ON ordermain.id = sub.mainid
        WHERE ordermain.id = #{id,jdbcType=INTEGER}
    </select>


    <select id="countOrderNum" resultType="int" parameterType="com.yunniu.farming.webadmin.model.OrderMain">
        SELECT IFNULL(count(*), 0)
        from order_main
        <where>
            <if test="customerid != null">
                AND customerid = #{customerid,jdbcType=INTEGER}
            </if>
            <if test="istatus != null">
                AND istatus = #{istatus,jdbcType=INTEGER}
            </if>
            <if test="ipaystatus != null">
                AND ipaystatus = #{ipaystatus,jdbcType=INTEGER}
            </if>
            <if test="ipaytype != null">
                AND ipaytype = #{ipaytype,jdbcType=INTEGER}
            </if>
            <if test="itype != null">
                AND itype = #{itype,jdbcType=INTEGER}
            </if>
        </where>
    </select>


    <select id="countOrderMoney" resultType="double" parameterType="com.yunniu.farming.webadmin.model.OrderMain">
        SELECT IFNULL(SUM(dactmoney), 0)
        from order_main
        <where>
            <if test="customerid != null">
                AND customerid = #{customerid,jdbcType=INTEGER}
            </if>

            <if test="istatus != null">
                AND istatus = #{istatus,jdbcType=INTEGER}
            </if>
            <if test="ipaystatus != null">
                AND ipaystatus = #{ipaystatus,jdbcType=INTEGER}
            </if>
            <if test="itype != null">
                AND itype = #{itype,jdbcType=INTEGER}
            </if>
            <if test="ipaytype != null">
                AND ipaytype = #{ipaytype,jdbcType=INTEGER}
            </if>
        </where>
    </select>

    <select id="getPageListByGroupOrderId" resultType="com.yunniu.farming.webadmin.model.OrderMain">
        select om.id,
               c.swxnick,
               om.sorderno,
               om.dactmoney,
               om.tpaytime,
               concat(om.sprovince, om.scity, om.sregion, om.scusaddr) as scusaddr,
               om.sremark,
               om.sdef1,
               om.sdef2,
               om.sdef3
        from order_main om
        left join customer c on om.customerid = c.id
        where om.group_order_id = #{id,jdbcType=BIGINT}
    </select>

    <select id="selectByGroupOrderId" resultMap="BaseResultMap">
        select *
        from order_main
        where group_order_id = #{id,jdbcType=BIGINT} and ipaystatus = 1
    </select>

    <select id="getLastOrderAddres" resultMap="BaseResultMap">
        select sdef1,
        sdef2,
        sdef3
        from order_main
        where customerid = #{userId}
        and group_order_id is not null
        and sdef1 is not null
        and sdef2 is not null
        and sdef3 is not null
        order by tordertime desc limit 1
    </select>

    <select id="selectByParamPageRefundGroupList" resultMap="ResultMap">
        SELECT
        <include refid="Full_Column_List"/>
        <!-- 关联子表信息 -->
        ,gb.id as groupBuyId
        ,gb.group_buy_title as groupBuyName
        FROM order_main ordermain
        left join group_order go on ordermain.group_order_id = go.id
        left join group_buy gb on go.group_buy_id = gb.id
        <where>
            ordermain.group_order_id IS NOT NULL
            AND ordermain.irefundstatus IS NOT NULL
            <if test="id != null">
                AND ordermain.id = #{id,jdbcType=INTEGER}
            </if>

            <if test="customerid != null">
                AND ordermain.customerid = #{customerid,jdbcType=INTEGER}
            </if>

            <if test="istatus != null">
                AND ordermain.istatus = #{istatus,jdbcType=INTEGER}
            </if>
            <if test="irefundstatus != null">
                AND ordermain.irefundstatus = #{irefundstatus,jdbcType=INTEGER}
            </if>
            <if test="itype != null">
                AND ordermain.itype = #{itype,jdbcType=INTEGER}
            </if>
            <if test="ipaytype != null">
                AND ordermain.ipaytype = #{ipaytype,jdbcType=INTEGER}
            </if>
            <if test="sorderno != null and sorderno != ''">
                AND ordermain.sorderno = #{sorderno,jdbcType=VARCHAR}
            </if>
            <if test="beginDate != null">
                AND ordermain.sorderdate <![CDATA[ >= ]]> #{beginDate,jdbcType=VARCHAR}
            </if>
            <if test="endDate != null">
                AND ordermain.sorderdate <![CDATA[ <= ]]> #{endDate,jdbcType=VARCHAR}
            </if>
        </where>
        <!-- 以时间倒序显示 -->
        ORDER BY ordermain.tordertime DESC
    </select>
</mapper>