<?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.salpa.model.mapper.ModelMapper">

    <resultMap type="com.salpa.model.domain.ProjectModelInfo" id="ProjectModelInfoResult">
        <id     property="projectId"            column="project_id"             />
        <result property="projectName"          column="project_name"           />
        <result property="projectDescription"   column="project_description"    />
        <result property="jsonFile"             column="json_file"              />
        <result property="deleted"              column="deleted"                />
        <result property="createAt"             column="create_at"              />
    </resultMap>

    <select id="getProjectIds" resultType="string">
        SELECT project_ids projectIds FROM sys_user_role WHERE user_id = #{userId}
    </select>

    <select id="getList" resultMap="ProjectModelInfoResult">
        SELECT p.project_id,p.project_name,p.project_description,p.json_file,p.deleted,p.create_at
        FROM project_model_info p WHERE p.deleted = 0
        <if test="list != null and list.size>0">
            AND p.project_id in
            <foreach collection="list" item="projectIds" open="(" separator="," close=")">
                #{projectIds}
            </foreach>
        </if>
        <if test="projectName != null and projectName != ''">
            AND project_name like concat('%', #{projectName}, '%')
        </if>
    </select>

    <select id="getById" resultMap="ProjectModelInfoResult">
        SELECT project_id,project_name,project_description,json_file,deleted,create_at FROM project_model_info WHERE project_id = #{projectId}
    </select>

    <select id="checkProjectNameUnique" parameterType="String" resultType="int">
        SELECT count(1) from project_model_info where project_name = #{projectName} and deleted = 0 limit 1
    </select>

    <select id="getUserList" resultType="map">
        SELECT user_id userId,user_name userName FROM sys_user WHERE del_flag = 0 AND user_name != 'admin'
    </select>

    <insert id="addProject" parameterType="ProjectModelInfo" useGeneratedKeys="true" keyProperty="projectId">
        insert into project_model_info(
        <if test="projectName != null and projectName != ''">project_name,</if>
        <if test="projectDescription != null and projectDescription != ''">project_description,</if>
        <if test="jsonFile != null and jsonFile != ''">json_file</if>
        )values(
        <if test="projectName != null and projectName != ''">#{projectName},</if>
        <if test="projectDescription != null and projectDescription != ''">#{projectDescription},</if>
        <if test="jsonFile != null and jsonFile != ''">#{jsonFile}</if>
        )
    </insert>

    <update id="updateProjectIds">
        UPDATE sys_user_role SET project_ids = #{projectIds} WHERE user_id = #{userId}
    </update>

    <insert id="addProjectIds">
        INSERT INTO sys_user_role(user_id,project_ids) values (#{userId},#{projectIds})
    </insert>

    <update id="updateProject">
        UPDATE project_model_info SET project_name = #{projectName},project_description = #{projectDescription},json_file = #{jsonFile}
        WHERE project_id = #{projectId}
    </update>

    <update id="deleteProject">
        UPDATE project_model_info SET deleted = 1 WHERE project_id = #{projectId}
    </update>

    <update id="updateJsonFile">
        UPDATE project_model_info SET json_file = #{jsonFile} WHERE project_Name = #{projectName}
    </update>
</mapper>