WorkTimeOrderMapper.xml 15.3 KB
Newer Older
nie'hong's avatar
nie'hong committed
1 2 3 4 5 6
<?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.WorkTimeOrderMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.wisenergy.model.app.WorkTimeOrder">
nie'hong's avatar
nie'hong committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
        <id column="work_id" property="workId"/>
        <result column="user_id" property="userId"/>
        <result column="project_id" property="projectId"/>
        <result column="dept_id" property="deptId"/>
        <result column="work_time" property="workTime"/>
        <result column="work_day" property="workDay"/>
        <result column="status" property="status"/>
        <result column="reviewer_id" property="reviewerId"/>
        <result column="des" property="des"/>
        <result column="reason" property="reason"/>
        <result column="type" property="type"/>
        <result column="is_overtime" property="isOvertime"/>
        <result column="reject_time" property="rejectTime"/>
        <result column="create_time" property="createTime"/>
        <result column="modify_time" property="modifyTime"/>
    </resultMap>

    <resultMap id="userWorkTimeStatisticsByProject" type="cn.wisenergy.model.dto.UserWorkTimeStatisticsByProject">
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <collection property="projectWorkTimeAndType" ofType="cn.wisenergy.model.dto.ProjectWorkTimeAndType">
            <result property="projectId" column="project_id"/>
            <result property="projectType" column="project_type"/>
            <result property="projectName" column="project_name"/>
nie'hong's avatar
nie'hong committed
31
            <result property="projectTypeName" column="type_name"/>
nie'hong's avatar
nie'hong committed
32 33 34
            <result property="isOvertime" column="is_overtime"/>
            <result property="workTime" column="work_time"/>
        </collection>
nie'hong's avatar
nie'hong committed
35 36
    </resultMap>

nie'hong's avatar
nie'hong committed
37 38 39 40 41 42 43 44 45 46 47 48
    <resultMap id="workTimeAndCostCollect" type="cn.wisenergy.model.dto.WorkTimeAndCostCollect" >
        <result property="type" column="type"/>
        <collection property="deptProjectWorkTimeAndCosts" resultMap="deptProjectWorkTimeAndCosts" >
        </collection>
    </resultMap>

    <resultMap id="deptProjectWorkTimeAndCosts" 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
49
            <result property="statisticsTime" column="statistics_time"/>
nie'hong's avatar
nie'hong committed
50 51 52 53 54
            <result property="totalTime" column="total_time"/>
            <result property="cost" column="cost"/>
        </collection>
    </resultMap>

nie'hong's avatar
nie'hong committed
55

cq990612's avatar
cq990612 committed
56 57
    <!-- 通用查询结果列 -->
    <sql id="vals">
cq990612's avatar
cq990612 committed
58
       #{userId},#{projectId},#{deptId},#{workTime},#{workDay},#{status},#{reviewerId},#{des},#{reason},#{type},#{isOvertime},#{rejectTime},now(),now()
cq990612's avatar
cq990612 committed
59 60
    </sql>
    <sql id="cols_exclude_id">
cq990612's avatar
cq990612 committed
61
      user_id, project_id, dept_id, work_time, work_day, status, reviewer_id, des, reason, type, is_overtime,reject_time, create_time, modify_time
nie'hong's avatar
nie'hong committed
62 63 64 65 66 67 68 69
    </sql>

    <sql id="criteria">
        <if test="workId != null">and work_id = #{workId}</if>
        <if test="userId != null">and user_id = #{userId}</if>
        <if test="projectId != null">and project_id =#{projectId}</if>
        <if test="deptId != null">and dept_id = #{deptId}</if>
        <if test="workTime != null">and work_time = #{workTime}</if>
nie'hong's avatar
nie'hong committed
70
        <if test="workDay != null">and work_day = #{workDay}</if>
nie'hong's avatar
nie'hong committed
71 72 73 74 75 76 77
        <if test=" status != null">and `status` = #{status}</if>
        <if test=" reviewerId != null">and reviewer_id = #{reviewerId}</if>
        <if test="des != null">and des =#{des}</if>
        <if test="reason != null">and reason = #{reason}</if>
        <if test="type != null">and type = #{type}</if>
        <if test="isOvertime != null">and is_overtime = #{isOvertime}</if>
        <if test="createTime != null">and create_time &gt;= #{createTime}</if>
