WorkProjectTimeCost.xml 8.2 KB
Newer Older
nie'hong's avatar
nie'hong committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?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"/>
nie'hong's avatar
nie'hong committed
25
            <result property="statisticsTime" column="statistics_time"/>
nie'hong's avatar
nie'hong committed
26 27 28 29 30
            <result property="totalTime" column="total_time"/>
            <result property="cost" column="cost"/>
        </collection>
    </resultMap>

31 32 33
    <resultMap id="projectStatisticsByMonth" type="cn.wisenergy.model.dto.ProjectStatisticsByMonth">
        <result property="projectId" column="project_id"/>
        <result property="projectName" column="project_name"/>
34
        <result property="deptName" column="dept_name"/>
nie'hong's avatar
nie'hong committed
35
        <result property="userName" column="user_name"/>
36 37
        <collection property="statisticsDateAndWorkTimes" ofType="cn.wisenergy.model.dto.ProjectStatisticsByMonth$StatisticsDateAndWorkTime">
            <result property="date" column="date"/>
nie'hong's avatar
nie'hong committed
38
            <result property="year" column="year"/>
39 40 41 42 43
            <result property="totalTime" column="total_time"/>
            <result property="cost" column="cost"/>
        </collection>
    </resultMap>

nie'hong's avatar
nie'hong committed
44 45 46 47 48 49
    <delete id="deleteStatisticsByQuarter">
        delete
        from work_project_time_cost
        where statistics_start >= #{date}
    </delete>

nie'hong's avatar
nie'hong committed
50

51 52
    <insert id="statisticsTimeOrderByMonth" useGeneratedKeys="true">
        INSERT INTO work_project_time_cost (
nie'hong's avatar
nie'hong committed
53
        project_id,
nie'hong's avatar
nie'hong committed
54
        user_id,
55
        dept_id,
56 57 58
        statistics_start,
        statistics_end,
        total_time,
59 60
        cost,
        create_time
61 62 63 64
        )
        SELECT
        project_id,
        t.user_id,
65
        t.dept_id,
66 67
        min(work_day),
        max(work_day),
68 69
        sum(work_time)/ 8,
        sum(work_time  * s.day_salary)/ 8,
70
        now() AS create_time
71 72
        FROM
        work_time_order t
nie'hong's avatar
nie'hong committed
73
        LEFT JOIN work_user_salary s ON t.user_id = s.user_id
74 75
        WHERE
        t.work_day >= #{date} AND s.create_time &lt;work_day AND s.end_time > work_day
nie'hong's avatar
nie'hong committed
76
        AND t.`status` IN (2, 5) AND project_id is not null
77 78
        GROUP BY
        project_id,
79
        t.dept_id,
80
        t.user_id,
nie'hong's avatar
nie'hong committed
81 82
        YEAR (work_day),
        MONTH (work_day)
nie'hong's avatar
nie'hong committed
83
        ORDER BY min(work_day)
84
     </insert>
nie'hong's avatar
nie'hong committed
85 86 87


    <select id="selectList" resultMap="workTimeAndCostCollect">
nie'hong's avatar
nie'hong committed
88
        SELECT p.type ,<if test="centreId == null">'汇总' AS dept_name,</if> <if test="centreId != null"> dept_name,p.dept_id,</if>p.id AS
nie'hong's avatar
nie'hong committed
89
        project_id,project_name,concat(min(statistics_start),'~' ,max(statistics_end)) AS statistics_time ,sum(total_time) AS total_time,sum(cost) AS cost
nie'hong's avatar
nie'hong committed
90
        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
91
        <where>
nie'hong's avatar
nie'hong committed
92 93
        <if test="managerId != null and deptId == null">
        AND p.manager_id = #{managerId}
94
        </if>
nie'hong's avatar
nie'hong committed
95 96
        <if test="deptId != null and managerId != null">
            AND (p.dept_id = #{deptId} OR p.manager_id = #{managerId})
nie'hong's avatar
nie'hong committed
97
        </if>
nie'hong's avatar
nie'hong committed
98 99 100
        <if test="deptId != null and managerId == null">
            AND p.dept_id = #{deptId}
        </if>
nie'hong's avatar
nie'hong committed
101 102 103
        <if test="centreId != null">
           AND centre_id = #{centreId}
        </if>
nie'hong's avatar
nie'hong committed
104 105 106
        <if test="firstDayOfMonth != null">
            AND statistics_start >= #{firstDayOfMonth}
        </if>
107
        </where>
nie'hong's avatar
nie'hong committed
108
        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
nie'hong's avatar
nie'hong committed
109
        ORDER BY p.type<if test="centreId != null">, d.sort</if>
nie'hong's avatar
nie'hong committed
110
    </select>
111 112 113


    <select id="selectListByMonth" resultMap="projectStatisticsByMonth">
nie'hong's avatar
nie'hong committed
114
        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
nie'hong's avatar
nie'hong committed
115
        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
nie'hong's avatar
nie'hong committed
116 117 118 119
        <where>
        <if test="projectId != null ">
            ptc.project_id = #{projectId}
        </if>
nie'hong's avatar
nie'hong committed
120 121 122 123 124 125 126
        <if test="projectIds != null ">
            ptc.project_id in
            <foreach collection="projectIds" item="projectId" open="(" close=")" separator=",">
                #{projectId}
            </foreach>
            AND year(statistics_start) = #{year}
        </if>
nie'hong's avatar
nie'hong committed
127
        <if test="deptId != null and managerId == null">
nie'hong's avatar
nie'hong committed
128 129
            AND p.dept_id = #{deptId} AND year(statistics_start) = #{year}
        </if>
nie'hong's avatar
nie'hong committed
130
        <if test="deptId != null and managerId != null">
nie'hong's avatar
nie'hong committed
131 132 133 134 135
            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>
nie'hong's avatar
nie'hong committed
136
        </where>
nie'hong's avatar
nie'hong committed
137
        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)
nie'hong's avatar
nie'hong committed
138 139 140
    </select>

    <select id="selectListStatistics" resultMap="projectStatisticsByMonth">
141
        select ptc.project_id AS project_id, project_name,
nie'hong's avatar
nie'hong committed
142
        group_CONCAT(DISTINCT YEAR (statistics_start),'年',MONTH (statistics_start),'月') AS date,year(statistics_start) AS year,
nie'hong's avatar
nie'hong committed
143
        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>
nie'hong's avatar
nie'hong committed
144
        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
145
        <where>
nie'hong's avatar
nie'hong committed
146 147 148
            <if test="projectId != null ">
                ptc.project_id = #{projectId}
            </if>
nie'hong's avatar
nie'hong committed
149 150 151 152 153
            <if test="projectIds != null">
                ptc.project_id in
                <foreach collection="projectIds" open="(" close=")" separator="," item="projectId">
                    #{projectId}
                </foreach>
nie'hong's avatar
nie'hong committed
154
                AND year(statistics_start) = #{year}
nie'hong's avatar
nie'hong committed
155
            </if>
nie'hong's avatar
nie'hong committed
156 157 158
            <if test="deptId != null and managerId == null">
                AND p.dept_id = #{deptId} AND year(statistics_start) = #{year}
            </if>
nie'hong's avatar
nie'hong committed
159
            <if test="deptId != null and managerId != null">
nie'hong's avatar
nie'hong committed
160 161 162 163 164
                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>
165
        </where>
nie'hong's avatar
nie'hong committed
166
        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>
nie'hong's avatar
nie'hong committed
167

168
    </select>
nie'hong's avatar
nie'hong committed
169

nie'hong's avatar
nie'hong committed
170
</mapper>