parent
c989748ec5
commit
6802161647
4 changed files with 148 additions and 25 deletions
@ -0,0 +1,76 @@ |
||||
package org.jeecg.common.util; |
||||
|
||||
import java.math.BigDecimal; |
||||
|
||||
public class ComputeUtils { |
||||
|
||||
//默认除法运算精度
|
||||
private static final int DEF_DIV_SCALE = 10; |
||||
|
||||
//构造器私有化,让这个类不能实例化
|
||||
private ComputeUtils() { |
||||
} |
||||
|
||||
/** |
||||
*提供精确的加法运算 |
||||
* @param v1 |
||||
* @param v2 |
||||
* @return |
||||
*/ |
||||
public static double add(double v1, double v2) { |
||||
BigDecimal b1 = BigDecimal.valueOf(v1); |
||||
BigDecimal b2 = BigDecimal.valueOf(v2); |
||||
return b1.add(b2).doubleValue(); |
||||
} |
||||
|
||||
/** |
||||
*精确的减法运算 |
||||
* @param v1 |
||||
* @param v2 |
||||
* @return |
||||
*/ |
||||
public static double sub(double v1, double v2) { |
||||
BigDecimal b1 = BigDecimal.valueOf(v1); |
||||
BigDecimal b2 = BigDecimal.valueOf(v2); |
||||
return b1.subtract(b2).doubleValue(); |
||||
} |
||||
|
||||
/** |
||||
* 精确的乘法运算 |
||||
* @param v1 |
||||
* @param v2 |
||||
* @return |
||||
*/ |
||||
public static double mul(double v1, double v2) { |
||||
BigDecimal b1 = BigDecimal.valueOf(v1); |
||||
BigDecimal b2 = BigDecimal.valueOf(v2); |
||||
return b1.multiply(b2).doubleValue(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
//提供(相对)精确的除法运算,当发生除不尽的情况时
|
||||
//精确到小数点后10位的数字四舍五入
|
||||
* @param v1 分子 |
||||
* @param v2 分母 |
||||
* @return |
||||
*/ |
||||
public static double div(double v1, double v2) { |
||||
BigDecimal b1 = BigDecimal.valueOf(v1); |
||||
BigDecimal b2 = BigDecimal.valueOf(v2); |
||||
return b1.divide(b2, DEF_DIV_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue(); |
||||
} |
||||
|
||||
public static double div2(double v1, double v2) { |
||||
BigDecimal b1 = BigDecimal.valueOf(v1); |
||||
BigDecimal b2 = BigDecimal.valueOf(v2); |
||||
return b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP).doubleValue(); |
||||
} |
||||
|
||||
public static void main(String[] args) { |
||||
// System.out.println("args = " + div(39,64));
|
||||
System.out.println("args2 = " + div2(39,64)); |
||||
|
||||
System.out.println("args = " + mul(div2(39,64),new Double(2))); |
||||
} |
||||
} |
@ -1,19 +1,51 @@ |
||||
package org.jeecg.modules.demo.annualScore.service.impl; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
import org.jeecg.common.util.ComputeUtils; |
||||
import org.jeecg.modules.demo.annualScore.entity.PersonalCompScore; |
||||
import org.jeecg.modules.demo.annualScore.entity.PersonalCompTotalScore; |
||||
import org.jeecg.modules.demo.annualScore.mapper.PersonalCompTotalScoreMapper; |
||||
import org.jeecg.modules.demo.annualScore.service.IPersonalCompTotalScoreService; |
||||
import org.springframework.beans.BeanUtils; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.transaction.annotation.Transactional; |
||||
import org.springframework.util.ObjectUtils; |
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
import java.util.Date; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.Optional; |
||||
|
||||
/** |
||||
* @Description: 个人总积分 |
||||
* @Author: jeecg-boot |
||||
* @Date: 2023-11-01 |
||||
* @Date: 2023-11-01 |
||||
* @Version: V1.0 |
||||
*/ |
||||
@Service |
||||
public class PersonalCompTotalScoreServiceImpl extends ServiceImpl<PersonalCompTotalScoreMapper, PersonalCompTotalScore> implements IPersonalCompTotalScoreService { |
||||
|
||||
@Override |
||||
@Transactional(rollbackFor = Exception.class) |
||||
public void saveOrUpdateTotalScore(List<PersonalCompScore> list) { |
||||
|
||||
List<PersonalCompTotalScore> updateList = new LinkedList<>(); |
||||
Optional.ofNullable(list).orElse(new LinkedList<>()).forEach(e -> { |
||||
PersonalCompTotalScore p = this.getOne(new LambdaQueryWrapper<PersonalCompTotalScore>() |
||||
.eq(PersonalCompTotalScore::getWorkOn, e.getWorkOn()) |
||||
.apply("limit 1")); |
||||
//为空则为首次累计,新增操作
|
||||
if ((ObjectUtils.isEmpty(p))) { |
||||
PersonalCompTotalScore p2 = new PersonalCompTotalScore(); |
||||
BeanUtils.copyProperties(e, p2); |
||||
updateList.add(p2); |
||||
} else { //更新操作
|
||||
p.setScore(ComputeUtils.add(p.getScore(), e.getScore())); |
||||
p.setUpdateTime(new Date()); |
||||
updateList.add(p); |
||||
} |
||||
}); |
||||
this.saveOrUpdateBatch(updateList); |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue