Просмотр исходного кода

新增项目月份数据接口与导出

anderx 1 год назад
Родитель
Сommit
0e0abc73fe

+ 29 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/project/ProjectTaskController.java

@@ -444,4 +444,33 @@ public class ProjectTaskController extends BaseController {
         projectTaskService.saveText(content);
         return AjaxResult.success();
     }
+
+
+    /**
+     * 获取项目月份打卡数据
+     * @param id 项目编号
+     * @param yearMonth  年月份
+     * @return 数据
+     */
+    @GetMapping("/getMonthClock")
+    public AjaxResult getMonthClock(@RequestParam("id") Integer id,@RequestParam("yearMonth") String yearMonth){
+        if (id==null){
+            return AjaxResult.error("项目编号不能为空");
+        }
+        if (yearMonth==null){
+            return AjaxResult.error("年月日期不能为空");
+        }
+        return AjaxResult.success(projectStaffRecordService.getMonthClock(id,yearMonth));
+    }
+
+    /**
+     * 获取项目月份打卡数据
+     * @param id 项目编号
+     * @param yearMonth  年月份
+     * @return 数据
+     */
+    @GetMapping("/getMonthClockExport")
+    public void getMonthClockExport(@RequestParam("id") Integer id,@RequestParam("yearMonth") String yearMonth,HttpServletResponse response){
+        projectStaffRecordService.getMonthClockExport(id,yearMonth,response);
+    }
 }

+ 1 - 1
ruoyi-admin/src/main/resources/application-druid-dev.yml

@@ -13,7 +13,7 @@ spring:
             # 主库数据源
             master:
 #                url: jdbc:mysql://localhost:3306/kede_yfdk?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
-                url: jdbc:mysql://localhost:3306/yfdk20231110?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+                url: jdbc:mysql://localhost:3306/yfdk20240724?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
                 username: root
                 password: 123456
             # 从库数据源

+ 25 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java

@@ -1836,4 +1836,29 @@ public class ExcelUtil<T>
         }
         return method;
     }
+
+    public void exportProjectMonthClockExcel(HttpServletResponse response,List<Map<String, Object>> listData, String projectName, String departmentName, String companyName,
+                                             Integer year,Integer month) {
+        title=StringUtils.format("{}年{}月工时记录表",year,month);
+        sheetName="导出表格";
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+        response.setCharacterEncoding("utf-8");
+        this.wb = new SXSSFWorkbook(500);
+        this.sheet = wb.createSheet();
+        wb.setSheetName(0, sheetName);
+        try
+        {
+//            writeSheet();
+            wb.write(response.getOutputStream());
+        }
+        catch (Exception e)
+        {
+            log.error("导出Excel异常{}", e.getMessage());
+        }
+        finally
+        {
+            IOUtils.closeQuietly(wb);
+        }
+
+    }
 }

+ 2 - 0
ruoyi-system/src/main/java/com/ruoyi/project/mapper/ProjectStaffRecordMapper.java

@@ -54,4 +54,6 @@ public interface ProjectStaffRecordMapper {
     void mateUserRecord(@Param("collect") List<Long> collect);
 
     void updateTianheType(@Param("collect")List<ProjectStaffRecord> newList);
+
+    List<ProjectStaffRecord> getMonthClock(@Param("id") Integer id, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);
 }

+ 5 - 0
ruoyi-system/src/main/java/com/ruoyi/project/service/ProjectStaffRecordService.java

@@ -9,6 +9,7 @@ import com.ruoyi.project.domain.ProjectStaffRecordLog;
 
 import javax.servlet.http.HttpServletResponse;
 import java.util.List;
+import java.util.Map;
 
 public interface ProjectStaffRecordService {
     AjaxResult add(ProjectStaffRecord in);
@@ -41,4 +42,8 @@ public interface ProjectStaffRecordService {
 
 
     void mateUserRecord(ProjectStaffRecordInput in);
+
+    List<Map<String,Object>> getMonthClock(Integer id, String yearMonth);
+
+    void getMonthClockExport(Integer id, String yearMonth, HttpServletResponse response);
 }

+ 175 - 0
ruoyi-system/src/main/java/com/ruoyi/project/service/impl/ProjectStaffRecordServiceImpl.java

@@ -13,6 +13,7 @@ import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.bean.BeanValidators;
 import com.ruoyi.common.utils.file.FileUtils;
+import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.project.bo.MyMonthDays;
 import com.ruoyi.project.bo.ProjectStaffRecordInput;
 import com.ruoyi.project.bo.ProjectStaffRecordOut;
@@ -44,6 +45,10 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.time.*;
+import java.time.format.DateTimeFormatter;
+import java.time.temporal.TemporalAdjusters;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
@@ -358,6 +363,8 @@ public class ProjectStaffRecordServiceImpl implements ProjectStaffRecordService
 
     }
 
+
+
     /**
      * 项目重新计算统计值
      */
