From 68021616474066ab1beb4114bfaba8f2ad696cb3 Mon Sep 17 00:00:00 2001 From: zhc077 <565291854> Date: Thu, 2 Nov 2023 11:18:01 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=B8=AA=E4=BA=BA=E6=80=BB=E7=A7=AF?= =?UTF-8?q?=E5=88=86=E5=8A=9F=E8=83=BD=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/jeecg/common/util/ComputeUtils.java | 76 +++++++++++++++++++ .../IPersonalCompTotalScoreService.java | 4 + .../impl/PersonalCompScoreServiceImpl.java | 57 ++++++++------ .../PersonalCompTotalScoreServiceImpl.java | 36 ++++++++- 4 files changed, 148 insertions(+), 25 deletions(-) create mode 100644 jeecg-boot-master/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/ComputeUtils.java diff --git a/jeecg-boot-master/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/ComputeUtils.java b/jeecg-boot-master/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/ComputeUtils.java new file mode 100644 index 00000000..6ef35b4b --- /dev/null +++ b/jeecg-boot-master/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/ComputeUtils.java @@ -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))); + } +} diff --git a/jeecg-boot-master/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/annualScore/service/IPersonalCompTotalScoreService.java b/jeecg-boot-master/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/annualScore/service/IPersonalCompTotalScoreService.java index 4077f92f..795a426d 100644 --- a/jeecg-boot-master/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/annualScore/service/IPersonalCompTotalScoreService.java +++ b/jeecg-boot-master/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/annualScore/service/IPersonalCompTotalScoreService.java @@ -1,8 +1,11 @@ package org.jeecg.modules.demo.annualScore.service; +import org.jeecg.modules.demo.annualScore.entity.PersonalCompScore; import org.jeecg.modules.demo.annualScore.entity.PersonalCompTotalScore; import com.baomidou.mybatisplus.extension.service.IService; +import java.util.List; + /** * @Description: 个人总积分 * @Author: jeecg-boot @@ -11,4 +14,5 @@ import com.baomidou.mybatisplus.extension.service.IService; */ public interface IPersonalCompTotalScoreService extends IService { + void saveOrUpdateTotalScore(List saveList); } diff --git a/jeecg-boot-master/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/annualScore/service/impl/PersonalCompScoreServiceImpl.java b/jeecg-boot-master/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/annualScore/service/impl/PersonalCompScoreServiceImpl.java index 8bab7b9f..e298bc4d 100644 --- a/jeecg-boot-master/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/annualScore/service/impl/PersonalCompScoreServiceImpl.java +++ b/jeecg-boot-master/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/annualScore/service/impl/PersonalCompScoreServiceImpl.java @@ -3,11 +3,13 @@ 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.apache.commons.lang3.StringUtils; +import org.jeecg.common.util.ComputeUtils; import org.jeecg.modules.demo.annualCompPoint.entity.AnnualCompPoint; import org.jeecg.modules.demo.annualCompPoint.service.IAnnualCompPointService; import org.jeecg.modules.demo.annualScore.entity.PersonalCompScore; import org.jeecg.modules.demo.annualScore.mapper.PersonalCompScoreMapper; import org.jeecg.modules.demo.annualScore.service.IPersonalCompScoreService; +import org.jeecg.modules.demo.annualScore.service.IPersonalCompTotalScoreService; import org.jeecg.modules.demo.annualcomp.entity.AnnualComp; import org.jeecg.modules.demo.annualcomp.service.IAnnualCompService; import org.jeecg.modules.demo.annualcompaward.entity.AnnualCompAward; @@ -22,9 +24,9 @@ import org.jeecg.modules.system.service.ISysDepartService; import org.jeecg.modules.system.service.ISysUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.ObjectUtils; -import java.math.BigDecimal; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -57,12 +59,16 @@ public class PersonalCompScoreServiceImpl extends ServiceImpl score = new AtomicReference<>((double) 0); if (StringUtils.equals("Y", entryForm)) { //团队赛 Optional.ofNullable(managementList).orElse(new ArrayList<>()).forEach(e -> { // 参与形式是团队时,队长积分*0.5,职员积分*0.2 //captain 是否队长,自关联表如果是队长则是为0.如果不是存储队长学号 if (StringUtils.equals("0", e.getCaptain())) { - score.set(mul(baseScore, ZERO_POINT_FIVE)); + score.set(ComputeUtils.mul(baseScore, ZERO_POINT_FIVE)); } else { - score.set(mul(baseScore, ZERO_POINT_TWO)); + score.set(ComputeUtils.mul(baseScore, ZERO_POINT_TWO)); } PersonalCompScore bean = new PersonalCompScore(); @@ -166,6 +172,11 @@ public class PersonalCompScoreServiceImpl extends ServiceImpl implements IPersonalCompTotalScoreService { + @Override + @Transactional(rollbackFor = Exception.class) + public void saveOrUpdateTotalScore(List list) { + + List updateList = new LinkedList<>(); + Optional.ofNullable(list).orElse(new LinkedList<>()).forEach(e -> { + PersonalCompTotalScore p = this.getOne(new LambdaQueryWrapper() + .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); + } }