<?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.xinelu.manage.mapper.labelfieldcontent.LabelFieldContentMapper">

    <resultMap type="LabelFieldContent" id="LabelFieldContentResult">
        <result property="id" column="id"/>
        <result property="fieldId" column="field_id"/>
        <result property="fieldName" column="field_name"/>
        <result property="fieldCode" column="field_code"/>
        <result property="fieldValue" column="field_value"/>
        <result property="portraitSn" column="portrait_sn"/>
        <result property="patientId" column="patient_id"/>
        <result property="portraitStatus" column="portrait_status"/>
        <result property="contentSort" column="content_sort"/>
        <result property="contentRemark" column="content_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"/>
    </resultMap>

    <resultMap type="com.xinelu.manage.vo.labelfieldcontent.LabelFieldAndPartitionDict" id="LabelFieldAndPartitionDictResult">
        <result property="taskPartitionDictId" column="task_partition_dict_id"/>
        <result property="taskPartitionDictName" column="task_partition_dict_name"/>
        <collection property="portraitSnVOList" javaType="java.util.List"
                    resultMap="portraitSnVOResult"/>
    </resultMap>

    <resultMap type="com.xinelu.manage.vo.labelfieldcontent.PortraitSnVO" id="portraitSnVOResult">
        <result property="portraitSn" column="portrait_sn"/>
        <collection property="groupingValues" javaType="java.util.List"
                    resultMap="GroupingValueResult"/>
    </resultMap>

    <resultMap type="com.xinelu.manage.vo.labelfieldcontent.GroupingValue" id="GroupingValueResult">
        <result property="labelFieldInfoId" column="labelFieldInfoId"/>
        <result property="fieldName" column="field_name"/>
        <result property="fieldCode" column="field_code"/>
        <result property="fieldType" column="field_type"/>
        <result property="fieldSort" column="field_sort"/>
        <result property="fieldRemark" column="field_remark"/>
        <result property="taskPartitionDictId" column="task_partition_dict_id"/>
        <result property="taskPartitionDictName" column="task_partition_dict_name"/>
        <result property="taskTypeId" column="task_type_id"/>
        <result property="taskTypeName" column="task_type_name"/>
        <result property="labelFieldContentId" column="labelFieldContentId"/>
        <result property="fieldId" column="field_id"/>
        <result property="fieldName" column="field_name"/>
        <result property="fieldCode" column="field_code"/>
        <result property="fieldValue" column="field_value"/>
        <result property="portraitSn" column="portrait_sn"/>
        <result property="patientId" column="patient_id"/>
        <result property="portraitStatus" column="portrait_status"/>
        <result property="contentSort" column="content_sort"/>
        <result property="contentRemark" column="content_remark"/>
    </resultMap>

    <sql id="selectLabelFieldContentVo">
        select id,
               field_id,
               field_name,
               field_code,
               field_value,
               portrait_sn,
               patient_id,
               portrait_status,
               content_sort,
               content_remark,
               create_by,
               create_time,
               update_by,
               update_time
        from label_field_content
    </sql>

    <select id="selectLabelFieldContentList" parameterType="LabelFieldContent" resultMap="LabelFieldContentResult">
        <include refid="selectLabelFieldContentVo"/>
        <where>
            <if test="fieldId != null ">
                and field_id = #{fieldId}
            </if>
            <if test="fieldName != null  and fieldName != ''">
                and field_name like concat('%', #{fieldName}, '%')
            </if>
            <if test="fieldCode != null  and fieldCode != ''">
                and field_code = #{fieldCode}
            </if>
            <if test="fieldValue != null  and fieldValue != ''">
                and field_value = #{fieldValue}
            </if>
            <if test="portraitSn != null  and portraitSn != ''">
                and portrait_sn = #{portraitSn}
            </if>
            <if test="patientId != null ">
                and patient_id = #{patientId}
            </if>
            <if test="portraitStatus != null  and portraitStatus != ''">
                and portrait_status = #{portraitStatus}
            </if>
            <if test="contentSort != null ">
                and content_sort = #{contentSort}
            </if>
            <if test="contentRemark != null  and contentRemark != ''">
                and content_remark = #{contentRemark}
            </if>
        </where>
        order by content_sort, create_time DESC
    </select>

    <select id="selectLabelFieldContentById" parameterType="Long"
            resultMap="LabelFieldContentResult">
        <include refid="selectLabelFieldContentVo"/>
        where id = #{id}
    </select>

    <select id="existCountByFieldIdAndContentName" resultType="java.lang.Integer">
        select count(1)
        from label_field_content
        where field_id = #{fieldId}
          and content_name = #{contentName}
    </select>

    <select id="existCountByContentNameExcludingId" resultType="java.lang.Integer">
        select count(1)
        from label_field_content
        where content_name = #{contentName}
          and field_id = #{fieldId}
          and id != #{id}
    </select>

    <insert id="insertLabelFieldContent" parameterType="LabelFieldContent" useGeneratedKeys="true"
            keyProperty="id">
        insert into label_field_content
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fieldId != null">field_id,
            </if>
            <if test="fieldName != null">field_name,
            </if>
            <if test="fieldCode != null">field_code,
            </if>
            <if test="fieldValue != null">field_value,
            </if>
            <if test="portraitSn != null">portrait_sn,
            </if>
            <if test="patientId != null">patient_id,
            </if>
            <if test="portraitStatus != null">portrait_status,
            </if>
            <if test="contentSort != null">content_sort,
            </if>
            <if test="contentRemark != null">content_remark,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fieldId != null">#{fieldId},
            </if>
            <if test="fieldName != null">#{fieldName},
            </if>
            <if test="fieldCode != null">#{fieldCode},
            </if>
            <if test="fieldValue != null">#{fieldValue},
            </if>
            <if test="portraitSn != null">#{portraitSn},
            </if>
            <if test="patientId != null">#{patientId},
            </if>
            <if test="portraitStatus != null">#{portraitStatus},
            </if>
            <if test="contentSort != null">#{contentSort},
            </if>
            <if test="contentRemark != null">#{contentRemark},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateLabelFieldContent" parameterType="LabelFieldContent">
        update label_field_content
        <trim prefix="SET" suffixOverrides=",">
            <if test="fieldId != null">field_id =
                #{fieldId},
            </if>
            <if test="fieldName != null">field_name =
                #{fieldName},
            </if>
            <if test="fieldCode != null">field_code =
                #{fieldCode},
            </if>
            <if test="fieldValue != null">field_value =
                #{fieldValue},
            </if>
            <if test="portraitSn != null">portrait_sn =
                #{portraitSn},
            </if>
            <if test="patientId != null">patient_id =
                #{patientId},
            </if>
            <if test="portraitStatus != null">portrait_status =
                #{portraitStatus},
            </if>
            <if test="contentSort != null">content_sort =
                #{contentSort},
            </if>
            <if test="contentRemark != null">content_remark =
                #{contentRemark},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLabelFieldContentById" parameterType="Long">
        delete
        from label_field_content
        where id = #{id}
    </delete>

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

    <select id="selectLabelFieldContent"
            resultType="com.xinelu.manage.vo.labelfieldcontent.LabelFieldContentVO">
        select id LabelFieldContentId,
        field_id,
        field_name,
        content_sort,
        content_remark
        from label_field_content
        <where>
            <if test="labelFieldIds != null and labelFieldIds.size() > 0">
                and field_id in
                <foreach item="labelFieldIds" collection="labelFieldIds" open="(" separator="," close=")">
                    #{labelFieldIds}
                </foreach>
            </if>
            <if test="patientId != null">
                and patient_id = #{patientId}
            </if>
        </where>
    </select>

    <select id="groupingValue" resultType="com.xinelu.manage.vo.labelfieldcontent.GroupingValue">
        select
        lfi.id labelFieldInfoId,
        lfi.field_name,
        lfi.field_code,
        lfi.field_type,
        lfi.field_sort,
        lfi.field_remark,
        lfi.task_partition_dict_id,
        lfi.task_partition_dict_name,
        lfi.task_type_id,
        lfi.task_type_name,
        lfc.id labelFieldContentId,
        lfc.field_id,
        lfc.patient_id,
        lfc.field_name,
        lfc.field_code,
        lfc.field_value,
        lfc.portrait_sn,
        lfc.portrait_status,
        lfc.content_sort
        from label_field_info lfi
        LEFT JOIN label_field_content lfc ON lfi.id =lfc.field_id
        where 1=1
        <if test="taskPartitionDictId != null and taskPartitionDictId != 0">
            and lfi.task_partition_dict_id = #{taskPartitionDictId}
        </if>
        <if test="taskPartitionDictId == null">
            and lfi.task_partition_dict_id is null
        </if>
        <if test="patientId != null">
            and lfc.patient_id = #{patientId}
        </if>
        <if test="portraitStatus != null">
            and lfc.portrait_status = #{portraitStatus}
        </if>
        Order by lfc.content_sort
    </select>

	<select id="selectByPatientId" resultType="com.xinelu.manage.vo.labelfieldcontent.LabelFieldInfoContentVo">
		select
		content.field_name,
		content.field_code,
		info.task_partition_dict_id,
		info.task_type_id,
		tpd.apply_range,
		content.field_value,
		content.portrait_sn
		from label_field_content content
		left join label_field_info info on content.field_id = info.id
		left join task_partition_dict tpd on info.task_partition_dict_id = tpd.id
		where content.patient_id = #{patientId} and content.portrait_status='INUSE'
	</select>

        <select id="labelFieldAndPartitionDict" resultType="com.xinelu.manage.vo.labelfieldcontent.GroupingValue" >
            select
            lfi.id labelFieldInfoId,
            lfi.field_name,
            lfi.field_code,
            lfi.field_type,
            lfi.field_sort,
            lfi.field_remark,
            lfi.task_partition_dict_id,
            lfi.task_partition_dict_name,
            lfi.task_type_id,
            lfi.task_type_name,
            lfc.id labelFieldContentId,
            lfc.field_id,
            lfc.patient_id,
            lfc.field_name,
            lfc.field_code,
            lfc.field_value,
            lfc.portrait_sn,
            lfc.portrait_status,
            lfc.content_sort
            from label_field_info lfi
            LEFT JOIN label_field_content lfc ON lfi.id =lfc.field_id
            LEFT JOIN task_partition_dict tpd on tpd.id = lfi.task_partition_dict_id
            LEFT JOIN task_type_dict ttd on ttd.id = tpd.task_type_id
            where 1=1
            <if test="taskPartitionDictId != null and taskPartitionDictId != 0">
                and lfi.task_partition_dict_id = #{taskPartitionDictId}
            </if>
            <if test="taskPartitionDictId == null">
                and lfi.task_partition_dict_id is null
            </if>
            <if test="patientId != null">
                and lfc.patient_id = #{patientId}
            </if>
            <if test="portraitStatus != null">
                and lfc.portrait_status = #{portraitStatus}
            </if>
            Order by ttd.task_type_sort, tpd.task_partition_sort,lfc.portrait_sn,lfi.field_sort
        </select>

    <insert id="insertLabelFieldContentList">
        insert into label_field_content(
        field_id,
        field_name,
        field_code,
        field_value,
        portrait_sn,
        patient_id,
        portrait_status,
        content_sort,
        content_remark,
        create_by,
        create_time
        )
        values
        <foreach item="GroupingValue" index="index" collection="list" separator=",">
            (
            #{GroupingValue.fieldId},
            #{GroupingValue.fieldName},
            #{GroupingValue.fieldCode},
            #{GroupingValue.fieldValue},
            #{GroupingValue.portraitSn},
            #{GroupingValue.patientId},
            #{GroupingValue.portraitStatus},
            #{GroupingValue.contentSort},
            #{GroupingValue.contentRemark},
            #{GroupingValue.createBy},
            #{GroupingValue.createTime}
            )
        </foreach>
    </insert>

    <update id="updateContentRemarkLabelFieldContentList">
        <foreach collection="list" item="labelFieldContentList" index="index" open="" close="" separator=";">
            update label_field_content
            <trim prefix="SET" suffixOverrides=",">
                <if test="labelFieldContentList.fieldId != null">field_id =
                    #{labelFieldContentList.fieldId},
                </if>
                <if test="labelFieldContentList.fieldName != null">field_name =
                    #{labelFieldContentList.fieldName},
                </if>
                <if test="labelFieldContentList.fieldCode != null">field_code =
                    #{labelFieldContentList.fieldCode},
                </if>
                <if test="labelFieldContentList.fieldValue != null">field_value =
                    #{labelFieldContentList.fieldValue},
                </if>
                <if test="labelFieldContentList.portraitSn != null">portrait_sn =
                    #{labelFieldContentList.portraitSn},
                </if>
                <if test="labelFieldContentList.patientId != null">patient_id =
                    #{labelFieldContentList.patientId},
                </if>
                <if test="labelFieldContentList.portraitStatus != null">portrait_status =
                    #{labelFieldContentList.portraitStatus},
                </if>
                <if test="labelFieldContentList.updateBy != null">update_by =
                    #{labelFieldContentList.updateBy},
                </if>
                <if test="labelFieldContentList.updateTime != null">update_time =
                    #{labelFieldContentList.updateTime},
                </if>
            </trim>
            where id = #{labelFieldContentList.labelFieldContentId}
        </foreach>
    </update>

    <select id="selectLabelFieldContentPortraitSn"
            resultType="com.xinelu.manage.domain.labelfieldcontent.LabelFieldContent">
        select
        lfi.task_partition_dict_id fieldId,
        lfc.portrait_sn
        from label_field_content lfc
        LEFT JOIN label_field_info lfi ON lfc.field_id = lfi.id
        <where>
            <if test="taskPartitionDictIds != null and taskPartitionDictIds.size() > 0">
                lfi.task_partition_dict_id in
                <foreach item="taskPartitionDictIds" collection="taskPartitionDictIds" open="(" separator="," close=")">
                    #{taskPartitionDictIds}
                </foreach>
            </if>
            <if test="patientId != null">
                AND patient_id = #{patientId}
            </if>
        </where>
    </select>
</mapper>