<?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.sqx.modules.orders.dao.OrdersDao">

    <select id="selectMaxCode" resultType="String">
        select max(orders_no) code from orders where orders_no like #{newData}
    </select>


    <insert id="insertOrders" useGeneratedKeys="true"  keyProperty="ordersId" parameterType="com.sqx.modules.orders.entity.Orders">
        INSERT INTO orders
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != ordersId and '' != ordersId">
                orders_id,
            </if>
            <if test="null != ordersNo and '' != ordersNo">
                orders_no,
            </if>
            <if test="null != tradeNo and '' != tradeNo">
                trade_no,
            </if>
            <if test="null != userId and '' != userId">
                user_id,
            </if>
            <if test="null != courseId and '' != courseId">
                course_id,
            </if>
            <if test="null != payMoney and '' != payMoney">
                pay_money,
            </if>
            <if test="null != payWay and '' != payWay">
                pay_way,
            </if>
            <if test="null != status">
                status,
            </if>
            <if test="null != createTime and '' != createTime">
                create_time,
            </if>
            <if test="null != refundContent and '' != refundContent">
                refund_content,
            </if>
            <if test="null != ordersType and '' != ordersType">
                orders_type,
            </if>
            <if test="null != courseDetailsId ">
                course_details_id,
            </if>
            <if test="null != vipNameType">
                vip_name_type
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != ordersId and '' != ordersId">
                #{ordersId},
            </if>
            <if test="null != ordersNo and '' != ordersNo">
                #{ordersNo},
            </if>
            <if test="null != tradeNo and '' != tradeNo">
                #{tradeNo},
            </if>
            <if test="null != userId and '' != userId">
                #{userId},
            </if>
            <if test="null != courseId and '' != courseId">
                #{courseId},
            </if>
            <if test="null != payMoney and '' != payMoney">
                #{payMoney},
            </if>
            <if test="null != payWay and '' != payWay">
                #{payWay},
            </if>
            <if test="null != status">
                #{status},
            </if>
            <if test="null != createTime and '' != createTime">
                #{createTime},
            </if>
            <if test="null != refundContent and '' != refundContent">
                #{refundContent},
            </if>
            <if test="null != ordersType and '' != ordersType">
                #{ordersType},
            </if>
            <if test="null != courseDetailsId ">
                #{courseDetailsId},
            </if>
            <if test="null != vipNameType">
                #{vipNameType}
            </if>
        </trim>
    </insert>

    <select id="selectOrdersByOrdersNo" resultType="com.sqx.modules.orders.entity.Orders">
        select o.*,u.user_name as userName,s.username as sysUserName,s.qd_code as qdCode from orders o
        left join tb_user u on o.user_id=u.user_id
        left join sys_user s on s.user_id=o.sys_user_id
        where 1=1
        <if test="userName!=null and userName!=''">
            and u.user_name LIKE CONCAT('%', #{userName}, '%')
        </if>
        <if test="qdCode!=null and qdCode!=''">
            and s.qd_code = #{qdCode}
        </if>
        <if test="sysUserName!=null and sysUserName!=''">
            and s.username LIKE CONCAT('%', #{sysUserName}, '%')
        </if>
        <if test="ordersNo!=null and ordersNo!=''">
            and o.orders_no LIKE CONCAT('%', #{ordersNo}, '%')
        </if>
        <if test="status!=null and status!=-1">
            and o.status=#{status}
        </if>
        <if test="userId!=null">
            and o.user_id=#{userId}
        </if>
        <if test="ordersType!=null">
            and o.orders_type=#{ordersType}
        </if>
        <if test="courseId!=null">
            and o.course_id=#{courseId}
        </if>
        <if test="sysUserId!=null">
            and o.sys_user_id=#{sysUserId}
        </if>
        <if test="startTime!=null and startTime!='' and endTime!=null and endTime!=''">
            and o.create_time BETWEEN #{startTime} and #{endTime}
        </if>
        <if test="startTime!=null and startTime!='' and  (endTime==null or endTime=='')">
            and o.create_time &gt;= #{startTime}
        </if>
        <if test="endTime!=null and endTime!='' and  (startTime==null or startTime=='')">
            and o.create_time &lt;= #{endTime}
        </if>
        order by o.create_time desc
    </select>

    <delete id="deleteOrders" parameterType="String">
        DELETE FROM orders WHERE orders_id IN
        <foreach collection="array" item="ids" open="(" separator="," close=")">
            #{ids}
        </foreach>
    </delete>


    <select id="statisticsIncomeMoney" resultType="Double">
        select sum(pay_money) from orders where  status=1
        <if test="ordersType!=null">
            and orders_type=#{ordersType}
        </if>
        <if test="flag!=null and flag==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{time},'%Y-%m-%d')
        </if>
        <if test="flag!=null and flag==2">
            and date_format(create_time,'%Y-%m')=date_format(#{time},'%Y-%m')
        </if>
        <if test="flag!=null and flag==3">
            and date_format(create_time,'%Y')=date_format(#{time},'%Y')
        </if>
    </select>

    <select id="selectOrdersByCourseIdAndUserId" resultType="com.sqx.modules.orders.entity.Orders">
        select * from orders where user_id=#{userId} and course_id=#{courseId} and status=1 order by create_time desc limit 1
    </select>

    <select id="selectOrdersMoneyList" resultType="com.sqx.modules.orders.entity.Orders">
        select o.*,c.title from orders o
        left join tb_user u on u.user_id=o.user_id
        left join course c on o.course_id=c.course_id
        where o.status=1
        <if test="flag!=null and flag==1">
            and date_format(o.create_time,'%Y-%m-%d')=date_format(#{time},'%Y-%m-%d')
        </if>
        <if test="flag!=null and flag==2">
            and date_format(o.create_time,'%Y-%m')=date_format(#{time},'%Y-%m')
        </if>
        <if test="flag!=null and flag==3">
            and date_format(o.create_time,'%Y')=date_format(#{time},'%Y')
        </if>
    </select>

    <select id="selectOrdersCount" resultType="Integer">
        select count(*) from orders where 1=1
        <if test="sysUserId!=null and sysUserId != 1">
            and sys_user_id=#{sysUserId}
        </if>
        <if test="status!=null">
            and status=#{status}
        </if>
        <if test="ordersType!=null">
            and orders_type=#{ordersType}
        </if>
        <if test="flag!=null and flag==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{time},'%Y-%m-%d')
        </if>
        <if test="flag!=null and flag==2">
            and date_format(create_time,'%Y-%m')=date_format(#{time},'%Y-%m')
        </if>
        <if test="flag!=null and flag==3">
            and date_format(create_time,'%Y')=date_format(#{time},'%Y')
        </if>
    </select>

    <select id="selectOrdersMoney" resultType="Double">
        select sum(pay_money) from orders
        where 1=1
        <if test="sysUserId!=null and sysUserId != 1">
            and sys_user_id=#{sysUserId}
        </if>
        <if test="status!=null">
            and status=#{status}
        </if>
        <if test="courseId!=null">
            and course_id=#{courseId}
        </if>
        <if test="ordersType!=null">
            and orders_type=#{ordersType}
        </if>
        <if test="flag!=null and flag==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{time},'%Y-%m-%d')
        </if>
        <if test="flag!=null and flag==2">
            and date_format(create_time,'%Y-%m')=date_format(#{time},'%Y-%m')
        </if>
        <if test="flag!=null and flag==3">
            and date_format(create_time,'%Y')=date_format(#{time},'%Y')
        </if>
    </select>

    <select id="selectFenXiaoMoney" resultType="Double">
        select
        <if test="type==1">
            sum(ifnull(one_money,0.00))
        </if>
        <if test="type==2">
            sum(ifnull(two_money,0.00))
        </if>
        <if test="type==3">
            sum(ifnull(qd_money,0.00))
        </if>
        from orders
        where status=1
        <if test="sysUserId!=null">
            and sys_user_id=#{sysUserId}
        </if>
        <if test="flag!=null and flag==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{time},'%Y-%m-%d')
        </if>
        <if test="flag!=null and flag==2">
            and date_format(create_time,'%Y-%m')=date_format(#{time},'%Y-%m')
        </if>
        <if test="flag!=null and flag==3">
            and date_format(create_time,'%Y')=date_format(#{time},'%Y')
        </if>
    </select>


    <select id="selectOrdersCountStatisticsByYear" resultType="Integer">
        select count(*) from orders
        where 1=1
        <if test="status!=null and status!=0">
            and status=#{status}
        </if>
        <if test="flag!=null and flag==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{time},'%Y-%m-%d')
        </if>
        <if test="flag!=null and flag==2">
            and date_format(create_time,'%Y-%m')=date_format(#{time},'%Y-%m')
        </if>
        <if test="flag!=null and flag==3">
            and date_format(create_time,'%Y')=date_format(#{time},'%Y')
        </if>
    </select>


    <select id="selectOrdersCountStatisticsByDay" resultType="Integer">
        SELECT count(*)
        FROM orders
                 LEFT JOIN disc_spinning_record record ON orders.orders_id = record.source_id and record.source = 'order'
        WHERE orders.user_id = #{userId}
          AND orders.`status` = 1
          AND orders.`pay_way` = 9
          AND orders.create_time > DATE_FORMAT(CURDATE(), '%Y-%m-%d 00:00:00')
          AND record.source_id IS NULL
        ORDER BY orders.create_time
    </select>

    <select id="selectOrdersByDay" resultType="com.sqx.modules.orders.entity.Orders">
        SELECT orders.*
        FROM orders
                 LEFT JOIN disc_spinning_record record ON orders.orders_id = record.source_id
        WHERE orders.user_id = #{userId}
          AND orders.`status` = 1
          AND orders.`pay_way` = 9
          AND orders.create_time > DATE_FORMAT(CURDATE(), '%Y-%m-%d 00:00:00')
          AND record.source_id IS NULL
        ORDER BY orders.create_time LIMIT 1
    </select>

    <select id="countOrderNum" resultType="Integer">
        SELECT
            count(*)
        FROM
            orders
        WHERE
              orders.user_id = #{userId}
          AND orders.`status` = 1
          AND orders.`pay_way` = 9
        <if test="time !=null and time != ''">
            and create_time > #{time}
        </if>
    </select>
    <select id="countPayOrderByDay" resultType="java.lang.Integer">
        SELECT count(*)
        FROM orders
        WHERE orders.user_id = #{userId}
          AND orders.`status` = 1
          AND orders.`pay_way` = 9
          AND orders.create_time > DATE_FORMAT(CURDATE(), '%Y-%m-%d 00:00:00')
    </select>

    <select id="sumPayByCourseId" resultType="java.math.BigDecimal">
        select sum(pay_money) as payMoney
        from orders
        where status = 1
          and course_id = #{courseId}
        group by course_id
    </select>

</mapper>
