<?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.ruoyi.system.mapper.TblProjectMapper">

    <resultMap type="TblProject" id="TblProjectResult">
        <result property="id" column="id"/>
        <result property="autoId" column="auto_id"/>
        <result property="name" column="name"/>
        <result property="remark" column="remark"/>
        <result property="leaderId" column="leader_id"/>
        <result property="leader" column="leader"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="contractId" column="contract_id"/>
        <result property="createBy" column="create_by"/>
        <result property="contractName" column="contract_name"/>
        <result property="createTime" column="create_time"/>
        <result property="buyers" column="buyers"/>
        <result property="sellers" column="sellers"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
    </resultMap>

    <sql id="selectTblProjectVo">
        select id,
               auto_id,
               name,
               remark,
               leader_id,
               leader,
               status,
               del_flag,
               contract_id,
               create_by,
               contract_name,
               create_time,
               buyers,
               sellers,
               update_by,
               update_time,
               start_date,
               end_date
        from tbl_project
    </sql>

    <select id="selectTblProjectList" parameterType="TblProject" resultMap="TblProjectResult">
        <include refid="selectTblProjectVo"/>
        <where>
            del_flag = '0'
            <if test="projectNoQuery != null and projectNoQuery != ''">and auto_id like
                concat('%',#{projectNoQuery},'%')
            </if>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="leaderId != null">and leader_id = #{leaderId}</if>
        </where>
        ${params.order}
    </select>

    <select id="selectTblProjectById" parameterType="String" resultMap="TblProjectResult">
        <include refid="selectTblProjectVo"/>
        where id = #{id}
    </select>

    <insert id="insertTblProject" parameterType="TblProject">
        insert into tbl_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            id,
            <if test="name != null and name != ''">name,</if>
            <if test="remark != null">remark,</if>
            <if test="leaderId != null">leader_id,</if>
            <if test="leader != null">leader,</if>
            <if test="status != null">status,</if>
            del_flag,
            <if test="contractId != null">contract_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="contractName != null">contract_name,</if>
            <if test="createTime != null">create_time,</if>
            <if test="buyers != null">buyers,</if>
            <if test="sellers != null">sellers,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            (select REPLACE(UUID(),'-','')),
            <if test="name != null and name != ''">#{name},</if>
            <if test="remark != null">#{remark},</if>
            <if test="leaderId != null">#{leaderId},</if>
            <if test="leader != null">#{leader},</if>
            <if test="status != null">#{status},</if>
            0,
            <if test="contractId != null">#{contractId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="contractName != null">#{contractName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="buyers != null">#{buyers},</if>
            <if test="sellers != null">#{sellers},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</if>
        </trim>
    </insert>

    <update id="updateTblProject" parameterType="TblProject">
        update tbl_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="leaderId != null">leader_id = #{leaderId},</if>
            <if test="leader != null">leader = #{leader},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="contractId != null">contract_id = #{contractId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="contractName != null">contract_name = #{contractName},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="buyers != null">buyers = #{buyers},</if>
            <if test="sellers != null">sellers = #{sellers},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTblProjectById" parameterType="String">
        update tbl_project
        set del_flag = '2'
        where id = #{id}
    </delete>

    <delete id="deleteTblProjectByIds" parameterType="String">
        update tbl_project set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectMyTblProjectList" parameterType="Long" resultMap="TblProjectResult">
        <include refid="selectTblProjectVo"/>,tbl_user_project
        <where>
            del_flag = '0'
            and (id=project_id or project_id='all')
            and user_id=#{userId}
        </where>
        order by CONVERT(name using gbk)
    </select>

    <select id="selectPMOTblProjectList" parameterType="UserIDQuery" resultMap="TblProjectResult">
        select project.id,
               project.name,
               project.remark,
               project.leader_id,
               project.leader,
               project.status,
               project.del_flag,
               project.contract_id,
               project.create_by,
               project.contract_name,
               project.create_time,
               project.buyers,
               project.sellers,
               project.update_by,
               project.update_time,
               project.start_date,
               project.end_date
        from tbl_project project
                 left join sys_user u on project.create_by = u.user_id
                 left join sys_dept d on u.dept_id = d.dept_id
        where project.del_flag = '0'
          and (project.leader_id = #{userId} or (1 = 1 ${params.dataScope}))
        order by CONVERT(name using gbk)
    </select>

    <select id="selectTblProjectListByUserProject" parameterType="Long" resultMap="TblProjectResult">
        <include refid="selectTblProjectVo"/>
        <where>
            id in (select project_id from tbl_user_project where user_id = #{userId})
            and del_flag = '0'
            and status = '1'
            <if test="leaderId != null and leaderId != -1">
                and leader_id = #{leaderId}
            </if>
        </where>
    </select>

</mapper>