Commit 0dfa1e45 authored by 刘_震's avatar 刘_震

工资-后端接口代码的实现

parent 085f8824
...@@ -3,6 +3,7 @@ package com.ruoyi.system.controller; ...@@ -3,6 +3,7 @@ package com.ruoyi.system.controller;
import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.model.wages.dto.WagesFormulaListDto; import com.ruoyi.system.model.wages.dto.WagesFormulaListDto;
import com.ruoyi.system.service.WagesCalculateConversionService; import com.ruoyi.system.service.WagesCalculateConversionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -21,7 +22,7 @@ public class WagesCalculateConversionController { ...@@ -21,7 +22,7 @@ public class WagesCalculateConversionController {
* 唐山矿转换表 * 唐山矿转换表
*/ */
@RequestMapping("/surface") @RequestMapping("/surface")
public AjaxResult conversionSurface(@RequestParam("")WagesFormulaListDto wagesFormulaListDto) { public AjaxResult conversionSurface( @RequestBody WagesFormulaListDto wagesFormulaListDto) {
return new AjaxResult(200, "成功", wagesCalculateConversionService.conversionSurface(wagesFormulaListDto)); return new AjaxResult(200, "成功", wagesCalculateConversionService.conversionSurface(wagesFormulaListDto));
} }
} }
package com.ruoyi.system.mapper;
import java.util.List;
/**
* @author haiwe
* @date 2024/5/22
*/
public interface WagesCalculateConversionMapper {
List<Object> select(String year);
}
package com.ruoyi.system.service.impl; package com.ruoyi.system.service.impl;
import cn.hutool.core.util.ReUtil;
import com.ruoyi.system.mapper.WagesCalculateConversionMapper;
import com.ruoyi.system.model.wages.dto.WagesFormulaDto;
import com.ruoyi.system.model.wages.dto.WagesFormulaListDto; import com.ruoyi.system.model.wages.dto.WagesFormulaListDto;
import com.ruoyi.system.model.wages.vo.WagesFormulaListVo;
import com.ruoyi.system.model.wages.vo.WagesFormulaVo;
import com.ruoyi.system.service.WagesCalculateConversionService; import com.ruoyi.system.service.WagesCalculateConversionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
/** /**
* 2024/5/21 * 2024/5/21
*/ */
@Service @Service
@RequiredArgsConstructor
public class WagesCalculateConversionServiceImpl implements WagesCalculateConversionService { public class WagesCalculateConversionServiceImpl implements WagesCalculateConversionService {
private final WagesCalculateConversionMapper wagesCalculateConversionMapper;
@Override @Override
public Object conversionSurface(WagesFormulaListDto wagesFormulaListDto) { public Object conversionSurface(WagesFormulaListDto wagesFormulaListDto) {
return null; WagesFormulaListVo listVo = new WagesFormulaListVo();
List<WagesFormulaVo> wagesFormulaVoList = new ArrayList<>();
List<WagesFormulaDto> wagesFormulaDtoList = wagesFormulaListDto.getWagesFormulaDtoList();
//获取传过来的值(date - 格式为:xxxx-MM)
String date = wagesFormulaListDto.getDate();
String[] parts = date.split("-");
String year = parts[0];
//根据时间去数据库中查询,得到每一条的list集合
List<Object> wagesList = wagesCalculateConversionMapper.select(year);
Object dep = wagesList.get(3);
double result = calculateFormula(wagesList, wagesFormulaDtoList);
// 设置部门信息
listVo.setDep(String.valueOf(dep));
// 构建WagesFormulaVo对象并添加到列表中
for (WagesFormulaDto wagesFormulaDto : wagesFormulaDtoList) {
WagesFormulaVo formulaVo = new WagesFormulaVo();
formulaVo.setLabel(wagesFormulaDto.getLabel());
formulaVo.setC(wagesFormulaDto.getC());
formulaVo.setValue(String.valueOf(result));
wagesFormulaVoList.add(formulaVo);
}
return listVo;
}
private double calculateFormula(List<Object> wagesList, List<WagesFormulaDto> wagesFormulaDtoList) {
double result = 0.0;
for (WagesFormulaDto wagesFormulaDto : wagesFormulaDtoList) {
String value = wagesFormulaDto.getValue();
String formula = extractOperands(value);
double operand = evaluateExpression(wagesList, formula);
result += operand;
}
return result;
}
private double evaluateExpression(List<Object> wagesList, String formula) {
String[] tokens = formula.split(" ");
double result = Double.parseDouble(tokens[0]);
for (int i = 1; i < tokens.length - 1; i += 2) {
String operator = tokens[i];
double operand = Double.parseDouble(tokens[i + 1]);
switch (operator) {
case "+":
result += operand;
break;
case "-":
result -= operand;
break;
case "*":
result *= operand;
break;
case "/":
result /= operand;
break;
default:
break;
}
}
return result;
}
private String extractOperands(String value) {
List<String> operands = new ArrayList<>();
StringBuilder currentOperand = new StringBuilder();
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (Character.isDigit(c) || c == '.') {
currentOperand.append(c);
} else if (isOperator(c)) {
String operand = currentOperand.toString().trim();
if (!operand.isEmpty()) {
operands.add(operand);
}
currentOperand = new StringBuilder();
operands.add(String.valueOf(c));
}
}
String lastOperand = currentOperand.toString().trim();
if (!lastOperand.isEmpty()) {
operands.add(lastOperand);
}
String result = String.join(" ", operands);
return result;
}
private boolean isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
} }
} }
<?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="com.ruoyi.system.mapper.WagesCalculateConversionMapper">
<select id="select" resultType="java.lang.Object" parameterType="java.lang.String">
select * from wages_details_original_data where year = #{year}
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment