<?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.entrust.mapper.BizEntrustMapper">

    <resultMap type="cn.ezhizao.project.business.entrust.domain.BizEntrust" id="BizEntrustResult">
        <id column="id" property="id"/>
    </resultMap>


    <select id="getList" parameterType="BizEntrust" resultMap="BizEntrustResult">
        SELECT t1.*, t2.account_name as to_account_name, t3.name as task_type_name, t4.name as company_name
        , t5.name as task_type_detail_name
        FROM biz_entrust t1
        left join biz_tenant t2 on t2.id = t1.to_tenant_id
        left join biz_company t4 on t4.id = t1.company_id
        left join biz_task_type t3 on t3.id = t1.task_type_id
        left join biz_task_type_detail t5 on t5.id = t1.task_type_detail_id
        LEFT JOIN biz_work_order t6 on t6.id = t1.work_order_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t6.deleted = 0
            <if test="id != null "> AND t1.id = #{id}</if>
            <if test="companyId != null "> AND t1.company_id = #{companyId}</if>
            <if test="fromTenantId != null "> AND t1.from_tenant_id = #{fromTenantId}</if>
            <if test="workMonth != null "> AND t1.work_month = #{workMonth}</if>
            <if test="workMonthFrom != null and workMonthTo != null"> and t1.work_month between #{workMonthFrom} and #{workMonthTo}</if>
            <if test="currentMonth != null "> AND t1.current_month = #{currentMonth}</if>
            <if test="toTenantId != null "> AND t1.to_tenant_id = #{toTenantId}</if>
            <if test="workOrderId != null "> AND t1.work_order_id = #{workOrderId}</if>
            <if test="workOrderIds != null and workOrderIds.size() > 0">
                AND t1.work_order_id IN
                <foreach collection="workOrderIds" index="index" item="item" separator="," open="(" close=")">
                    #{item,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="taskTypeId != null "> AND t1.task_type_id = #{taskTypeId}</if>
            <if test="taskTypeDetailId != null "> AND t1.task_type_detail_id = #{taskTypeDetailId}</if>
            <if test="type != null "> AND t3.type = #{type}</if>
            <if test="fromId != null "> AND t1.from_id = #{fromId}</if>
            <if test="tenantId != null "> AND t1.tenant_id = #{tenantId}</if>
        </trim>
    </select>

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