<?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.OrderSettlementApplicationMapper">
    
    <resultMap type="orderSettlementApplicationListVO" id="OrderSettlementApplicationResult">
        <result property="id"    column="id"    />
        <result property="orderId"    column="order_id"    />
        <result property="applicantType"    column="applicant_type"    />
        <result property="applicantId"    column="applicant_id"    />
        <result property="amount"    column="amount"    />
        <result property="applicationUserId"    column="application_user_id"    />
        <result property="applicationTime"    column="application_time"    />
        <result property="payeeMethod"    column="payee_method"    />
        <result property="payeeAccount"    column="payee_account"    />
        <result property="payeeBankName"    column="payee_bank_name"    />
        <result property="payeeName"    column="payee_name"    />
        <result property="payeeRemark"    column="payee_remark"    />
        <result property="auditorUserId"    column="auditor_user_id"    />
        <result property="auditorTime"    column="auditor_time"    />
        <result property="auditorStatus"    column="auditor_status"    />
        <result property="auditorRemark"    column="auditor_remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <sql id="selectOrderSettlementApplicationVo">
        select id, order_id, applicant_type, applicant_id, amount, application_user_id, application_time, payee_method, payee_account, payee_bank_name, payee_name, payee_remark, auditor_user_id, auditor_time, auditor_status, auditor_remark, create_time, update_time, create_by, update_by from l_order_settlement_application
    </sql>
    
    <select id="selectOrderSettlementApplicationById" resultType="com.leromro.core.domain.vo.OrderSettlementApplicationDetailVO">
        SELECT
            a.id as id,
            a.application_time as applyTime,
            application_user.nick_name AS applyUserName,
            sys_dept.dept_name AS applyUserName,
            l_second_order.second_order_id AS secondOrderId,
            l_second_order.out_trade_no AS outTradeNo,
            l_second_order.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,
            l_second_order.service_start_time AS serviceStartTime,
            l_second_order.service_end_time AS serviceEndTime,
            l_second_order.service_total_price AS orderTotalPrice,
            l_second_order.order_status AS orderStatus,
            area.dept_name AS areaName,
            service_centre.dept_name AS serviceCentreName,
            l_second_order.area_distribution_amount AS areaDistributionAmount,
            l_second_order.area_settlement_status AS areaSettlementStatus,
            l_second_order.area_settlement_time AS areaSettlementTime,
            l_second_order.service_centre_distribution_amount AS serviceCentreDistributionAmount,
            l_second_order.service_centre_settlement_status AS serviceCentreSettlementStatus,
            l_second_order.service_centre_settlement_time AS serviceCentreSettlementTime,
            auditor_user.nick_name as auditorUserNickName,
            a.auditor_time as auditorTime,
            a.auditor_status as auditorStatus,
            a.auditor_remark as auditorRemark
            FROM l_order_settlement_application a
            left join l_second_order on a.order_id = l_second_order.second_order_id
            LEFT JOIN l_main_orders mo ON l_second_order.main_order_id = mo.main_order_id
            LEFT JOIN sys_user auditor_user ON a.auditor_user_id = auditor_user.user_id
            LEFT JOIN sys_user application_user ON a.application_user_id = application_user.user_id
            left join sys_dept on a.applicant_id = sys_dept.dept_id
            LEFT JOIN sys_user su_user ON mo.user_id = su_user.user_id
            LEFT JOIN sys_user su_volunteer ON l_second_order.volunteer_id = su_volunteer.user_id
            left join l_volunteer_info lvi on l_second_order.volunteer_info_id = lvi.volunteer_info_id
            left join sys_dept area on l_second_order.area_distribution_id = area.dept_id
            left join sys_dept service_centre on l_second_order.service_center_id = service_centre.dept_id
            where id = #{id}
    </select>

    <select id="selectOrderSettlementApplicationList" resultType="com.leromro.core.domain.vo.OrderSettlementApplicationListVO">
        SELECT
        a.id as id,
        a.application_time as applyTime,
        application_user.nick_name AS applyUserName,
        sys_dept.dept_name AS applyUserName,
        l_second_order.second_order_id AS secondOrderId,
        l_second_order.out_trade_no AS outTradeNo,
        l_second_order.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,
        l_second_order.service_start_time AS serviceStartTime,
        l_second_order.service_end_time AS serviceEndTime,
        l_second_order.service_total_price AS orderTotalPrice,
        l_second_order.order_status AS orderStatus,
        area.dept_name AS areaName,
        service_centre.dept_name AS serviceCentreName,
        l_second_order.area_distribution_amount AS areaDistributionAmount,
        l_second_order.area_settlement_status AS areaSettlementStatus,
        l_second_order.area_settlement_time AS areaSettlementTime,
        l_second_order.service_centre_distribution_amount AS serviceCentreDistributionAmount,
        l_second_order.service_centre_settlement_status AS serviceCentreSettlementStatus,
        l_second_order.service_centre_settlement_time AS serviceCentreSettlementTime,
        auditor_user.nick_name as auditorUserNickName,
        a.auditor_time as auditorTime,
        a.auditor_status as auditorStatus,
        a.auditor_remark as auditorRemark
        FROM l_order_settlement_application a
        left join l_second_order on a.order_id = l_second_order.second_order_id
        LEFT JOIN l_main_orders mo ON l_second_order.main_order_id = mo.main_order_id
        LEFT JOIN sys_user auditor_user
        ON a.auditor_user_id = auditor_user.user_id
        LEFT JOIN sys_user application_user
        ON a.application_user_id = application_user.user_id
        left join sys_dept on a.applicant_id = sys_dept.dept_id
        LEFT JOIN sys_user su_user ON mo.user_id = su_user.user_id
        LEFT JOIN sys_user su_volunteer ON l_second_order.volunteer_id = su_volunteer.user_id
        left join l_volunteer_info lvi on l_second_order.volunteer_info_id = lvi.volunteer_info_id
        left join sys_dept area on l_second_order.area_distribution_id = area.dept_id
        left join sys_dept service_centre on l_second_order.service_center_id = service_centre.dept_id
        <where>
            <if test="deptIds!=null and deptIds.size()>0">
                and a.applicant_id in
                <foreach collection="deptIds" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="auditorStatus!=null and auditorStatus!=''">
                and a.auditor_status = #{auditorStatus}
            </if>
            <if test="auditorTimeStart != null">
                AND a.auditor_time >= #{auditorTimeStart}
            </if>
            <if test="auditorTimeEnd != null">
                <![CDATA[
                    AND a.auditor_time <= #{auditorTimeEnd}
                ]]>
            </if>
            <if test="auditorUserName!=null and auditorUserName!=''">
                AND auditor_user.nick_name LIKE CONCAT('%', #{auditorUserName}, '%')
            </if>
            <if test="applyTimeStart != null">
                AND a.application_time >= #{applyTimeStart}
            </if>
            <if test="applyTimeEnd != null">
                <![CDATA[
                    AND a.application_time <= #{applyTimeEnd}
                ]]>
            </if>
            <if test="applyUserName!=null and applyUserName!=''">
                AND application_user.nick_name LIKE CONCAT('%', #{applyUserName}, '%')
            </if>
            <if test="applicant!=null and applicant!=''">
                AND sys_dept.dept_name LIKE CONCAT('%', #{applicant}, '%')
            </if>
            <if test="orderNo != null and orderNo != ''">
                AND a.out_trade_no LIKE CONCAT('%', #{orderNo}, '%')
            </if>
            <if test="startTime != null">
                AND l_second_order.create_time >= #{startTime}
            </if>
            <if test="endTime != null">
                <![CDATA[
                    AND l_second_order.create_time <= #{endTime}
                ]]>
            </if>
            <if test="serviceCompleteStartTime != null ">
                AND a.work_end_time >= #{serviceCompleteStartTime}
            </if>
            <if test="serviceCompleteEndTime != null">
                <![CDATA[
                    AND a.work_end_time <= #{serviceCompleteEndTime}
                ]]>
            </if>
        </where>
    </select>

    <insert id="insertOrderSettlementApplication" parameterType="OrderSettlementApplication" useGeneratedKeys="true" keyProperty="id">
        insert into l_order_settlement_application
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="applicantType != null">applicant_type,</if>
            <if test="applicantId != null and applicantId != ''">applicant_id,</if>
            <if test="amount != null">amount,</if>
            <if test="applicationUserId != null">application_user_id,</if>
            <if test="applicationTime != null">application_time,</if>
            <if test="payeeMethod != null">payee_method,</if>
            <if test="payeeAccount != null and payeeAccount != ''">payee_account,</if>
            <if test="payeeBankName != null and payeeBankName != ''">payee_bank_name,</if>
            <if test="payeeName != null and payeeName != ''">payee_name,</if>
            <if test="payeeRemark != null">payee_remark,</if>
            <if test="auditorUserId != null">auditor_user_id,</if>
            <if test="auditorTime != null">auditor_time,</if>
            <if test="auditorStatus != null">auditor_status,</if>
            <if test="auditorRemark != null">auditor_remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="applicantType != null">#{applicantType},</if>
            <if test="applicantId != null and applicantId != ''">#{applicantId},</if>
            <if test="amount != null">#{amount},</if>
            <if test="applicationUserId != null">#{applicationUserId},</if>
            <if test="applicationTime != null">#{applicationTime},</if>
            <if test="payeeMethod != null">#{payeeMethod},</if>
            <if test="payeeAccount != null and payeeAccount != ''">#{payeeAccount},</if>
            <if test="payeeBankName != null and payeeBankName != ''">#{payeeBankName},</if>
            <if test="payeeName != null and payeeName != ''">#{payeeName},</if>
            <if test="payeeRemark != null">#{payeeRemark},</if>
            <if test="auditorUserId != null">#{auditorUserId},</if>
            <if test="auditorTime != null">#{auditorTime},</if>
            <if test="auditorStatus != null">#{auditorStatus},</if>
            <if test="auditorRemark != null">#{auditorRemark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
         </trim>
    </insert>

    <update id="updateOrderSettlementApplication" parameterType="OrderSettlementApplication">
        update l_order_settlement_application
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="applicantType != null">applicant_type = #{applicantType},</if>
            <if test="applicantId != null and applicantId != ''">applicant_id = #{applicantId},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="applicationUserId != null">application_user_id = #{applicationUserId},</if>
            <if test="applicationTime != null">application_time = #{applicationTime},</if>
            <if test="payeeMethod != null">payee_method = #{payeeMethod},</if>
            <if test="payeeAccount != null and payeeAccount != ''">payee_account = #{payeeAccount},</if>
            <if test="payeeBankName != null and payeeBankName != ''">payee_bank_name = #{payeeBankName},</if>
            <if test="payeeName != null and payeeName != ''">payee_name = #{payeeName},</if>
            <if test="payeeRemark != null">payee_remark = #{payeeRemark},</if>
            <if test="auditorUserId != null">auditor_user_id = #{auditorUserId},</if>
            <if test="auditorTime != null">auditor_time = #{auditorTime},</if>
            <if test="auditorStatus != null">auditor_status = #{auditorStatus},</if>
            <if test="auditorRemark != null">auditor_remark = #{auditorRemark},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOrderSettlementApplicationById" parameterType="Long">
        delete from l_order_settlement_application where id = #{id}
    </delete>

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