<?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.newfeifan.mall.module.distri.dal.mysql.shopsettlement.ShopSettlementMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.zhongxing.cn/MyBatis/x-plugins/
     -->

    <resultMap id="BaseResultMap" type="cn.newfeifan.mall.module.distri.service.dailybill.dto.OrderDTO">
        <id column="id" property="id" />
        <collection ofType="list" property="items" select="getOrderItem" column="id">
        </collection>
    </resultMap>
    <update id="setOrderItemById">
        update trade_order_item
        set settlement_time = now()
        where id in
            <foreach item="ids" collection="ids" open="(" separator="," close=")">
                #{ids}
            </foreach>
    </update>
    <update id="updateOrderById">
        update trade_order
        set status = 30
        where id in
        <foreach item="ids" collection="ids" open="(" separator="," close=")">
            #{ids}
        </foreach>
    </update>

    <select id="getOrderByMerchantIdWithsShopId"
            resultMap="BaseResultMap">
        select o.id,o.no,o.user_id from trade_order o
        join trade_order_item item on o.id = item.order_id
        where pay_status = 1
        and o.deleted = 0
        and o.shop_id = #{shopId}
        and o.merchant_id = #{merchantId}
        and date(o.create_time) <![CDATA[ <= ]]> DATE_SUB(now(),INTERVAL 7 DAY)
        and item.settlement_time is null
        and item.after_sale_status = 0
    </select>

    <select id="getOrderItem" resultType="cn.newfeifan.mall.module.distri.service.dailybill.dto.OrderItemDTO">
        select id,order_id,count,price,delivery_price,pay_price,high_precision_price,promotion_fee,cost_price,pay_integral,pay_rmb,
               settlement_price,high_precision_settlement_price,pay_consumption_points from trade_order_item
        where deleted = 0
          and settlement_time is null
          and after_sale_status = 0
          and order_id = #{id}
    </select>
    <select id="getShopName" resultType="java.lang.String">
            select name from sale_shop where id = #{shopId}
    </select>
</mapper>