<?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.ruoyi.system.mapper.PosOrderMapper">

    <resultMap type="PosOrder" id="PosOrderResult">
        <result property="id"    column="id"    />
        <result property="ddId"    column="dd_id"    />
        <result property="shId"    column="sh_id"    />
        <result property="mdId"    column="md_id"    />
        <result property="cretim"    column="cretim"    />
        <result property="shdzId"    column="shdz_id"    />
        <result property="userId"    column="user_id"    />
        <result property="amount"    column="amount"    />
        <result property="remarks"    column="remarks"    />
        <result property="food"    column="food"    />
        <result property="state"    column="state"    />
        <result property="type"    column="type"    />
        <result property="delryTime"    column="delry_time"    />
        <result property="yhId"    column="yh_id"    />
        <result property="jvli"    column="jvli"    />
        <result property="freight"    column="freight"    />
        <result property="diningStatus"    column="dining_status"    />
        <result property="qsId"    column="qs_id"    />
        <result property="payUrl"    column="pay_url"    />
        <result property="collectPayment"    column="collect_payment"    />
        <result property="activity"    column="activity"    />
        <result property="kefuState"    column="kefu_state"    />
        <result property="kefuContent"    column="kefu_content"    />
        <result property="kefuRepeat"    column="kefu_repeat"    />
        <result property="repeatDdId"    column="repeat_dd_id"    />
        <result property="shAddress"    column="sh_address"    />
        <result property="mdActivity" column="md_activity" />
        <result property="yhName" column="yh_name" />
        <result property="mdYhId" column="md_yh_id" />
        <result property="mdYhName" column="md_yh_name" />
        <result property="mdDiscountAmount" column="md_discount_amount" />
        <result property="salesName" column="sales_name" />
        <result property="mdSalesName" column="md_sales_name" />
        <result property="salesReduction" column="sales_reduction" />
        <result property="mdSalesReduction" column="md_sales_reduction" />
        <result property="points" column="points" />
        <result property="pointsReduction" column="points_reduction" />
        <result property="sdTime" column="sd_time" />
        <result property="payType" column="pay_type" />
        <result property="parentDdId" column="parent_dd_id" />
        <result property="orderCategory" column="order_category" />
        <result property="tableId" column="table_id" />
        <result property="tableNo" column="table_no" />
        <result property="logo" column="logo" />
        <result property="posName" column="pos_name" />
        <result property="foodAmount" column="food_amount" />
        <result property="pickUpNum" column="pick_up_num" />
        <result property="pickUpTime" column="pick_up_time" />
        <result property="reservePhone" column="reserve_phone" />
        <result property="isAccepted" column="is_accepted" />
        <result property="isDisplay" column="is_display" />
        <result property="displayTime" column="display_time" />
        <result property="deliveryStatus" column="delivery_status" />
        <result property="payStatus" column="pay_status" />
        <result property="afterSaleStatus" column="after_sale_status" />
    </resultMap>

    <sql id="selectPosOrderVo">
        select id, dd_id, sh_id, md_id, cretim, shdz_id, user_id,sh_address, amount, remarks, state, type, delry_time,food,yh_id,yh_name,md_yh_id,md_yh_name,md_discount_amount,jvli,freight,dining_status,qs_id,pay_url,collect_payment,activity,md_activity,kefu_state,kefu_content,kefu_repeat,repeat_dd_id,sales_name,md_sales_name,sales_reduction,md_sales_reduction,points,points_reduction,sd_time,pay_type,parent_dd_id,order_category,table_id,table_no,logo,pos_name,food_amount,pick_up_num,pick_up_time,reserve_phone,is_accepted,is_display,display_time,delivery_status,pay_status,after_sale_status from pos_order
    </sql>

    <select id="selectPosOrderList" parameterType="PosOrder" resultMap="PosOrderResult">
        <include refid="selectPosOrderVo"/>
        <where>
            <if test="ddId != null "> and dd_id = #{ddId}</if>
            <if test="shId != null "> and sh_id = #{shId}</if>
            <if test="mdId != null "> and md_id = #{mdId}</if>
            <if test="cretim != null "> and cretim = #{cretim}</if>
            <if test="shdzId != null "> and shdz_id = #{shdzId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="amount != null "> and amount = #{amount}</if>
            <if test="state != null "> and state = #{state}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="delryTime != null  and delryTime != ''"> and delry_time = #{delryTime}</if>
            <if test="cretimStart != null  and cretimStart != ''"> and cretim > date_format(#{cretimStart},'%y-%m-%d')</if>
            <if test="cretimEnd != null  and cretimEnd != ''"> and cretim &lt; DATE_FORMAT(DATE_ADD(date_format(#{cretimEnd},'%y-%m-%d'),INTERVAL 1 DAY),'%Y-%m-%d')</if>
            <if test="collectPayment != null  and collectPayment != ''"> and collect_payment = #{collectPayment}</if>
            <if test="mdActivity != null"> and md_activity = #{mdActivity}</if>
            <if test="yhName != null and yhName != ''"> and yh_name = #{yhName}</if>
            <if test="mdYhId != null"> and md_yh_id = #{mdYhId}</if>
            <if test="mdYhName != null and mdYhName != ''"> and md_yh_name = #{mdYhName}</if>
            <if test="mdDiscountAmount != null"> and md_discount_amount = #{mdDiscountAmount}</if>
            <if test="salesName != null and salesName != ''"> and sales_name = #{salesName}</if>
            <if test="mdSalesName != null and mdSalesName != ''"> and md_sales_name = #{mdSalesName}</if>
            <if test="salesReduction != null"> and sales_reduction = #{salesReduction}</if>
            <if test="mdSalesReduction != null"> and md_sales_reduction = #{mdSalesReduction}</if>
            <if test="points != null"> and points = #{points}</if>
            <if test="pointsReduction != null"> and points_reduction = #{pointsReduction}</if>
            <if test="sdTime != null"> and sd_time = #{sdTime}</if>
            <if test="payType != null"> and pay_type =#{payType}</if>
            <if test="parentDdId != null and parentDdId != ''"> and parent_dd_id = #{parentDdId}</if>
            <if test="orderCategory != null and orderCategory != ''"> and order_category = #{orderCategory}</if>
            <if test="tableId != null"> and table_id = #{tableId}</if>
            <if test="logo != null and logo != ''"> and logo = #{logo}</if>
            <if test="posName != null and posName != ''"> and pos_name = #{posName}</if>
            <if test="payStatus != null"> and pay_status = #{payStatus}</if>
            <if test="deliveryStatus != null"> and delivery_status = #{deliveryStatus}</if>
            <if test="afterSaleStatus != null"> and after_sale_status = #{afterSaleStatus}</if>
        </where>
        order by id desc
    </select>

    <select id="selectPosOrderShList" parameterType="PosOrder" resultMap="PosOrderResult">
        <include refid="selectPosOrderVo"/>
        <where>
            <if test="ddId != null "> and dd_id = #{ddId}</if>
            <if test="shId != null "> and sh_id = #{shId}</if>
            <if test="mdId != null "> and md_id = #{mdId}</if>
            <if test="cretim != null "> and cretim = #{cretim}</if>
            <if test="shdzId != null "> and shdz_id = #{shdzId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="amount != null "> and amount = #{amount}</if>
            <if test="state != null "> and state > #{state} and state &lt; 10</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="delryTime != null  and delryTime != ''"> and delry_time = #{delryTime}</if>
            <if test="cretimStart != null  and cretimStart != ''"> and cretim > date_format(#{cretimStart},'%y-%m-%d')</if>
            <if test="cretimEnd != null  and cretimEnd != ''"> and cretim &lt; DATE_FORMAT(DATE_ADD(date_format(#{cretimEnd},'%y-%m-%d'),INTERVAL 1 DAY),'%Y-%m-%d')</if>
            <if test="collectPayment != null  and collectPayment != ''"> and collect_payment = #{collectPayment}</if>
            <if test="mdActivity != null"> and md_activity = #{mdActivity}</if>
            <if test="yhName != null and yhName != ''"> and yh_name = #{yhName}</if>
            <if test="mdYhId != null"> and md_yh_id = #{mdYhId}</if>
            <if test="mdYhName != null and mdYhName != ''"> and md_yh_name = #{mdYhName}</if>
            <if test="mdDiscountAmount != null"> and md_discount_amount = #{mdDiscountAmount}</if>
            <if test="salesName != null and salesName != ''"> and sales_name = #{salesName}</if>
            <if test="mdSalesName != null and mdSalesName != ''"> and md_sales_name = #{mdSalesName}</if>
            <if test="salesReduction != null"> and sales_reduction = #{salesReduction}</if>
            <if test="mdSalesReduction != null"> and md_sales_reduction = #{mdSalesReduction}</if>
            <if test="points != null"> and points = #{points}</if>
            <if test="pointsReduction != null"> and points_reduction = #{pointsReduction}</if>
             <if test="sdTime != null"> and sd_time =#{sdTime}</if>
            <if test="payType != null"> and pay_type =#{payType}</if>
            <if test="parentDdId != null and parentDdId != ''"> and parent_dd_id = #{parentDdId}</if>
            <if test="orderCategory != null and orderCategory != ''"> and order_category = #{orderCategory}</if>
            <if test="tableId != null"> and table_id = #{tableId}</if>
            <if test="logo != null and logo != ''"> and logo = #{logo}</if>
            <if test="posName != null and posName != ''"> and pos_name = #{posName}</if>
        </where>
        order by kefu_state desc,cretim desc
    </select>

    <select id="selectPosOrderById" parameterType="Long" resultMap="PosOrderResult">
        <include refid="selectPosOrderVo"/>
        where id = #{id}
    </select>

    <insert id="insertPosOrder" parameterType="PosOrder" useGeneratedKeys="true" keyProperty="id">
        insert into pos_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="ddId != null">dd_id,</if>
            <if test="shId != null">sh_id,</if>
            <if test="mdId != null">md_id,</if>
            <if test="cretim != null">cretim,</if>
            <if test="shdzId != null">shdz_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="amount != null">amount,</if>
            <if test="remarks != null">remarks,</if>
            <if test="state != null">state,</if>
            <if test="type != null">type,</if>
            <if test="delryTime != null and delryTime != ''">delry_time,</if>
            <if test="collectPayment != null and collectPayment != ''">collect_payment,</if>
            <if test="mdActivity != null">md_activity,</if>
            <if test="yhName != null and yhName != ''">yh_name,</if>
            <if test="mdYhId != null">md_yh_id,</if>
            <if test="mdYhName != null and mdYhName != ''">md_yh_name,</if>
            <if test="mdDiscountAmount != null">md_discount_amount,</if>
            <if test="salesName != null and salesName != ''">sales_name,</if>
            <if test="mdSalesName != null and mdSalesName != ''">md_sales_name,</if>
            <if test="salesReduction != null">sales_reduction,</if>
            <if test="mdSalesReduction != null">md_sales_reduction,</if>
            <if test="points != null">points,</if>
            <if test="pointsReduction != null">points_reduction,</if>
            <if test="sdTime != null">sd_time,</if>
            <if test="payType != null">pay_type,</if>
            <if test="parentDdId != null and parentDdId != ''">parent_dd_id,</if>
            <if test="orderCategory != null and orderCategory != ''">order_category,</if>
            <if test="tableId != null">table_id,</if>
            <if test="tableNo != null and tableNo != ''">table_no,</if>
            <if test="logo != null and logo != ''">logo,</if>
            <if test="posName != null and posName != ''">pos_name,</if>
            <if test="foodAmount != null">food_amount,</if>
            <if test="pickUpNum != null">pick_up_num,</if>
            <if test="pickUpTime != null and pickUpTime != ''">pick_up_time,</if>
            <if test="reservePhone != null and reservePhone != ''">reserve_phone,</if>
            <if test="isAccepted != null">is_accepted,</if>
            <if test="isDisplay != null">is_display,</if>
            <if test="displayTime != null">display_time,</if>
            <if test="deliveryStatus != null">delivery_status,</if>
            <if test="payStatus != null">pay_status,</if>
            <if test="afterSaleStatus != null">after_sale_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="ddId != null">#{ddId},</if>
            <if test="shId != null">#{shId},</if>
            <if test="mdId != null">#{mdId},</if>
            <if test="cretim != null">#{cretim},</if>
            <if test="shdzId != null">#{shdzId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="amount != null">#{amount},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="state != null">#{state},</if>
            <if test="type != null">#{type},</if>
            <if test="delryTime != null and delryTime != ''">#{delryTime},</if>
            <if test="collectPayment != null and collectPayment != ''">#{collectPayment},</if>
            <if test="mdActivity != null">#{mdActivity},</if>
            <if test="yhName != null and yhName != ''">#{yhName},</if>
            <if test="mdYhId != null">#{mdYhId},</if>
            <if test="mdYhName != null and mdYhName != ''">#{mdYhName},</if>
            <if test="mdDiscountAmount != null">#{mdDiscountAmount},</if>
            <if test="salesName != null and salesName != ''">#{salesName},</if>
            <if test="mdSalesName != null and mdSalesName != ''">#{mdSalesName},</if>
            <if test="salesReduction != null">#{salesReduction},</if>
            <if test="mdSalesReduction != null">#{mdSalesReduction},</if>
            <if test="points != null">#{points},</if>
            <if test="pointsReduction != null">#{pointsReduction},</if>
            <if test="sdTime != null">#{sdTime},</if>
            <if test="payType != null">#{payType},</if>
            <if test="parentDdId != null and parentDdId != ''">#{parentDdId},</if>
            <if test="orderCategory != null and orderCategory != ''">#{orderCategory},</if>
            <if test="tableId != null">#{tableId},</if>
            <if test="tableNo != null and tableNo != ''">#{tableNo},</if>
            <if test="logo != null and logo != ''">#{logo},</if>
            <if test="posName != null and posName != ''">#{posName},</if>
            <if test="foodAmount != null">#{foodAmount},</if>
            <if test="pickUpNum != null">#{pickUpNum},</if>
            <if test="pickUpTime != null and pickUpTime != ''">#{pickUpTime},</if>
            <if test="reservePhone != null and reservePhone != ''">#{reservePhone},</if>
            <if test="isAccepted != null">#{isAccepted},</if>
            <if test="isDisplay != null">#{isDisplay},</if>
            <if test="displayTime != null">#{displayTime},</if>
            <if test="deliveryStatus != null">#{deliveryStatus},</if>
            <if test="payStatus != null">#{payStatus},</if>
            <if test="afterSaleStatus != null">#{afterSaleStatus},</if>
         </trim>
    </insert>

    <update id="updatePosOrder" parameterType="PosOrder">
        update pos_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="ddId != null">dd_id = #{ddId},</if>
            <if test="shId != null">sh_id = #{shId},</if>
            <if test="mdId != null">md_id = #{mdId},</if>
            <if test="cretim != null">cretim = #{cretim},</if>
            <if test="shdzId != null">shdz_id = #{shdzId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="state != null">state = #{state},</if>
            <if test="type != null">type = #{type},</if>
            <if test="kefuState != null">kefu_state = #{kefuState},</if>
            <if test="kefuContent != null">kefu_content = #{kefuContent},</if>
            <if test="delryTime != null and delryTime != ''">delry_time = #{delryTime},</if>
            <if test="collectPayment != null and collectPayment != ''">collect_payment = #{collectPayment},</if>
            <if test="mdActivity != null">md_activity = #{mdActivity},</if>
            <if test="yhName != null and yhName != ''">yh_name = #{yhName},</if>
            <if test="mdYhId != null">md_yh_id = #{mdYhId},</if>
            <if test="mdYhName != null and mdYhName != ''">md_yh_name = #{mdYhName},</if>
            <if test="mdDiscountAmount != null">md_discount_amount = #{mdDiscountAmount},</if>
            <if test="salesName != null and salesName != ''">sales_name = #{salesName},</if>
            <if test="mdSalesName != null and mdSalesName != ''">md_sales_name = #{mdSalesName},</if>
            <if test="salesReduction != null">sales_reduction = #{salesReduction},</if>
            <if test="mdSalesReduction != null">md_sales_reduction = #{mdSalesReduction},</if>
            <if test="points != null">points = #{points},</if>
            <if test="pointsReduction != null">points_reduction = #{pointsReduction},</if>
            <if test="sdTime != null">sd_time = #{sdTime},</if>
            <if test="payType != null">pay_type = #{payType},</if>
            <if test="parentDdId != null and parentDdId != ''">parent_dd_id = #{parentDdId},</if>
            <if test="orderCategory != null and orderCategory != ''">order_category = #{orderCategory},</if>
            <if test="tableId != null">table_id = #{tableId},</if>
            <if test="tableNo != null and tableNo != ''">table_no = #{tableNo},</if>
            <if test="logo != null and logo != ''">logo = #{logo},</if>
            <if test="posName != null and posName != ''">pos_name = #{posName},</if>
            <if test="foodAmount != null">food_amount = #{foodAmount},</if>
            <if test="pickUpNum != null">pick_up_num = #{pickUpNum},</if>
            <if test="pickUpTime != null and pickUpTime != ''">pick_up_time = #{pickUpTime},</if>
            <if test="reservePhone != null and reservePhone != ''">reserve_phone = #{reservePhone},</if>
            <if test="isAccepted != null">is_accepted = #{isAccepted},</if>
            <if test="isDisplay != null">is_display = #{isDisplay},</if>
            <if test="displayTime != null">display_time = #{displayTime},</if>
            <if test="deliveryStatus != null">delivery_status = #{deliveryStatus},</if>
            <if test="payStatus != null">pay_status = #{payStatus},</if>
            <if test="afterSaleStatus != null">after_sale_status = #{afterSaleStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePosOrderById" parameterType="Long">
        delete from pos_order where id = #{id}
    </delete>

    <delete id="deletePosOrderByIds" parameterType="String">
        delete from pos_order where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="countOrderByNowYear" resultType="int">
        SELECT COUNT(*)
        FROM pos_order
        <where>
            <if test="nowYearFirstDay != null">
                cretim &gt;= #{nowYearFirstDay}
            </if>
        </where>
    </select>

    <select id="countOrderSuccessByNowYear" resultType="int">
        SELECT COUNT(*)
        FROM pos_order
        <where>
            <if test="nowYearFirstDay != null">
                cretim &gt;= #{nowYearFirstDay}
            </if>
            AND state in (5, 11)
        </where>
    </select>

    <select id="countOrderFailByNowYear" resultType="int">
        SELECT COUNT(*)
        FROM pos_order
        <where>
            <if test="nowYearFirstDay != null">
                cretim &gt;= #{nowYearFirstDay}
            </if>
            AND state = 10
        </where>
    </select>

    <select id="totalTurnoverByNowYear" resultType="Double">
        SELECT sum(amount)
        FROM pos_order
        <where>
            <if test="nowYearFirstDay != null">
                cretim &gt;= #{nowYearFirstDay}
            </if>
            AND state in (5, 11)
        </where>
    </select>
    <select id="getOrderCountByDate" resultType="int">
        SELECT IFNULL(COUNT(*),0) as amount FROM pos_order
        <where>
            <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
                and cretim between #{startDate} and #{endDate}
            </if>
            <if test="startDate != null and startDate != '' and (endDate == null or endDate == '')">
                and cretim >= #{startDate}
            </if>
            <if test="(startDate == null or startDate == '') and (endDate != null and endDate != '')">
                and cretim &lt;= #{endDate}
            </if>
        </where>
    </select>
    <select id="getOrderSumByDate" resultType="int">
    SELECT IFNULL(SUM(amount),0) as amount FROM pos_order
        <where>
            <if test="startDate != null and startDate != '' and endDate != null and endDate != ''">
                and cretim between #{startDate} and #{endDate}
            </if>
            <if test="startDate != null and startDate != '' and (endDate == null or endDate == '')">
                and cretim >= #{startDate}
            </if>
            <if test="(startDate == null or startDate == '') and (endDate != null and endDate != '')">
                and cretim &lt;= #{endDate}
            </if>
        </where>
    </select>
</mapper>
