<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.base.dao.ApplicationExamineDAO">
    <resultMap id="ApplicationExamineMap" type="com.jpsoft.shinestar.modules.base.entity.ApplicationExamine">
        <id property="id" column="id_"/>
        <result property="applicationRoleId" column="application_role_id"/>
        <result property="applicationCompanyId" column="application_company_id"/>
        <result property="examineRoleId" column="examine_role_id"/>
        <result property="examineCompanyId" column="examine_company_id"/>
        <result property="skipExamineRoleId" column="skip_examine_role_id"/>
        <result property="skipExamineCompanyId" column="skip_examine_company_id"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark_"/>
        <result property="level" column="level_"/>
        <result property="type" column="type_"/>
        <result property="days" column="days_"/>
        <result property="includeSubordinate" column="include_subordinate"/>
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.ApplicationExamine">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into base_application_examine
	    (id_,application_role_id,application_company_id,examine_role_id,examine_company_id,del_flag,
	    create_by,create_time,update_by,update_time,remark_,
	    level_,type_,days_,include_subordinate)
		values
		(
			#{id,jdbcType=VARCHAR}
			,#{applicationRoleId,jdbcType=VARCHAR}
			,#{applicationCompanyId,jdbcType=VARCHAR}
			,#{examineRoleId,jdbcType=VARCHAR}
			,#{examineCompanyId,jdbcType=VARCHAR}
			,#{delFlag,jdbcType= NUMERIC }
			,#{createBy,jdbcType=VARCHAR}
			,#{createTime,jdbcType= TIMESTAMP }
			,#{updateBy,jdbcType=VARCHAR}
			,#{updateTime,jdbcType= TIMESTAMP }
			,#{remark,jdbcType=VARCHAR}
			,#{level,jdbcType=INTEGER}
			,#{type,jdbcType=VARCHAR}
			,#{days,jdbcType=INTEGER}
			,#{includeSubordinate,jdbcType= NUMERIC }
		)
	]]>
    </insert>
    <delete id="delete" parameterType="string">
        delete from base_application_examine where id_=#{id,jdbcType=VARCHAR}
    </delete>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.ApplicationExamine">
        update base_application_examine
        <set>
            <if test="applicationRoleId!=null">
                application_role_id=#{applicationRoleId,jdbcType=VARCHAR},
            </if>
            <if test="applicationCompanyId!=null">
                application_company_id=#{applicationCompanyId,jdbcType=VARCHAR},
            </if>
            <if test="examineRoleId!=null">
                examine_role_id=#{examineRoleId,jdbcType=VARCHAR},
            </if>
            <if test="examineCompanyId!=null">
                examine_company_id=#{examineCompanyId,jdbcType=VARCHAR},
            </if>
            <if test="skipExamineRoleId!=null">
                skip_examine_role_id=#{skipExamineRoleId,jdbcType=VARCHAR},
            </if>
            <if test="skipExamineCompanyId!=null">
                skip_examine_company_id=#{skipExamineCompanyId,jdbcType=VARCHAR},
            </if>
            <if test="delFlag!=null">
                del_flag=#{delFlag,jdbcType= NUMERIC },
            </if>
            <if test="createBy!=null">
                create_by=#{createBy,jdbcType=VARCHAR},
            </if>
            <if test="createTime!=null">
                create_time=#{createTime,jdbcType= TIMESTAMP },
            </if>
            <if test="updateBy!=null">
                update_by=#{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime!=null">
                update_time=#{updateTime,jdbcType= TIMESTAMP },
            </if>
            <if test="remark!=null">
                remark_=#{remark,jdbcType=VARCHAR},
            </if>
            <if test="level!=null">
                level_=#{level,jdbcType=INTEGER},
            </if>
            <if test="type!=null">
                type_=#{type,jdbcType=VARCHAR},
            </if>
            <if test="days!=null">
                days_=#{days,jdbcType=INTEGER},
            </if>
            <if test="includeSubordinate!=null">
                include_subordinate=#{includeSubordinate,jdbcType=NUMERIC},
            </if>
        </set>
        where id_=#{id}
    </update>
    <select id="get" parameterType="string" resultMap="ApplicationExamineMap">
        select * from base_application_examine where id_=#{0}
    </select>
    <select id="exist" parameterType="string" resultType="int">
        select count(*) from base_application_examine where id_=#{0}
    </select>
    <select id="list" resultMap="ApplicationExamineMap">
        select * from base_application_examine
    </select>
    <select id="search" parameterType="hashmap" resultMap="ApplicationExamineMap">
        <![CDATA[
			SELECT
                a.*
            FROM
                base_application_examine a
                LEFT JOIN base_company_info b ON a.application_company_id = b.id_
		]]>
        <where>
            a.del_flag = 0
            <if test="searchParams.id != null">
                and a.ID_ like #{searchParams.id}
            </if>
            <if test="searchParams.applicationRoleId != null">
                and find_in_set(#{searchParams.applicationRoleId},a.application_role_id)
            </if>
            <if test="searchParams.applicationCompanyId != null">
                and a.application_company_id = #{searchParams.applicationCompanyId}
            </if>
            <if test="searchParams.includeSubordinate != null">
                and a.include_subordinate = #{searchParams.includeSubordinate}
            </if>
            <if test="searchParams.level != null">
                and a.level_ = #{searchParams.level}
            </if>
            <if test="searchParams.type != null">
                and a.type_ = #{searchParams.type}
            </if>
            <if test="searchParams.applicationCompanyCode != null">
                and a.application_company_id in (
                SELECT id_ FROM base_company_info WHERE code_ LIKE #{searchParams.applicationCompanyCode}
                )
            </if>
            <if test="searchParams.examineCompanyId != null">
                and a.examine_company_id like #{searchParams.examineCompanyId}
            </if>
            <if test="searchParams.examineCompanyCode != null">
                and a.examine_company_id in (
                SELECT id_ FROM base_company_info WHERE code_ LIKE #{searchParams.examineCompanyCode}
                )
            </if>
            <if test="searchParams.applicationPermissionCode != null">
                <if test="searchParams.examinePermissionCode != null">
                    and (
                    a.application_company_id in (
                    SELECT id_ FROM base_company_info WHERE code_ LIKE #{searchParams.applicationPermissionCode}
                    )
                    or a.examine_company_id in (
                    SELECT id_ FROM base_company_info WHERE code_ LIKE #{searchParams.examinePermissionCode}
                    )
                    )
                </if>
                <if test="searchParams.examinePermissionCode == null">
                    and a.application_company_id in (
                    SELECT id_ FROM base_company_info WHERE code_ LIKE #{searchParams.applicationPermissionCode}
                    )
                </if>
            </if>
            <if test="searchParams.applicationPermissionCode == null">
                <if test="searchParams.examinePermissionCode != null">
                    and a.examine_company_id in (
                    SELECT id_ FROM base_company_info WHERE code_ LIKE #{searchParams.examinePermissionCode}
                    )
                </if>
            </if>
        </where>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>

    <select id="findByApplicationCompanyAndRole" resultMap="ApplicationExamineMap">
        <![CDATA[
		select * from base_application_examine
		where del_flag = 0
		and application_company_id = #{applicationCompanyId}
		and find_in_set(#{applicationRoleId},application_role_id)
		]]>


    </select>

    <select id="findByApplicationCompanyAndRoleAndType" resultMap="ApplicationExamineMap">
        <![CDATA[
		select * from base_application_examine
		where del_flag = 0
		and application_company_id = #{companyId}
		and find_in_set(#{personRoleId},application_role_id)
		and type_ = #{type}
		]]>
    </select>

    <select id="findByApplicationCompanyAndRoleAndTypeAndDays" resultMap="ApplicationExamineMap">
		select * from base_application_examine
		where del_flag = 0
		and application_company_id = #{companyId}
        and find_in_set(#{personRoleId},application_role_id)
		and type_ = #{type}
        <if test="level != null">
		and level_ = #{level}
        </if>
        <if test="days != null">
            <![CDATA[
                and days_ < #{days}
            ]]>
        </if>
        order by days_ desc
    </select>
</mapper>