<?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.SecondOrdersMapper">

    <insert id="insertList" parameterType="java.util.List">
        INSERT INTO l_second_order (
        second_order_id,
        out_trade_no,
        main_order_id,
        volunteer_id,
        service_category,
        service_one_price,
        work_start_time,
        work_number,
        single_quantity,
        service_duration,
        address_id,
        work_date,
        create_time,
        create_by,
        update_time,
        update_by,
        order_status,
        volunteer_settlement_status,
        volunteer_info_id,
        remark,
        service_total_price,
        work_end_time,
        second_order_create_time,
        province_code,
        province_name,
        city_code,
        city_name,
        district_code,
        district_name,
        address,
        latitude,
        longitude,
        area_distribution_ratio,
        area_distribution_id,
        area_settlement_status,
        allot_status,
        service_center_id,
        service_centre_distribution_ratio,
        service_centre_settlement_status
        ) VALUES
        <foreach collection="list" item="secondOrder" separator=",">
            <if test="secondOrder != null">
                (
                #{secondOrder.secondOrderId},
                #{secondOrder.outTradeNo},
                #{secondOrder.mainOrderId},
                #{secondOrder.volunteerId},
                #{secondOrder.serviceCategory},
                #{secondOrder.serviceOnePrice},
                #{secondOrder.workStartTime},
                #{secondOrder.workNumber},
                #{secondOrder.singleQuantity},
                #{secondOrder.serviceDuration},
                #{secondOrder.addressId},
                #{secondOrder.workDate},
                #{secondOrder.createTime},
                #{secondOrder.createBy},
                #{secondOrder.updateTime},
                #{secondOrder.updateBy},
                #{secondOrder.orderStatus},
                #{secondOrder.volunteerSettlementStatus},
                #{secondOrder.volunteerInfoId},
                #{secondOrder.remark},
                #{secondOrder.serviceTotalPrice},
                #{secondOrder.workEndTime},
                #{secondOrder.secondOrderCreateTime},
                #{secondOrder.provinceCode},
                #{secondOrder.provinceName},
                #{secondOrder.cityCode},
                #{secondOrder.cityName},
                #{secondOrder.districtCode},
                #{secondOrder.districtName},
                #{secondOrder.address},
                #{secondOrder.latitude},
                #{secondOrder.longitude},
                #{secondOrder.areaDistributionRatio},
                #{secondOrder.areaDistributionId},
                #{secondOrder.areaSettlementStatus},
                #{secondOrder.allotStatus},
                #{secondOrder.serviceCenterId},
                #{secondOrder.serviceCentreDistributionRatio},
                #{secondOrder.serviceCentreSettlementStatus}
                )
            </if>
        </foreach>
    </insert>
    <update id="updatedeptAmountList" parameterType="java.util.List">
        <if test="list != null and !list.isEmpty()">
            UPDATE l_second_order
            SET
            area_distribution_amount = CASE second_order_id
            <foreach collection="list" item="order">
                WHEN #{order.secondOrderId} THEN #{order.areaDistributionAmount}
            </foreach>
            END,
            volunteer_settlement_status = CASE second_order_id
            <foreach collection="list" item="order">
                WHEN #{order.secondOrderId} THEN #{order.volunteerSettlementStatus}
            </foreach>
            END
            WHERE second_order_id IN
            <foreach collection="list" item="order" open="(" separator="," close=")">
                #{order.secondOrderId}
            </foreach>
        </if>
    </update>

    <!--  <resultMap type="LVolunteerOrder" id="LVolunteerOrderResult">
          <result property="id"    column="id"    />
          <result property="orderId"    column="order_id"    />
          <result property="volunteerId"    column="volunteer_id"    />
          <result property="serviceCategory"    column="service_category"    />
          <result property="serviceType"    column="service_type"    />
          <result property="serviceSubject"    column="service_subject"    />
          <result property="workDate"    column="work_date"    />
          <result property="workStartTime"    column="work_start_time"    />
          <result property="workEndTime"    column="work_end_time"    />
          <result property="workNumber"    column="work_number"    />
          <result property="hours"    column="hours"    />
          <result property="addressId"    column="address_id"    />
          <result property="score"    column="score"    />
          <result property="remark"    column="remark"    />
          <result property="createTime"    column="create_time"    />
          <result property="createBy"    column="create_by"    />
          <result property="updateTime"    column="update_time"    />
          <result property="updateBy"    column="update_by"    />
          <result property="remake"    column="remake"    />
      </resultMap>

      <sql id="selectLVolunteerOrderVo">
          select id, order_id, volunteer_id, service_category, service_type, service_subject, work_date, work_start_time, work_end_time, work_number, hours, address_id, score, remark, create_time, create_by, update_time, update_by, remake from l_volunteer_order
      </sql>

      <select id="selectLVolunteerOrderList" parameterType="LVolunteerOrder" resultMap="LVolunteerOrderResult">
          <include refid="selectLVolunteerOrderVo"/>
          <where>
              <if test="orderId != null  and orderId != ''"> and order_id = #{orderId}</if>
              <if test="volunteerId != null "> and volunteer_id = #{volunteerId}</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="workDate != null "> and work_date = #{workDate}</if>
              <if test="workStartTime != null "> and work_start_time = #{workStartTime}</if>
              <if test="workEndTime != null "> and work_end_time = #{workEndTime}</if>
              <if test="workNumber != null "> and work_number = #{workNumber}</if>
              <if test="hours != null "> and hours = #{hours}</if>
              <if test="addressId != null "> and address_id = #{addressId}</if>
              <if test="score != null "> and score = #{score}</if>
              <if test="remake != null  and remake != ''"> and remake = #{remake}</if>
          </where>
      </select>

      <select id="selectLVolunteerOrderById" parameterType="Long" resultMap="LVolunteerOrderResult">
          <include refid="selectLVolunteerOrderVo"/>
          where id = #{id}
      </select>

      <insert id="insertLVolunteerOrder" parameterType="LVolunteerOrder" useGeneratedKeys="true" keyProperty="id">
          insert into l_volunteer_order
          <trim prefix="(" suffix=")" suffixOverrides=",">
              <if test="orderId != null">order_id,</if>
              <if test="volunteerId != null">volunteer_id,</if>
              <if test="serviceCategory != null">service_category,</if>
              <if test="serviceType != null">service_type,</if>
              <if test="serviceSubject != null">service_subject,</if>
              <if test="workDate != null">work_date,</if>
              <if test="workStartTime != null">work_start_time,</if>
              <if test="workEndTime != null">work_end_time,</if>
              <if test="workNumber != null">work_number,</if>
              <if test="hours != null">hours,</if>
              <if test="addressId != null">address_id,</if>
              <if test="score != null">score,</if>
              <if test="remark != null">remark,</if>
              <if test="createTime != null">create_time,</if>
              <if test="createBy != null">create_by,</if>
              <if test="updateTime != null">update_time,</if>
              <if test="updateBy != null">update_by,</if>
              <if test="remake != null">remake,</if>
           </trim>
          <trim prefix="values (" suffix=")" suffixOverrides=",">
              <if test="orderId != null">#{orderId},</if>
              <if test="volunteerId != null">#{volunteerId},</if>
              <if test="serviceCategory != null">#{serviceCategory},</if>
              <if test="serviceType != null">#{serviceType},</if>
              <if test="serviceSubject != null">#{serviceSubject},</if>
              <if test="workDate != null">#{workDate},</if>
              <if test="workStartTime != null">#{workStartTime},</if>
              <if test="workEndTime != null">#{workEndTime},</if>
              <if test="workNumber != null">#{workNumber},</if>
              <if test="hours != null">#{hours},</if>
              <if test="addressId != null">#{addressId},</if>
              <if test="score != null">#{score},</if>
              <if test="remark != null">#{remark},</if>
              <if test="createTime != null">#{createTime},</if>
              <if test="createBy != null">#{createBy},</if>
              <if test="updateTime != null">#{updateTime},</if>
              <if test="updateBy != null">#{updateBy},</if>
              <if test="remake != null">#{remake},</if>
           </trim>
      </insert>

      <update id="updateLVolunteerOrder" parameterType="LVolunteerOrder">
          update l_volunteer_order
          <trim prefix="SET" suffixOverrides=",">
              <if test="orderId != null">order_id = #{orderId},</if>
              <if test="volunteerId != null">volunteer_id = #{volunteerId},</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="workDate != null">work_date = #{workDate},</if>
              <if test="workStartTime != null">work_start_time = #{workStartTime},</if>
              <if test="workEndTime != null">work_end_time = #{workEndTime},</if>
              <if test="workNumber != null">work_number = #{workNumber},</if>
              <if test="hours != null">hours = #{hours},</if>
              <if test="addressId != null">address_id = #{addressId},</if>
              <if test="score != null">score = #{score},</if>
              <if test="remark != null">remark = #{remark},</if>
              <if test="createTime != null">create_time = #{createTime},</if>
              <if test="createBy != null">create_by = #{createBy},</if>
              <if test="updateTime != null">update_time = #{updateTime},</if>
              <if test="updateBy != null">update_by = #{updateBy},</if>
              <if test="remake != null">remake = #{remake},</if>
          </trim>
          where id = #{id}
      </update>

      <delete id="deleteLVolunteerOrderById" parameterType="Long">
          delete from l_volunteer_order where id = #{id}
      </delete>

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

    <select id="selectVolunteerOrder" resultType="com.leromro.core.domain.vo.VolunteerOrdersVO">
        select lso.second_order_id,
               lso.main_order_id,
               lso.out_trade_no,
        lso.volunteer_settlement_status,
        lso.work_date,
        lso.work_start_time,
        lso.work_end_time,
        lso.service_duration,
        lso.score,
        lso.remark,
        lso.second_order_id,
        lso.order_status      as orderStatus,
        lso.service_one_price as serviceOnePrice,
        lso.single_quantity ,
        lso.service_total_price,
        lso.volunteer_settlement_status,
        lso.province_code,
        lso.province_name,
        lso.city_code,
        lso.city_name,
        lso.district_code,
        lso.district_name,
        lso.district_code,
        lso.district_name,
        lso.address,
        lso.latitude,
        lso.longitude,
        lso.service_start_time,
        lso.service_end_time,
        lmo.user_id,
        lmo.address         as address,
        lmo.client_name       as name,
        (select avatar from sys_user su where su.user_id = lmo.user_id) as avatar
        from l_second_order lso
        left join l_main_orders lmo on lso.main_order_id = lmo.main_order_id
        <where>
            <if test="userId != null">and lso.volunteer_id = #{userId}</if>
            <if test="orderStatus != null ">and lso.order_status = #{orderStatus}</if>
        </where>
        order by lso.create_time desc
    </select>

    <select id="getCurrentOrgSecondOrderList" resultType="com.leromro.core.domain.SecondOrder">
        select * from l_second_order
        <where>
            <if test="dto.provinceCode != null">and province_code = #{dto.provinceCode}</if>
            <if test="dto.cityCode != null">and city_code = #{dto.cityCode}</if>
            <if test="dto.districtCode != null">and district_code = #{dto.districtCode}</if>
            <if test="dto.startDate != null">and create_time >= #{dto.startDate}</if>
        </where>
    </select>
    <select id="getTodayAllOrderList" resultType="com.leromro.core.domain.SecondOrder">
        select * from l_second_order
        where
            order_status in (1,2,4)
            and create_time &gt;= DATE(NOW())
    </select>


    <select id="selectSettlementOrderList" resultType="com.leromro.core.domain.vo.SettlementOrderListVO">
        SELECT
        a.second_order_id AS orderId,
        a.out_trade_no AS outTradeNo,
        a.create_time AS orderTime,
        su_user.nick_name AS userNickName,
        lvi.name AS volunteerName,
        lvi.business_describe AS businessDescribe,
        lvi.business_tier_name AS businessTierName,
        a.service_start_time AS serviceStartTime,
        a.service_end_time AS serviceEndTime,
        a.service_total_price AS orderTotalPrice,
        a.order_status AS orderStatus,
        area.dept_name AS areaName,
        service_centre.dept_name AS serviceCentreName,
        a.area_distribution_amount AS areaDistributionAmount,
        a.area_settlement_status AS areaSettlementStatus,
        a.area_settlement_time AS areaSettlementTime,
        a.area_payer_method AS areaPayerMethod,
        a.area_payer_account AS areaPayerAccount,
        a.area_payer_account_bank_name AS areaPayerAccountBankName,
        a.area_payee_method AS areaPayeeMethod,
        a.area_payee_account AS areaPayeeAccount,
        a.area_payee_bank_name AS areaPayeeBankName,
        a.area_payee_name AS areaPayeeName,
        a.service_centre_distribution_amount AS serviceCentreDistributionAmount,
        a.service_centre_settlement_status AS serviceCentreSettlementStatus,
        a.service_centre_settlement_time AS serviceCentreSettlementTime,
        a.service_centre_payer_method AS serviceCentrePayerMethod,
        a.service_centre_payer_account AS serviceCentrePayerAccount,
        a.service_centre_payer_bank_name AS serviceCentrePayerBankName,
        a.service_centre_payee_method AS serviceCentrePayeeMethod,
        a.service_centre_payee_account AS serviceCentrePayeeAccount,
        a.service_centre_payee_bank_name AS serviceCentrePayeeBankName,
        a.service_centre_payee_name AS serviceCentrePayeeName
        FROM l_second_order a
        left join sys_dept area on a.area_distribution_id = area.dept_id
        left join sys_dept service_centre on a.service_center_id = service_centre.dept_id
        LEFT JOIN l_main_orders mo ON a.main_order_id = mo.main_order_id
        LEFT JOIN sys_user su_user ON mo.user_id = su_user.user_id
        LEFT JOIN sys_user su_volunteer ON a.volunteer_id = su_volunteer.user_id
        left join sys_user as_user on a.area_settlement_user_id = as_user.user_id
        left join sys_user scs_user on a.service_centre_settlement_user_id = scs_user.user_id
        left join l_volunteer_info lvi on a.volunteer_info_id = lvi.volunteer_info_id
        <where>
            a.volunteer_settlement_status = 20
            <if test="areaDistributionId!=null">
                AND a.area_distribution_id = #{areaDistributionId}
            </if>
            <if test="serviceDistributionId!=null">
                AND a.service_center_id = #{serviceDistributionId}
            </if>
            <if test="orderNo != null and orderNo != ''">
                AND a.out_trade_no LIKE CONCAT('%', #{orderNo}, '%')
            </if>
            <if test="areaName != null and areaName != ''">
                AND area.dept_name LIKE CONCAT('%', #{areaName}, '%')
            </if>
            <if test="serviceCentreName != null and serviceCentreName != ''">
                AND service_centre.dept_name LIKE CONCAT('%', #{serviceCentreName}, '%')
            </if>
            <if test="businessManagementId != null and businessManagementId != ''">
                AND mo.business_management_id = #{businessManagementId}
            </if>
            <if test="orderStatus != null and orderStatus != ''">
                AND a.order_status = #{orderStatus}
            </if>
            <if test="userPhone != null and userPhone != ''">
                AND su_user.phonenumber LIKE CONCAT('%', #{userPhone}, '%')
            </if>
            <if test="volunteerPhone != null and volunteerPhone != ''">
                AND su_volunteer.phonenumber LIKE CONCAT('%', #{volunteerPhone}, '%')
            </if>
            <if test="startTime != null">
                AND a.create_time >= #{startTime}
            </if>
            <if test="endTime != null">
                <![CDATA[
                    AND a.create_time <= #{endTime}
                ]]>
            </if>
            <if test="serviceCompleteStartTime != null ">
                AND a.service_start_time >= #{serviceStartTime}
            </if>
            <if test="serviceCompleteEndTime != null">
                <![CDATA[
                    AND a.work_end_time <= #{serviceEndTime}
                ]]>
            </if>
            <if test="areaSettlementStatus != null and areaSettlementStatus != ''">
                AND a.area_settlement_status = #{areaSettlementStatus}
            </if>
            <if test="areaSettlementName != null and areaSettlementName != ''">
                AND as_user.nick_name LIKE CONCAT('%', #{areaSettlementName}, '%')
            </if>
            <if test="areaSettlementStartTime != null">
                AND a.area_settlement_time >= #{areaSettlementStartTime}
            </if>
            <if test="areaSettlementEndTime != null">
                <![CDATA[
                    AND a.area_settlement_time <= #{areaSettlementEndTime}
                ]]>
            </if>
            <if test="serviceCentreSettlementStatus != null and serviceCentreSettlementStatus != ''">
                AND a.service_centre_settlement_status = #{serviceCentreSettlementStatus}
            </if>
            <if test="serviceCentreSettlementName != null and serviceCentreSettlementName!=''">
                AND scs_user.nick_name LIKE CONCAT('%', #{serviceCentreSettlementName}, '%')
            </if>
            <if test="serviceCentreSettlementStartTime != null">
                AND a.service_centre_settlement_time >= #{serviceCentreSettlementStartTime}
            </if>
            <if test="serviceCentreSettlementEndTime != null">
                <![CDATA[
                    AND a.service_centre_settlement_time <= #{serviceCentreSettlementEndTime}
                ]]>
            </if>
        </where>
    </select>
    <select id="selectSecondOrderInfoVO" resultType="com.leromro.core.domain.vo.SecondOrderInfoVO"
            parameterType="java.lang.Long">
        select
            lso.second_order_id,lso.score,lso.service_total_price,lso.order_status,lso.volunteer_settlement_status,lso.address,
            lso.volunteer_review,lso.volunteer_picture,lso.service_category,lso.user_review,lso.user_picture,
            lso.ability_score,lso.clothing_score,lso.protocol_score,lso.quality_score,lso.province_code,lso.province_name,lso.city_name,lso.city_code,
            lso.work_date,lso.work_start_time,lso.single_quantity,lso.work_number,lso.out_trade_no as secondOutTradeNo,lso.service_duration,
            (select sd.dept_name from sys_dept sd where lso.area_distribution_id = sd.dept_id) as areaDistributionName,
            (select sd.dept_name from sys_dept sd where lso.service_center_id = sd.dept_id) as serviceCenterName
        from l_second_order lso where lso.main_order_id = #{mainOrderId};
    </select>

    <select id="getSettlementOrderDetail" resultType="com.leromro.core.domain.vo.SettlementOrderDetailVO">
        SELECT
        a.second_order_id AS orderId,
        a.out_trade_no AS outTradeNo,
        a.create_time AS orderTime,
        su_user.nick_name AS userNickName,
        lvi.name AS volunteerName,
        lvi.business_describe AS businessDescribe,
        lvi.business_tier_name AS businessTierName,
        a.service_start_time AS serviceStartTime,
        a.service_end_time AS serviceEndTime,
        a.service_total_price AS orderTotalPrice,
        a.order_status AS orderStatus,
        area.dept_name AS areaName,
        service_centre.dept_name AS serviceCentreName,
        a.area_distribution_amount AS areaDistributionAmount,
        a.area_settlement_status AS areaSettlementStatus,
        a.area_settlement_time AS areaSettlementTime,
        a.area_payer_method AS areaPayerMethod,
        a.area_payer_account AS areaPayerAccount,
        a.area_payer_account_bank_name AS areaPayerAccountBankName,
        a.area_payee_method AS areaPayeeMethod,
        a.area_payee_account AS areaPayeeAccount,
        a.area_payee_bank_name AS areaPayeeBankName,
        a.area_payee_name AS areaPayeeName,
        a.service_centre_distribution_amount AS serviceCentreDistributionAmount,
        a.service_centre_settlement_status AS serviceCentreSettlementStatus,
        a.service_centre_settlement_time AS serviceCentreSettlementTime,
        a.service_centre_payer_method AS serviceCentrePayerMethod,
        a.service_centre_payer_account AS serviceCentrePayerAccount,
        a.service_centre_payer_bank_name AS serviceCentrePayerBankName,
        a.service_centre_payee_method AS serviceCentrePayeeMethod,
        a.service_centre_payee_account AS serviceCentrePayeeAccount,
        a.service_centre_payee_bank_name AS serviceCentrePayeeBankName,
        a.service_centre_payee_name AS serviceCentrePayeeName
        FROM l_second_order a
        left join sys_dept area on a.area_distribution_id = area.dept_id
        left join sys_dept service_centre on a.service_center_id = service_centre.dept_id
        LEFT JOIN l_main_orders mo ON a.main_order_id = mo.main_order_id
        LEFT JOIN sys_user su_user ON mo.user_id = su_user.user_id
        LEFT JOIN sys_user su_volunteer ON a.volunteer_id = su_volunteer.user_id
        left join sys_user as_user on a.area_settlement_user_id = as_user.user_id
        left join sys_user scs_user on a.service_centre_settlement_user_id = scs_user.user_id
        left join l_volunteer_info lvi on a.volunteer_info_id = lvi.volunteer_info_id
        <where>
            a.second_order_id = #{orderId}
        </where>
    </select>
    <select id="selectAppSecondOrderInfo" resultType="com.leromro.core.domain.vo.AppSecoundOrderInfoVO"
            parameterType="java.lang.Long">
        select
            lso.second_order_id,lso.order_status,lso.volunteer_settlement_status,lso.volunteer_review,lso.volunteer_picture,lso.user_review
                ,lso.user_picture,lso.work_date,lso.work_start_time,lso.work_end_time,lso.single_quantity,lso.work_number,lso.service_duration,
            lso.province_code,lso.province_name,lso.city_code,lso.city_name,lso.district_code,lso.district_name,lso.latitude,lso.longitude,lso.out_trade_no
                ,lso.clothing_score,lso.ability_score,lso.quality_score,lso.protocol_score,lvi.business_tier_name businessTireName,lmo.client_telephone as clientPhoneNumber,lmo.client_name clientName,lso.remark,lso.score,
            CONCAT( IFNULL(lso.province_name, ''),IFNULL(lso.city_name, ''), IFNULL(lso.district_name, ''), IFNULL(lso.address, '') ) AS address,
            (select sd.dept_name from sys_dept sd where sd.dept_id = lso.area_distribution_id) as areaDistributionName,
            (select sd.dept_name from  sys_dept sd where sd.dept_id = lso.service_center_id) as  serviceCenterName
        from (l_second_order lso left join l_volunteer_info lvi on lso.volunteer_info_id = lvi.volunteer_info_id )left join l_main_orders lmo on lmo.main_order_id = lso.main_order_id
        where lso.second_order_id = #{secondOrderId}
    </select>
    <select id="selectWebSecondOrderInfo" resultType="com.leromro.core.domain.vo.WebSecondOrderInfoVO"
            parameterType="java.lang.Long">
        select
            lso.*,lvi.business_tier_name businessTireName,lmo.client_telephone as clientPhoneNumber,lmo.client_name clientName,
            CONCAT( IFNULL(lso.province_name, ''),IFNULL(lso.city_name, ''), IFNULL(lso.district_name, ''), IFNULL(lso.address, '') ) AS address,
            (select sd.dept_name from sys_dept sd where sd.dept_id = lso.area_distribution_id) as areaDistributionName,
            (select sd.dept_name from  sys_dept sd where sd.dept_id = lso.service_center_id) as  serviceCenterName
        from (l_second_order lso left join l_volunteer_info lvi on lso.volunteer_info_id = lvi.volunteer_info_id )left join l_main_orders lmo on lmo.main_order_id = lso.main_order_id
        where lso.second_order_id = #{secondOrderId}

    </select>

</mapper>