成本分析修改3

This commit is contained in:
liuzongkun999
2025-03-13 00:03:59 +08:00
parent 0ce49d1504
commit 6c14d159d3
10 changed files with 133 additions and 237 deletions

View File

@@ -120,7 +120,7 @@ public class SysOaCostController extends BaseController {
@GetMapping("/{projectId}")
public R<SysOaCostRow> getDetail(@NotNull(message = "项目id不能为空")
@PathVariable Long projectId) {
return R.ok(iSysOaCostService.getDetail(projectId));
return R.ok(iSysOaCostService.calcProjectDetail(projectId));
}
/**

View File

@@ -0,0 +1,12 @@
package com.ruoyi.oa.domain.vo;
import lombok.Data;
// 报销明细VO
@Data
public class ClaimVO {
private Long claimId;
private String nickName;
private Double cost;
private String remark;
}

View File

@@ -0,0 +1,12 @@
package com.ruoyi.oa.domain.vo;
import lombok.Data;
// 物料出库明细VO
@Data
public class MaterialVO {
private Long materialId;
private String name;
private Double price;
private Integer amount;
}

View File

@@ -1,34 +0,0 @@
package com.ruoyi.oa.domain.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SysOaCostList {
/**
* userCostList
*/
private Long userId;
private String nickName;
private Double laborCost;
private Double attendanceNum;
/**
* materialList
*/
private Long materialId;
private String name;
private Double price;
private Long amount;
/**
* claimList
*/
private Long claimId;
// private String nickName; 申请人姓名
private Double cost;
private String remark;
}

View File

@@ -6,8 +6,7 @@ import java.util.List;
@Data
public class SysOaCostRow {
List<SysOaCostList> userCostList;
List<SysOaCostList> materialList;
List<SysOaCostList> claimList;
List<SysOaCostList> otherList;
private List<UserCostVO> userCostList;
private List<MaterialVO> materialList;
private List<ClaimVO> claimList;
}

View File

@@ -0,0 +1,12 @@
package com.ruoyi.oa.domain.vo;
import lombok.Data;
// 人力成本明细VO
@Data
public class UserCostVO {
private Long userId;
private String nickName;
private Double laborCost; // 计算结果字段
private Double attendenceNum; // 计算结果字段
}

View File

@@ -10,6 +10,7 @@ import com.ruoyi.oa.domain.SysOaOutWarehouse;
import com.ruoyi.oa.domain.SysOaProject;
import com.ruoyi.oa.domain.bo.SysOaProjectBo;
import com.ruoyi.oa.domain.vo.SysOaCostAllVo;
import com.ruoyi.oa.domain.vo.SysOaCostRow;
import com.ruoyi.oa.domain.vo.SysOaOutWarehouseListVo;
import com.ruoyi.oa.domain.vo.SysOaProjectVo;
import com.ruoyi.common.core.mapper.BaseMapperPlus;
@@ -17,6 +18,7 @@ import org.apache.ibatis.annotations.Param;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 项目管理Mapper接口
@@ -37,4 +39,6 @@ public interface SysOaProjectMapper extends BaseMapperPlus<SysOaProjectMapper, S
Page<SysOaCostAllVo> selectListToCost(@Param("page") Page<SysOaProject> page,@Param(Constants.WRAPPER) QueryWrapper<SysOaProjectBo> queryWrapper);
Map<String, Object> selectProjectDetails(@Param("projectId") Long projectId);
}

View File

@@ -48,15 +48,6 @@ public interface ISysOaCostService {
*/
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
/**
* 查询所有项目成本
*/
TableDataInfo<SysOaCostAllVo> AllList(String projectName, String createTime);
/**
* 查询详情
*/
SysOaCostRow getDetail(Long projectId);
/**
@@ -68,4 +59,10 @@ public interface ISysOaCostService {
*/
TableDataInfo<SysOaCostAllVo> calcProjectCostList(SysOaCostBo bo, PageQuery pageQuery);
/**
* 项目成本查询详情改进
* @param projectId
* @return
*/
SysOaCostRow calcProjectDetail(Long projectId);
}

View File