cq990612's avatar
cq990612 committed
78
        <if test="rejectTime !=null">and reject_time = #{rejectTime}</if>
nie'hong's avatar
nie'hong committed
79 80
    </sql>

cq990612's avatar
cq990612 committed
81
    <resultMap id="dayWorkTimeAndType" type="cn.wisenergy.model.dto.DayWorkTimeAndType">
nie'hong's avatar
nie'hong committed
82
        <result property="userId" column="user_id"/>
cq990612's avatar
cq990612 committed
83 84 85 86 87 88 89 90
        <result property="day" column="day(work_day)"/>
        <result property="type" column="type"/>
        <result property="workTime" column="sum(work_time)"/>
    </resultMap>

    <resultMap id="monthlyWorkHourStatistics" type="cn.wisenergy.model.dto.MonthlyWorkingHoursStatistics">
        <result property="count" column="count(1)"/>
        <result property="workTime" column="sum(work_time)"/>
91 92 93
        <result property="projectType" column="id"/>
        <result property="typeName" column="name"/>
        <result property="approved" column="approved"/>
cq990612's avatar
cq990612 committed
94 95 96 97 98 99
    </resultMap>

    <sql id="table">
        work_time_order
    </sql>

nie'hong's avatar
nie'hong committed
100
    <insert id="save" keyProperty="workId" useGeneratedKeys="true">
cq990612's avatar
cq990612 committed
101 102 103 104 105 106 107 108
        insert into
        <include refid="table"/>
        (<include refid="cols_exclude_id"/>)
        value(
        <include refid="vals"/>
        )
    </insert>

cq990612's avatar
cq990612 committed
109

cq990612's avatar
cq990612 committed
110 111 112
    <update id="updateByIds">
        UPDATE
        <include refid="table"/>
nie'hong's avatar
nie'hong committed
113
        SET status = 2,reviewer_id = #{reviewerId},modify_time = now()
cq990612's avatar
cq990612 committed
114 115 116 117 118
        WHERE work_id IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>
nie'hong's avatar
nie'hong committed
119

cq990612's avatar
cq990612 committed
120 121 122
    <update id="updateStatusById">
        UPDATE
        <include refid="table"/>
cq990612's avatar
cq990612 committed
123
        SET status = 3,reason = #{reason},reviewer_id = #{reviewerId},modify_time=now(),reject_time=now()
cq990612's avatar
cq990612 committed
124
        WHERE work_id = #{id}
cq990612's avatar
cq990612 committed
125 126 127 128 129 130

    </update>
    <update id="updateAutoExamine">
        UPDATE
        <include refid="table"/>
        SET status = 5,modify_time=now()
nie'hong's avatar
nie'hong committed
131
        <where>work_id IN
cq990612's avatar
cq990612 committed
132 133 134 135
            <foreach collection="workIds" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </where>
cq990612's avatar
cq990612 committed
136 137 138 139
    </update>


    <select id="getByDay" resultType="cn.wisenergy.model.app.WorkTimeOrder">
nie'hong's avatar
nie'hong committed
140 141 142 143
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        <include refid="table"/>
cq990612's avatar
cq990612 committed
144 145 146
        <where>
            <if test="null !=userId">
                <if test="null != userId">
nie'hong's avatar
nie'hong committed
147
                    AND user_id = #{userId}
cq990612's avatar
cq990612 committed
148 149
                </if>
                <if test="null != workDay">
