<?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.ximai.mes.md.mapper.MdSapFieldMapper">

    <resultMap id="mdSapFieldMap" type="MdSapField">
        <id column="id" property="id"/>
        <result column="type" property="type"/>
        <result column="data_type" property="dataType"/>
        <result column="name" property="name"/>
        <result column="field" property="field"/>
        <result column="sap_field" property="sapField"/>
        <result column="default_value" property="defaultValue"/>
        <result column="required" property="required"/>
        <result column="show" property="show"/>
        <result column="err_msg" property="errMsg"/>
        <result column="length" property="length"/>
        <result column="reg_ex" property="regEx"/>
        <result column="min" property="min"/>
        <result column="max" property="max"/>
        <result column="dict_id" property="dictId"/>
        <result column="parent_id" property="parentId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
    </resultMap>
    <resultMap id="mdSapField" extends="mdSapFieldMap" type="MdSapField">
        <collection property="subList" ofType="MdSapField">
            <result column="sub_id" property="id"/>
            <result column="sub_name" property="name"/>
        </collection>
    </resultMap>

    <select id="listByMap" resultMap="mdSapField">
        select t1.*, t2.id sub_id, t2.name sub_name
        from md_sap_field t1
        left join md_sap_field t2 on t1.id = t2.parent_id
        <where>
            <if test="id != null">and t1.id = #{id}</if>
            <if test="type != null">and t1.type = #{type}</if>
            <if test="pId != null">and t1.parent_id in (${pId}, 0)</if>
            <if test="parentId != null">and t1.parent_id = #{parentId}</if>
            <if test="name != null and name != ''">and t1.name = #{name}</if>
        </where>
        <if test="size != null and offset != null">
            limit ${offset}, ${size}
        </if>
    </select>

    <select id="countByMap" resultType="java.lang.Integer">
        select count(*)
        from md_sap_field t1
        <where>
            <if test="id != null">and t1.id = #{id}</if>
            <if test="pId != null">and t1.parent_id in (${pId}, 0)</if>
            <if test="parentId != null">and t1.parent_id = #{parentId}</if>
            <if test="name != null and name != ''">and t1.name = #{name}</if>
        </where>
    </select>

    <resultMap id="SapFieldVo" type="com.ximai.mes.md.vo.SapFieldVo">
        <id column="id" property="id"/>
        <result column="type" property="type"/>
        <result column="data_type" property="dataType"/>
        <result column="name" property="name"/>
        <result column="field" property="field"/>
        <result column="sap_field" property="sapField"/>
        <result column="required" property="required"/>
        <result column="show" property="show"/>
        <result column="err_msg" property="errMsg"/>
        <result column="length" property="length"/>
        <result column="reg_ex" property="regEx"/>
        <result column="min" property="min"/>
        <result column="max" property="max"/>
        <result column="dict_id" property="dictId"/>
        <collection property="dictList" ofType="MdSapDictData">
            <result column="data_id" property="id"/>
            <result column="value" property="value"/>
            <result column="dict_name" property="name"/>
        </collection>
        <collection property="children" column="id" ofType="com.ximai.mes.md.vo.SapFieldVo" select="listByParentId"/>
    </resultMap>

    <select id="listByParentId" resultMap="SapFieldVo">
        SELECT t1.id, t1.data_type, t1.`name`, t1.field, t1.sap_field, t1.default_value, t1.required, t1.show, t1.err_msg, t1.length, t1.reg_ex, t1.min, t1.max, t1.dict_id, t1.parent_id,
            t2.id data_id, t2.`name` dict_name, t2.`value`
        FROM md_sap_field t1
        LEFT JOIN md_sap_dict_data t2 ON t2.dict_id = t1.dict_id
        where t1.parent_id = #{id}
    </select>

    <select id="listVoByParentId" resultMap="SapFieldVo">
        SELECT t1.id, t1.data_type, t1.`name`, t1.field, t1.sap_field, t1.required, t1.show, t1.err_msg, t1.length, t1.reg_ex, t1.min, t1.max, t1.dict_id, t1.parent_id,
            t2.id data_id, t2.`name` dict_name, t2.`value`
        FROM md_sap_field t1
        LEFT JOIN md_sap_dict_data t2 ON t2.dict_id = t1.dict_id
        <where>
            <if test="parentId != null">and t1.parent_id = #{parentId}</if>
            <if test="type != null">and t1.type = #{type}</if>
        </where>
    </select>

    <update id="updateParentIdByIds">
        update md_sap_field set parent_id = #{id}, update_time = #{updateTime}
        where id in
        <foreach collection="subList" item="item" open="(" close=")" separator=",">#{item.id}</foreach>
    </update>
</mapper>