@@ -1,6 +1,7 @@
package com.ruoyi.oa.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
@@ -138,196 +139,7 @@ public class SysOaCostServiceImpl implements ISysOaCostService {
return baseMapper.deleteBatchIds(ids) > 0;
}
/**
* 查询所有项目成本
*/
@Override
public TableDataInfo<SysOaCostAllVo> AllList(String projectName, String createTime) {
LambdaQueryWrapper<SysOaProject> lqw = Wrappers.lambdaQuery(SysOaProject.class);
if (StringUtils.isNotBlank(projectName)) {
lqw.like(SysOaProject::getProjectName, projectName);
}
List<SysOaProject> projects = projectMapper.selectList(lqw);
if (projects.isEmpty()) {
return TableDataInfo.build();
}
List<SysOaCostAllVo> sysOaCostAllVos = new ArrayList<>();
for (SysOaProject project : projects) {
SysOaCostAllVo sysOaCostAllVo = new SysOaCostAllVo();
Long projectId = project.getProjectId();
sysOaCostAllVo.setProjectId(projectId);
// 2、 查询所有项目对应的物料
List<SysOaOutWarehouse> outWarehouses = outWarehouseMapper.List(projectId);
Double materialCost = 0.0;
if (!outWarehouses.isEmpty()) {
Map<Long, SysOaWarehouse> warehouseMap = warehouseMapper.selectList(Wrappers.lambdaQuery(SysOaWarehouse.class)).stream().collect(Collectors.toMap(SysOaWarehouse::getId, Function.identity()));
materialCost = outWarehouses.stream().map(v -> {
Long warehouseId = v.getWarehouseId();
BigDecimal amount = BigDecimal.valueOf(v.getAmount());
SysOaWarehouse warehouse = warehouseMap.get(warehouseId);
if (warehouse == null) {
return BigDecimal.ZERO;
}
BigDecimal price = BigDecimal.valueOf(warehouse.getPrice());
return amount.multiply(price);
}).reduce(BigDecimal.ZERO, BigDecimal::add).doubleValue();
}
sysOaCostAllVo.setMaterialCost(materialCost);
// 3、 查询所有项目对应的工资费用
LambdaQueryWrapper<SysOaAttendance> lqw2 = Wrappers.lambdaQuery(SysOaAttendance.class);
lqw2.eq(SysOaAttendance::getProjectId, projectId);
List<SysOaAttendance> attendances = attendanceMapper.selectList(lqw2);
Double userCost = 0.0;
if (!attendances.isEmpty()) {
// 分组出每个用户的天数
Map<Long, Double> daynumMap = attendances.stream().collect(Collectors.groupingBy(
SysOaAttendance::getUserId,
Collectors.summingDouble(attendance -> {
double attendanceDay = Optional.ofNullable(attendance.getAttendanceDay()).orElse(0L);
double dayLength = Optional.ofNullable(attendance.getDayLength()).orElse(0.0);
double hour = Optional.ofNullable(attendance.getHour()).orElse(0.0);
return (attendanceDay * dayLength * 9 + hour)/9;
})
));
// 获取user表中的劳务费用
List<SysUser> users = userMapper.selectList(Wrappers.lambdaQuery(SysUser.class));
Map<Long, SysUser> userMap = users.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
// 计算用工总价
userCost = attendances.stream().map(v -> {
Long userId = v.getUserId();
SysUser user = userMap.get(userId);
if (user == null) {
return BigDecimal.ZERO;
}
Long laborCost = Optional.ofNullable(user.getLaborCost()).orElse(0L);
Double daynum = daynumMap.getOrDefault(userId, 0.0);
BigDecimal laborCostBigDecimal = BigDecimal.valueOf(laborCost);
BigDecimal daynumBigDecimal = BigDecimal.valueOf(daynum);
return laborCostBigDecimal.multiply(daynumBigDecimal);
}).reduce(BigDecimal.ZERO, BigDecimal::add).doubleValue();
}
sysOaCostAllVo.setUserCost(userCost);
// 4、 查询所有项目对应的差旅费用
List<SysOaClaim> claims = claimMapper.List(projectId);
Double claimCost = 0.0;
if (!claims.isEmpty()) {
claimCost = claims.stream().map(v -> BigDecimal.valueOf(v.getCost())).reduce(BigDecimal.ZERO, BigDecimal::add).doubleValue();
}
sysOaCostAllVo.setClaimCost(claimCost);
// 5、 设置总费用
sysOaCostAllVo.setCost(materialCost + userCost + claimCost);
// 6、 将 sysOaCostAllVo 添加到 sysOaCostAllVos 中
sysOaCostAllVos.add(sysOaCostAllVo);
}
return TableDataInfo.build(sysOaCostAllVos);
}
/**
* 查询详情
*/
@Override
public SysOaCostRow getDetail(Long projectId) {
SysOaCostRow sysOaCostRow = new SysOaCostRow();
List<SysOaCostList> UserCostList = new ArrayList<>();
List<SysOaCostList> MaterialList = new ArrayList<>();
List<SysOaCostList> ClaimList = new ArrayList<>();
List<SysOaCostList> OtherList = new ArrayList<>();
// 1、 查询项目信息
SysOaProject project = projectMapper.selectById(projectId);
if (project == null) {
throw new ServiceException("项目信息不存在项目ID: " + projectId);
}
// 2、 查询项目对应的每个人的工资详情
LambdaQueryWrapper<SysOaAttendance> lqw = Wrappers.lambdaQuery(SysOaAttendance.class);
lqw.eq(SysOaAttendance::getProjectId, projectId);
// 查询出该项目下每个用户的工作情况
List<SysOaAttendance> attendances = attendanceMapper.selectList(lqw);
if (!attendances.isEmpty()) {
// 查询出该项目下每个用户的工作天数
// 分组出每个用户的天数
// 按用户 ID 分组,并计算每个用户的 attendance_day * day_length * 9 + hour 的总和
Map<Long, Double> daynumMap = attendances.stream()
.collect(Collectors.groupingBy(
SysOaAttendance::getUserId, // 按用户 ID 分组
Collectors.summingDouble(attendance ->
(attendance.getDayLength() * 9 + attendance.getHour())/9
)
));
List<SysUser> users = userMapper.selectList(Wrappers.lambdaQuery(SysUser.class));
Map<Long, SysUser> userMap = users.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
// 获取user表中的人力成本
// attendances 遍历去重获取每个用户的userid
Set<Long> userIds = daynumMap.keySet();
for (Long userId : userIds) {
SysUser user = userMap.get(userId);
if (user == null) {
continue;
}
SysOaCostList sysOaCostList = new SysOaCostList();
double dayNum = daynumMap.get(userId) == null ? 0.0 : daynumMap.get(userId);
BigDecimal dayNumBigDecimal = new BigDecimal(dayNum);
long laborCost = user.getLaborCost() == null ? 0L : user.getLaborCost();
sysOaCostList.setUserId(userId);
sysOaCostList.setNickName(user.getNickName());
sysOaCostList.setLaborCost(dayNumBigDecimal.multiply(BigDecimal.valueOf(laborCost)).setScale(1, RoundingMode.HALF_UP).doubleValue());
sysOaCostList.setAttendanceNum(dayNumBigDecimal.setScale(1, RoundingMode.HALF_UP).doubleValue());
UserCostList.add(sysOaCostList);
}
}
// 3、 查询项目对应的物料详情
LambdaQueryWrapper<SysOaOutWarehouse> lqw1 = Wrappers.lambdaQuery(SysOaOutWarehouse.class);
lqw1.eq(SysOaOutWarehouse::getProjectId, projectId);
List<SysOaOutWarehouse> outWarehouses = outWarehouseMapper.selectList(lqw1);
//log.info("查询项目对应的物料详情:{}", outWarehouses);
if (!outWarehouses.isEmpty()) {
Map<Long, SysOaWarehouse> warehouseMap = warehouseMapper.selectList(Wrappers.lambdaQuery(SysOaWarehouse.class)).stream().collect(Collectors.toMap(SysOaWarehouse::getId, Function.identity()));
//log.info("查询项目对应的物料详情:{}", warehouseMap);
for (SysOaOutWarehouse outWarehouse : outWarehouses) {
SysOaWarehouse warehouse = warehouseMap.get(outWarehouse.getWarehouseId());
SysOaCostList sysOaCostList = new SysOaCostList();
sysOaCostList.setMaterialId(outWarehouse.getWarehouseId());
sysOaCostList.setName(warehouse.getName());
sysOaCostList.setPrice(warehouse.getPrice());
sysOaCostList.setAmount(outWarehouse.getAmount());
MaterialList.add(sysOaCostList);
}
}
// 4、 查询项目对应的差旅详情
LambdaQueryWrapper<SysOaClaim> lqw2 = Wrappers.lambdaQuery(SysOaClaim.class);
lqw2.eq(SysOaClaim::getProjectId, projectId);
List<SysOaClaimVo> claims = claimMapper.selectVoList(lqw2);
Map<Long, SysOaClaimVo> claimMap = claims.stream().collect(Collectors.toMap(SysOaClaimVo::getClaimId, Function.identity()));
Set<Long> claimIds = claimMap.keySet();
if (!claims.isEmpty()) {
for (Long claimId : claimIds) {
SysOaCostList sysOaCostList = new SysOaCostList();
SysOaClaimVo claim = claimMap.get(claimId);
sysOaCostList.setClaimId(claim.getClaimId());
sysOaCostList.setNickName(userMapper.selectUserById(claim.getUserId()).getNickName());
sysOaCostList.setCost(claim.getCost());
sysOaCostList.setRemark(claim.getRemark());
ClaimList.add(sysOaCostList);
}
}
sysOaCostRow.setUserCostList(UserCostList);
sysOaCostRow.setMaterialList(MaterialList);
sysOaCostRow.setClaimList(ClaimList);
sysOaCostRow.setOtherList(OtherList);
return sysOaCostRow;
}
@Override
public TableDataInfo<SysOaCostAllVo> calcProjectCostList(SysOaCostBo bo, PageQuery pageQuery) {
@@ -339,4 +151,14 @@ public class SysOaCostServiceImpl implements ISysOaCostService {
// 查询user laborCost 查attendance 拿签到
return TableDataInfo.build(sysOaCostAllVos);
}
/**
* 项目成本查询详情改进
*/
@Override
public SysOaCostRow calcProjectDetail(Long projectId) {
Map<String, Object> result = projectMapper.selectProjectDetails(projectId);
String jsonData = (String) result.get("data");
return JSON.parseObject(jsonData, SysOaCostRow.class);
}
}

