<?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 (
        `type`,
        project_id,
        user_id,
        dept_id,
        statistics_start,
        statistics_end,
        total_time,
        cost,
        create_time
        )
        SELECT
        t.type,
        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)
        GROUP BY
        `type`,
        project_id,
        t.dept_id,
        t.user_id,
        YEAR (work_day),
        MONTH (work_day)
        ORDER BY min(work_day)
     </insert>


    <select id="selectListByProjectAndBusiness" resultMap="workTimeAndCostCollect">
        SELECT ptc.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 ptc.type, <if test="centreId == null"> dept_name,</if> <if test="centreId != null"> dept_name,p.dept_id,</if>p.id,project_name
        ORDER BY ptc.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)
        order by convert(project_name using gbk)
    </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>
        order by convert(project_name using gbk)
    </select>

    <select id="selectListByOtherType" resultMap="workTimeAndCostCollect">
        SELECT ptc.type ,<if test="centreId == null">'汇总' AS dept_name,</if> <if test="centreId != null"> dept_name,u.dept_id,</if>
         concat(YEAR(statistics_start),'年') AS statistics_time ,sum(total_time) AS total_time,sum(cost) AS cost,d.sort AS d_sort
        from work_project_time_cost ptc join work_user u on u.id=ptc.user_id join work_dept d on ptc.dept_id = d.id
        <where>
            ptc.type not in(1,2)AND YEAR (statistics_start) &lt; YEAR (NOW())
            <if test="deptId != null ">
                AND ptc.dept_id = #{deptId}
            </if>
            <if test="centreId != null">
                AND d.centre_id = #{centreId}
            </if>
            <if test="firstDayOfMonth != null">
                AND statistics_start >= #{firstDayOfMonth}
            </if>

        </where>
        GROUP BY ptc.type, dept_name,year(statistics_start) <if test="centreId != null"> ,u.dept_id</if>

        union

        SELECT ptc.type ,<if test="centreId == null">'汇总' AS dept_name,</if> <if test="centreId != null"> dept_name,u.dept_id,</if>
         concat(YEAR (statistics_start),'年',MONTH (statistics_start),'月') AS statistics_time ,sum(total_time) AS total_time,sum(cost) AS cost,d.sort AS d_sort
        from work_project_time_cost ptc join work_user u on u.id=ptc.user_id join work_dept d on ptc.dept_id = d.id
        <where>
            ptc.type not in(1,2) AND YEAR(statistics_start) = YEAR (NOW())
            <if test="deptId != null ">
                AND ptc.dept_id = #{deptId}
            </if>
            <if test="centreId != null">
                AND d.centre_id = #{centreId}
            </if>
            <if test="firstDayOfMonth != null">
                AND statistics_start >= #{firstDayOfMonth}
            </if>

        </where>
        GROUP BY ptc.type, dept_name,month(statistics_start) <if test="centreId != null"> ,u.dept_id</if>

        ORDER BY <if test="centreId != null">d_sort,</if>statistics_time
    </select>

</mapper>