<?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.company.mapper.BizCompanyMapper">

    <resultMap type="cn.ezhizao.project.business.company.domain.BizCompany" id="BizCompanyResult">
        <id column="id" property="id"/>
        <result column="source_category_id" property="sourceCategoryId" />
        <association property="sourceCategoryName" javaType="java.lang.String" select="getSourceCategoryName" column="source_category_id"/>
        <association property="referrerDataSource" javaType="java.lang.String" select="getReferrerDataSource" column="source_category_id"/>
    </resultMap>

    <select id="getSourceCategoryName" resultType="java.lang.String">
        select title from biz_source where id = #{source_category_id}
    </select>
    <select id="getReferrerDataSource" resultType="java.lang.String">
        select referrer_data_source from biz_source where id = #{source_category_id}
    </select>

    <select id="getList" parameterType="BizCompany" resultMap="BizCompanyResult">
        SELECT
        c.*, s.nick_name as leader_name, a.nick_name as adviser_name
        FROM biz_company c
        LEFT JOIN sys_user s ON c.leader_id=s.user_id
        Left join sys_user a on c.adviser_id = a.user_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            c.deleted = 0
            <if test="id != null  and id != ''">AND c.id = #{id}</if>
            <if test="category != null  and category != ''">AND c.category = #{category}</if>
            <if test="code != null  and code != ''">AND c.code = #{code}</if>
            <if test="name != null  and name != ''">AND c.name like concat('%', #{name}, '%')</if>
            <if test="isZero != null ">AND c.is_zero = #{isZero}</if>
            <if test="keyword != null  and keyword != ''">AND (c.name like concat('%', #{keyword}, '%') or c.code like concat('%', #{keyword}, '%'))</if>
            <if test="tenantId != null"> AND (c.tenant_id = #{tenantId}
                or exists (select 1 from biz_entrust entrust where entrust.deleted = 0 and entrust.company_id =
                c.id and entrust.task_type_id = 1 and entrust.to_tenant_id = #{tenantId}) )</if>
        </trim>
    </select>

    <select id="getById" parameterType="Long" resultMap="BizCompanyResult">
        SELECT * FROM biz_company WHERE id = #{id}
    </select>


    <select id="getUserById" resultType="String">
        SELECT nick_name FROM sys_user WHERE user_id = #{id}
    </select>

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