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

    <resultMap type="TblProjectCost" id="TblProjectCostResult">
        <result property="id" column="id"/>
        <result property="projectId" column="project_id"/>
        <result property="groupId" column="group_id"/>
        <result property="projectName" column="project_name"/>
        <result property="user" column="user"/>
        <result property="userName" column="user_name"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="costDay" column="cost_day"/>
        <result property="remark" column="remark"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>


    <sql id="selectTblProjectCostVo">
        select c.id,
               c.project_id,
               c.group_id,
               c.project_name,
               c.user,
               c.user_name,
               c.start_date,
               c.end_date,
               c.cost_day,
               c.remark,
               c.create_by,
               c.create_time,
               c.update_by,
               c.update_time
        from tbl_project_cost c
                 left join sys_user u on c.user = u.user_id
                 left join sys_dept d on u.dept_id = d.dept_id
    </sql>

    <select id="selectTblProjectCostList" parameterType="TblProjectCost" resultMap="TblProjectCostResult">
        <include refid="selectTblProjectCostVo"/>
        <where>

            <if test="projectId != null  and projectId != ''">and c.project_id = #{projectId}</if>
            <if test="user != null  and user != ''">and c.user = #{user}</if>
            <if test="startDate != null ">and c.end_date &gt;= #{startDate}</if>
            <if test="endDate != null ">and c.start_date &lt;= #{endDate}</if>
            <if test="params != null and params.loginUser != null">
                and (d.dept_id IN ( select dept_id FROM sys_dept where leader_id = #{params.loginUser})
                or u.user_id IN ( select user_id FROM sys_user where mgt_user_id = #{params.loginUser})
                or c.project_id IN ( select id FROM tbl_project where leader_id = #{params.loginUser}))
            </if>
            ${params.dataScope}
        </where>
        order by end_date desc,start_date desc,user,id
    </select>

    <select id="selectTblProjectCostById" parameterType="TblProjectCost" resultMap="TblProjectCostResult">
        <include refid="selectTblProjectCostVo"/>
        where c.id = #{id}
        ${params.dataScope}
    </select>

    <select id="selectTblProjectCostSumMember" parameterType="TblProjectCost" resultMap="TblProjectCostResult">
        select c.project_id,c.project_name,c.user,c.user_name,GREATEST(min(c.start_date),#{startDate}) as
        start_date,least(max(c.end_date),#{endDate}) as end_date,
        sum(c.cost_day*(datediff(least(c.end_date,#{endDate}),GREATEST(c.start_date,#{startDate}))+1)/(datediff(c.end_date,c.start_date)+1))
        as cost_day,GROUP_CONCAT(c.remark SEPARATOR '&lt;BR&gt;' ) as remark
        from tbl_project_cost c
        left join sys_user u on c.user=u.user_id
        left join sys_dept d on u.dept_id = d.dept_id
        left join tbl_project p on c.project_id = p.id
        <where>
            <if test="projectId != null  and projectId != ''">and c.project_id = #{projectId}</if>
            <if test="user != null  and user != ''">and c.user = #{user}</if>
            <if test="startDate != null ">and c.end_date &gt;= #{startDate}</if>
            <if test="endDate != null ">and c.start_date &lt;= #{endDate}</if>
            and (c.project_id in (select project_id from tbl_user_project up where up.user_id =#{params.userId})
            or p.leader_id = #{params.userId}
            )
        </where>
        group by c.project_id,c.project_name,c.user,c.user_name
        order by c.user,c.project_id,cost_day desc
    </select>

    <select id="selectTblProjectCostSumProject" parameterType="TblProjectCost" resultMap="TblProjectCostResult">
        select c.project_id,c.project_name,c.user,c.user_name,GREATEST(min(c.start_date),#{startDate}) as
        start_date,least(max(c.end_date),#{endDate}) as end_date,
        sum(c.cost_day*(datediff(least(c.end_date,#{endDate}),GREATEST(c.start_date,#{startDate}))+1)/(datediff(c.end_date,c.start_date)+1))
        as cost_day,GROUP_CONCAT(c.remark SEPARATOR '&lt;BR&gt;' ) as remark
        from tbl_project_cost c
        left join tbl_project p
        on c.project_id = p.id
        <where>
            <if test="projectId != null  and projectId != ''">and c.project_id = #{projectId}</if>
            <if test="user != null  and user != ''">and c.user = #{user}</if>
            <if test="startDate != null ">and c.end_date &gt;= #{startDate}</if>
            <if test="endDate != null ">and c.start_date &lt;= #{endDate}</if>
            and (c.project_id in (select project_id from tbl_user_project up where up.user_id =#{params.userId})
            or p.leader_id = #{params.userId}
            )
        </where>
        group by c.project_id,c.project_name,c.user,c.user_name
        order by c.project_id,c.user,cost_day desc
    </select>

    <select id="selectTblProjectCostLastData" parameterType="Long" resultMap="TblProjectCostResult">
        select GROUP_CONCAT(project_id) as project_id, end_date
        from tbl_project_cost
        where user = #{userId}
        GROUP BY end_date
        ORDER BY end_date desc LIMIT 0,1
    </select>

    <insert id="insertTblProjectCost" parameterType="TblProjectCost">
        insert into tbl_project_cost
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="groupId != null">group_id,</if>
            <if test="projectName != null and projectName != ''">project_name,</if>
            <if test="user != null">user,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="costDay != null">cost_day,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="groupId != null">#{groupId},</if>
            <if test="projectName != null and projectName != ''">#{projectName},</if>
            <if test="user != null">#{user},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="costDay != null">#{costDay},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <insert id="insertTblProjectCostList" parameterType="Arraylist">
        insert into
        tbl_project_cost(id,project_id,group_id,project_name,user,user_name,start_date,end_date,cost_day,remark,create_by,create_time)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.id},#{item.projectId},#{item.groupId},(select name from tbl_project where
            id=#{item.projectId}),#{item.user},#{item.userName},#{item.startDate},#{item.endDate},#{item.costDay},#{item.remark},#{item.createBy},#{item.createTime})
        </foreach>
    </insert>

    <update id="updateTblProjectCost" parameterType="TblProjectCost">
        update tbl_project_cost
        <trim prefix="SET" suffixOverrides=",">
            <if test="costDay != null">cost_day = #{costDay},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="projectId != null and projectId != ''">project_id = #{projectId},</if>
            <if test="projectName != null and projectName != ''">project_name = #{projectName},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateTblProjectCostName" parameterType="TblProject">
        update tbl_project_cost
        set project_name=#{name}
        where project_id = #{id}
    </update>

    <delete id="deleteTblProjectCostById" parameterType="String">
        delete
        from tbl_project_cost
        where id = #{id}
    </delete>

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

    <update id="updateUserUpdatetime" parameterType="Long">
        update tbl_user_updatetime
        set update_time= CURRENT_TIME()
        where user_id = #{user}
    </update>

    <insert id="insertUserUpdatetime" parameterType="Long">
        insert
        tbl_user_updatetime(user_id,update_time) values(
        #{user},
        CURRENT_TIME
        (
        )
        )
    </insert>

</mapper>