<?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.frequency.order.mapper.OrderMapper">

    <resultMap type="com.ruoyi.frequency.order.entity.Order" id="OrderResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="title"    column="title"    />
        <result property="sourceMaterial"    column="source_material"    />
        <result property="originAmount"    column="origin_amount"    />
        <result property="amount"    column="amount"    />
        <result property="changeAmount"    column="change_amount"    />
        <result property="changeUserType"    column="change_user_type"    />
        <result property="changeStatus"    column="change_status"    />
        <result property="changeRemark"    column="change_remark"    />
        <result property="confirmDate"    column="confirm_date"    />
        <result property="customerId"    column="customer_id"    />
        <result property="storeId"    column="store_id"    />
        <result property="saleAmount"    column="sale_amount"    />
        <result property="saleId"    column="sale_id"    />
        <result property="content"    column="content"    />
        <result property="status"    column="status"    />
        <result property="appealStatus"    column="appeal_status"    />
        <result property="appealUserType"    column="appeal_user_type"    />
        <result property="sourceUserType"    column="source_user_type"    />
        <result property="coverImage"    column="cover_image"    />
        <result property="images"    column="images"    />
        <result property="firstPrice"    column="first_price"    />
        <result property="firstStatus"    column="first_status"    />
        <result property="firstPayType"    column="first_pay_type"    />
        <result property="secondPayType"    column="second_pay_type"    />
        <result property="orderNo"    column="order_no"    />
        <result property="aliBusinessId"    column="ali_business_id"    />
        <result property="firstPayTime"    column="first_pay_time"    />
        <result property="secondPayTime"    column="second_pay_time"    />
        <result property="worksTypeName"    column="worksTypeName"    />
        <result property="originDeliverTime" column="origin_deliver_time"/>
        <result property="deliverTime" column="deliver_time"/>
        <result property="extensionDeliverTime" column="extension_deliver_time"/>
        <result property="extensionUserType" column="extension_user_type"/>
        <result property="extensionStatus" column="extension_status"/>
        <result property="extensionRemark" column="extension_remark"/>
        <result property="appealId" column="appealId"/>
        <result property="ifHighend" column="if_highend"/>
    </resultMap>

    <sql id="selectOrderVo">
        SELECT
            t.id,
            t.create_time,
            t.del_flag,
            t.title,
            t.source_material,
            t.origin_amount,
            t.amount,
            t.change_amount,
            t.change_user_type,
            t.change_status,
            t.change_remark,
            t.confirm_date,
            t.customer_id,
            t.store_id,
            t.sale_amount,
            t.sale_id,
            t.content,
            case when t.is_customer_evaluate = 2 and t.is_store_evaluate = 2 then 5
                 else t.`status` end status,
            t.appeal_status,
            t.appeal_user_type,
            t.source_user_type,
            t.cover_image,
            t.images,
            t.first_price,
            t.first_status,
            t.first_pay_type,
            t.second_pay_type,
            t.order_no,
            t.ali_business_id,
            t.first_pay_time,
            t.second_pay_time,
            t.works_type_id,
            t.first_pay_order_no,
            t.second_pay_order_no,
            t.is_customer_evaluate,
            t.is_store_evaluate,
            t.second_status,
            t.origin_deliver_time,
            t.deliver_time,
            t.extension_deliver_time,
            t.extension_user_type,
            t.extension_status,
            t.extension_remark,
            t.if_highend,
            CONCAT(t3.`name`, '-', t2.`name` ) worksTypeName,
            (select a.id from t_appeal a where a.order_id = t.id and a.del_flag = 1 order by a.create_time desc limit 1) as appealId
        FROM
            t_order AS t
            LEFT JOIN t_works_type AS t2 ON t.works_type_id = t2.id
            LEFT JOIN t_works_type AS t3 ON t2.pid = t3.id
            LEFT JOIN t_customer AS c ON t.customer_id = c.id
            LEFT JOIN t_store AS s ON t.store_id = s.id
    </sql>

    <select id="selectOrderList" parameterType="com.ruoyi.frequency.order.entity.Order" resultMap="OrderResult">
        <include refid="selectOrderVo"/>
        <where>
            <if test="title != null  and title != ''"> and t.title like concat('%',#{title}, '%')</if>
            <if test="storeId != null  and storeId != ''"> and t.store_id = #{storeId}</if>
            <if test="status != null and status != ''">
                <choose>
                    <when test="status == 5">
                        and t.is_customer_evaluate = 2
                        and t.is_store_evaluate = 2
                    </when>
                    <otherwise>
                        and t.status = #{status}
                    </otherwise>
                </choose>
            </if>
            <if test="isCustomerEvaluate != null and isCustomerEvaluate != '' "> and t.is_customer_evaluate = #{isCustomerEvaluate}</if>
            <if test="isStoreEvaluate != null and isStoreEvaluate != '' "> and t.is_store_evaluate = #{isStoreEvaluate}</if>
            <if test="orderNo != null  and orderNo != ''"> and t.order_no like concat('%',#{orderNo}, '%')</if>
            <if test="beginTime != null ">
                and  t.create_time >= #{beginTime}

            </if>
            <if test="endTime != null ">
                and  t.create_time &lt; #{endTime}
            </if>

            <if test="confirmBeginTime != null ">
                and  t.confirm_date >= #{confirmBeginTime}

            </if>
            <if test="confirmEndTime != null ">
                and  t.confirm_date &lt;= #{confirmEndTime}
            </if>

            <if test="worksTypeId != null and worksTypeId != ''">
                and (
                t2.id = #{worksTypeId}
                or t3.id = #{worksTypeId}
                )
            </if>
            <if test="customerInfo != null and customerInfo.nickname != null  and customerInfo.nickname != ''">
                and c.nickname like concat('%',#{customerInfo.nickname}, '%')
            </if>
            <if test="customerInfo != null and customerInfo.mobile != null  and customerInfo.mobile != ''">
                and c.mobile like concat('%',#{customerInfo.mobile}, '%')
            </if>
            <if test="storeInfo != null and storeInfo.nickname != null  and storeInfo.nickname != ''">
                and s.nickname like concat('%',#{storeInfo.nickname}, '%')
            </if>
            <if test="storeInfo != null and storeInfo.mobile != null  and storeInfo.mobile != ''">
                and s.mobile like concat('%',#{storeInfo.mobile}, '%')
            </if>
            <if test="ifHighend != null and ifHighend != '' ">
                and t.if_highend = #{ifHighend}
            </if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectOrderById" parameterType="Long" resultMap="OrderResult">
        <include refid="selectOrderVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertOrder" parameterType="com.ruoyi.frequency.order.entity.Order">
        insert into t_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="title != null">title,</if>
            <if test="sourceMaterial != null">source_material,</if>
            <if test="originAmount != null">origin_amount,</if>
            <if test="amount != null">amount,</if>
            <if test="changeAmount != null">change_amount,</if>
            <if test="changeUserType != null">change_user_type,</if>
            <if test="changeStatus != null">change_status,</if>
            <if test="changeRemark != null">change_remark,</if>
            <if test="confirmDate != null">confirm_date,</if>
            <if test="customerId != null">customer_id,</if>
            <if test="storeId != null">store_id,</if>
            <if test="saleAmount != null">sale_amount,</if>
            <if test="saleId != null">sale_id,</if>
            <if test="content != null">content,</if>
            <if test="status != null">status,</if>
            <if test="appealStatus != null">appeal_status,</if>
            <if test="appealUserType != null">appeal_user_type,</if>
            <if test="sourceUserType != null">source_user_type,</if>
            <if test="coverImage != null">cover_image,</if>
            <if test="images != null">images,</if>
            <if test="firstPrice != null">first_price,</if>
            <if test="firstStatus != null">first_status,</if>
            <if test="firstPayType != null">first_pay_type,</if>
            <if test="secondPayType != null">second_pay_type,</if>
            <if test="orderNo != null">order_no,</if>
            <if test="aliBusinessId != null">ali_business_id,</if>
            <if test="firstPayTime != null">first_pay_time,</if>
            <if test="secondPayTime != null">second_pay_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="title != null">#{title},</if>
            <if test="sourceMaterial != null">#{sourceMaterial},</if>
            <if test="originAmount != null">#{originAmount},</if>
            <if test="amount != null">#{amount},</if>
            <if test="changeAmount != null">#{changeAmount},</if>
            <if test="changeUserType != null">#{changeUserType},</if>
            <if test="changeStatus != null">#{changeStatus},</if>
            <if test="changeRemark != null">#{changeRemark},</if>
            <if test="confirmDate != null">#{confirmDate},</if>
            <if test="customerId != null">#{customerId},</if>
            <if test="storeId != null">#{storeId},</if>
            <if test="saleAmount != null">#{saleAmount},</if>
            <if test="saleId != null">#{saleId},</if>
            <if test="content != null">#{content},</if>
            <if test="status != null">#{status},</if>
            <if test="appealStatus != null">#{appealStatus},</if>
            <if test="appealUserType != null">#{appealUserType},</if>
            <if test="sourceUserType != null">#{sourceUserType},</if>
            <if test="coverImage != null">#{coverImage},</if>
            <if test="images != null">#{images},</if>
            <if test="firstPrice != null">#{firstPrice},</if>
            <if test="firstStatus != null">#{firstStatus},</if>
            <if test="firstPayType != null">#{firstPayType},</if>
            <if test="secondPayType != null">#{secondPayType},</if>
            <if test="orderNo != null">#{orderNo},</if>
            <if test="aliBusinessId != null">#{aliBusinessId},</if>
            <if test="firstPayTime != null">#{firstPayTime},</if>
            <if test="secondPayTime != null">#{secondPayTime},</if>
        </trim>
    </insert>

    <update id="updateOrder" parameterType="com.ruoyi.frequency.order.entity.Order">
        update t_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="title != null">title = #{title},</if>
            <if test="sourceMaterial != null">source_material = #{sourceMaterial},</if>
            <if test="originAmount != null">origin_amount = #{originAmount},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="changeAmount != null">change_amount = #{changeAmount},</if>
            <if test="changeUserType != null">change_user_type = #{changeUserType},</if>
            <if test="changeStatus != null">change_status = #{changeStatus},</if>
            <if test="changeRemark != null">change_remark = #{changeRemark},</if>
            <if test="confirmDate != null">confirm_date = #{confirmDate},</if>
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="storeId != null">store_id = #{storeId},</if>
            <if test="saleAmount != null">sale_amount = #{saleAmount},</if>
            <if test="saleId != null">sale_id = #{saleId},</if>
            <if test="content != null">content = #{content},</if>
            <if test="status != null">status = #{status},</if>
            <if test="appealStatus != null">appeal_status = #{appealStatus},</if>
            <if test="appealUserType != null">appeal_user_type = #{appealUserType},</if>
            <if test="sourceUserType != null">source_user_type = #{sourceUserType},</if>
            <if test="coverImage != null">cover_image = #{coverImage},</if>
            <if test="images != null">images = #{images},</if>
            <if test="firstPrice != null">first_price = #{firstPrice},</if>
            <if test="firstStatus != null">first_status = #{firstStatus},</if>
            <if test="firstPayType != null">first_pay_type = #{firstPayType},</if>
            <if test="secondPayType != null">second_pay_type = #{secondPayType},</if>
            <if test="orderNo != null">order_no = #{orderNo},</if>
            <if test="aliBusinessId != null">ali_business_id = #{aliBusinessId},</if>
            <if test="firstPayTime != null">first_pay_time = #{firstPayTime},</if>
            <if test="secondPayTime != null">second_pay_time = #{secondPayTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOrderById" parameterType="Long">
        delete from t_order where id = #{id}
    </delete>

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

    <select id="orderPage" resultType="com.ruoyi.controller.resp.OrderPageResp">
        SELECT
        t.id,
        t.create_time,
        t.del_flag,
        t.title,
        t.content,
        t.amount,
        t.source_material,
        t.store_id,
        t.confirm_date,
        t.drawing_num,
        t.customer_id,
        t.first_status,
        t.first_price,
        case when #{userVo.userType} = 1 and t.is_customer_evaluate = 2 then 5
        when #{userVo.userType} = 2 and t.is_store_evaluate = 2 then 5
        else t.`status` end status,
        t.cover_image,
        t.images,
        t.order_no,
        t.change_status,
        t.appeal_status,
        t.appeal_user_type,
        t2.`name` worksTypeName,
        t.if_highend,
        (select a.id from t_appeal a where a.order_id = t.id and a.del_flag = 1 order by a.create_time desc limit 1) as
        appealId,
        IF((SELECT count(1) from t_order_receiving tor where tor.del_flag = 1 and tor.order_id = t.id and tor.store_id =
        #{userVo.userId})> 0,2,1) isReceivingOrder
        FROM
        t_order AS t
        LEFT JOIN t_works_type AS t2 ON t.works_type_id = t2.id
        LEFT JOIN t_works_type AS t3 ON t2.pid = t3.id
        LEFT JOIN t_store s on t.store_id = s.id
        LEFT JOIN t_level l ON l.type = 1 and l.id = (
        SELECT
        l2.id
        FROM
        t_level l2
        WHERE
        l2.del_flag = 1
        and l2.type =2
        and l2.compliance &lt;= s.income
        ORDER BY
        l2.compliance DESC
        LIMIT 1
        )
        left join t_customer c on c.id = t.customer_id
        WHERE
        t.del_flag = 1
        AND (t.first_status = 2
        OR (
        t.first_status = 1
        AND t.store_id IS NOT NULL
        )
        )
        <if test="!req.isHall">
            AND (
            (#{userVo.userType} = 1 and t.customer_id = #{userVo.userId})
            or
            (
            (t.`status`= 1
            and (SELECT
            COUNT( 1 )
            FROM
            t_order_receiving tor
            WHERE
            tor.del_flag = 1
            AND tor.order_id = t.id
            AND tor.store_id = #{userVo.userId}) > 0)
            or
            (#{userVo.userType} = 2 and t.store_id = #{userVo.userId})
            )
            )
            AND (
            (#{userVo.userType} = 1 )
            or
            (#{userVo.userType} = 2 and t.`status`!= 1)
            )

        </if>

        <if test="req.isEachOther">
            AND (
            (#{userVo.userType} = 1 and t.customer_id = #{userVo.userId}
            and #{req.otherUserType} = 2 and t.store_id = #{req.otherUserId}
            )
            or
            (#{userVo.userType} = 2 and t.store_id = #{userVo.userId}
            and #{req.otherUserType} = 1 and t.customer_id = #{req.otherUserId}
            )
            )
        </if>

        <if test="req.isHall">
            <choose>
                <when test="req.status != null and req.status != ''">
                    AND t.`status` = 1
                    AND t.first_status = 2
                    AND t.store_id is null
                    AND
                    (SELECT
                    COUNT( 1 )
                    FROM
                    t_order_receiving tor
                    WHERE
                    tor.del_flag = 1
                    AND tor.order_id = t.id
                    AND tor.store_id = #{userVo.userId}) > 0
                </when>
                <otherwise>
                    AND
                    (SELECT
                    COUNT( 1 )
                    FROM
                    t_order_receiving tor
                    WHERE
                    tor.del_flag = 1
                    AND tor.order_id = t.id
                    AND tor.store_id = #{userVo.userId}) = 0
                    AND t.`status` = 1
                    and t.first_status = 2
                    and t.store_id is null
                </otherwise>
            </choose>

            <if test="req.blockUserList!=null and req.blockUserList.size() > 0">
                AND (
                (t.customer_id not in
                <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                    IF(#{userVo.userType} = 1,#{userVo.userId},'')
                </foreach>)
                )
            </if>

            <if test="req.shieldUserList!=null and req.shieldUserList.size() > 0">
                AND (
                (t.customer_id not in
                <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                    IF(#{userVo.userType} = 1,#{userVo.userId},'')
                </foreach>)
                )
            </if>

        </if>

        <if test="!req.isHall and req.status != null">
            <if test="req.status == 5 and userVo.userType == 1">
                and t.is_customer_evaluate = 2
            </if>
            <if test="req.status == 5 and userVo.userType == 2">
                and t.is_store_evaluate = 2
            </if>
            <if test="req.status != 5 ">
                <if test="!req.status">
                    and t.`status` = 1
                    and t.first_status = 1
                </if>
                <if test="req.status == 1">
                    and t.`status` = 1
                    and t.first_status = 2
                    and t.store_id is null
                </if>
                <if test="req.status and req.status != 1">
                    and t.`status` = #{req.status}
                    <if test="userVo.userType == 1">
                        and t.is_customer_evaluate = 1
                    </if>
                    <if test="userVo.userType == 2">
                        and t.is_store_evaluate = 1
                    </if>
                </if>
            </if>
        </if>
        <if test="req.beginTime != null ">
            and t.create_time >= #{req.beginTime}
        </if>
        <if test="req.endTime != null ">
            and t.create_time &lt;= #{req.endTime}
        </if>
        <if test="req.typeIds!=null and req.typeIds.size()>0">
            AND t.works_type_id IN
            <foreach collection="req.typeIds" index="index" item="i" open="("
                     separator="," close=")">
                #{i}
            </foreach>
        </if>
        <if test="req.keyword!=null and req.keyword!=''">
            <if test="userVo.userType == 1">
                AND (
                s.nickname LIKE CONCAT('%',#{req.keyword},'%')
                or
                s.name LIKE CONCAT('%',#{req.keyword},'%')
                or
                t2.name LIKE CONCAT('%',#{req.keyword},'%')
                or
                t3.name LIKE CONCAT('%',#{req.keyword},'%')
                or
                t.order_no LIKE CONCAT('%',#{req.keyword},'%')
                )
            </if>
            <if test="userVo.userType == 2">
                AND (
                c.nickname LIKE CONCAT('%',#{req.keyword},'%')
                or
                c.name LIKE CONCAT('%',#{req.keyword},'%')
                or
                t2.name LIKE CONCAT('%',#{req.keyword},'%')
                or
                t3.name LIKE CONCAT('%',#{req.keyword},'%')
                or
                t.order_no LIKE CONCAT('%',#{req.keyword},'%')
                )
            </if>
            <if test="userVo.userType != 1 and userVo.userType != 2">
                AND (
                t.order_no LIKE CONCAT('%',#{req.keyword},'%')
                )
            </if>
        </if>
        <if test="req.ifHighend!=null and req.ifHighend!=''">
            AND t.if_highend = #{req.ifHighend}
        </if>
        ORDER BY
        <choose>
            <when test="req.sortType != null and req.sortType != ''">
                <if test="req.sortType == 1">
                    t.create_time DESC
                </if>
                <if test="req.sortType == 2">
                    <if test="req.lifting == 1">
                        t.amount
                    </if>
                    <if test="req.lifting == 2">
                        t.amount DESC
                    </if>
                </if>
            </when>
            <otherwise>
                t.create_time DESC
            </otherwise>
        </choose>

    </select>


    <select id="orderCount" resultType="java.lang.Integer">
        SELECT
            count(1)
        FROM
            t_order AS t
        WHERE
            t.del_flag = 1
          and t.first_status = 2
          AND (
            (#{userVo.userType} = 1 and t.customer_id = #{userVo.userId})
           or
            (#{userVo.userType} = 2 and t.store_id = #{userVo.userId})
            )
         <if test="req!=null">
             <if test="req.otherUserId!=null and req.otherUserId!=''">
                 AND (
                 (#{req.otherUserType} = 1 and t.customer_id = #{req.otherUserId})
                 or
                 (#{req.otherUserType} = 2 and t.store_id = #{req.otherUserId})
                 )
             </if>
         </if>
    </select>
    <select id="orderList" resultType="com.ruoyi.controller.resp.OrderDetailResp">
        SELECT
        t.id,
        t.create_time,
        t.del_flag,
        t.title,
        t.content,
        t.amount,
        t.source_material,
        t.store_id,
        t.customer_id,
        case when #{userVo.userType} = 1 and t.is_customer_evaluate = 2 then 5
        when #{userVo.userType} = 2 and t.is_store_evaluate = 2 then 5
        else t.`status` end status,
        t.cover_image,
        t.images,
        t.order_no,
        t2.`name` AS worksTypeName,
        t.origin_amount,
        t.change_amount,
        t.change_user_type,
        t.change_status,
        t.change_remark,
        t.confirm_date,
        t.sale_amount,
        t.sale_id,
        t.appeal_status,
        t.appeal_user_type,
        t.source_user_type,
        t.first_price,
        t.first_status,
        t.first_pay_type,
        t.second_pay_type,
        t.first_pay_time,
        t.second_pay_time,
        t.ali_business_id,
        t.works_type_id,
        t.first_pay_order_no,
        t.second_pay_order_no,
        t.drawing_num,
        t.origin_deliver_time,
        t.deliver_time,
        t.extension_deliver_time,
        t.extension_user_type,
        t.extension_status,
        t.extension_remark,
        t.if_highend,
        (select a.id from t_appeal a where a.order_id = t.id and a.del_flag = 1 order by a.create_time desc limit 1) as appealId
        FROM
        t_order AS t
        LEFT JOIN t_works_type AS t2 ON t.works_type_id = t2.id
        WHERE
        t.del_flag = 1
        and t.id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="orderDetail" resultType="com.ruoyi.controller.resp.OrderDetailResp">
        SELECT
            t.id,
            t.create_time,
            t.del_flag,
            t.title,
            t.content,
            t.amount,
            t.source_material,
            t.store_id,
            t.customer_id,
            case when #{userVo.userType} = 1 and t.is_customer_evaluate = 2 then 5
                 when #{userVo.userType} = 2 and t.is_store_evaluate = 2 then 5
                 else t.`status` end status,
            t.cover_image,
            t.images,
            t.order_no,
            t2.`name` AS worksTypeName,
            t.origin_amount,
            t.change_amount,
            t.change_user_type,
            t.change_status,
            t.change_remark,
            t.confirm_date,
            t.sale_amount,
            t.sale_id,
            t.appeal_status,
            t.appeal_user_type,
            t.source_user_type,
            t.first_price,
            t.first_status,
            t.first_pay_type,
            t.second_pay_type,
            t.first_pay_time,
            t.second_pay_time,
            t.ali_business_id,
            t.works_type_id,
            t.first_pay_order_no,
            t.second_pay_order_no,
            t.drawing_num,
            t.origin_deliver_time,
            t.deliver_time,
            t.extension_deliver_time,
            t.extension_user_type,
            t.extension_status,
            t.extension_remark,
            t.if_highend,
            (select a.id from t_appeal a where a.order_id = t.id and a.del_flag = 1 order by a.create_time desc limit 1) as appealId,
            IF((SELECT count(1) from t_order_receiving tor where tor.del_flag = 1 and tor.order_id = t.id and tor.store_id = #{userVo.userId})> 0,2,1) isReceivingOrder
        FROM
            t_order AS t
            LEFT JOIN t_works_type AS t2 ON t.works_type_id = t2.id
        WHERE
            t.id = #{id}
    </select>

    <select id="selectMonthBeforeAmount" resultType="java.math.BigDecimal">
        SELECT
            IFNULL((
                       SELECT
                           sum(o.amount)
                       FROM
                           t_order o
                       WHERE
                           o.`status` > 2
                         AND o.del_flag = 1
                         AND o.store_id = #{userVo.userId}
                         AND DATE_FORMAT(o.create_time,'%y%m') = DATE_FORMAT(#{beginOfMonth},'%y%m')
                   ),
                   0)
    </select>


    <select id="statistics" parameterType="com.ruoyi.frequency.order.entity.Order" resultType="com.ruoyi.frequency.order.model.result.OrderCount">
        SELECT
        count( t.id ) orderNum,
        ifnull( sum( IF ( t.`status` != - 1, t.amount, 0 ) ), 0 ) orderPrice,
        IFNULL(
        SUM(
        IF
        (
        t.`status` != - 1,
        IF
        (
        t.first_status = 2
        AND t.second_status = 2,
        t.amount,
        IF
        ( t.first_status = 2 AND t.second_status = 1, t.first_price, 0 )),
        0
        )),
        0
        ) payPrice,
        IFNULL(
        SUM(
        IF
        (
        t.`status` != - 1,
        IF
        (
        t.first_status = 1
        AND t.second_status = 1,
        t.amount,
        IF
        ( t.first_status = 2 AND t.second_status = 1, t.amount - t.first_price, 0 )),
        0
        )),
        0
        ) noPayPrice,
        IFNULL( IF ( t.`status` != - 1, t.first_give_price + t.second_give_price, 0 ), 0 ) givePrice
        FROM
        t_order AS t
        Where
        t.id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
</mapper>
