<?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.ProjectSettingsMapper">

    <resultMap type="ProjectSettings" id="ProjectSettingsResult">
        <result property="id"    column="id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="inHisTaskTypes"    column="in_his_task_types"    />
        <result property="outHisTaskTypes"    column="out_his_task_types"    />
        <result property="companyId"    column="company_id"    />
        <result property="createdAt"    column="created_at"    />
        <result property="updatedAt"    column="updated_at"    />
        <!--  联表字段 -->
        <result property="companyName"    column="company_name" />
        <result property="deptName"       column="dept_name" />
    </resultMap>

    <sql id="selectProjectSettingsVo">
        SELECT
        ps.id,
        ps.dept_id,
        ps.in_his_task_types,
        ps.out_his_task_types,
        ps.created_at,
        ps.updated_at,
        ps.company_id,

        c.company_name AS company_name,
        d.dept_name    AS dept_name
        FROM project_settings ps
        LEFT JOIN company c
        ON c.company_id = ps.company_id
        AND IFNULL(c.is_del, 0) = 0
        LEFT JOIN company_dept d
        ON d.dept_id = ps.dept_id
        AND d.del_flag = '0'
    </sql>

    <select id="selectProjectSettingsList" parameterType="ProjectSettings" resultMap="ProjectSettingsResult">
        <include refid="selectProjectSettingsVo"/>
        <where>
            <if test="deptId != null and deptId != ''">
                AND ps.dept_id = #{deptId}
            </if>
            <if test="companyId != null and companyId != ''">
                AND ps.company_id = #{companyId}
            </if>
            <if test="inHisTaskTypes != null and inHisTaskTypes != ''">
                AND ps.in_his_task_types = #{inHisTaskTypes}
            </if>
            <if test="outHisTaskTypes != null and outHisTaskTypes != ''">
                AND ps.out_his_task_types = #{outHisTaskTypes}
            </if>
            <if test="createdAt != null">
                AND ps.created_at = #{createdAt}
            </if>
            <if test="updatedAt != null">
                AND ps.updated_at = #{updatedAt}
            </if>
        </where>
        GROUP BY ps.id
    </select>

    <select id="selectProjectSettingsById"
            parameterType="long"
            resultMap="ProjectSettingsResult">
        <include refid="selectProjectSettingsVo"/>
        WHERE ps.id = #{id}
    </select>

    <insert id="insertProjectSettings" parameterType="ProjectSettings" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO project_settings
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null and deptId != ''">dept_id,</if>
            <if test="inHisTaskTypes != null">in_his_task_types,</if>
            <if test="outHisTaskTypes != null">out_his_task_types,</if>
            <if test="companyId != null">company_id,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="updatedAt != null">updated_at,</if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="deptId != null and deptId != ''">#{deptId},</if>
            <if test="inHisTaskTypes != null">#{inHisTaskTypes},</if>
            <if test="outHisTaskTypes != null">#{outHisTaskTypes},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="updatedAt != null">#{updatedAt},</if>
        </trim>
    </insert>

    <update id="updateProjectSettings" parameterType="ProjectSettings">
        UPDATE project_settings
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null and deptId != ''">dept_id = #{deptId},</if>
            <if test="companyId != null and companyId != ''">company_id = #{companyId},</if>
            <if test="inHisTaskTypes != null">in_his_task_types = #{inHisTaskTypes},</if>
            <if test="outHisTaskTypes != null">out_his_task_types = #{outHisTaskTypes},</if>
            <if test="createdAt != null">created_at = #{createdAt},</if>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
        </trim>
        WHERE id = #{id}
    </update>

    <delete id="deleteProjectSettingsById" parameterType="long">
        DELETE FROM project_settings WHERE id = #{id}
    </delete>

    <delete id="deleteProjectSettingsByIds" parameterType="String">
        DELETE FROM project_settings
        WHERE id IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="countByDeptId" parameterType="java.lang.String" resultType="int">
        SELECT COUNT(1)
        FROM project_settings ps
        WHERE ps.dept_id = #{deptId}
    </select>

    <select id="selectProjectSettingsByCompanyId" parameterType="java.lang.String" resultMap="ProjectSettingsResult">
        <include refid="selectProjectSettingsVo"/>
        WHERE ps.company_id = #{companyId}
        LIMIT 1
    </select>

</mapper>
