<?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"/>
        <collection property="statisticsDateAndWorkTimes" ofType="cn.wisenergy.model.dto.ProjectStatisticsByMonth$StatisticsDateAndWorkTime">
            <result property="date" column="date"/>
            <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 / 8 * s.day_salary),
        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 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 project_id in
        <foreach collection="projectIds" open="(" close=")" separator="," item="projectId">
            #{projectId}
        </foreach>
        <if test="deptId != null">
            AND dept_id = #{deptId}
        </if>
        <if test="types != null">
            AND type in
            <foreach collection="types" open="(" close=")" separator="," item="type">
                #{type}
            </foreach>
        </if>
        <if test="centreId != null">
           AND centre_id = #{centreId}
        </if>
        <if test="firstDayOfMonth != null">
            AND statistics_start >= #{firstDayOfMonth}
        </if>
        GROUP BY type, <if test="centreId == null"> dept_name,</if> <if test="centreId != null"> dept_name,p.dept_id,</if>p.id,project_name
        <if test="centreId != null"> ORDER BY </if>
    </select>


    <select id="selectListByMonth" resultMap="projectStatisticsByMonth">
        select <if test="deptId != null">'合计' AS project_name,</if> <if test="projectId != null"> project_name,</if>  group_CONCAT(DISTINCT YEAR (statistics_start),'年',MONTH (statistics_start),'月') AS date,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 ptc.dept_id = d.id
        <where>
        <if test="projectId != null ">
            ptc.project_id = #{projectId}
        </if>
        <if test="deptId != null">
            AND p.dept_id = #{deptId} AND year(statistics_start) = #{year}
        </if>
        </where>
        group by <if test="projectId != null"> project_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,
        sum(total_time) AS total_time, sum(cost) AS cost <if test="projectId != null "> ,d.dept_name AS dept_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
        <where>
        <if test="projectId != null ">
            ptc.project_id = #{projectId}
        </if>
        <if test="deptId != null">
            AND p.dept_id = #{deptId} AND year(statistics_start) = #{year}
        </if>
        </where>
        group by ptc.project_id,project_name,year(statistics_start),MONTH(statistics_start) <if test="projectId != null "> ,d.dept_name</if>
    </select>

</mapper>