<?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.reconciliation.priceList.mapper.BizEntrustPriceMapper">

    <resultMap type="cn.ezhizao.project.business.reconciliation.priceList.domain.BizEntrustPrice"
               id="BizEntrustPriceResult">
        <id column="id" property="id"/>
        <result property="taskTypeId" column="task_type_id"/>
        <result property="fromTenantId" column="from_tenant_id"/>

        <result property="alterType" column="alter_type"/>

        <result property="toTenantId" column="to_tenant_id"/>
        <collection property="fromAccountName" ofType="java.lang.String" select="getFromAccountName"
                    column="from_tenant_id"/>
        <collection property="toAccountName" ofType="java.lang.String" select="getToAccountName" column="to_tenant_id"/>
        <collection property="taskTypeName" ofType="java.lang.String" select="getTaskTypeName" column="task_type_id"/>
        <collection property="taskTypeDetailName" ofType="java.lang.String" select="getTaskTypeDetailName"
                    column="task_type_detail_id"/>

    </resultMap>

    <resultMap type="cn.ezhizao.project.business.reconciliation.priceList.domain.BizTaskType" id="BizTaskTypeResult">
        <id column="id" property="id"/>
    </resultMap>

    <resultMap type="cn.ezhizao.project.tenant.domain.Tenant" id="TenantResult">
        <id column="id" property="id"/>
    </resultMap>

    <resultMap type="cn.ezhizao.project.business.reconciliation.priceList.domain.BizTaskTypeDetail"
               id="BizTaskTypeDetailResult">
        <id column="id" property="id"/>
    </resultMap>

    <select id="getList" parameterType="BizEntrustPrice" resultMap="BizEntrustPriceResult">
        SELECT * FROM biz_entrust_price e
        LEFT JOIN biz_task_type t ON e.task_type_id = t.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            e.deleted = 0
            AND exists (select 1 from biz_task_type ts
            where ts.deleted = 0
            and ts.id=e.task_type_id
            and ts.type=1)
            <if test="alterType != null  and alterType != ''">AND e.alter_type = #{alterType}</if>
            <if test="annualIncome != null  and annualIncome != ''">AND e.annual_income = #{annualIncome}</if>
            <if test="fromTenantId != null ">AND e.from_tenant_id = #{fromTenantId}</if>
            <if test="taskTypeId != null ">AND e.task_type_id = #{taskTypeId}</if>
            <if test="isChanged != null ">AND e.is_changed = #{isChanged}</if>
            <if test="isZero != null and isZero!=''">AND e.is_zero = #{isZero}</if>
            <if test="fromAccountName != null  and fromAccountName!=''">
                AND exists (select t.id from biz_tenant t
                where t.deleted = 0
                and e.from_tenant_id=t.id
                and t.account_name like concat('%', #{fromAccountName}, '%')
                )
            </if>
            <if test="toAccountName != null and toAccountName!=''">
                AND exists (select t.id from biz_tenant t
                where t.deleted = 0
                and e.to_tenant_id=t.id
                and t.account_name like concat('%', #{toAccountName}, '%')
                )
            </if>
            <if test="toTenantId != null ">AND e.to_tenant_id = #{toTenantId}</if>
        </trim>
    </select>


    <select id="getOnceList" parameterType="BizEntrustPrice" resultMap="BizEntrustPriceResult">
        SELECT * FROM biz_entrust_price e
        LEFT JOIN biz_task_type t ON e.task_type_id = t.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            e.deleted = 0
            AND exists (select 1 from biz_task_type ts
            where ts.deleted = 0
            and ts.id=e.task_type_id
            and ts.type=2)
            <if test="taskTypeId != null ">AND e.task_type_id = #{taskTypeId}</if>
            <if test="taskTypeDetailId != null ">AND e.task_type_detail_id = #{taskTypeDetailId}</if>
            <if test="provinceCode != null ">AND e.province_code = #{provinceCode}</if>
            <if test="cityCode != null ">AND e.city_code = #{cityCode}</if>
            <if test="districtCode != null ">AND e.district_code = #{districtCode}</if>
            <if test="toTenantId != null ">AND e.to_tenant_id = #{toTenantId}</if>
        </trim>
    </select>

    <select id="getTenantList" parameterType="Tenant" resultMap="TenantResult">
        SELECT * FROM biz_tenant
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="accountName != null">
                AND account_name like concat('%', #{accountName}, '%')
            </if>
        </trim>
    </select>

    <select id="getTaskTypeName" resultType="java.lang.String">
        SELECT name
        FROM biz_task_type
        WHERE id = #{task_type_id}
          AND deleted = 0
    </select>

    <select id="getTaskTypeDetailName" resultType="java.lang.String">
        SELECT name
        FROM biz_task_type_detail
        WHERE id = #{task_type_detail_id}
          AND deleted = 0
    </select>

    <select id="getTaskTypeDetailList" parameterType="BizTaskTypeDetail" resultMap="BizTaskTypeDetailResult">
        SELECT *
        FROM biz_task_type_detail
        WHERE deleted = 0
          AND task_type_id = #{id}
    </select>

    <select id="getTaskType" parameterType="BizTaskType" resultMap="BizTaskTypeResult">
        SELECT *
        FROM biz_task_type
        WHERE deleted = 0
          AND type = #{type}
    </select>

    <select id="getFromAccountName" resultType="java.lang.String">
        SELECT account_name
        FROM biz_tenant
        WHERE id = #{from_tenant_id}
    </select>

    <select id="getToAccountName" resultType="java.lang.String">
        SELECT account_name
        FROM biz_tenant
        WHERE id = #{to_tenant_id}
    </select>

    <delete id="physicalDelete">
        DELETE FROM biz_entrust_price
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
            <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
