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

    <resultMap type="cn.ezhizao.project.business.entrustPrice.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="toTenantId" column="to_tenant_id"/>
    </resultMap>
    <select id="getList" parameterType="BizEntrustPrice" resultMap="BizEntrustPriceResult">
        SELECT * FROM biz_entrust_price e
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            e.deleted = 0
            <if test="price != null ">AND e.price = #{price}</if>
            <if test="annualIncome != null  and annualIncome != ''">AND e.annual_income = #{annualIncome}</if>
            <if test="isChanged != null ">AND e.is_changed = #{isChanged}</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="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="getTaskTypeName" resultType="java.lang.String">
        SELECT name
        FROM biz_task_type
        WHERE id = #{task_type_id}
          AND deleted = 0
          AND type = 1
    </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>
