<?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.leromro.core.mapper.MainOrdersMapper">
    
   <!-- <resultMap type="MainOrders" id="MainOrdersResult">
        <result property="orderId"    column="order_id"    />
        <result property="userId"    column="user_id"    />
        <result property="serviceOnePrice"    column="service_one_price"    />
        <result property="serviceTotalPrice"    column="service_total_price"    />
        <result property="orderStatus"    column="order_status"    />
        <result property="serviceCategory"    column="service_category"    />
        <result property="totalTimes"    column="total_times"    />
        <result property="finishTimes"    column="finish_times"    />
        <result property="serviceStartDate"    column="service_start_date"    />
        <result property="serviceFinishDate"    column="service_finish_date"    />
        <result property="startTime"    column="start_time"    />
        <result property="serviceDuration"    column="service_duration"    />
        <result property="paymentAmount"    column="payment_amount"    />
        <result property="paymentMethod"    column="payment_method"    />
        <result property="volunteerId"    column="volunteer_id"    />
        <result property="serviceAddressId"    column="service_address_id"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="remark"    column="remark"    />
        <result property="isDelete"    column="is_delete"    />
    </resultMap>

    <sql id="selectLMainOrdersVo">
        select order_id, user_id, service_one_price, service_total_price, order_status, service_category, service_type, service_subject, total_times, finish_times, service_start_date, service_finish_date, start_time, service_duration, payment_amount, payment_method, volunteer_id, service_address_id, update_time, update_by, create_time, create_by, remark, is_delete from l_main_orders
    </sql>

    <select id="selectMainOrdersList" parameterType="MainOrders" resultMap="LMainOrdersResult">
        <include refid="selectLMainOrdersVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="serviceOnePrice != null "> and service_one_price = #{serviceOnePrice}</if>
            <if test="serviceTotalPrice != null "> and service_total_price = #{serviceTotalPrice}</if>
            <if test="orderStatus != null "> and order_status = #{orderStatus}</if>
            <if test="serviceCategory != null "> and service_category = #{serviceCategory}</if>
            <if test="serviceType != null "> and service_type = #{serviceType}</if>
            <if test="serviceSubject != null "> and service_subject = #{serviceSubject}</if>
            <if test="totalTimes != null "> and total_times = #{totalTimes}</if>
            <if test="finishTimes != null "> and finish_times = #{finishTimes}</if>
            <if test="serviceStartDate != null "> and service_start_date = #{serviceStartDate}</if>
            <if test="serviceFinishDate != null "> and service_finish_date = #{serviceFinishDate}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="serviceDuration != null "> and service_duration = #{serviceDuration}</if>
            <if test="paymentAmount != null "> and payment_amount = #{paymentAmount}</if>
            <if test="paymentMethod != null "> and payment_method = #{paymentMethod}</if>
            <if test="volunteerId != null "> and volunteer_id = #{volunteerId}</if>
            <if test="serviceAddressId != null "> and service_address_id = #{serviceAddressId}</if>
            <if test="isDelete != null "> and is_delete = #{isDelete}</if>
        </where>
    </select>
    
    <select id="selectMainOrdersByOrderId" parameterType="String" resultMap="MainOrdersResult">
        <include refid="selectLMainOrdersVo"/>
        where order_id = #{orderId}
    </select>-->

    <!--<insert id="insertMainOrders" parameterType="MainOrders">
        insert into l_main_orders
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mainOrderId != null">main_order_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="serviceOnePrice != null">service_one_price,</if>
            <if test="serviceTotalPrice != null">service_total_price,</if>
            <if test="orderStatus != null">order_status,</if>
            <if test="serviceCategory != null">service_category,</if>
            <if test="serviceType != null">service_type,</if>
            <if test="serviceSubject != null">service_subject,</if>
            <if test="totalTimes != null">total_times,</if>
            <if test="finishTimes != null">finish_times,</if>
            <if test="serviceStartDate != null">service_start_date,</if>
            <if test="serviceFinishDate != null">service_finish_date,</if>
            <if test="startTime != null">start_time,</if>
            <if test="serviceDuration != null">service_duration,</if>
            <if test="paymentAmount != null">payment_amount,</if>
            <if test="paymentMethod != null">payment_method,</if>
            <if test="volunteerId != null">volunteer_id,</if>
            <if test="addressId != null">address_id,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="remark != null">remark,</if>
            <if test="isDelete != null">is_delete,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mainOrderId != null">#{mainOrderId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="serviceOnePrice != null">#{serviceOnePrice},</if>
            <if test="serviceTotalPrice != null">#{serviceTotalPrice},</if>
            <if test="orderStatus != null">#{orderStatus},</if>
            <if test="serviceType != null">#{serviceType},</if>
            <if test="serviceSubject != null">#{serviceSubject},</if>
            <if test="serviceCategory != null">#{serviceCategory},</if>
            <if test="totalTimes != null">#{totalTimes},</if>
            <if test="finishTimes != null">#{finishTimes},</if>
            <if test="serviceStartDate != null">#{serviceStartDate},</if>
            <if test="serviceFinishDate != null">#{serviceFinishDate},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="serviceDuration != null">#{serviceDuration},</if>
            <if test="paymentAmount != null">#{paymentAmount},</if>
            <if test="paymentMethod != null">#{paymentMethod},</if>
            <if test="volunteerId != null">#{volunteerId},</if>
            <if test="addressId != null">#{addressId},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="remark != null">#{remark},</if>
            <if test="isDelete != null">#{isDelete},</if>
         </trim>
    </insert>-->
    <update id="startService" parameterType="java.lang.Long">
        update l_main_orders set order_status = 3 where main_order_id = (select main_order_id from l_second_order where second_order_id = #{secondOrderId});
    </update>
    <select id="selectByUserId" resultType="com.leromro.core.domain.vo.MainOrdersVO">
        select lmo.main_order_id ,lvi.business_tier_name ,lvi.business_describe , lmo.create_time,lmo.service_total_price,lmo.order_status,lvi.name,lvi.volunteer_picture,lmo.create_time
        from l_main_orders lmo left join l_volunteer_info lvi on lmo.volunteer_info_id = lvi.volunteer_info_id
        <where>
            <if test="orderStatus != null and orderStatus != ''">
                AND order_status IN
                <foreach item="status" collection="orderStatus" open="(" separator="," close=")">
                    #{status}
                </foreach>
            </if>
            <if test="userId != null">
                and lmo.user_id = #{userId}
            </if>
        </where>
        order by lmo.create_time desc
    </select>

    <select id="webMainOrderList" resultType="com.leromro.core.domain.vo.webMainOrderListVO">
        select lmo.*,lvi.project_name,lvi.business_tier_name,lvi.name,lvi.phonenumber
        from l_main_orders lmo left join l_volunteer_info lvi on lmo.volunteer_info_id = lvi.volunteer_info_id
        <where>
            <if test="dto.deptId != null and dto.deptId != ''">and lmo.service_center_id = #{dto.deptId}</if>
            <if test="dto.name != null and dto.name !=''">and lvi.name = #{dto.name}</if>
            <if test="dto.clientTelephone != null and dto.clientTelephone != ''">and lmo.client_telephone = #{dto.clientTelephone}</if>
            <if test="dto.phonenumber != null and dto.phonenumber != ''">and lvi.phonenumber = #{dto.phonenumber}</if>
            <if test="dto.businessManagementId != null and dto.businessManagementId != ''">and lvi.business_management_id = #{dto.businessManagementId}</if>
        </where>
        order by lmo.create_time desc
    </select>
    <select id="selectWebMainOrderInfoVO" resultType="com.leromro.core.domain.vo.WebMainOrdersInfoVO"
            parameterType="java.lang.Long">
        select lmo.*,
               lvi.name              as VolunteerName,
               lvi.phonenumber       as VolunteerTelephone,
               lvi.volunteer_picture as VolunteerPicture,
               lvi.business_tier_name as businessTierName
        from l_main_orders lmo
                 left join l_volunteer_info lvi on lmo.volunteer_info_id = lvi.volunteer_info_id
        where lmo.main_order_id = #{mainOrderId};
    </select>
    <select id="selectOrderInfoByUserId" resultType="com.leromro.core.domain.vo.MainOrderInfoVO"
            parameterType="java.lang.Long">
        select
            lmo.address,lmo.client_name as name,lmo.client_telephone as telephone, lmo.province_name,lmo.city_name ,lmo.district_name,lmo.service_total_price,lmo.payment_method,lmo.order_status,lmo.main_order_id,lmo.out_trade_no,
            lvi.volunteer_info_id,lvi.volunteer_id,lvi.age,lvi.certification_picture,lvi.name as volunteerName ,lvi.phonenumber,lvi.sex,lvi.skill_describe,lvi.volunteer_picture,
            lmo.service_category,lmo.service_duration,lmo.service_one_price,lvi.business_unit,lmo.single_quantity,lmo.total_times,lvi.business_describe,
            lmo.business_management_id,(select lbm.business_tier_name from l_business_management lbm where lmo.business_management_id = lbm.business_management_id) as businessManagementName
        from l_main_orders lmo left join l_volunteer_info lvi on lmo.volunteer_info_id = lvi.volunteer_info_id where main_order_id = #{mainOrderId} ;
    </select>
    <!--
        <update id="updateMainOrders" parameterType="MainOrders">
            update l_main_orders
            <trim prefix="SET" suffixOverrides=",">
                <if test="userId != null">user_id = #{userId},</if>
                <if test="serviceOnePrice != null">service_one_price = #{serviceOnePrice},</if>
                <if test="serviceTotalPrice != null">service_total_price = #{serviceTotalPrice},</if>
                <if test="orderStatus != null">order_status = #{orderStatus},</if>
                <if test="serviceCategory != null">service_category = #{serviceCategory},</if>
                <if test="serviceType != null">service_type = #{serviceType},</if>
                <if test="serviceSubject != null">service_subject = #{serviceSubject},</if>
                <if test="totalTimes != null">total_times = #{totalTimes},</if>
                <if test="finishTimes != null">finish_times = #{finishTimes},</if>
                <if test="serviceStartDate != null">service_start_date = #{serviceStartDate},</if>
                <if test="serviceFinishDate != null">service_finish_date = #{serviceFinishDate},</if>
                <if test="startTime != null">start_time = #{startTime},</if>
                <if test="serviceDuration != null">service_duration = #{serviceDuration},</if>
                <if test="paymentAmount != null">payment_amount = #{paymentAmount},</if>
                <if test="paymentMethod != null">payment_method = #{paymentMethod},</if>
                <if test="volunteerId != null">volunteer_id = #{volunteerId},</if>
                <if test="serviceAddressId != null">service_address_id = #{serviceAddressId},</if>
                <if test="updateTime != null">update_time = #{updateTime},</if>
                <if test="updateBy != null">update_by = #{updateBy},</if>
                <if test="createTime != null">create_time = #{createTime},</if>
                <if test="createBy != null">create_by = #{createBy},</if>
                <if test="remark != null">remark = #{remark},</if>
                <if test="isDelete != null">is_delete = #{isDelete},</if>
            </trim>
            where order_id = #{orderId}
        </update>

        <delete id="deleteMainOrdersByOrderId" parameterType="String">
            delete from l_main_orders where order_id = #{orderId}
        </delete>

        <delete id="deleteMainOrdersByOrderIds" parameterType="String">
            delete from l_main_orders where order_id in
            <foreach item="orderId" collection="array" open="(" separator="," close=")">
                #{orderId}
            </foreach>
        </delete>-->
</mapper>