<?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="com.ruoyi.project.benyi.mapper.ByAssessmentcontentMapper">

    <resultMap type="ByAssessmentcontent" id="ByAssessmentcontentResult">
        <result property="id" column="id"/>
        <result property="parentId" column="parentId"/>
        <result property="name" column="name"/>
        <result property="iselement" column="iselement"/>
        <result property="scope" column="scope"/>
        <result property="createTime" column="create_time"/>
        <result property="sort" column="sort"/>
        <result property="ckbz" column="ckbz"/>
        <result property="jyjy"    column="jyjy"    />

        <result property="childid"    column="childid"    />
    </resultMap>

    <sql id="selectByAssessmentcontentVo">
        select id, parentId, name, iselement, scope, create_time, sort, ckbz, jyjy from by_assessmentcontent
    </sql>

    <select id="selectByAssessmentcontentList" parameterType="ByAssessmentcontent"
            resultMap="ByAssessmentcontentResult">
        <include refid="selectByAssessmentcontentVo"/>
        <where>
            <if test="parentId != null ">and parentId = #{parentId}</if>
            <!--<if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>-->
            <if test="name != null  and name != ''">and name  = #{name}</if>
            <if test="iselement != null  and iselement != ''">and iselement = #{iselement}</if>
            <if test="scope != null  and scope != ''">and scope = #{scope}</if>
        </where>
        order by sort
    </select>

    <select id="selectByAssessmentcontentstudyList" parameterType="ByAssessmentcontent"
            resultMap="ByAssessmentcontentResult">
select * from (
select -1 as id, -2 as parentId, title as name, 'N' as iselement, 0 as sort  from by_assessmentintroduce
union all
select dict_value,-1,dict_label,'N', 0 from sys_dict_data where dict_type='sys_dm_pgyssyfw'
union all
select id,parentId,name,iselement,sort from by_assessmentcontent where scope=1 and parentId=1 and iselement='N'
union all
select id,parentId,name,iselement,sort from by_assessmentcontent where scope=2 and parentId=2 and iselement='N'
union all
select id,parentId,name,iselement,sort from by_assessmentcontent where scope=3 and parentId=3 and iselement='N'
) t
order by t.sort
    </select>

    <select id="selectByAssessmentcontentById" parameterType="Long" resultMap="ByAssessmentcontentResult">
        <include refid="selectByAssessmentcontentVo"/>
        where id = #{id}
    </select>

    <select id="hasChildByAssessmentcontentId" parameterType="Long" resultType="int">
		select count(1) from by_assessmentcontent
		where parentId = #{id}
	</select>

    <select id="selectCountElement" parameterType="ByAssessmentcontent" resultType="int">
		select count(*) from by_assessmentcontent where find_in_set(id, getChildLst(#{id})) and scope=#{scope} and iselement='Y';
	</select>

    <!--sort 暂且代替childid-->
    <select id="selectCountElementByChild" parameterType="ByAssessmentcontent" resultType="int">
		select count(*) from (
select a.* from by_assessmentcontent a
right join by_assessmentchild b on a.id=b.contentid where b.childid=#{sort} and b.scope=#{scope}) temp
where find_in_set(id, getChildLst(#{id})) ;
	</select>

    <!--根据节点id获取领域目标-->
    <select id="selectNodeByid" parameterType="Long" resultMap="ByAssessmentcontentResult">
		select id, parentId, name, iselement, scope, create_time, sort, ckbz  from by_assessmentcontent
where find_in_set(id, getChildLst(#{id})) and parentId>#{id} and iselement='N' order by id;
	</select>

    <!--根据节点id获取领域目标-->
    <select id="selectChildNodeByParentid" parameterType="Long" resultMap="ByAssessmentcontentResult">
		select id, parentId, name, iselement, scope, create_time, sort, ckbz  from by_assessmentcontent
where find_in_set(id, getChildLst(#{id})) and id!=#{id} order by id;
	</select>

    <!--根据幼儿id获取未评选的指标-->
    <select id="selectNoByAssessmentcontentListByChild" parameterType="Long" resultMap="ByAssessmentcontentResult">
		SELECT id, parentId, name, iselement, scope, create_time, sort, ckbz, jyjy  FROM by_assessmentcontent
where id not in(select contentid from by_assessmentchild where childid=#{childid})
order by id;
	</select>

    <insert id="insertByAssessmentcontent" parameterType="ByAssessmentcontent" useGeneratedKeys="true" keyProperty="id">
        insert into by_assessmentcontent
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null ">parentId,</if>
            <if test="name != null  and name != ''">name,</if>
            <if test="iselement != null  and iselement != ''">iselement,</if>
            <if test="scope != null  and scope != ''">scope,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="sort != null ">sort,</if>
            <if test="ckbz != null  and ckbz != ''">ckbz,</if>
            <if test="jyjy != null  and jyjy != ''">jyjy,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null ">#{parentId},</if>
            <if test="name != null  and name != ''">#{name},</if>
            <if test="iselement != null  and iselement != ''">#{iselement},</if>
            <if test="scope != null  and scope != ''">#{scope},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="sort != null ">#{sort},</if>
            <if test="ckbz != null  and ckbz != ''">#{ckbz},</if>
            <if test="jyjy != null  and jyjy != ''">#{jyjy},</if>
        </trim>
    </insert>

    <update id="updateByAssessmentcontent" parameterType="ByAssessmentcontent">
        update by_assessmentcontent
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null ">parentId = #{parentId},</if>
            <if test="name != null  and name != ''">name = #{name},</if>
            <if test="iselement != null  and iselement != ''">iselement = #{iselement},</if>
            <if test="scope != null  and scope != ''">scope = #{scope},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="sort != null ">sort = #{sort},</if>
            <if test="ckbz != null  and ckbz != ''">ckbz = #{ckbz},</if>
            <if test="jyjy != null  and jyjy != ''">jyjy = #{jyjy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteByAssessmentcontentById" parameterType="Long">
        delete from by_assessmentcontent where id = #{id}
    </delete>

    <delete id="deleteByAssessmentcontentByIds" parameterType="String">
        delete from by_assessmentcontent where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>