<?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.fs.project.mapper.ProjectTaskTypeMapper">

    <resultMap type="ProjectTaskType" id="ProjectTaskTypeResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="taskTypeIds"   column="task_type_ids"  />
        <result property="productIds"    column="product_ids"    />
        <result property="maxAmount"    column="max_amount"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="companyName" column="company_name"/>
        <result property="projectName" column="project_name"/>
        <result property="deptName" column="dept_name"/>

    </resultMap>

    <sql id="selectProjectTaskTypeVo">
        SELECT
        ptt.id,
        ptt.company_id,
        ptt.project_id,
        ptt.dept_id,
        ptt.task_type_ids,
        ptt.product_ids,
        ptt.max_amount,
        ptt.status,
        ptt.create_time,
        ptt.update_time,
        ptt.create_by,
        ptt.update_by,
        ptt.del_flag,

        c.company_name        AS company_name,
        p.project_name        AS project_name,
        cd.dept_name          AS dept_name

        FROM project_task_type ptt
        LEFT JOIN company c
        ON c.company_id = ptt.company_id
        AND c.is_del = 0

        LEFT JOIN project p
        ON p.id = ptt.project_id
        AND p.del_flag = 0

        LEFT JOIN company_dept cd
        ON cd.dept_id = ptt.dept_id
        AND cd.del_flag = '0'
    </sql>



    <select id="selectProjectTaskTypeList"
            parameterType="ProjectTaskType"
            resultMap="ProjectTaskTypeResult">
        <include refid="selectProjectTaskTypeVo"/>
        <where>
            <if test="companyId != null"> and ptt.company_id = #{companyId}</if>
            <if test="projectId != null"> and ptt.project_id = #{projectId}</if>
            <if test="deptId != null"> and ptt.dept_id = #{deptId}</if>
            <if test="taskTypeIds != null and taskTypeIds != ''">
                and ptt.task_type_ids = #{taskTypeIds}
            </if>
            <if test="productIds != null and productIds != ''">
                and ptt.product_ids = #{productIds}
            </if>
            <if test="maxAmount != null"> and ptt.max_amount = #{maxAmount}</if>
            <if test="status != null"> and ptt.status = #{status}</if>
        </where>

        GROUP BY ptt.id

    </select>


    <select id="selectProjectTaskTypeById"
            parameterType="Long"
            resultMap="ProjectTaskTypeResult">
        <include refid="selectProjectTaskTypeVo"/>
        where ptt.id = #{id}
    </select>


    <insert id="insertProjectTaskType" parameterType="ProjectTaskType" useGeneratedKeys="true" keyProperty="id">
        insert into project_task_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="taskTypeIds != null">task_type_ids,</if>
            <if test="productIds != null">product_ids,</if>
            <if test="maxAmount != null">max_amount,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="taskTypeIds != null">#{taskTypeIds},</if>
            <if test="productIds != null">#{productIds},</if>
            <if test="maxAmount != null">#{maxAmount},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateProjectTaskType" parameterType="ProjectTaskType">
        update project_task_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="taskTypeIds != null">task_type_ids = #{taskTypeIds},</if>
            <if test="productIds != null">product_ids = #{productIds},</if>
            <if test="maxAmount != null">max_amount = #{maxAmount},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteProjectTaskTypeById" parameterType="Long">
        delete from project_task_type where id = #{id}
    </delete>

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

    <select id="selectProjectOptionsByDept"  resultType="com.fs.project.vo.ProjectOptionVO">
        SELECT DISTINCT p.id AS projectId, p.project_name AS projectName
        FROM project_task_type ptt
        JOIN project p ON p.id = ptt.project_id
        WHERE ptt.del_flag = 0
        AND ptt.status = 1
        AND ptt.dept_id = #{deptId}

        <if test="projectName != null and projectName != ''">
            AND p.project_name LIKE CONCAT('%', #{projectName}, '%')
        </if>
        ORDER BY p.project_name
        LIMIT 50

    </select>
    <select id="queryAllData" resultType="com.fs.project.domain.ProjectTaskType">
        select * from project_task_type
        <where>
            <if test="companyId != null">AND company_id = #{companyId}</if>
            <if test="deptId != null">AND dept_id = #{deptId}</if>
            <if test="productId != null">
                AND FIND_IN_SET(#{productId}, product_ids) > 0
            </if>
        </where>
    </select>
</mapper>