@@ -767,4 +774,172 @@ public class ProjectStaffRecordServiceImpl implements ProjectStaffRecordService
         }
         return myDuration;
     }
+
+    @Override
+    public List<Map<String,Object>> getMonthClock(Integer id, String yearMonth) {
+        ProjectTask projectTask = projectTaskMapper.selectByPrimaryKey(Long.valueOf(id));
+        SysDept sysDept = deptService.selectDeptByPid(projectTask.getId());
+        String projectName=projectTask.getName();
+        String departmentName=sysDept.getDeptName();
+        String companyName=sysDept.getCompanyName();
+        if (sysDept.getCompanyId()==null){
+            companyName=sysDept.getDeptName();
+        }
+        LocalDate startDate =  LocalDate.parse(yearMonth, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
+        startDate=startDate.withDayOfMonth(1);
+        int year=startDate.getYear();
+        int month=startDate.getMonthValue();
+        int lastMonth = month;
+        if(startDate.getMonthValue()== Month.DECEMBER.getValue()){
+            month=1;
+            year=startDate.getYear()+1;
+        }else {
+            lastMonth=startDate.getMonthValue()+1;
+        }
+        LocalDate endDate = LocalDate.of(year,lastMonth,1);
+        List<ProjectStaffRecord> monthClock = projectStaffRecordMapper.getMonthClock(id, startDate, endDate);
+        monthClock = monthClock.stream().filter(e -> e.getAid() == 48).collect(Collectors.toList());
+        LocalDate lastDay = startDate.with(TemporalAdjusters.lastDayOfMonth());
+        //获取当月每天与星期
+        List<Map<String, Object>> list = getDaysMap(lastDay, startDate);
+        List<Map<String,Object>> listData = new ArrayList<>();
+        //将数据与表头匹配
+        for (ProjectStaffRecord e : monthClock) {
+            //先匹配是否存在这个人,不存证则需要新增
+            LocalDate recordTime =  e.getRecordTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
+            if (listData.isEmpty()){
+                pushDate(e, list, recordTime, listData);
+            }else {
+                boolean flag = false;
+                for (Map<String, Object> m : listData) {
+                    String name = (String) m.get("name");
+                    if (name.equals(e.getName())){
+                        flag=true;
+                        //存在则需要匹配存入
+                        List<Map<String, Object>> maps = (List<Map<String, Object>>) m.get("data");
+                        for (Map<String, Object> map2 : maps) {
+                            Integer date = (Integer) map2.get("date");
+                            Integer dayOfMonth = recordTime.getDayOfMonth();
+                            //比对日期,如果日期有参数相加,没有则赋值
+                            if (dayOfMonth.equals(date)) {
+                                Double o = (Double) map2.get("projectDuration");
+                                if (o==null){
+                                    map2.put("projectDuration", e.getDuration());
+                                }else {
+                                    map2.put("projectDuration", o+e.getDuration());
+                                }
+                                map2.put("notDuration", 8- e.getDuration()>0?8- e.getDuration():0);
+                                break;
+                            }
+                        }
+                    }
+                }
+                if (!flag){
+                    pushDate(e, list, recordTime, listData);
+                }
+            }
+        }
+        //将数据统计,计算出所有工时与分摊率
+        pushDurationAndProportion(listData);
+        System.out.println("projectName:"+projectName);
+        System.out.println("depName:"+departmentName);
+        System.out.println("companyName:"+companyName);
+
+        return listData;
+    }
+
+    @Override
+    public void getMonthClockExport(Integer id, String yearMonth, HttpServletResponse response) {
+        ProjectTask projectTask = projectTaskMapper.selectByPrimaryKey(Long.valueOf(id));
+        SysDept sysDept = deptService.selectDeptByPid(projectTask.getId());
+        String projectName=projectTask.getName();
+        String departmentName=sysDept.getDeptName();
+        String companyName=sysDept.getCompanyName();
+        if (sysDept.getCompanyId()==null){
+            companyName=sysDept.getDeptName();
+        }
+        LocalDate startDate =  LocalDate.parse(yearMonth, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
+        startDate=startDate.withDayOfMonth(1);
+        int year=startDate.getYear();
+        int month=startDate.getMonthValue();
+        List<Map<String, Object>> listData = getMonthClock(id, yearMonth);
+        ExcelUtil<Object> util = new ExcelUtil<>(Object.class);
+        util.exportProjectMonthClockExcel(response,listData, projectName, departmentName, companyName, year,month);
+    }
+
+    private void pushDurationAndProportion(List<Map<String, Object>> listData) {
+        for (Map<String, Object> map : listData) {
+            BigDecimal projectDuration = new BigDecimal(0);
+            BigDecimal notDuration = new BigDecimal(0);
+            List<Map<String, Object>> maps = (List<Map<String, Object>>) map.get("data");
+            for (Map<String, Object> map2 : maps) {
+                if (map2.get("projectDuration")!=null){
+                    projectDuration = projectDuration.add(new BigDecimal(map2.get("projectDuration").toString()));
+                }
+                if (map2.get("notDuration")!=null){
+                    notDuration = notDuration.add(new BigDecimal(map2.get("notDuration").toString()));
+                }
+            }
+            map.put("projectDuration", projectDuration);
+            map.put("notDuration", notDuration);
+            BigDecimal counts = projectDuration.add(notDuration);
+            map.put("projectProportion", projectDuration.divide(counts,2, RoundingMode.HALF_UP));
+            map.put("notProportion", notDuration.divide(counts,2, RoundingMode.HALF_UP));
+        }
+    }
+
+    private List<Map<String, Object>> getDaysMap(LocalDate lastDay, LocalDate startDate) {
+        List<Map<String,Object>> list =new ArrayList<>();
+        //先计算出月份表头
+        for (int i = 1; i < lastDay.getDayOfMonth(); i++){
+            startDate = startDate.plusDays(1);
+            Map<String,Object> map = new HashMap<>();
+            map.put("date", startDate.getDayOfMonth());
+            DayOfWeek dayOfWeek = startDate.getDayOfWeek();
+            switch (dayOfWeek){
+                case MONDAY:
+                    map.put("week","一");
+                    break;
+                case TUESDAY:
+                    map.put("week","二");
+                    break;
+                case WEDNESDAY:
+                    map.put("week","三");
+                    break;
+                case THURSDAY:
+                    map.put("week","四");
+                    break;
+                case FRIDAY:
+                    map.put("week","五");
+                    break;
+                case SATURDAY:
+                    map.put("week","六");
+                    break;
+                case SUNDAY:
+                    map.put("week","日");
+                    break;
+            }
+            map.put("projectDuration", null);
+            map.put("notDuration", null);
+            list.add(map);
+        }
+        return list;
+    }
+
+    private void pushDate(ProjectStaffRecord e, List<Map<String, Object>> list, LocalDate recordTime, List<Map<String, Object>> listData) {
+        Map<String,Object> map = new LinkedHashMap<>();
+        map.put("name", e.getName());
+        //找出匹配的日期并设置时间
+        for (Map<String, Object> map2 : list) {
+            Integer date = (Integer) map2.get("date");
+            if (recordTime.getDayOfMonth()==date) {
+                map2.put("projectDuration", e.getDuration());
+                map2.put("notDuration", 8- e.getDuration()>0?8- e.getDuration():0);
+            }
+        }
+        map.put("data", list);
+        listData.add(map);
+    }
+
+
 }

+ 2 - 0
ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysDeptMapper.java

@@ -119,4 +119,6 @@ public interface SysDeptMapper
     List<SysDept> getCompany();
 
     SysDept selectByDepName(@Param("deptName") String deptName, @Param("companyId") Long companyId);
+
+    SysDept selectDeptByPid(Long id);
 }

