<?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="cn.ezhizao.project.business.upgrade.mapper.BizWorkOrderDetailUpdateMapper">

    <resultMap type="cn.ezhizao.project.business.upgrade.domain.BizWorkOrderDetailUpdate" id="BizWorkOrderDetailResult">
        <id column="id" property="id"/>
        <result column="contract_id" jdbcType="BIGINT" property="contractId"/>
        <association property="totalityMoney" javaType="java.math.BigDecimal" select="getTotalityMoney" column="contract_id"/>
        <association property="accountsReceivable" javaType="java.math.BigDecimal" select="getAccountsReceivable" column="contract_id"/>
    </resultMap>


    <select id="getList" parameterType="BizWorkOrderDetailUpdate" resultMap="BizWorkOrderDetailResult">
        SELECT * FROM biz_work_order_detail_update
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="id != null "> AND id = #{id}</if>
            <if test="tenantId != null "> AND tenant_id = #{tenantId}</if>
            <if test="workOrderId != null "> AND work_order_id = #{workOrderId}</if>
            <if test="companyId != null "> AND company_id = #{companyId}</if>
            <if test="contractDetailId != null "> AND contract_detail_id = #{contractDetailId}</if>
            <if test="contractId != null "> AND contract_id = #{contractId}</if>
            <if test="serviceNum != null "> AND service_num = #{serviceNum}</if>
            <if test="freeNum != null "> AND free_num = #{freeNum}</if>
        </trim>
    </select>
    <select id="selectByUpgradeId" parameterType="BizWorkOrderDetail" resultType="cn.ezhizao.project.business.workOrder.domain.BizWorkOrderDetail">
        SELECT * FROM biz_work_order_detail_update
        <where>
            <if test="tenantId != null "> AND tenant_id = #{tenantId}</if>
        </where>
    </select>
    <select id="getAccountsReceivable" resultType="java.math.BigDecimal">
        SELECT true_amount FROM biz_archive_input_update
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted= 0
            <if test="contractId!= null "> AND id =#{contractId}</if>
        </trim>
    </select>

    <select id="getTotalityMoney" resultType="java.math.BigDecimal">
        SELECT SUM(arrive_amount)FROM biz_collection
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted= 0
            AND  status = 1
            <if test="contractId!= null "> AND contract_id =#{contractId}</if>

        </trim>
    </select>

    <delete id="physicalDelete">
        DELETE FROM biz_work_order_detail_update
        <where>
            AND id  IN
            <foreach item="item" index="index" collection="detailUpdateList" open="(" separator="," close=")">
                #{item.id}
            </foreach>
        </where>
    </delete>
</mapper>