View File

@@ -150,4 +150,76 @@
</select>
<select id="selectProjectDetails" parameterType="Long" resultType="java.util.Map">
SELECT
JSON_OBJECT(
'userCostList', COALESCE((
SELECT JSON_ARRAYAGG(
JSON_OBJECT(
'userId', CAST(uc.userId AS CHAR),
'nickName', COALESCE(uc.nickName, ''),
'laborCost', ROUND(COALESCE(uc.laborCost, 0),1),
'attendenceNum', ROUND(COALESCE(uc.attendenceNum, 0),1)
)
)
FROM (
SELECT
a.user_id AS userId,
u.nick_name AS nickName,
COALESCE(SUM(ROUND((a.day_length * 9 + a.hour) / 9,1) * u.labor_cost), 0) AS laborCost,
COALESCE(ROUND(SUM((a.day_length * 9 + a.hour) / 9),1), 0) AS attendenceNum
FROM sys_oa_attendance a
INNER JOIN sys_user u ON a.user_id = u.user_id
WHERE a.project_id = #{projectId} AND a.del_flag = 0
GROUP BY a.user_id
) uc
), JSON_ARRAY()),
'materialList', COALESCE((
SELECT JSON_ARRAYAGG(
JSON_OBJECT(
'materialId', CAST(ml.materialId AS CHAR),
'name', COALESCE(ml.name, ''),
'price', COALESCE(ml.price, 0),
'amount', COALESCE(ml.amount, 0)
)
)
FROM (
SELECT
ow.id AS materialId,
COALESCE(w.name, '') AS name,
COALESCE(w.price, 0) AS price,
COALESCE(ow.amount, 0) AS amount
FROM sys_oa_out_warehouse ow
INNER JOIN sys_oa_warehouse w ON ow.warehouse_id = w.id
WHERE ow.project_id = #{projectId} AND ow.del_flag = 0
) ml
), JSON_ARRAY()),
'claimList', COALESCE((
SELECT JSON_ARRAYAGG(
JSON_OBJECT(
'claimId', CAST(cl.claimId AS CHAR),
'nickName', COALESCE(cl.nickName, ''),
'cost', COALESCE(cl.cost, 0),
'remark', COALESCE(cl.remark, '')
)
)
FROM (
SELECT
c.claim_id AS claimId,
COALESCE(u.nick_name, '') AS nickName,
COALESCE(c.cost, 0) AS cost,
COALESCE(c.remark, '') AS remark
FROM sys_oa_claim c
INNER JOIN sys_user u ON c.user_id = u.user_id
WHERE c.project_id = #{projectId} AND c.del_flag = 0
) cl
), JSON_ARRAY())
) AS data
FROM sys_oa_project p
WHERE p.project_id = #{projectId}
</select>
</mapper>