Browse Source

部门4 增删改

Reiskuchen 5 years ago
parent
commit
d04f18fff8

+ 36 - 2
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/DepartmentController.java

@@ -5,7 +5,7 @@ import com.management.platform.service.DepartmentService;
 import com.management.platform.util.HttpRespMsg;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestMapping;
-
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.servlet.http.HttpServletRequest;
@@ -25,9 +25,43 @@ public class DepartmentController {
     @Autowired
     private DepartmentService departmentService;
 
+    /**
+     * 获取带有层级的部门列表
+     */
     @RequestMapping("/list")
-    public HttpRespMsg getDepartment(HttpServletRequest request){
+    public HttpRespMsg getDepartment(HttpServletRequest request) {
         return departmentService.getDepartmentList(request);
     }
+
+    /**
+     * 新增部门
+     * name 部门名称
+     * parentId 父级部门id
+     */
+    @RequestMapping("/add")
+    public HttpRespMsg insertDepartment(@RequestParam String name, Integer parentId, HttpServletRequest request) {
+        return departmentService.insertDepartment(name, parentId, request);
+    }
+
+    /**
+     * 修改部门
+     * id 部门id
+     * name 部门名称
+     * parentId 父级部门id
+     */
+    @RequestMapping("/edit")
+    public HttpRespMsg updateDepartment(@RequestParam Integer id, @RequestParam String name, Integer parentId,
+                                        HttpServletRequest request) {
+        return departmentService.updateDepartment(id, name, parentId, request);
+    }
+
+    /**
+     * 删除部门
+     * id 部门id
+     */
+    @RequestMapping("/delete")
+    public HttpRespMsg deleteDepartment(@RequestParam Integer id, HttpServletRequest request) {
+        return departmentService.deleteDepartment(id, request);
+    }
 }
 

+ 3 - 2
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/UserController.java

@@ -59,12 +59,13 @@ public class UserController {
 
     /**
      * 获取员工的列表
+     * departmentId 检索的部门id
      * pageIndex 页面索引
      * pageSize 页面大小
      */
     @RequestMapping("/getEmployeeList")
-    public HttpRespMsg getEmployeeList(@RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
-        return userService.getEmployeeList(pageIndex, pageSize, request);
+    public HttpRespMsg getEmployeeList(Integer departmentId, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
+        return userService.getEmployeeList(departmentId, pageIndex, pageSize, request);
     }
 
     /**

+ 2 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/entity/Department.java

@@ -1,5 +1,6 @@
 package com.management.platform.entity;
 
+import com.baomidou.mybatisplus.annotation.FieldStrategy;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
@@ -40,7 +41,7 @@ public class Department extends Model<Department> {
     /**
      * 上级部门id
      */
-    @TableField("superior_id")
+    @TableField(value = "superior_id", updateStrategy = FieldStrategy.IGNORED)
     private Integer superiorId;
 
     /**

+ 9 - 9
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/entity/User.java

@@ -1,20 +1,20 @@
 package com.management.platform.entity;
 
-import java.math.BigDecimal;
-
 import com.baomidou.mybatisplus.annotation.FieldStrategy;
-import com.baomidou.mybatisplus.extension.activerecord.Model;
-import com.baomidou.mybatisplus.annotation.TableId;
-import java.time.LocalDateTime;
 import com.baomidou.mybatisplus.annotation.TableField;
-import java.io.Serializable;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.extension.activerecord.Model;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.experimental.Accessors;
 
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.time.LocalDateTime;
+
 /**
  * <p>
- * 
+ *
  * </p>
  *
  * @author 吴涛涛
@@ -25,7 +25,7 @@ import lombok.experimental.Accessors;
 @Accessors(chain = true)
 public class User extends Model<User> {
 
-    private static final long serialVersionUID=1L;
+    private static final long serialVersionUID = 1L;
 
     /**
      * 主键 雪花算法生成
@@ -84,7 +84,7 @@ public class User extends Model<User> {
     /**
      * 部门表外键
      */
-    @TableField("department_id")
+    @TableField(value = "department_id", strategy = FieldStrategy.IGNORED)
     private Integer departmentId;
 
 

+ 7 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/DepartmentService.java

@@ -15,5 +15,12 @@ import javax.servlet.http.HttpServletRequest;
  * @since 2020-02-11
  */
 public interface DepartmentService extends IService<Department> {
+    HttpRespMsg insertDepartment(String departmentName, Integer superiorId, HttpServletRequest request);
+
+    HttpRespMsg updateDepartment(Integer departmentId, String departmentName, Integer superiorId,
+                                 HttpServletRequest request);
+
+    HttpRespMsg deleteDepartment(Integer departmentId, HttpServletRequest request);
+
     HttpRespMsg getDepartmentList(HttpServletRequest request);
 }

+ 1 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/UserService.java

@@ -22,7 +22,7 @@ public interface UserService extends IService<User> {
 
     HttpRespMsg getUserInfo(String id);
 
-    HttpRespMsg getEmployeeList(Integer pageIndex, Integer pageSize, HttpServletRequest request);
+    HttpRespMsg getEmployeeList(Integer departmentId, Integer pageIndex, Integer pageSize, HttpServletRequest request);
 
     HttpRespMsg deleteUser(String userId, HttpServletRequest request);
 

+ 87 - 2
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/DepartmentServiceImpl.java

@@ -32,6 +32,91 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
     @Resource
     private DepartmentMapper departmentMapper;
 
+    //新增部门
+    @Override
+    public HttpRespMsg insertDepartment(String departmentName, Integer superiorId, HttpServletRequest request) {
+        HttpRespMsg httpRespMsg = new HttpRespMsg();
+        try {
+            Integer companyId = userMapper.selectById(request.getHeader("Token")).getCompanyId();
+            if (superiorId == null || departmentMapper.selectCount(new QueryWrapper<Department>()
+                    .eq("department_id", superiorId)
+                    .eq("company_id", companyId)) == 1) {
+                Department department = new Department()
+                        .setDepartmentName(departmentName)
+                        .setSuperiorId(superiorId)
+                        .setCompanyId(companyId);
+                if (departmentMapper.insert(department) == 0) {
+                    httpRespMsg.setError("新增失败");
+                }
+            } else {
+                httpRespMsg.setError("无所选父级部门");
+            }
+        } catch (NullPointerException e) {
+            httpRespMsg.setError("验证失败");
+            return httpRespMsg;
+        }
+        return httpRespMsg;
+    }
+
+    //更新部门
+    @Override
+    public HttpRespMsg updateDepartment(Integer departmentId, String departmentName, Integer superiorId,
+                                        HttpServletRequest request) {
+        HttpRespMsg httpRespMsg = new HttpRespMsg();
+        try {
+            Integer companyId = userMapper.selectById(request.getHeader("Token")).getCompanyId();
+            Department department = departmentMapper.selectById(departmentId);
+            if (!department.getCompanyId().equals(companyId)) {
+                httpRespMsg.setError("不能修改其他公司的部门");
+            } else if (!department.getSuperiorId().equals(superiorId) && checkBranch(departmentId)) {
+                httpRespMsg.setError("不能在有子级部门时修改父级部门");
+            } else if (superiorId != null && departmentMapper.selectCount(new QueryWrapper<Department>()
+                    .eq("department_id", superiorId)
+                    .eq("company_id", companyId)) == 0) {
+                httpRespMsg.setError("无所选父级部门");
+            } else {
+                if (departmentMapper.updateById(department
+                        .setDepartmentName(departmentName)
+                        .setSuperiorId(superiorId)) == 0) {
+                    httpRespMsg.setError("修改失败");
+                }
+            }
+        } catch (NullPointerException e) {
+            httpRespMsg.setError("验证失败");
+            return httpRespMsg;
+        }
+        return httpRespMsg;
+    }
+
+    //删除部门
+    @Override
+    public HttpRespMsg deleteDepartment(Integer departmentId, HttpServletRequest request) {
+        HttpRespMsg httpRespMsg = new HttpRespMsg();
+        try {
+            Integer companyId = userMapper.selectById(request.getHeader("Token")).getCompanyId();
+            Department department = departmentMapper.selectById(departmentId);
+            if (!department.getCompanyId().equals(companyId)) {
+                httpRespMsg.setError("不能删除其他公司的部门");
+            } else if (checkBranch(departmentId)) {
+                httpRespMsg.setError("不能在有子级部门时删除部门");
+            } else {
+                if (departmentMapper.deleteById(department) == 0) {
+                    httpRespMsg.setError("修改失败");
+                }
+            }
+        } catch (NullPointerException e) {
+            httpRespMsg.setError("验证失败");
+            return httpRespMsg;
+        }
+        return httpRespMsg;
+    }
+
+    //查看有无子节点
+    private boolean checkBranch(Integer id) {
+        return departmentMapper.selectCount(new QueryWrapper<Department>().eq("superior_id", id)) > 0;
+    }
+
+    //获取部门列表
     @Override
     public HttpRespMsg getDepartmentList(HttpServletRequest request) {
         HttpRespMsg httpRespMsg = new HttpRespMsg();
@@ -91,11 +176,11 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
                 if (superiorId != null) {
                     //有父部门的话 那么下方很可能也是相同的父部门 所以要把类似的它们存入最终栈中
                     stack3.push(first);
-                    //首先把栈中所有相同父部门的部门存入最终栈
+                    //首先把栈中所有相同父部门的部门存入最终栈
                     while (stack2.peek().getParentId() != null && stack2.peek().getParentId().equals(superiorId)) {
                         stack3.push(stack2.pop());
                     }
-                    //首先把栈中所有相同父部门的部门存入最终栈
+                    //首先把栈中所有相同父部门的部门存入最终栈
                     while (stack1.peek().getParentId() != null && stack1.peek().getParentId().equals(superiorId)) {
                         stack3.push(stack1.pop());
                     }

+ 21 - 6
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/TimeCalculationServiceImpl.java

@@ -169,20 +169,35 @@ public class TimeCalculationServiceImpl extends ServiceImpl<TimeCalculationMappe
                 dataMap.put("date", date);
                 List<Map<String, Object>> list = new ArrayList<>();
                 //遍历所有时间记录
-                for (TimeCalculationShow timeCalculation : dataList) {
+                for (int i = 0; i < dataList.size(); i++) {
+                    TimeCalculationShow time = dataList.get(i);
                     //如果这一天就是我们需要的那天的话
-                    if (timeCalculation.getDate().toString().equals(date)) {
+                    if (time.getDate().toString().equals(date)) {
                         Map<String, Object> map = new HashMap<>();
                         //把这一天的开始时间和结束时间以小时:分钟的字符串形式以及段时间的长度返回
-                        map.put("startTime", timeCalculation.getStartTime().format(DateTimeFormatter.ofPattern("HH:mm")));
-                        map.put("endTime", timeCalculation.getEndTime().format(DateTimeFormatter.ofPattern("HH:mm")));
-                        Integer todayTime = timeCalculation.getDuration();
+                        map.put("startTime", time.getStartTime().format(DateTimeFormatter.ofPattern("HH:mm")));
+                        map.put("endTime", time.getEndTime().format(DateTimeFormatter.ofPattern("HH:mm")));
+                        Integer todayTime = time.getDuration();
                         map.put("duration", convertSecond(todayTime));
                         total += todayTime;
                         list.add(map);
+                        dataList.remove(i--);
                     }
-                    /*后续可以用迭代器把重复数据删掉*/
                 }
+                /*上为优化后的遍历 如果测试无误就可以继续用了*/
+//                for (TimeCalculationShow timeCalculation : dataList) {
+//                    //如果这一天就是我们需要的那天的话
+//                    if (timeCalculation.getDate().toString().equals(date)) {
+//                        Map<String, Object> map = new HashMap<>();
+//                        //把这一天的开始时间和结束时间以小时:分钟的字符串形式以及段时间的长度返回
+//                        map.put("startTime", timeCalculation.getStartTime().format(DateTimeFormatter.ofPattern("HH:mm")));
+//                        map.put("endTime", timeCalculation.getEndTime().format(DateTimeFormatter.ofPattern("HH:mm")));
+//                        Integer todayTime = timeCalculation.getDuration();
+//                        map.put("duration", convertSecond(todayTime));
+//                        total += todayTime;
+//                        list.add(map);
+//                    }
+//                }
                 dataMap.put("time", list);
                 //这里检查如果只需要一天记录的话 说明是pc端界面那个地方 那就再多计算一个当天总工作时间
                 if (dateList.size() == 1) {

+ 1 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/UserServiceImpl.java

@@ -152,7 +152,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
 
     //获取员工的列表
     @Override
-    public HttpRespMsg getEmployeeList(Integer pageIndex, Integer pageSize, HttpServletRequest request) {
+    public HttpRespMsg getEmployeeList(Integer departmentId, Integer pageIndex, Integer pageSize, HttpServletRequest request) {
         HttpRespMsg httpRespMsg = new HttpRespMsg();
         try {
             User requester = userMapper.selectById(request.getHeader("Token"));