<?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.iocoder.yudao.module.order.mapper.RepairOrderInfoMapper">

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

    <select id="queryListPage" resultType="cn.iocoder.yudao.module.order.vo.RepairOrderInfoRespVO">
        SELECT
        roi.*
        FROM
        repair_order_info roi
        <where>
            roi.deleted = 0
            <if test="entity.orderNo != null  and entity.orderNo != ''">
                and roi.order_no like concat('%', #{entity.orderNo}, '%')
            </if>
            <if test="entity.goodsTitle != null  and entity.goodsTitle != ''">
                and roi.goods_title like concat('%', #{entity.goodsTitle}, '%')
            </if>
            <if test="entity.goodsType != null  and entity.goodsType != ''">
                and roi.goods_type = #{entity.goodsType}
            </if>
            <if test="entity.cusName != null  and entity.cusName != ''">
                and roi.cus_name like concat('%', #{entity.cusName}, '%')
            </if>
            <if test="entity.orderStatus != null  and entity.orderStatus != ''">
                and roi.order_status = #{entity.orderStatus}
            </if>
            <if test="entity.payType != null  and entity.payType != ''">
                and roi.pay_type = #{entity.payType}
            </if>
        </where>
        order by roi.create_time desc
    </select>

    <select id="getOrderPageByStatus" resultType="cn.iocoder.yudao.module.order.vo.RepairOrderInfoRespVO">
        select roi.*,drt.tickets_work_status as status,drt.tickets_status as mainStatus
        from repair_order_info roi
        left join dl_repair_tickets drt on
        roi.goods_id = drt.id
        where roi.deleted = '0'
        <if test="map.userId != null and map.userId != ''">
            and roi.user_id = #{map.userId}
        </if>
        <if test="map.creator != null and map.creator != ''">
            and roi.creator = #{map.creator}
        </if>
        <if test="map.orderStatus != null  and map.orderStatus != ''">
            and roi.order_status = #{map.orderStatus}
        </if>
        <if test="map.selectType != null and map.selectType != ''">
            <choose>
                <when test="map.selectType == 'working'">
                    --    维修中的订单   --
                and (drt.tickets_status = '04' OR drt.tickets_status = '05' OR drt.tickets_status = '05' OR drt.tickets_status ='01')
                </when>
                <when test="map.selectType == 'waitingPay'">
                    --    待支付的订单   --
                and (roi.pay_type is not null AND roi.order_status='0' )
                </when>
                <when test="map.selectType == 'waitingComment'">
                    --    待评价的订单   --
                and (roi.comment_star is null AND roi.order_status='1' )
                </when>
            </choose>
        </if>
        <if test="map.status != null and map.status != ''">
            <choose>
                <when test="map.status != '00'">
                    and drt.tickets_work_status = #{map.status}
                </when>
                <when test="map.status == '00'">
                    and roi.comment_time is null and roi.order_status = '1'
                </when>
            </choose>
        </if>
        order by roi.create_time desc
    </select>
    <select id="census" resultType="cn.iocoder.yudao.module.order.vo.RepairOrderCensusVO">
        SELECT
            -- 统计已入账和未入账的数量
            SUM(CASE WHEN order_status = '1' THEN pay_money ELSE 0 END)                     AS credited,
            SUM(CASE WHEN order_status = '0' THEN goods_price ELSE 0 END)                   AS notCredited,
            -- 统计已入账的细分情况
            SUM(CASE WHEN order_status = '1' AND pay_type = '01' THEN pay_money ELSE 0 END) AS onlinePay,
            SUM(CASE WHEN order_status = '1' AND pay_type = '02' THEN pay_money ELSE 0 END) AS cashPay,
            SUM(CASE WHEN order_status = '1' AND pay_type = '03' THEN pay_money ELSE 0 END) AS signedPay
        FROM repair_order_info;
    </select>
</mapper>