nie'hong's avatar
nie'hong committed
150
                    AND DATE_FORMAT(work_day, '%Y%m%d') = DATE_FORMAT(#{workDay} ,'%Y%m%d')
cq990612's avatar
cq990612 committed
151 152 153 154 155 156 157 158 159 160 161
                </if>
            </if>
        </where>

    </select>
    <select id="queryByPage" resultType="cn.wisenergy.model.app.WorkTimeOrder">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        <include refid="table"/>
        <where>
cq990612's avatar
cq990612 committed
162
            <if test="null != projectId">
nie'hong's avatar
nie'hong committed
163
                AND project_id = #{projectId}
cq990612's avatar
cq990612 committed
164 165
            </if>
            <if test="null != status">
cq990612's avatar
cq990612 committed
166 167 168 169
                AND status in
                <foreach collection="status" item="sta" open="(" close=")" separator=",">
                    #{sta}
                </foreach>
cq990612's avatar
cq990612 committed
170
            </if>
cq990612's avatar
cq990612 committed
171
            <if test="null != type">
nie'hong's avatar
nie'hong committed
172
                AND type = #{type}
cq990612's avatar
cq990612 committed
173 174 175
            </if>
            <if test="null != type">
                AND dept_id = #{deptId}
cq990612's avatar
cq990612 committed
176 177
            </if>
        </where>
cq990612's avatar
cq990612 committed
178
         ORDER BY work_day DESC
cq990612's avatar
cq990612 committed
179
    </select>
nie'hong's avatar
nie'hong committed
180 181 182

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
cq990612's avatar
cq990612 committed
183
        work_id, user_id, project_id,  dept_id, work_time, work_day, status, reviewer_id, des, reason, type, is_overtime, create_time, modify_time,reject_time
nie'hong's avatar
nie'hong committed
184 185
    </sql>

nie'hong's avatar
nie'hong committed
186
    <select id="statisticsByProjectType" resultMap="monthlyWorkHourStatistics">
187 188
        select t2.id AS id , t2.name AS name ,count(1),sum(work_time)
        from work_time_order t join work_type t2 on t.type = t2.id
nie'hong's avatar
nie'hong committed
189
        where user_id = #{userId} AND work_day >= #{currentMonthFirstDay} AND t.status != 3
nie'hong's avatar
nie'hong committed
190
        group by type
nie'hong's avatar
nie'hong committed
191 192 193
    </select>

    <select id="listByDateAndUserId" resultMap="dayWorkTimeAndType">
nie'hong's avatar
nie'hong committed
194 195 196 197 198 199 200 201 202 203
        select user_id,day(work_day),CASE when `type` = 3 then "请假" when `type` = 4 then "调休" else "正常" end AS `type`,
        sum(work_time)
        from
        <include refid="table"/>
        where year(work_day) = #{year} AND month(work_day) = #{month} AND (status = 2 OR status=5)AND user_id IN
        <foreach collection="userIds" separator="," close=")" item="userId" open="(">
            #{userId}
        </foreach>
        group by user_id,day(work_day), CASE when `type` = 3 then "请假" when `type` = 4 then "调休" else "正常" end
        order by day(work_day)
nie'hong's avatar
nie'hong committed
204
    </select>
cq990612's avatar
cq990612 committed
205

nie'hong's avatar
nie'hong committed
206 207
    <select id="getCountByCondition" resultType="integer" parameterType="map">
        select count(1)
nie'hong's avatar
nie'hong committed
208 209
        from
        <include refid="table"/>
nie'hong's avatar
nie'hong committed
210 211
        <where>
            <include refid="criteria"/>
nie'hong's avatar
nie'hong committed
212
            <if test="startModifyTime != null">
nie'hong's avatar
nie'hong committed
213 214
                AND modify_time>=#{startModifyTime}
            </if>
nie'hong's avatar
nie'hong committed
215
            <if test="startWorkTime != null">
nie'hong's avatar
nie'hong committed
216
                AND work_day>=#{startWorkTime}
nie'hong's avatar
nie'hong committed
217
            </if>
nie'hong's avatar
nie'hong committed
218
            <if test="projectIdList != null">
nie'hong's avatar
nie'hong committed
219
                AND project_id IN
nie'hong's avatar
nie'hong committed
220
                <foreach collection="projectIdList" item="projectId" open="(" close=")" separator=",">
nie'hong's avatar
nie'hong committed
221 222 223 224 225
                    #{projectId}
                </foreach>
            </if>
            <if test="statusArray != null">
                AND `status` IN
nie'hong's avatar
nie'hong committed
226 227
                <foreach collection="statusArray" item="workStatus" open="(" close=")" separator=",">
                  #{workStatus}
nie'hong's avatar
nie'hong committed
228
                </foreach>
nie'hong's avatar
nie'hong committed
229
            </if>
nie'hong's avatar
nie'hong committed
230
            <if test="typeList != null">
nie'hong's avatar
nie'hong committed
231
                AND `type` IN
nie'hong's avatar
nie'hong committed
232 233 234 235 236 237 238
                <foreach collection="typeList" item="type" open="(" close=")" separator=",">
                    #{type}
                </foreach>
            </if>
            <if test="deptIdList != null">
                AND `type` IN
                <foreach collection="deptIdList" item="dept" open="(" close=")" separator=",">
nie'hong's avatar
nie'hong committed
239 240 241 242 243
                    #{type}
                </foreach>
            </if>
        </where>
    </select>
nie'hong's avatar
nie'hong committed
244

nie'hong's avatar
nie'hong committed
245
    <select id="getWorkTimeAndCostCollect" resultMap="workTimeAndCostCollect" >
nie'hong's avatar
nie'hong committed
246
        SELECT t.type AS type, t.dept_id AS dept_id,dept_name,
nie'hong's avatar
nie'hong committed
247
          t.project_id AS project_id ,IF (YEAR (work_day) = YEAR (NOW()), concat(YEAR (work_day),'年',MONTH (work_day),'月') , concat(YEAR(work_day), '年'))  AS statistics_time ,ROUND(SUM(t.work_time)/8,2) AS
nie'hong's avatar
nie'hong committed
248
          total_time, SUM(t.work_time * s.day_salary)/8 AS cost ,project_name
nie'hong's avatar
nie'hong committed
249 250
        FROM work_time_order t LEFT JOIN work_project p ON t.project_id=p.id LEFT JOIN work_dept d ON t.dept_id=d.id LEFT JOIN work_user_salary s ON s.user_id = t.user_id
        WHERE status IN (2,5) AND s.create_time &lt; t.work_day AND s.end_time > t.work_day
nie'hong's avatar
nie'hong committed
251 252 253 254 255 256
        <if test="projectIds != null">
        AND (t.project_id IN
            <foreach collection="projectIds" item="projectId" open="(" close=")" separator=",">
                #{projectId}
            </foreach>
        OR t.project_id IS null)
nie'hong's avatar
nie'hong committed
257
        </if>
nie'hong's avatar
nie'hong committed
258 259
        <if test="firstDayOfMonth != null">
            AND work_day >= #{firstDayOfMonth}
nie'hong's avatar
nie'hong committed
260 261 262 263 264 265 266 267
        </if>
         <if test="deptId != null">
            AND t.dept_id = #{deptId}
        </if>
        AND t.type in
        <foreach collection="types" item="type" open="(" close=")" separator=",">
            #{type}
        </foreach>
268 269 270
        <if test="centreId != null">
            AND centre_id = #{centreId}
        </if>
nie'hong's avatar
nie'hong committed
271 272
        group by t.type,t.dept_id, dept_name, t.project_id,p.project_name, if(YEAR (work_day) = YEAR(NOW()),',month(work_day)','')
        order by t.dept_id,work_day
nie'hong's avatar
nie'hong committed
273 274
    </select>

nie'hong's avatar
nie'hong committed
275
    <select id="getDaysByDateAndStatus" resultType="date">
276
        select distinct(work_day)
nie'hong's avatar
nie'hong committed
277 278
        from
        <include refid="table"/>
nie'hong's avatar
nie'hong committed
279
        where year(#{date}) = year(work_day) AND month(#{date}) = month(work_day) AND user_id = #{userId} AND status != 3
cq990612's avatar
cq990612 committed
280
    </select>
nie'hong's avatar
nie'hong committed
281

cq990612's avatar
cq990612 committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    <select id="geByMonth" resultType="cn.wisenergy.model.app.WorkTimeOrder">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        <include refid="table"/>
        <where>
            <if test="null != userId">
                AND user_id = #{userId}
            </if>
            <if test="null != workMonth">
                AND DATE_FORMAT(work_day, '%Y%m') = DATE_FORMAT(#{workMonth} ,'%Y%m')
            </if>
            <if test="null != status">
                AND status = #{status}
            </if>
        </where>


cq990612's avatar
cq990612 committed
300 301 302 303 304 305 306 307
    </select>
    <select id="getAutoOrder" resultType="cn.wisenergy.model.app.WorkTimeOrder">
        select
        <include refid="Base_Column_List"/>
        FROM
        <include refid="table"/>
        <where>
            status IN (1,4)
cq990612's avatar
cq990612 committed
308
            AND TIMESTAMPDIFF(HOUR,create_time,now()) > #{autoExamine}
cq990612's avatar
cq990612 committed
309 310 311
        </where>


312
    </select>
nie'hong's avatar
nie'hong committed
313 314 315


    <select id="getUserProjectWorkTimeStatistics" resultMap="userWorkTimeStatisticsByProject">
nie'hong's avatar
nie'hong committed
316
        SELECT <if test="isAll == true"> u.id AS user_id, u.`name` AS user_name,</if> <if test="isAll == false">'合计' AS user_name ,</if> t.project_id AS project_id, t.type AS project_type, p.project_name AS project_name, case t.is_overtime when 1 then '加班' ELSE '正常' end AS is_overtime,SUM(t.work_time) AS work_time
nie'hong's avatar
nie'hong committed
317
        from work_time_order t left join work_project p on t.project_id = p.id join  work_user u on t.user_id = u.id
318
        where
nie'hong's avatar
nie'hong committed
319 320
        t.status in (2,5) AND
        t.work_day &lt;= #{endDate}
321 322 323
        <if test="startDate != null">
            AND   t.work_day > #{startDate}
        </if>
nie'hong's avatar
nie'hong committed
324 325 326 327 328
        AND project_id in
        <foreach collection="projectIds" item="projectId" open="(" close=")" separator=",">
            #{projectId}
        </foreach>
        GROUP BY <if test="isAll == true"> u.id,u.`name`,</if>t.project_id,t.type,p.project_name, t.is_overtime
nie'hong's avatar
nie'hong committed
329
    </select>
330 331 332 333

    <select id="selectCountByTypeAndStatus" resultType="cn.wisenergy.model.dto.MonthlyWorkingHoursStatistics">
        select type AS projectType, count(1) AS approved
        from <include refid="table"/>
nie'hong's avatar
nie'hong committed
334
        where user_id = #{userId} and work_day >= #{firstDayOfMonth} AND `type` is not null AND status in
335 336 337 338 339
        <foreach collection="status" open="(" close=")" separator="," item="s">
        #{s}
       </foreach>
       GROUP BY type
    </select>
nie'hong's avatar
nie'hong committed
340 341

    <select id="selectListByType" resultMap="userWorkTimeStatisticsByProject">
342
        SELECT <if test="isAll"> u.id AS user_id, u.`name` AS user_name,</if> <if test="!isAll"> '合计' AS user_name,</if>   ty.id AS project_type, ty.name AS project_name, case t.is_overtime when 1 then '加班' ELSE '正常' end AS is_overtime,IFNULL(SUM(t.work_time),0) AS work_time
nie'hong's avatar
nie'hong committed
343 344 345 346
        from work_type ty left join work_time_order t on t.type = ty.id left join  work_user u on t.user_id = u.id
        where
        (t.work_day &lt;= #{endDate} or t.work_day is null) AND (t.status IN (2, 5) or t.status is null)
        <if test="startDate != null">
nie'hong's avatar
nie'hong committed
347
            AND  (t.work_day > #{startDate} or t.work_day is null)
nie'hong's avatar
nie'hong committed
348 349 350 351 352
        </if>
        AND ty.id in
        <foreach collection="queryType" item="type" open="(" close=")" separator=",">
            #{type}
        </foreach>
nie'hong's avatar
nie'hong committed
353 354
         <if test="userIds.size != 0">
        AND ( t.user_id in
nie'hong's avatar
nie'hong committed
355 356 357
        <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
            #{userId}
        </foreach>
nie'hong's avatar
nie'hong committed
358 359
          OR t.user_id is null)
         </if>
360
        GROUP BY <if test="isAll">u.id,u.`name`,</if> ty.id,ty.name, t.is_overtime
nie'hong's avatar
nie'hong committed
361
    </select>
nie'hong's avatar
nie'hong committed
362
</mapper>