<?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="cn.wisenergy.mapper.WorkProjectMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.wisenergy.model.app.WorkProject">
        <id column="id" property="id"/>
        <result column="oa_project_id" property="oaProjectId"/>
        <result column="project_name" property="projectName"/>
        <result column="type" property="type"/>
        <result column="manager_id" property="managerId"/>
        <result column="dept_id" property="deptId"/>
        <result column="work_time" property="workTime"/>
        <result column="cost_budget" property="costBudget"/>
        <result column="is_conclusion" property="isConclusion"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="create_time" property="createTime"/>
        <result column="modify_time" property="modifyTime"/>
        <result column="business_id" property="businessId"/>
        <result column="business_name" property="businessName"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, oa_project_id, project_name,business_id,business_name, type, manager_id, dept_id, work_time, cost_budget, is_conclusion,start_time,end_time, create_time, modify_time
    </sql>

    <sql id="table">
        work_project
    </sql>
    <!--查询条件-->
    <sql id="criteria">
        <if test="id != null">and id = #{id}</if>
        <if test="oaProjectId != null">and oa_project_id = #{oaProjectId}</if>
        <if test="projectName != null">and project_name = #{projectName}</if>
        <if test="businessId !=null"> and business_id = #{businessId}</if>
         <if test="businessName !=null"> and business_name = #{businessName}</if>
        <if test="type != null">and `type` =#{type}</if>
        <if test="managerId != null">and manager_id = #{managerId}</if>
        <if test="deptId != null">and dept_id = #{deptId}</if>
        <if test="workTime != null">and work_time =#{workTime}</if>
        <if test="costBudget != null">and cost_budget = #{costBudget}</if>
        <if test="isConclusion != null">and is_conclusion =#{isConclusion}</if>
        <if test="startTime != null">and start_time =#{startTime}</if>
        <if test="endTime != null">and end_time =#{endTime}</if>
        <if test="createTime != null">and create_time &gt;= #{createTime}</if>
        <if test="modifyTime != null">and modify_time &gt;= modifyTime</if>
    </sql>

    <update id="updateIsConclusionById">
        UPDATE
        <include refid="table"/>
        SET is_conclusion = 0
        WHERE id = #{id}
    </update>
    <update id="updateProject">
        UPDATE
        <include refid="table"/>
        <set>
            <if test="null != startTime">
                start_time = #{startTime},
            </if>
            <if test="null != endTime">
                end_time = #{endTime},
            </if>
            <if test="null != workTime">
                work_time = #{workTime},
            </if>
            <if test="null != costBudget">
                cost_budget =#{costBudget},
            </if>
            <if test="null != managerId">
                manager_id = #{managerId},
            </if>
            <if test="null != deptId">
                dept_id = #{deptId},
            </if>
            <if test="null != businessId">
                business_id = #{businessId},
            </if>
            <if test="null != businessName">
                business_name = #{businessName},
            </if>
            modify_time = now()
        </set>
        <where>
            <if test="null != projectId">
                id = #{projectId}
            </if>

        </where>


    </update>

    <select id="getProjectsByCriteria" resultMap="BaseResultMap" parameterType="map">
        select
        <include refid="Base_Column_List"/>
        from
        <include refid="table"/>
        <where>
            <if test="isConclusion != null">
                is_conclusion = #{isConclusion}
            </if>
            <if test="managerId != null">
                AND manager_id = #{managerId}
            </if>
            <if test="deptId != null">
                OR dept_id = #{deptId}
            </if>
            <if test="deptIds != null">
                OR dept_id IN
                <foreach collection="deptIds" item="deptId" separator="," open="(" close=")">
                    #{deptId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getProjectsByIds" resultType="cn.wisenergy.model.app.WorkProject">
        select
        <include refid="Base_Column_List"/>
        from
        <include refid="table"/>
        <where>

            <if test="ids != null">
                AND id IN
                <foreach collection="ids" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </if>
            <if test="null !=isConclusion">
                AND is_conclusion = #{isConclusion}
            </if>
        </where>
    </select>
    <select id="getManagerProjectsDto" resultType="cn.wisenergy.model.dto.ManagerProjectsDto">
        select
        p.id,p.oa_project_id,p.project_name,p.manager_id,lu.manager_name,p.dept_id,p.modify_time,p.type,p.create_time,p.is_conclusion,
        GROUP_CONCAT(DISTINCT(u.name)) as 'participants',p.cost_budget,p.work_time,p.start_time,p.end_time,
        if(cost is NULL,0,0 + cast(cost as char)) as 'currentLaborCost',if(total_time is NULL,0,0 + cast(total_time as
        char)) as 'totalCurrentWorkingHours',
        if(p.end_time>=curdate(),'否','是') as 'isItOverdue',
        if(p.create_time = p.modify_time,'否','是') as 'isThereABudgetChange'
        from work_project p LEFT JOIN (select project_id,SUM(total_time) as 'total_time',SUM(cost)/10000 as 'cost'
        FROM work_project_time_cost
        GROUP BY project_id) o on p.id = o.project_id
        LEFT JOIN (select id,`name` as 'manager_name'from work_user) lu on p.manager_id=lu.id
        LEFT JOIN work_user_project up ON p.id = up.project_id
        LEFT JOIN work_user u on u.id = up.user_id
        <where>

            <if test="null != userId">
                AND p.manager_id = #{userId}
            </if>
            <if test="null !=type">
                AND p.type = #{type}
            </if>
            <if test="null !=isConclusion">
                AND p.is_conclusion = #{isConclusion}
            </if>

            <if test="deptIds != null">
                OR (p.dept_id IN
                <foreach collection="deptIds" item="deptId" separator="," open="(" close=")">
                    #{deptId}
                </foreach>
                <if test="null !=isConclusion">
                    AND p.is_conclusion = #{isConclusion}
                </if>
                <if test="null !=type">
                    AND p.type = #{type}
                </if>
                )
            </if>

        </where>

        GROUP BY p.id
        ORDER BY p.is_conclusion DESC, p.create_time DESC

    </select>


    <select id="getListByCriteria" resultType="cn.wisenergy.model.app.WorkProject">
        select
        <include refid="Base_Column_List"/>
        from
        <include refid="table"/>
        <where>
            <include refid="criteria"/>
        </where>
    </select>

    <select id="getProjectIdByManager" resultType="java.lang.Integer">
        select id
        from
        <include refid="table"/>
        where manager_id = #{userId}
    </select>

    <select id="getListByCentreIdAndIsConclusion" resultType="cn.wisenergy.model.app.WorkProject">
        select
        <include refid="Base_Column_List"/>
        from work_project p join work_dept d on p.dept_id = d.id
        where centre_id = #{centreId}
        <if test="isConclusion != null">
            AND is_conclusion = #{isConclusion}
        </if>
    </select>


</mapper>