<?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.CompanyInfoDAO">
    <resultMap id="CompanyInfoMap" type="com.jpsoft.shinestar.modules.base.entity.CompanyInfo">
        <id property="id" column="id_"/>
        <result property="level" column="level_"/>
        <result property="sortNo" column="sort_no"/>
        <result property="name" column="name_"/>
        <result property="fullName" column="full_name"/>
        <result property="logo" column="logo_"/>
        <result property="remark" column="remark_"/>
        <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="delFlag" column="del_flag"/>
        <result property="parentId" column="parent_id"/>
        <result property="code" column="code_"/>
        <result property="parentName" column="parent_name"/>
        <result property="type" column="type_"/>
        <result property="wallUrl" column="wall_url" />
        <result property="sceneId" column="scene_id" />
        <result property="regionId" column="region_id"/>
        <result property="sceneName" column="scene_name" />
        <result property="regionName" column="region_name" />
        <result property="relationCompanyId" column="relation_company_id" />
        <result property="typeName" column="type_name" />
        <result property="enableRegister" column="enable_register" />
        <result property="unitType" column="unit_type" />
        <result property="hasNext" column="has_next"/>
        <result property="isMobilePayment" column="is_mobile_payment"/>
        <result property="healthyReportNoticeEnable" column="healthy_report_notice_enable" />
        <result property="healthyReportNoticeTime" column="healthy_report_notice_time" />
        <result property="healthyReportSubmitTime" column="healthy_report_submit_time" />
        <result property="isOutsourcer" column="is_outsourcer" />
        <result property="classify" column="classify_" />
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.CompanyInfo">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into base_company_info
	    (id_,level_,sort_no,name_,full_name,logo_,remark_,create_by,create_time,
	    update_by,update_time,del_flag,parent_id,code_,type_,
	    wall_url,scene_id,region_id,relation_company_id,enable_register,unit_type,is_mobile_payment,
	    healthy_report_notice_enable,healthy_report_notice_time,healthy_report_submit_time,is_outsourcer,
	    classify_)
		values
		(
			#{id,jdbcType=VARCHAR}
			,#{level,jdbcType= NUMERIC }
			,#{sortNo,jdbcType= NUMERIC }
			,#{name,jdbcType=VARCHAR}
			,#{fullName,jdbcType=VARCHAR}
			,#{logo,jdbcType=VARCHAR}
			,#{remark,jdbcType=VARCHAR}
			,#{createBy,jdbcType=VARCHAR}
			,#{createTime,jdbcType= TIMESTAMP }
			,#{updateBy,jdbcType=VARCHAR}
			,#{updateTime,jdbcType= TIMESTAMP }
			,#{delFlag,jdbcType= NUMERIC }
			,#{parentId,jdbcType=VARCHAR}
			,#{code,jdbcType=VARCHAR}
			,#{type,jdbcType=VARCHAR}
            ,#{wallUrl,jdbcType=VARCHAR}
            ,#{sceneId,jdbcType=VARCHAR}
            ,#{regionId,jdbcType=VARCHAR}
            ,#{relationCompanyId,jdbcType=VARCHAR}
            ,#{enableRegister,jdbcType= NUMERIC }
            ,#{unitType,jdbcType=VARCHAR}
            ,#{isMobilePayment,jdbcType= NUMERIC }
			,#{healthyReportNoticeEnable,jdbcType= NUMERIC}
			,#{healthyReportNoticeTime,jdbcType= VARCHAR }
			,#{healthyReportSubmitTime,jdbcType= VARCHAR }
			,#{isOutsourcer,jdbcType= NUMERIC }
			,#{classify,jdbcType= VARCHAR }
		)
	]]>
    </insert>
    <delete id="delete" parameterType="string">
        delete from base_company_info where id_=#{id,jdbcType=VARCHAR}
    </delete>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.CompanyInfo">
        update base_company_info
        <set>
            <if test="level!=null">
                level_=#{level,jdbcType= NUMERIC },
            </if>
            <if test="sortNo!=null">
                sort_no=#{sortNo,jdbcType= NUMERIC },
            </if>
            <if test="name!=null">
                name_=#{name,jdbcType=VARCHAR},
            </if>
            <if test="name!=null">
                full_name=#{fullName,jdbcType=VARCHAR},
            </if>
            <if test="logo!=null">
                logo_=#{logo,jdbcType=VARCHAR},
            </if>
            <if test="remark!=null">
                remark_=#{remark,jdbcType=VARCHAR},
            </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="delFlag!=null">
                del_flag=#{delFlag,jdbcType= NUMERIC },
            </if>
            <if test="parentId!=null">
                parent_id=#{parentId,jdbcType=VARCHAR},
            </if>
            <if test="code!=null">
                code_=#{code,jdbcType=VARCHAR},
            </if>
            <if test="type!=null">
                type_=#{type,jdbcType=VARCHAR},
            </if>
            <if test="wallUrl!=null">
                wall_url=#{wallUrl,jdbcType= VARCHAR },
            </if>
            <if test="sceneId!=null">
                scene_id=#{sceneId,jdbcType= VARCHAR },
            </if>
            <if test="regionId!=null">
                region_id=#{regionId,jdbcType= VARCHAR },
            </if>
            <if test="relationCompanyId!=null">
                relation_company_id=#{relationCompanyId,jdbcType= VARCHAR },
            </if>
            <if test="enableRegister!=null">
                enable_register=#{enableRegister,jdbcType= NUMERIC },
            </if>
            <if test="unitType!=null">
                unit_type=#{unitType,jdbcType= VARCHAR },
            </if>
            <if test="isMobilePayment!=null">
                is_mobile_payment=#{isMobilePayment,jdbcType= NUMERIC },
            </if>
            <if test="healthyReportNoticeEnable!=null">
                healthy_report_notice_enable=#{healthyReportNoticeEnable,jdbcType= VARCHAR },
            </if>
            <if test="healthyReportNoticeTime!=null">
                healthy_report_notice_time=#{healthyReportNoticeTime,jdbcType= VARCHAR },
            </if>
            <if test="healthyReportSubmitTime!=null">
                healthy_report_submit_time=#{healthyReportSubmitTime,jdbcType= VARCHAR },
            </if>
            <if test="isOutsourcer!=null">
                is_outsourcer=#{isOutsourcer,jdbcType= NUMERIC },
            </if>
            <if test="classify!=null">
                classify_=#{classify,jdbcType= VARCHAR },
            </if>
        </set>
        where id_=#{id}
    </update>
    <update id="updateCode">
        update base_company_info
        set code_ = concat(#{newCode},substr(code_,#{startIndex}))
        where code_ like #{oldCode}
    </update>
    <select id="get" parameterType="string" resultMap="CompanyInfoMap">
        select a.*,b.name_ as parent_name
        from base_company_info a
        left join base_company_info b on a.parent_id = b.id_
        where a.id_=#{0}
    </select>
    <select id="exist" parameterType="string" resultType="int">
        select count(*) from base_company_info where id_=#{0} and del_flag = 0
    </select>
    <select id="list" resultMap="CompanyInfoMap">
        select * from base_company_info where del_flag=0 order by level_ asc,sort_no asc
    </select>
    <select id="search" parameterType="hashmap" resultMap="CompanyInfoMap">
        <![CDATA[
            select
            a.*,
            b.name_ as parent_name,
            c.scene_name as scene_name,
            d.name_ as region_name,
            e.scene_name as type_name
            from base_company_info a
            left join base_company_info b on a.parent_id = b.id_
            left join base_scene_info c on a.scene_id = c.id_
            left join base_region_info d on a.region_id = d.id_
            left join base_scene_info e on a.type_ = e.id_
		]]>
        <where>
            a.del_flag = 0
            <if test="searchParams.name != null">
                and a.name_ like #{searchParams.name}
            </if>
            <if test="searchParams.parentCode != null">
                and b.code_ like #{searchParams.parentCode}
            </if>
            <trim prefix="and (" prefixOverrides="AND|OR" suffix=")">
                <if test="searchParams.bindCompanyCode != null">
                    and a.code_ like #{searchParams.bindCompanyCode}
                </if>
                <if test="searchParams.relateCompanyList!=null">
                    <foreach collection="searchParams.relateCompanyList" item="item" open="or a.id_ in(" separator="," close=")">
                        #{item.id}
                    </foreach>
                </if>
            </trim>
            <if test="searchParams.sceneId != null">
                and a.scene_id = #{searchParams.sceneId}
            </if>
            <if test="searchParams.type != null">
                and a.type_ = #{searchParams.type}
            </if>
            <if test="searchParams.unitType != null">
                and a.unit_type = #{searchParams.unitType}
            </if>
            <if test="searchParams.enableRegister != null">
                and a.enable_register = #{searchParams.enableRegister}
            </if>
        </where>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            a.${sort.name} ${sort.order}
        </foreach>
    </select>
    <select id="findByCompanyCode" resultMap="CompanyInfoMap">
        select a.*,b.name_ as parent_name
        from base_company_info a
        left join base_company_info b on a.parent_id = b.id_
        where a.del_flag=0
        <trim prefix="and (" prefixOverrides="AND|OR" suffix=")">
            <if test="code!=null">
                a.code_ like #{code}
            </if>
            <if test="code==null">
                a.code_='-1'
            </if>
            <if test="personId!=null">
                or a.id_ in (
                    select c.company_id from base_person_company c where c.person_id = #{personId}
                )
            </if>
        </trim>
        order by a.level_ asc,a.sort_no asc,a.name_ asc
    </select>
    <select id="countByParentId" resultType="long">
        select count(*) from base_company_info
        <where>
            del_flag=0
            <if test="parentId!=null">
                and parent_id=#{parentId,jdbcType=VARCHAR}
            </if>
            <if test="parentId==null">
                and (parent_id is null or parent_id='')
            </if>
        </where>
    </select>
    <select id="countByCompanyCode" resultType="long">
        select count(*)
        from base_company_info
        where del_flag=0
        and (
            code_ like #{code}
        <if test="personId!=null">
            or id_ in (
            select c.company_id from base_person_company c where c.person_id = #{personId}
            )
        </if>
        )
    </select>
    <select id="findByParentId" resultMap="CompanyInfoMap">
        select a.*,b.name_ as parent_name
        from base_company_info a
        left join base_company_info b on a.parent_id = b.id_
        where a.del_flag=0
        <if test="parentId!=null">
            and a.parent_id=#{parentId,jdbcType=VARCHAR}
        </if>
        <if test="parentId==null">
            and (a.parent_id is null or a.parent_id='')
        </if>
        order by a.level_ asc,a.sort_no asc,a.name_ asc
    </select>
    <select id="findByName" parameterType="string" resultMap="CompanyInfoMap">
        select a.*,b.name_ as parent_name
        from base_company_info a
        left join base_company_info b on a.parent_id = b.id_
        where (a.name_=#{0} or a.full_name=#{0})
        and a.del_flag = 0
        limit 1
    </select>

    <select id="findByCompanyCodeAndType" resultMap="CompanyInfoMap">
        select * from base_company_info
        where del_flag = 0
        and code_ like #{code}
        <if test="type!=null">
            and type_ = #{type}
        </if>
        order by level_ asc,sort_no asc
    </select>

    <select id="findByRegionIdAndSceneId" resultMap="CompanyInfoMap">
        <![CDATA[

        select * from base_company_info
        where region_id = #{regionId}
        and scene_id = #{sceneId}
        and del_flag = 0
        ]]>

    </select>

    <select id="findAllCompanyByCode" resultMap="CompanyInfoMap">
        <![CDATA[
            select * from base_company_info
            where del_flag = 0
            and code_ like #{code}
        ]]>
    </select>

    <select id="findBySceneIdAndType" resultMap="CompanyInfoMap">
        <![CDATA[
        select * from base_company_info
        where del_flag = 0
        and scene_id = #{sceneId}
        and type_ = #{type}
        ]]>
    </select>

    <select id="findByRelationCompanyId" resultMap="CompanyInfoMap">
        <![CDATA[
        select * from base_company_info
        where del_flag = 0
        and relation_company_id = #{relationCompanyId}
        ]]>
    </select>

    <select id="findByRegionIdAndSceneIdAndEnableRegister" resultMap="CompanyInfoMap">
        <![CDATA[
       select * from base_company_info
        where region_id = #{regionId}
        and scene_id = #{sceneId}
        and del_flag = 0
        and enable_register = #{enableRegister}
        ]]>
    </select>
    <select id="findByType" resultMap="CompanyInfoMap">
        <![CDATA[
        select * from base_company_info
        where del_flag = 0
        and type_ = #{type}
        ]]>
    </select>
    <select id="findByUnitType" resultMap="CompanyInfoMap">
        <![CDATA[
        select * from base_company_info
        where del_flag = 0
        and unit_type = #{unitType}
        ]]>
    </select>
    <select id="findByHealthyReportTime" resultMap="CompanyInfoMap">
        select * from base_company_info
        where del_flag=0
        and healthy_report_notice_time = #{healthyReportNoticeTime}
        and healthy_report_notice_enable = 1
        and unit_type = '1'
    </select>
    <select id="findByIdIgnoreDel" resultMap="CompanyInfoMap">
        <![CDATA[
        select * from base_company_info
        where id_ = #{companyId}
        ]]>
    </select>

    <select id="findByOutscourcer" resultMap="CompanyInfoMap">
        <![CDATA[
        select * from base_company_info
        where del_flag=0
        and is_outsourcer = #{isOutsourcer}
        order by level_ asc,sort_no asc
        ]]>
    </select>

    <select id="findByCompanyCodeAndOutscourcer" resultMap="CompanyInfoMap">
        select a.*,b.name_ as parent_name
        from base_company_info a
        left join base_company_info b on a.parent_id = b.id_
        where a.del_flag=0
        and a.is_outsourcer = #{isOutsourcer}
        <trim prefix="and (" prefixOverrides="AND|OR" suffix=")">
            <if test="code!=null">
                a.code_ like #{code}
            </if>
            <if test="code==null">
                a.code_='-1'
            </if>
            <if test="personId!=null">
                or a.id_ in (
                select c.company_id from base_person_company c where c.person_id = #{personId}
                )
            </if>
        </trim>
        order by a.level_ asc,a.sort_no asc,a.name_ asc
    </select>
    <select id="findByClassify" resultMap="CompanyInfoMap">
        <![CDATA[
            SELECT
             *
            FROM
                base_company_info
            WHERE
                del_flag = 0
                and find_in_set(#{classify},classify_)>0
            ORDER BY
            level_ ASC,
            sort_no ASC
        ]]>
    </select>
</mapper>