<?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.WorkProjectTimeCostMapper">
    <resultMap id="workProjectTimeCost" type="cn.wisenergy.model.app.WorkProjectTimeCost">
        <id column="id" property="id"/>
        <result property="projectId" column="project_id"/>
        <result property="statisticsStart" column="statistics_start"/>
        <result property="statisticsEnd" column="statistics_end"/>
        <result property="totalTime" column="total_time"/>
        <result property="cost" column="cost"/>
    </resultMap>

    <resultMap id="workTimeAndCostCollect" type="cn.wisenergy.model.dto.WorkTimeAndCostCollect">
        <result property="type" column="type"/>
        <collection property="deptProjectWorkTimeAndCosts" resultMap="deptProjectWorkTimeAndCost"/>
    </resultMap>

    <resultMap id="deptProjectWorkTimeAndCost" type="cn.wisenergy.model.dto.DeptProjectWorkTimeAndCost">
        <result property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
        <collection property="projectWorkTimeAndCostStatistics"
                    ofType="cn.wisenergy.model.dto.ProjectWorkTimeAndCostStatistics">
            <result property="projectId" column="project_id"/>
            <result property="projectName" column="project_name"/>
            <result property="statisticsTime" column="statistics_time"/>
            <result property="totalTime" column="total_time"/>
            <result property="cost" column="cost"/>
        </collection>
    </resultMap>

    <resultMap id="projectStatisticsByMonth" type="cn.wisenergy.model.dto.ProjectStatisticsByMonth">
        <result property="projectId" column="project_id"/>
        <result property="projectName" column="project_name"/>
        <result property="deptName" column="dept_name"/>
        <result property="userName" column="user_name"/>
        <collection property="statisticsDateAndWorkTimes" ofType="cn.wisenergy.model.dto.ProjectStatisticsByMonth$StatisticsDateAndWorkTime">
            <result property="date" column="date"/>
            <result property="year" column="year"/>
            <result property="totalTime" column="total_time"/>
            <result property="cost" column="cost"/>
        </collection>
    </resultMap>

    <delete id="deleteStatisticsByQuarter">
        delete
        from work_project_time_cost
        where statistics_start >= #{date}
    </delete>


    <insert id="statisticsTimeOrderByMonth" useGeneratedKeys="true">
        INSERT INTO work_project_time_cost (
        project_id,
        user_id,
        dept_id,
        statistics_start,
        statistics_end,
        total_time,
        cost,
        create_time
        )
        SELECT
        project_id,
        t.user_id,
        t.dept_id,
        min(work_day),
        max(work_day),
        sum(work_time)/ 8,
        sum(work_time  * s.day_salary)/ 8,
        now() AS create_time
        FROM
        work_time_order t
        LEFT JOIN work_user_salary s ON t.user_id = s.user_id
        WHERE
        t.work_day >= #{date} AND s.create_time &lt;work_day AND s.end_time > work_day
        AND t.`status` IN (2, 5) AND project_id is not null
        GROUP BY
        project_id,
        t.dept_id,
        t.user_id,
        YEAR (work_day),
        MONTH (work_day)
        ORDER BY min(work_day)
     </insert>


    <select id="selectList" resultMap="workTimeAndCostCollect">
        SELECT p.type ,<if test="centreId == null">'汇总' AS dept_name,</if> <if test="centreId != null"> dept_name,p.dept_id,</if>p.id AS
        project_id,project_name,concat(min(statistics_start),'~' ,max(statistics_end)) AS statistics_time ,sum(total_time) AS total_time,sum(cost) AS cost
        from work_project_time_cost ptc join work_project p on ptc.project_id=p.id join work_dept d on p.dept_id=d.id
        <where>
        <if test="managerId != null and deptId == null">
        AND p.manager_id = #{managerId}
        </if>
        <if test="deptId != null and managerId != null">
            AND (p.dept_id = #{deptId} OR p.manager_id = #{managerId})
        </if>
        <if test="deptId != null and managerId == null">
            AND p.dept_id = #{deptId}
        </if>
        <if test="centreId != null">
           AND centre_id = #{centreId}
        </if>
        <if test="firstDayOfMonth != null">
            AND statistics_start >= #{firstDayOfMonth}
        </if>
        </where>
        GROUP BY p.type, <if test="centreId == null"> dept_name,</if> <if test="centreId != null"> dept_name,p.dept_id,</if>p.id,project_name
        ORDER BY p.type<if test="centreId != null">, d.sort</if>
    </select>


    <select id="selectListByMonth" resultMap="projectStatisticsByMonth">
        select <if test="projectId == null and projectIds == null">'合计' AS project_name,</if> <if test="projectId != null"> project_name, dept_name,</if> <if test=" projectIds != null"> project_id,project_name, dept_name,</if> group_CONCAT(DISTINCT YEAR (statistics_start),'年',MONTH (statistics_start),'月') AS date,year(statistics_start) AS year, sum(total_time) AS total_time, sum(cost) AS cost
        from work_project_time_cost ptc join work_project p on ptc.project_id = p.id join work_dept d on p.dept_id = d.id
        <where>
        <if test="projectId != null ">
            ptc.project_id = #{projectId}
        </if>
        <if test="projectIds != null ">
            ptc.project_id in
            <foreach collection="projectIds" item="projectId" open="(" close=")" separator=",">
                #{projectId}
            </foreach>
            AND year(statistics_start) = #{year}
        </if>
        <if test="deptId != null and managerId == null">
            AND p.dept_id = #{deptId} AND year(statistics_start) = #{year}
        </if>
        <if test="deptId != null and managerId != null">
            AND (p.dept_id = #{deptId} OR p.manager_id= #{managerId}) AND year(statistics_start) = #{year}
        </if>
        <if test="deptId == null and managerId != null">
            AND p.manager_id = #{managerId} AND year(statistics_start) = #{year}
        </if>
        </where>
        group by <if test="projectId != null and projectIds == null"> project_name,</if> <if test="projectId == null and projectIds != null"> project_name,dept_name,</if>year(statistics_start),MONTH(statistics_start)
    </select>

    <select id="selectListStatistics" resultMap="projectStatisticsByMonth">
        select ptc.project_id AS project_id, project_name,
        group_CONCAT(DISTINCT YEAR (statistics_start),'年',MONTH (statistics_start),'月') AS date,year(statistics_start) AS year,
        sum(total_time) AS total_time, sum(cost) AS cost <if test="projectId != null || (projectIds != null and !isAll)"> ,d.dept_name AS dept_name</if> <if test="projectIds != null and isAll">,d.dept_name AS dept_name,u.name AS user_name </if>
        from work_project_time_cost ptc join work_project p on ptc.project_id = p.id join work_dept d on ptc.dept_id = d.id join work_user u on ptc.user_id = u.id
        <where>
            <if test="projectId != null ">
                ptc.project_id = #{projectId}
            </if>
            <if test="projectIds != null">
                ptc.project_id in
                <foreach collection="projectIds" open="(" close=")" separator="," item="projectId">
                    #{projectId}
                </foreach>
                AND year(statistics_start) = #{year}
            </if>
            <if test="deptId != null and managerId == null">
                AND p.dept_id = #{deptId} AND year(statistics_start) = #{year}
            </if>
            <if test="deptId != null and managerId != null">
                AND (p.dept_id = #{deptId} OR p.manager_id= #{managerId}) AND year(statistics_start) = #{year}
            </if>
            <if test="deptId == null and managerId != null">
                AND p.manager_id = #{managerId} AND year(statistics_start) = #{year}
            </if>
        </where>
        group by ptc.project_id,project_name,year(statistics_start),MONTH(statistics_start) <if test="projectId != null || (projectIds != null and !isAll) "> ,d.dept_name</if> <if test="projectIds != null and isAll">,d.dept_name ,u.name </if>

    </select>

</mapper>