+ 2 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/ISysDeptService.java

@@ -129,4 +129,6 @@ public interface ISysDeptService
 
 
     SysDept selectByDepName(String deptName, Long company);
+
+    SysDept selectDeptByPid(Long id);
 }

+ 5 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java

@@ -350,6 +350,11 @@ public class SysDeptServiceImpl implements ISysDeptService
         return deptMapper.selectByDepName(deptName, company);
     }
 
+    @Override
+    public SysDept selectDeptByPid(Long id) {
+        return deptMapper.selectDeptByPid(id);
+    }
+
 
     /**
      * 递归列表

+ 9 - 1
ruoyi-system/src/main/resources/mapper/project/ProjectStaffRecordMapper.xml

@@ -323,6 +323,7 @@
           and b.record_time = #{clockDate}
     </select>
 
+
     <update id="mateUserRecord">
         update project_staff_record
         set duration =check_duration,check_status=2
@@ -341,5 +342,12 @@
             #{e.id}
         </foreach>
     </update>
-
+    <select id="getMonthClock" resultMap="BaseResultMap">
+        select
+        <include refid="Base_Column_List" />
+        from project_staff_record
+        where process_status=2
+        and pid = #{id}
+        and record_time between #{startDate} and #{endDate}
+    </select>
 </mapper>

+ 9 - 0
ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml

@@ -117,6 +117,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 		</if>
 	</select>
 
+
     <insert id="insertDept" parameterType="SysDept">
  		insert into sys_dept(
  			<if test="deptId != null and deptId != 0">dept_id,</if>
@@ -202,4 +203,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 		update sys_dept set del_flag = '2' where dept_id = #{deptId}
 	</delete>
 
+
+	<select id="selectDeptByPid" resultMap="SysDeptResult">
+		select c.dept_id,c.dept_name,c.parent_id,c.ancestors,c.order_num,c.leader,c.phone,c.email,c.status,c.create_time,
+		       c.create_by,c.company_id,c.company_name,c.company_logo,c.company_full_name,c.max_duration
+		from project_task a left join sys_user b on a.aid =b.user_id
+							left join sys_dept c on b.dept_id =c.dept_id
+		where a.id= #{id}
+	</select>
 </mapper>