<?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.cms.project.mapper.CmsProjectMapper">

    <resultMap type="CmsProject" id="CmsProjectResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="description" column="description"/>
        <result property="userId" column="user_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="identifier" column="identifier"/>
        <result property="username" column="username"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <association property="dept" column="dept_id" javaType="com.microservices.system.api.domain.SysDept"
                     resultMap="deptResult"/>
    </resultMap>

    <resultMap id="deptResult" type="com.microservices.system.api.domain.SysDept">
        <id property="deptId" column="dept_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="ancestors" column="ancestors"/>
        <result property="orderNum" column="order_num"/>
        <result property="leader" column="leader"/>
        <result property="status" column="dept_status"/>
    </resultMap>

    <sql id="selectCmsProjectVo"> select id,
                                         name,
                                         description,
                                         user_id,
                                         dept_id,
                                         identifier,
                                         username,
                                         create_by,
                                         create_time
                                  from cms_project </sql>
    <sql id="selectCmsProjectAndDeptVo"> select p.id,
                                                p.name,
                                                p.description,
                                                p.user_id,
                                                p.dept_id,
                                                p.identifier,
                                                p.username,
                                                p.create_by,
                                                p.create_time,
                                                d.dept_id,
                                                d.parent_id,
                                                d.ancestors,
                                                d.dept_name,
                                                d.order_num,
                                                d.leader,
                                                d.status as dept_status
                                         from cms_project p
                                                  left join
                                              sys_dept d on p.dept_id = d.dept_id </sql>

    <select id="selectCmsProjectList" parameterType="CmsProject" resultMap="CmsProjectResult">
        <include refid="selectCmsProjectAndDeptVo"/>
        <where>
            <if test="name != null  and name != ''">and d.dept_name like concat('%',
                #{name}, '%')
            </if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
    </select>

    <select id="selectCmsProjectById" parameterType="Long" resultMap="CmsProjectResult">
        <include refid="selectCmsProjectVo"/>
        where id = #{id}
    </select>
    <select id="selectCmsProjectByDeptId" resultMap="CmsProjectResult">
        <include refid="selectCmsProjectVo"/>
        where dept_id = #{deptId}
    </select>
    <select id="selectCmsProjectByIdentifier" resultMap="CmsProjectResult">
        <include refid="selectCmsProjectVo"/>
        where identifier = #{identifier}
    </select>

    <insert id="insertCmsProject" parameterType="CmsProject" useGeneratedKeys="true" keyProperty="id">
        insert into cms_project
        <trim
                prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null">name,</if>
            <if
                    test="description != null">description,
            </if>
            <if test="userId != null">user_id,</if>
            <if
                    test="deptId != null">dept_id,
            </if>
            <if test="identifier != null">identifier,</if>
            <if
                    test="username != null">username,
            </if>
            <if test="createBy != null and createBy != ''">
                create_by,
            </if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null">#{name},</if>
            <if
                    test="description != null">#{description},
            </if>
            <if test="userId != null">#{userId},</if>
            <if
                    test="deptId != null">#{deptId},
            </if>
            <if test="identifier != null">#{identifier},</if>
            <if
                    test="username != null">#{username},
            </if>
            <if
                    test="createBy != null and createBy != ''">#{createBy},
            </if>
            sysdate()
        </trim>
    </insert>

    <update id="updateCmsProject" parameterType="CmsProject">
        update cms_project
        <trim prefix="SET"
              suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="description != null">description =
                #{description},
            </if>
            <if test="userId != null">user_id = #{userId},</if>
            <if
                    test="deptId != null">dept_id = #{deptId},
            </if>
            <if test="identifier != null">identifier
                = #{identifier},
            </if>
            <if test="username != null">username = #{username},</if>
            <if
                    test="updateBy != null and updateBy != ''">update_by = #{updateBy},
            </if>
            update_time
            = sysdate()
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCmsProjectById" parameterType="Long"> delete
                                                            from cms_project
                                                            where id = #{id} </delete>

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