<?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 映射文件
  @Author: Antigravity
-->
<mapper namespace="org.dromara.erp.mapper.ErpOrderMapper">

    <!-- 自定义多表联查订单及外部 ERP 状态 -->
    <select id="selectOrderVoPage" resultType="org.dromara.erp.domain.vo.ErpOrderVo">
        SELECT 
            o.FRowID as rowId,
            o.FCode as code,
            o.FPlacer as placer,
            o.FPlaceTime as placeTime,
            o.FIsConfirmed as isConfirmed,
            o.FUrgentFlag as urgentFlag,
            o.FErpDocCode as docCode,
            o.CreateTime as createTime,
            o.FClientID as clientId,
            o.FClientName as clientName,
            m.Finished as finished,
            m.ExFinished as exFinished,
            m.Reviewed as reviewed,
            m.Approved as approved,
            m.Canceled as canceled,
            CASE 
                WHEN m.Finished = 1 THEN 5
                WHEN m.Finished = 0 AND m.ExFinished = 1 THEN 4
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 1 THEN 3
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 1 THEN 2
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 0 AND m.Canceled = 1 THEN 6
                WHEN (m.Canceled IS NULL OR m.Canceled = 0) AND (m.Finished IS NULL OR m.Finished = 0) 
                     AND (m.ExFinished IS NULL OR m.ExFinished = 0) AND (m.Reviewed IS NULL OR m.Reviewed = 0) 
                     AND (m.Approved IS NULL OR m.Approved = 0) AND o.FIsConfirmed = 1 THEN 1
                ELSE 0
            END as status
        FROM t_Applet_Order o
        LEFT JOIN t_Sale_OrderMaster m ON o.FErpDocCode = m.DocCode
        <where>
            <if test="query.code != null and query.code != ''">
                AND o.FCode LIKE concat('%', #{query.code}, '%')
            </if>
            <if test="query.placer != null">
                AND o.FPlacer = #{query.placer}
            </if>
            <if test="query.docCode != null and query.docCode != ''">
                AND o.DocCode LIKE concat('%', #{query.docCode}, '%')
            </if>
            <if test="query.isConfirmed != null">
                <choose>
                    <when test="query.isConfirmed == 0">
                        AND o.FIsConfirmed = 0 
                        AND (m.Finished IS NULL OR m.Finished = 0)
                        AND (m.ExFinished IS NULL OR m.ExFinished = 0)
                        AND (m.Reviewed IS NULL OR m.Reviewed = 0)
                        AND (m.Approved IS NULL OR m.Approved = 0)
                        AND (m.Canceled IS NULL OR m.Canceled = 0)
                    </when>
                    <when test="query.isConfirmed == 1">
                        AND (
                            o.FIsConfirmed = 1 
                            OR m.Finished = 1 
                            OR m.ExFinished = 1 
                            OR m.Reviewed = 1 
                            OR m.Approved = 1 
                            OR m.Canceled = 1
                        )
                    </when>
                </choose>
            </if>
            <if test="query.status != null">
                <choose>
                    <when test="query.status == 0">
                        AND o.FIsConfirmed = 0 
                        AND (m.Finished IS NULL OR m.Finished = 0)
                        AND (m.ExFinished IS NULL OR m.ExFinished = 0)
                        AND (m.Reviewed IS NULL OR m.Reviewed = 0)
                        AND (m.Approved IS NULL OR m.Approved = 0)
                        AND (m.Canceled IS NULL OR m.Canceled = 0)
                    </when>
                    <when test="query.status == 1">
                        AND o.FIsConfirmed = 1
                        AND (m.Finished IS NULL OR m.Finished = 0)
                        AND (m.ExFinished IS NULL OR m.ExFinished = 0)
                        AND (m.Reviewed IS NULL OR m.Reviewed = 0)
                        AND (m.Approved IS NULL OR m.Approved = 0)
                        AND (m.Canceled IS NULL OR m.Canceled = 0)
                    </when>
                    <when test="query.status == 2">
                        AND m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 1
                    </when>
                    <when test="query.status == 3">
                        AND m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 1
                    </when>
                    <when test="query.status == 4">
                        AND m.Finished = 0 AND m.ExFinished = 1
                    </when>
                    <when test="query.status == 5">
                        AND m.Finished = 1
                    </when>
                    <when test="query.status == 6">
                        AND m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 0 AND m.Canceled = 1
                    </when>
                </choose>
            </if>
        </where>
        ORDER BY o.CreateTime DESC
    </select>

    <!-- 自定义多表联查订单列表及外部 ERP 状态 -->
    <select id="selectOrderVoList" resultType="org.dromara.erp.domain.vo.ErpOrderVo">
        SELECT 
            o.FRowID as rowId,
            o.FCode as code,
            o.FPlacer as placer,
            o.FPlaceTime as placeTime,
            o.FIsConfirmed as isConfirmed,
            o.FUrgentFlag as urgentFlag,
            o.FErpDocCode as docCode,
            o.CreateTime as createTime,
            o.FClientID as clientId,
            o.FClientName as clientName,
            m.Finished as finished,
            m.ExFinished as exFinished,
            m.Reviewed as reviewed,
            m.Approved as approved,
            m.Canceled as canceled,
            CASE 
                WHEN m.Finished = 1 THEN 5
                WHEN m.Finished = 0 AND m.ExFinished = 1 THEN 4
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 1 THEN 3
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 1 THEN 2
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 0 AND m.Canceled = 1 THEN 6
                WHEN (m.Canceled IS NULL OR m.Canceled = 0) AND (m.Finished IS NULL OR m.Finished = 0) 
                     AND (m.ExFinished IS NULL OR m.ExFinished = 0) AND (m.Reviewed IS NULL OR m.Reviewed = 0) 
                     AND (m.Approved IS NULL OR m.Approved = 0) AND o.FIsConfirmed = 1 THEN 1
                ELSE 0
            END as status
        FROM t_Applet_Order o
        LEFT JOIN t_Sale_OrderMaster m ON o.FErpDocCode = m.DocCode
        <where>
            <if test="query.code != null and query.code != ''">
                AND o.FCode = #{query.code}
            </if>
            <if test="query.placer != null">
                AND o.FPlacer = #{query.placer}
            </if>
            <if test="query.docCode != null and query.docCode != ''">
                AND o.DocCode = #{query.docCode}
            </if>
            <if test="query.isConfirmed != null">
                <choose>
                    <when test="query.isConfirmed == 0">
                        AND o.FIsConfirmed = 0 
                        AND (m.Finished IS NULL OR m.Finished = 0)
                        AND (m.ExFinished IS NULL OR m.ExFinished = 0)
                        AND (m.Reviewed IS NULL OR m.Reviewed = 0)
                        AND (m.Approved IS NULL OR m.Approved = 0)
                        AND (m.Canceled IS NULL OR m.Canceled = 0)
                    </when>
                    <when test="query.isConfirmed == 1">
                        AND (
                            o.FIsConfirmed = 1 
                            OR m.Finished = 1 
                            OR m.ExFinished = 1 
                            OR m.Reviewed = 1 
                            OR m.Approved = 1 
                            OR m.Canceled = 1
                        )
                    </when>
                </choose>
            </if>
            <if test="query.status != null">
                <choose>
                    <when test="query.status == 0">
                        AND o.FIsConfirmed = 0 
                        AND (m.Finished IS NULL OR m.Finished = 0)
                        AND (m.ExFinished IS NULL OR m.ExFinished = 0)
                        AND (m.Reviewed IS NULL OR m.Reviewed = 0)
                        AND (m.Approved IS NULL OR m.Approved = 0)
                        AND (m.Canceled IS NULL OR m.Canceled = 0)
                    </when>
                    <when test="query.status == 1">
                        AND o.FIsConfirmed = 1
                        AND (m.Finished IS NULL OR m.Finished = 0)
                        AND (m.ExFinished IS NULL OR m.ExFinished = 0)
                        AND (m.Reviewed IS NULL OR m.Reviewed = 0)
                        AND (m.Approved IS NULL OR m.Approved = 0)
                        AND (m.Canceled IS NULL OR m.Canceled = 0)
                    </when>
                    <when test="query.status == 2">
                        AND m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 1
                    </when>
                    <when test="query.status == 3">
                        AND m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 1
                    </when>
                    <when test="query.status == 4">
                        AND m.Finished = 0 AND m.ExFinished = 1
                    </when>
                    <when test="query.status == 5">
                        AND m.Finished = 1
                    </when>
                    <when test="query.status == 6">
                        AND m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 0 AND m.Canceled = 1
                    </when>
                </choose>
            </if>
        </where>
        ORDER BY o.CreateTime DESC
    </select>

    <!-- 自定义单条联查订单及外部 ERP 状态 -->
    <select id="selectOrderVoById" resultType="org.dromara.erp.domain.vo.ErpOrderVo">
        SELECT 
            o.FRowID as rowId,
            o.FCode as code,
            o.FPlacer as placer,
            o.FPlaceTime as placeTime,
            o.FIsConfirmed as isConfirmed,
            o.FUrgentFlag as urgentFlag,
            o.FErpDocCode as docCode,
            o.CreateTime as createTime,
            o.FClientID as clientId,
            o.FClientName as clientName,
            m.Finished as finished,
            m.ExFinished as exFinished,
            m.Reviewed as reviewed,
            m.Approved as approved,
            m.Canceled as canceled,
            CASE 
                WHEN m.Finished = 1 THEN 5
                WHEN m.Finished = 0 AND m.ExFinished = 1 THEN 4
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 1 THEN 3
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 1 THEN 2
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 0 AND m.Canceled = 1 THEN 6
                WHEN (m.Canceled IS NULL OR m.Canceled = 0) AND (m.Finished IS NULL OR m.Finished = 0) 
                     AND (m.ExFinished IS NULL OR m.ExFinished = 0) AND (m.Reviewed IS NULL OR m.Reviewed = 0) 
                     AND (m.Approved IS NULL OR m.Approved = 0) AND o.FIsConfirmed = 1 THEN 1
                ELSE 0
            END as status
        FROM t_Applet_Order o
        LEFT JOIN t_Sale_OrderMaster m ON o.FErpDocCode = m.DocCode
        WHERE o.FRowID = #{rowId}
    </select>

    <!-- 按综合状态统计订单数量 -->
    <select id="countOrderStatus" resultType="java.util.LinkedHashMap">
        SELECT 
            CASE 
                WHEN m.Finished = 1 THEN 5
                WHEN m.Finished = 0 AND m.ExFinished = 1 THEN 4
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 1 THEN 3
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 1 THEN 2
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 0 AND m.Canceled = 1 THEN 6
                WHEN (m.Canceled IS NULL OR m.Canceled = 0) AND (m.Finished IS NULL OR m.Finished = 0) 
                     AND (m.ExFinished IS NULL OR m.ExFinished = 0) AND (m.Reviewed IS NULL OR m.Reviewed = 0) 
                     AND (m.Approved IS NULL OR m.Approved = 0) AND o.FIsConfirmed = 1 THEN 1
                ELSE 0
            END as statusKey,
            COUNT(1) as statusCount
        FROM t_Applet_Order o
        LEFT JOIN t_Sale_OrderMaster m ON o.FErpDocCode = m.DocCode
        GROUP BY 
            CASE 
                WHEN m.Finished = 1 THEN 5
                WHEN m.Finished = 0 AND m.ExFinished = 1 THEN 4
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 1 THEN 3
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 1 THEN 2
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 0 AND m.Canceled = 1 THEN 6
                WHEN (m.Canceled IS NULL OR m.Canceled = 0) AND (m.Finished IS NULL OR m.Finished = 0) 
                     AND (m.ExFinished IS NULL OR m.ExFinished = 0) AND (m.Reviewed IS NULL OR m.Reviewed = 0) 
                     AND (m.Approved IS NULL OR m.Approved = 0) AND o.FIsConfirmed = 1 THEN 1
                ELSE 0
            END
        ORDER BY statusKey
    </select>

    <!-- 按客户ID分页查询订单（不限制下单人） -->
    <select id="selectOrderVoPageByClientId" resultType="org.dromara.erp.domain.vo.ErpOrderVo">
        SELECT 
            o.FRowID as rowId,
            o.FCode as code,
            o.FPlacer as placer,
            o.FPlaceTime as placeTime,
            o.FIsConfirmed as isConfirmed,
            o.FUrgentFlag as urgentFlag,
            o.FErpDocCode as docCode,
            o.CreateTime as createTime,
            o.FClientID as clientId,
            o.FClientName as clientName,
            m.Finished as finished,
            m.ExFinished as exFinished,
            m.Reviewed as reviewed,
            m.Approved as approved,
            m.Canceled as canceled,
            CASE 
                WHEN m.Finished = 1 THEN 5
                WHEN m.Finished = 0 AND m.ExFinished = 1 THEN 4
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 1 THEN 3
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 1 THEN 2
                WHEN m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 0 AND m.Canceled = 1 THEN 6
                WHEN (m.Canceled IS NULL OR m.Canceled = 0) AND (m.Finished IS NULL OR m.Finished = 0) 
                     AND (m.ExFinished IS NULL OR m.ExFinished = 0) AND (m.Reviewed IS NULL OR m.Reviewed = 0) 
                     AND (m.Approved IS NULL OR m.Approved = 0) AND o.FIsConfirmed = 1 THEN 1
                ELSE 0
            END as status
        FROM t_Applet_Order o
        LEFT JOIN t_Sale_OrderMaster m ON o.FErpDocCode = m.DocCode
        <where>
            <if test="query.clientId != null and query.clientId != ''">
                AND o.FClientID = #{query.clientId}
            </if>
            <if test="query.status != null">
                <choose>
                    <when test="query.status == 0">
                        AND o.FIsConfirmed = 0 
                        AND (m.Finished IS NULL OR m.Finished = 0)
                        AND (m.ExFinished IS NULL OR m.ExFinished = 0)
                        AND (m.Reviewed IS NULL OR m.Reviewed = 0)
                        AND (m.Approved IS NULL OR m.Approved = 0)
                        AND (m.Canceled IS NULL OR m.Canceled = 0)
                    </when>
                    <when test="query.status == 1">
                        AND o.FIsConfirmed = 1
                        AND (m.Finished IS NULL OR m.Finished = 0)
                        AND (m.ExFinished IS NULL OR m.ExFinished = 0)
                        AND (m.Reviewed IS NULL OR m.Reviewed = 0)
                        AND (m.Approved IS NULL OR m.Approved = 0)
                        AND (m.Canceled IS NULL OR m.Canceled = 0)
                    </when>
                    <when test="query.status == 2">
                        AND m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 1
                    </when>
                    <when test="query.status == 3">
                        AND m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 1
                    </when>
                    <when test="query.status == 4">
                        AND m.Finished = 0 AND m.ExFinished = 1
                    </when>
                    <when test="query.status == 5">
                        AND m.Finished = 1
                    </when>
                    <when test="query.status == 6">
                        AND m.Finished = 0 AND m.ExFinished = 0 AND m.Reviewed = 0 AND m.Approved = 0 AND m.Canceled = 1
                    </when>
                </choose>
            </if>
        </where>
        ORDER BY o.CreateTime DESC
    </select>

</mapper>
