杜洪波
2024-10-09 0265a4b398b7618880d5dcb6fdb50939409eb2a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.product.administration.service;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.alibaba.fastjson.JSONObject;
import com.product.administration.config.CmnConst;
import com.product.administration.util.WorkDayUtil;
import com.product.common.lang.StringUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.permission.PermissionService;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.service.support.QueryFilterService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.util.BaseUtil;
 
@Component
public class WorkAttendanceService extends AbstractBaseService {
 
    @Autowired
    BaseDao baseDao;
    @Autowired
    PermissionService permissionService;
    @Autowired
    QueryFilterService queryFilterService;
 
    /**
     * 考勤信息列表
     * 
     * @param fse
     * @return
     */
    public DataTableEntity listAttendanceInfo(FieldSetEntity fse) {
        String queryFilter = null;
        if (!BaseUtil.dataTableIsEmpty(fse.getSubDataTable("systemSeniorQueryString"))) {
            queryFilter = queryFilterService.getQueryFilter(fse);
        }
        //String dataFilter = permissionService.getDataFilter(CmnConst.ORG_LEVEL_UUID);
        //周杰 2022-02-17 条件由ORG_LEVEL_UUID改为CREATE_BY
        String dataFilter = permissionService.getDataFilter(fse.getTableName(),CmnConst.CREATED_BY);
        if (!StringUtils.isEmpty(dataFilter)) {
            if (StringUtils.isEmpty(queryFilter)) {
                queryFilter = dataFilter;
            } else {
                queryFilter += " AND " + dataFilter;
            }
        }
 
        DataTableEntity dt = baseDao.listTable(CmnConst.PRODUCT_OA_ATTENDANCE_INFO, queryFilter, null, null, null,
                fse.getInteger(CmnConst.PAGESIZE), fse.getInteger(CmnConst.CPAGE));
        baseDao.loadPromptData(dt);
        return dt;
    }
 
    /**
     * 考勤信息详情
     * 
     * @param uuid
     * @return
     */
    public FieldSetEntity findAttendanceInfo(String uuid) {
        return baseDao.getFieldSetEntity(CmnConst.PRODUCT_OA_ATTENDANCE_INFO, uuid, false);
    }
 
    /**
     * 考勤信息保存
     * 
     * @param fse
     * @return
     */
    public String saveAttendanceInfo(FieldSetEntity fse) {
        if (StringUtils.isEmpty(fse.getUUID())) {
            return baseDao.add(fse);
        } else {
            fse.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
            fse.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date());
            baseDao.update(fse);
            return fse.getUUID();
        }
    }
 
    /**
     * 考勤信息删除
     * 
     * @param uuid
     * @return
     */
    public boolean deleteAttendanceInfo(String uuid) {
        return baseDao.delete(CmnConst.PRODUCT_OA_ATTENDANCE_INFO, "uuid=?", new Object[] { uuid });
    }
 
    /**
     *     获取员工工作信息
     * @param user_id
     * @param yearAndMonth
     * @return
     */
    public JSONObject getStaffWorkInfo(Integer user_id, String yearAndMonth) {
        //信息汇总
        JSONObject jsonWorkInfo=new JSONObject();
        
        //获取年月日
        if (yearAndMonth.length() > 10) {
            yearAndMonth = yearAndMonth.substring(0, 10);
        }
        
        //获取当月工作时长
        int totalHours=WorkDayUtil.queryMonthDay(yearAndMonth) * 7;
        jsonWorkInfo.put("should_arrived_hours", WorkDayUtil.queryMonthDay(yearAndMonth) * 7);    //该月应工作小时
        
        //获取请假信息
        Integer [] leaveHours = getLeaveHours(user_id, yearAndMonth);
        jsonWorkInfo.put("casual_leave_hours", leaveHours[1] * 7);    //事假时长
        jsonWorkInfo.put("sick_leave_hours", leaveHours[2] * 7);    //病假时长
        jsonWorkInfo.put("paid_leave_hours", leaveHours[3] * 7);    //带薪休假时长
        jsonWorkInfo.put("have_arrived_hours", totalHours-leaveHours[0] * 7);    //该月实工作小时
        
        //获取出差信息
        int tripHours = getBusinessTripHours(user_id, yearAndMonth);
        jsonWorkInfo.put("business_trip_hours", tripHours * 7);        //出差时长
        
        return jsonWorkInfo;
    }
    
    /**
     *     获取请假时长
     * @param user_id
     * @param yearAndMonth
     * @return
     */
    public Integer[] getLeaveHours(Integer user_id, String yearAndMonth) {
        StringBuilder sb=new StringBuilder();
        sb.append(" SELECT ");
        sb.append(" SUM(day) t0, ");
        sb.append(" SUM(CASE WHEN leave_type=1 THEN day ELSE 0 END ) t1, ");
        sb.append(" SUM(CASE WHEN leave_type=2 THEN day ELSE 0 END ) t2, ");
        sb.append(" SUM(CASE WHEN leave_type <>1 and leave_type<>2 THEN day ELSE 0 END ) t3 ");
        sb.append(" FROM product_oa_ask_for_leave ");
        sb.append(" WHERE flow_flag=2 ");
        sb.append(" AND DATE_FORMAT(start_time,'%Y-%m') =? ");
        sb.append(" AND created_by=? ");
        FieldSetEntity fse=baseDao.getFieldSetEntityBySQL(sb.toString(), new Object[] {yearAndMonth.substring(0, 7), user_id}, false);
        return new Integer[] {fse.getInteger("t0")==null?0:fse.getInteger("t0"), fse.getInteger("t1")==null?0:fse.getInteger("t1"), fse.getInteger("t2")==null?0:fse.getInteger("t2"), fse.getInteger("t3")==null?0:fse.getInteger("t3")};
    }
    
    /**
     *     获取出差时长
     * @param user_id
     * @param yearAndMonth
     * @return
     */
    public Integer getBusinessTripHours(Integer user_id, String yearAndMonth) {
        String sql="SELECT SUM(total_days)total FROM product_oa_business_trip WHERE flow_flag=2 AND DATE_FORMAT(business_start_time,'%Y-%m') = ? AND applicant=?";
        FieldSetEntity fse=baseDao.getFieldSetEntityBySQL(sql, new Object[] {yearAndMonth.substring(0, 7), user_id}, false);
        return fse.getInteger("total")==null?0:fse.getInteger("total");
    }
}