<?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.microservices.pms.enterprise.mapper.PmsEnterpriseMapper">

    <resultMap type="com.microservices.pms.enterprise.domain.PmsEnterprise" id="PmsEnterpriseResult">
        <result property="id"    column="id"    />
        <result property="enterpriseName"    column="enterprise_name"    />
        <result property="enterpriseLocation" column="enterprise_location"/>
        <result property="enterpriseDescription"    column="enterprise_description"    />
        <result property="enterpriseIdentifier"    column="enterprise_identifier"    />
        <result property="deptId"    column="dept_id"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="gitlinkOrgId"    column="gitlink_org_id"    />
        <result property="enterpriseImage"    column="enterprise_image"    />
    </resultMap>

    <sql id="selectPmsEnterpriseAliasVo">
        select pe.id,
               pe.enterprise_name,
               pe.enterprise_location,
               pe.enterprise_description,
               pe.enterprise_identifier,
               pe.dept_id,
               pe.update_time,
               pe.update_by,
               pe.create_time,
               pe.create_by,
               pe.del_flag,
               pe.gitlink_org_id,
               pe.enterprise_image
        from pms_enterprise pe
    </sql>

    <sql id="selectPmsEnterpriseVo">
        select id,
               enterprise_name,
               enterprise_location,
               enterprise_description,
               enterprise_identifier,
               dept_id,
               update_time,
               update_by,
               create_time,
               create_by,
               del_flag,
               gitlink_org_id,
               enterprise_image
        from pms_enterprise
    </sql>

    <select id="selectPmsEnterpriseList" parameterType="PmsEnterprise" resultMap="PmsEnterpriseResult">
        <include refid="selectPmsEnterpriseAliasVo"/>
        <where>
            <if test="pmsEnterprise.enterpriseName != null  and pmsEnterprise.enterpriseName != ''">and
                pe.enterprise_name like concat('%',
                #{pmsEnterprise.enterpriseName}, '%')
            </if>
            <if test="pmsEnterprise.enterpriseIdentifier != null  and pmsEnterprise.enterpriseIdentifier != ''">and
                pe.enterprise_identifier =
                #{pmsEnterprise.enterpriseIdentifier}
            </if>
            <if test="pmsEnterprise.deptId != null ">and pe.dept_id = #{pmsEnterprise.deptId}</if>
            <if test="pmsEnterprise.gitlinkOrgId != null  and pmsEnterprise.gitlinkOrgId != ''">and pe.gitlink_org_id =
                #{pmsEnterprise.gitlinkOrgId}
            </if>

            <if test="deptIdList != null ">and pe.dept_id in
                <foreach item="deptId" collection="deptIdList" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
        </where>
    </select>
    
    <select id="selectPmsEnterpriseById" parameterType="Long" resultMap="PmsEnterpriseResult">
        <include refid="selectPmsEnterpriseVo"/>
        where id = #{id}
    </select>
    <select id="selectPmsEnterpriseByIdentifier" resultMap="PmsEnterpriseResult">
        <include refid="selectPmsEnterpriseVo"/>
        where enterprise_identifier = #{identifier}
    </select>
    <select id="selectPmsEnterpriseByName" resultMap="PmsEnterpriseResult">
        <include refid="selectPmsEnterpriseVo"/>
        where enterprise_name = #{enterpriseName}
    </select>
    <select id="selectPmsEnterpriseByDeptId" resultMap="PmsEnterpriseResult">
        <include refid="selectPmsEnterpriseVo"/>
        where dept_id = #{deptId}
    </select>
    <select id="selectPmsEnterpriseByGitlinkOrgId" resultMap="PmsEnterpriseResult">
        <include refid="selectPmsEnterpriseVo"/>
        where gitlink_org_id=#{gitlinkOrgId}
    </select>

    <insert id="insertPmsEnterprise" parameterType="PmsEnterprise" useGeneratedKeys="true" keyProperty="id">
        insert into pms_enterprise
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="enterpriseName != null and enterpriseName != ''">enterprise_name,</if>
            <if test="enterpriseLocation != null and enterpriseLocation != ''">enterprise_location,</if>
            <if test="enterpriseDescription != null">enterprise_description,</if>
            <if test="enterpriseIdentifier != null and enterpriseIdentifier != ''">enterprise_identifier,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="gitlinkOrgId != null">gitlink_org_id,</if>
            <if test="enterpriseImage != null">enterprise_image,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="enterpriseName != null and enterpriseName != ''">#{enterpriseName},</if>
            <if test="enterpriseLocation != null and enterpriseLocation != ''">#{enterpriseLocation},</if>
            <if test="enterpriseDescription != null">#{enterpriseDescription},</if>
            <if test="enterpriseIdentifier != null and enterpriseIdentifier != ''">#{enterpriseIdentifier},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="gitlinkOrgId != null">#{gitlinkOrgId},</if>
            <if test="enterpriseImage != null">#{enterpriseImage},</if>
         </trim>
    </insert>

    <update id="updatePmsEnterprise" parameterType="PmsEnterprise">
        update pms_enterprise
        <trim prefix="SET" suffixOverrides=",">
            <if test="enterpriseName != null and enterpriseName != ''">enterprise_name = #{enterpriseName},</if>
            <if test="enterpriseLocation != null and enterpriseLocation != ''">enterprise_location =
                #{enterpriseLocation},
            </if>
            <if test="enterpriseDescription != null">enterprise_description = #{enterpriseDescription},</if>
            <if test="enterpriseIdentifier != null and enterpriseIdentifier != ''">enterprise_identifier = #{enterpriseIdentifier},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="gitlinkOrgId != null">gitlink_org_id = #{gitlinkOrgId},</if>
            <if test="enterpriseImage != null">enterprise_image = #{enterpriseImage},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePmsEnterpriseById" parameterType="Long">
        delete from pms_enterprise where id = #{id}
    </delete>

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