<?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"/>
        <result property="postName" column="post_name"/>
        <result property="unitPrice" column="unit_price"/>
        <result property="postCost" column="post_cost"/>
        <result property="leader" column="leader"/>
    </resultMap>
    <resultMap type="com.ruoyi.common.core.domain.entity.SysUser" id="SysUserResult">
        <id property="userId" column="user_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="loginName" column="login_name"/>
        <result property="userName" column="user_name"/>
        <result property="userType" column="user_type"/>
        <result property="email" column="email"/>
        <result property="phonenumber" column="phonenumber"/>
        <result property="sex" column="sex"/>
        <result property="avatar" column="avatar"/>
        <result property="password" column="password"/>
        <result property="salt" column="salt"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="loginIp" column="login_ip"/>
        <result property="loginDate" column="login_date"/>
        <result property="pwdUpdateDate" column="pwd_update_date"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="mgtUserId" column="mgt_user_id"/>
        <result property="deptLeader" column="dept_leader"/>
        <result property="userLeader" column="user_leader"/>
        <result property="postName" column="post_name"/>
        <association property="dept" column="dept_id" javaType="com.ruoyi.common.core.domain.entity.SysDept"
                     resultMap="deptResult"/>
        <collection property="roles" javaType="java.util.List" resultMap="RoleResult"/>
    </resultMap>

    <resultMap id="deptResult" type="com.ruoyi.common.core.domain.entity.SysDept">
        <id property="deptId" column="dept_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="orderNum" column="order_num"/>
        <result property="leader" column="leader"/>
        <result property="status" column="dept_status"/>
    </resultMap>

    <resultMap id="RoleResult" type="com.ruoyi.common.core.domain.entity.SysRole">
        <id property="roleId" column="role_id"/>
        <result property="roleName" column="role_name"/>
        <result property="roleKey" column="role_key"/>
        <result property="roleSort" column="role_sort"/>
        <result property="dataScope" column="data_scope"/>
        <result property="status" column="role_status"/>
    </resultMap>

    <resultMap id="ProjectUserCostResult" type="com.ruoyi.system.domain.ProjectUserCost">
        <result property="projectId" column="project_id"/>
        <result property="userId" column="user_id"/>
        <result property="costDay" column="cost_day"/>
        <result property="postCost" column="post_cost"/>
    </resultMap>
    <resultMap id="ProjectResult" type="com.ruoyi.system.domain.TblProject">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
    </resultMap>
    <resultMap id="SysUserNameResult" type="com.ruoyi.common.core.domain.entity.SysUser">
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
    </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 ( c.user = #{params.loginUser}
                or c.create_by in(select login_name from sys_user where user_id = #{params.loginUser})
                <if test="params.pmo == 1">
                    or 1 = 1
                </if>
                )
            </if>
        </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 tmp.*,
        post.post_name,
        post.post_cost as unit_price,
        pp.leader,
        tmp.cost_day*post.post_cost/21.75/8 as post_cost from
        (
        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.user in (select user_id from sys_user su where su.mgt_user_id =#{params.userId}
            or su.dept_id in (select dept_id from sys_dept dept where dept.leader_id = #{params.userId}))
            <if test="params.pmo == 1">
                or 1 = 1
            </if>
            )
        </where>
        group by c.project_id,c.project_name,c.user,c.user_name
        ) tmp
        left join sys_user_post up on tmp.user = up.user_id
        left join sys_post post on post.post_id = up.post_id
        left join tbl_project pp on pp.id = tmp.project_id
        order by tmp.user,tmp.project_id,tmp.cost_day desc
    </select>

    <select id="selectTblProjectCostSumProject" parameterType="TblProjectCost" resultMap="TblProjectCostResult">
        select tmp.*,
        post.post_name,
        post.post_cost as unit_price,
        pp.leader,
        tmp.cost_day*post.post_cost/21.75/8 as post_cost from
        (
        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 ( p.leader_id = #{params.userId}
            <if test="params.pmo == 1">
                or 1 = 1
            </if>
            )
        </where>
        group by c.project_id,c.project_name,c.user,c.user_name
        ) tmp
        left join sys_user_post up on tmp.user = up.user_id
        left join sys_post post on post.post_id = up.post_id
        left join tbl_project pp on pp.id = tmp.project_id
        order by tmp.project_id,tmp.user,tmp.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>
    <select id="selectUserByProjectLeader" resultMap="SysUserResult">
        select u.user_id,
        u.dept_id,
        u.login_name,
        u.user_name,
        u.user_type,
        u.email,
        u.avatar,
        u.phonenumber,
        u.password,
        u.sex,
        u.salt,
        u.status,
        u.del_flag,
        u.login_ip,
        u.login_date,
        u.create_by,
        u.create_time,
        u.remark,
        d.dept_name,
        d.leader
        from sys_user u
        left join sys_dept d on u.dept_id = d.dept_id
        left join tbl_user_updatetime ut on u.user_id = ut.user_id
        where u.del_flag = '0'
        AND u.status = '0'
        <if test="userId == null">
            and 1 = 0
        </if>
        <if test="userId != -1">
            and (u.user_id in (select user_id from tbl_user_project where project_id in (select id from tbl_project
            where
            leader_id = #{userId} and del_flag = '0'))
            or u.user_id = #{userId})
        </if>
    </select>
    <select id="selectProjectUserCostByDate" parameterType="TblProjectCost" resultMap="ProjectUserCostResult">
        select
        tmp.project_id as project_id,
        tmp.user as user_id,
        tmp.cost_day as cost_day,
        tmp.cost_day * post.post_cost / 21.75 / 8 as post_cost
        from (select cost.project_id, cost.user, sum(cost.cost_day) cost_day
        from tbl_project_cost cost
        <where>
            <if test="startDate != null ">and cost.end_date &gt;= #{startDate}</if>
            <if test="endDate != null ">and cost.start_date &lt;= #{endDate}</if>
        </where>
        group by cost.project_id, cost.user) tmp
        left join sys_user_post up on tmp.user = up.user_id
        left join sys_post post on post.post_id = up.post_id
    </select>
    <select id="selectAllProjectName" resultMap="ProjectResult">
        select id,
               name
        from tbl_project
    </select>
    <select id="selectAllSysUser" resultMap="SysUserNameResult">
        select user_id,
               user_name
        from sys_user
    </select>
</mapper>