<?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"/>
        <result column="conclusion_time" property="conclusionTime"/>
    </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,conclusion_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>
        <if test="conclusionTime != null">and conclusion_time &gt;= conclusionTime</if>
    </sql>

    <update id="updateIsConclusionById">
        UPDATE
        <include refid="table"/>
        SET is_conclusion = 0, conclusion_time = now()
        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>
            <if test="null != oaProjectId">
                oa_project_id = #{oaProjectId},
            </if>
            <if test="null != projectName">
                project_name = #{projectName},
            </if>
            <if test="isModify !=null">
                modify_time = now()
            </if>
        </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.business_id,p.business_name,p.manager_id,d.dept_name,lu.manager_name,p.dept_id,p.modify_time,p.type,p.create_time,p.is_conclusion,
        GROUP_CONCAT(DISTINCT(u.name) ORDER BY CONVERT( u.name USING gbk )) 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_dept d on p.dept_id=d.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="deptAndManager >0">
                <if test="null !=type">
                    AND p.type = #{type}
                </if>
                <if test="null !=isConclusion">
                    AND p.is_conclusion = #{isConclusion}
                </if>
                <if test="deptIds != null and deptIds.size>0">
                    AND p.dept_id IN
                    <foreach collection="deptIds" item="deptId" separator="," open="(" close=")">
                        #{deptId}
                    </foreach>
                </if>
                <if test="null != key and key!=''">
                    AND (
                    p.project_name like concat('%',#{key},'%') OR
                    lu.manager_name like concat('%',#{key},'%')
                    )
                </if>
            </if>

            <if test="null != userId">
                OR (p.manager_id = #{userId}
                <if test="null !=type">
                    AND p.type = #{type}
                </if>
                <if test="null !=isConclusion">
                    AND p.is_conclusion = #{isConclusion}
                </if>
                <if test="deptIds != null and deptIds.size>0">
                    AND p.dept_id IN
                    <foreach collection="deptIds" item="deptId" separator="," open="(" close=")">
                        #{deptId}
                    </foreach>
                </if>
                <if test="null != key and key!=''">
                    AND (
                    p.project_name like concat('%',#{key},'%') OR
                    lu.manager_name like concat('%',#{key},'%')
                    )
                </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>
            <if test="isConclusion != null">
                AND is_conclusion = #{isConclusion}
            </if>
            <if test="deptId != null and managerId == null">
                AND dept_id = #{deptId}
            </if>
            <if test="deptId != null and managerId != null">
                AND (dept_id = #{deptId} OR manager_id = #{managerId})
            </if>
            <if test="managerId != null and deptId == null">
                AND manager_id = #{managerId}
            </if>

            <if test="year != null">
                AND YEAR (create_time) &lt;= #{year} AND( year(conclusion_time) >= #{year} or is_conclusion = 1)
            </if>
        </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>

    <select id="getLastDate" resultType="java.lang.String">
    SELECT
	CONCAT(YEAR (IF (is_conclusion = 0,max(work_day),now())),
		'年',
		MONTH (IF (is_conclusion = 0,max(work_day),now())),
			'月'
		)
	FROM
		work_project p
	JOIN work_time_order t ON p.id = t.project_id
	WHERE
		p.id = #{projectId}
    </select>

    <resultMap id="orderChangeType" type="cn.wisenergy.model.dto.OrderChangeTypeDto">
        <id column="type" property="type"/>
        <result column="type_name" property="typeName"/>
        <collection property="orderChangeProjectDtos" ofType="cn.wisenergy.model.dto.OrderChangeProjectDto">
            <id column="project_id" property="projectId"/>
            <result column="project_name" property="projectName"/>
            <collection property="deptUserDtos" resultMap = "deptUserDto"/>
        </collection>
    </resultMap>
    <resultMap id="deptUserDto" type="cn.wisenergy.model.dto.DeptUserDto">
        <id column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <collection property="userDtos" ofType="cn.wisenergy.model.dto.UserDto">
        <id column="user_id" property="id"/>
        <result column="user_name" property="name"/>
        </collection>
    </resultMap>

    <select id="getOrderChangeTypeDtos" resultMap="orderChangeType">
        select p.type,t.name 'type_name',p.id 'project_id',p.project_name,up.user_id,u.name 'user_name',u.dept_id,d.dept_name,d1.id,d1.dept_name
        from work_project p LEFT JOIN work_user_project up on p.id = up.project_id
        LEFT JOIN work_user u ON up.user_id = u.id
        LEFT JOIN (select id,dept_name,sort FROM work_dept)d1 ON d1.id = p.dept_id
        LEFT JOIN work_dept d ON d.id = u.dept_id
        LEFT JOIN work_type t ON p.type = t.id
<where>
    p.is_conclusion = 1
    <if test="userId !=null">
     AND   p.manager_id = #{userId}
    </if>
</where>
       ORDER BY t.sort,d1.sort,p.id
    </select>

    <select id="getListByOaIds" resultType="cn.wisenergy.model.app.WorkProject">
      select <include refid="Base_Column_List"/>
      from <include refid="table"/>
      where oa_project_id in
      <foreach collection="oaProjectIds" item="oaProjectId" open="(" close=")" separator=",">
          #{oaProjectId}
      </foreach>

    </select>

    <select id="getListByDeptAndType" resultType="cn.wisenergy.model.app.WorkProject">
        select <include refid="Base_Column_List"/>
        from <include refid="table"/>
        where dept_id = #{deptId} AND type = #{typeId} AND (oa_project_id like CONCAT("%" ,#{projectNameOrNo},"%") OR project_name like CONCAT("%" ,#{projectNameOrNo},"%"))
    </select>

    <select id="getByProjectNameOrOaId" resultType="cn.wisenergy.model.app.WorkProject">
        select <include refid="Base_Column_List"/>
        from <include refid="table"/>
        where project_name = #{projectName} OR oa_project_id = #{oaId}
    </select>


</mapper>
