第2-4-8章 规则引擎Drools实战(1)-个人所得税计算器

news/2024/4/29 9:56:20/文章来源:https://blog.csdn.net/weixin_42208775/article/details/128090740

文章目录

    • 9. Drools实战
      • 9.1 个人所得税计算器
        • 9.1.1 名词解释
        • 9.1.2 计算规则
          • 9.1.2.1 新税制主要有哪些变化?
          • 9.1.2.2 资较高人员本次个税较少,可能到年底扣税增加?
          • 9.1.2.3 关于年度汇算清缴
          • 9.1.2.4 个人所得税预扣率表(居民个人工资、薪金所得预扣预缴适用)
          • 9.1.2.5 五险一金缴费比例
        • 9.1.3 实现步骤
          • 9.1.3.1 创建maven工程calculation并配置pom.xml文件
          • 9.1.3.2 创建/resources/application.yml文件
          • 9.1.3.3 编写配置类DroolsConfig
          • 9.1.3.4 编写各种实体类
          • 9.1.3.5 在resources/rules下创建规则文件calculation.drl文件
          • 9.1.3.6 创建RuleServiceImpl
          • 9.1.3.7 创建RuleController
          • 9.1.3.8 创建启动类DroolsApplication

9. Drools实战

全套代码及资料全部完整提供,点此处下载

9.1 个人所得税计算器

本小节我们需要通过Drools规则引擎来根据规则计算个人所得税,最终数据效果如下:

在这里插入图片描述

9.1.1 名词解释

税前月收入:即税前工资,指交纳个人所得税之前的总工资

应纳税所得额:指按照税法规定确定纳税人在一定期间所获得的所有应税收入减除在该纳税期间依法允许减除的各种支出后的余额

税率:是对征税对象的征收比例或征收额度

速算扣除数:指为解决超额累进税率分级计算税额的复杂技术问题,而预先计算出的一个数据,可以简化计算过程

扣税额:是指实际缴纳的税额

税后工资:是指扣完税后实际到手的工资收入

9.1.2 计算规则

要实现个人所得税计算器,需要了解2019个税扣缴变化:

应纳税所得额=(月收入-五险一金-起征点-依法确定的其他扣除-专项附加扣除)*适用税率

9.1.2.1 新税制主要有哪些变化?
  1. 扣缴义务人在支付居民个人工资、薪金所得时,需按照“累计预扣法”核算预扣个人所得税。较之前按月的税率表变化成按年计税,适用个人所得税预扣率表
  2. 首次设立了子女教育、继续教育、大病医疗、住房贷款利息或住房租金、赡养老人六项专项附加扣除。(在税前工资扣除相应额度后核算个税)
9.1.2.2 资较高人员本次个税较少,可能到年底扣税增加?

〔例1〕某职员2015年入职,2019年每月应发工资均为10000元,每月减除费用5000元,“三险一金”等专项扣除为1500元,从1月起享受子女教育专项附加扣除1000元,没有减免收入及减免税额等情况,以前三个月为例,应当按照以下方法计算预扣预缴税额:

1月份:(10000-5000-1500-1000)×3% =75元;

2月份:(10000×2-5000×2-1500×2-1000×2)×3%-75 =75元;

3月份:(10000×3-5000×3-1500×3-1000×3)×3%-75-75 =75元;

进一步计算可知,该纳税人全年累计预扣预缴应纳税所得额为30000元,一直适用3%的税率,因此各月应预扣预缴的税款相同

〔例2〕某职员2015年入职,2019年每月应发工资均为30000元,每月减除费用5000元,“三险一金”等专项扣除为4500元,享受子女教育、赡养老人两项专项附加扣除共计2000元,没有减免收入及减免税额等情况,以前三个月为例,应当按照以下方法计算各月应预扣预缴税额:

1月份:(30000–5000-4500-2000)×3% = 555元;

2月份:(30000×2-5000×2-4500×2-2000×2)×10% -2520 -555 =625元;

3月份:(30000×3-5000×3-4500×3-2000×3)×10% -2520 -555-625 =1850元;

按照上述算法,每一个月应纳个税额:

项目1月2月3月4月5月6月7月8月9月10月11月12月
每月税前工资300003000030000300003000030000300003000030000300003000030000
扣除各项减免额后应对税率3%10%10%10%10%10%10%20%20%20%20%20%
月应预扣预缴个税5556251850185018501850185022503700370037003700

上述计算结果表明,由于2月份累计预扣预缴应纳税所得额为37000元,已适用10%的税率,因此2月份和3月份应预扣预缴有所增高。8月份已适用于20%的税率。根据上述计算结果表明,虽每月工资薪金额和相关扣除额相同,年度内越往后,应纳税所得额越大适用税率越高,则预扣预缴税款可能会越大,会出现类似“12月取得工资10000元预扣预缴的个税税款,比1月份取得工资薪金30000元预扣预缴的个税税款多的”现象。

〔例3〕某职员2022年每月应发工资均为15000元,每月减除费用5000元,养老保险个人缴纳8%,为15000×8%=1200元,失业保险个人缴纳0.5%,为15000×0.5%=75元,医疗保险个人缴纳2%+3元,为15000×2%+3=303元,住房公积金个人缴纳比例12%,为15000×12%=1800元,享受住房租金专项附加扣除1500元,没有减免收入及减免税额等情况,以前三个月的计算为例,应当按照以下方法计算各月应预扣预缴税额:

应纳税所得额=月收入-五险一金-起征点-依法确定的其他扣除-专项附加扣除

1月应纳税所得额=15000–5000-1200-75-303-1800-1500=5122元

1月份:1月应纳税所得额 × 3% = 153.66元;

2月份:1月应纳税所得额 × 2 × 3% -153.66 =153.66元;

3月份:1月应纳税所得额 × 3 × 3% -153.66 - 153.66=153.66元;

按照上述算法,每一个月应纳个税额:

项目1月2月3月4月5月6月7月8月9月10月11月12月
每月税前工资150001500015000150001500015000150001500015000150001500015000
扣除各项减免额后应对税率3%3%3%3%3%3%3%10%10%10%10%10%
月应预扣预缴个税153.66153.66153.66153.66153.66153.66153.66501.98512.2512.2512.2512.2

上述计算结果表明,由于8月份累计预扣预缴应纳税所得额为40976元,已适用10%的税率,因此8月份应预扣预缴有所增高。

9.1.2.3 关于年度汇算清缴

一般来讲,有以下情形之一的,您可以选择在次年3月1日至6月30日内,自行向汇缴地主管税务机关办理汇算清缴申报时进行专项附加扣除,税款多退少补;(1)不愿意通过单位办理扣除,未将相关专项附加扣除信息报送给任职受雇单位的;

(2)在同一纳税年度涉及有两处工资薪金所得或涉及劳务报酬所得的;

(3)有大病医疗支出项目的;

(4)纳税年度内未享受或未足额享受专项附加扣除等情形。

9.1.2.4 个人所得税预扣率表(居民个人工资、薪金所得预扣预缴适用)
级数全年累计预扣预缴应纳税所得额税率(%)速算扣除数
1不超过36,000元的部分30
2超过36,000元至144,000元的部分102520
3超过144,000元至300,000元的部分2016920
4超过300,000元至420,000元的部分2531920
5超过420,000元至660,000元的部分3052920
6超过660,000元至960,000元的部分3585920
7超过960,000元的部分45181920
9.1.2.5 五险一金缴费比例

五险一金缴费比例:养老保险单位21%,个人8%;医疗保险单位9%,个人2%+3元;失业保险单位0.5%,个人0.5%;工伤保险和生育保险由单位缴纳,劳动者不用缴;住房公积金根据企业的实际情况,选择住房公积金缴费比例,下限为5%,最高不超12%。

9.1.3 实现步骤

本实战案例我们基于Spring Boot整合Drools的方式来实现。

目录结构

在这里插入图片描述

9.1.3.1 创建maven工程calculation并配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starters</artifactId><version>2.0.6.RELEASE</version></parent><groupId>cn.itcast</groupId><artifactId>calculation</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><!--drools规则引擎--><dependency><groupId>org.drools</groupId><artifactId>drools-core</artifactId><version>7.6.0.Final</version></dependency><dependency><groupId>org.drools</groupId><artifactId>drools-compiler</artifactId><version>7.6.0.Final</version></dependency><dependency><groupId>org.drools</groupId><artifactId>drools-templates</artifactId><version>7.6.0.Final</version></dependency><dependency><groupId>org.kie</groupId><artifactId>kie-api</artifactId><version>7.6.0.Final</version></dependency><dependency><groupId>org.kie</groupId><artifactId>kie-spring</artifactId><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId></exclusion><exclusion><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></exclusion><exclusion><groupId>org.springframework</groupId><artifactId>spring-core</artifactId></exclusion><exclusion><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></exclusion></exclusions><version>7.6.0.Final</version></dependency></dependencies><build><finalName>${project.artifactId}</finalName><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.*</include></includes><filtering>false</filtering></resource></resources><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build>
</project>
9.1.3.2 创建/resources/application.yml文件
server:port: 8080
spring:application:name: calculation
9.1.3.3 编写配置类DroolsConfig
package com.guohaowei.drools.config;
import cn.hutool.core.date.DatePattern;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.runtime.KieContainer;
import org.kie.internal.io.ResourceFactory;
import org.kie.spring.KModuleBeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;import java.io.IOException;
/*** 规则引擎配置类*/
@Configuration
public class DroolsConfig {//指定规则文件存放的目录private static final String RULES_PATH = "rules/";private final KieServices kieServices = KieServices.Factory.get();@Bean@ConditionalOnMissingBeanpublic KieFileSystem kieFileSystem() throws IOException {System.setProperty("drools.dateformat", DatePattern.NORM_DATE_PATTERN);KieFileSystem kieFileSystem = kieServices.newKieFileSystem();ResourcePatternResolver resourcePatternResolver =new PathMatchingResourcePatternResolver();Resource[] files =resourcePatternResolver.getResources("classpath*:" + RULES_PATH + "*.*");String path;for (Resource file : files) {path = RULES_PATH + file.getFilename();kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8"));}return kieFileSystem;}@Bean@ConditionalOnMissingBeanpublic KieContainer kieContainer() throws IOException {KieRepository kieRepository = kieServices.getRepository();kieRepository.addKieModule(kieRepository::getDefaultReleaseId);KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem());kieBuilder.buildAll();return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());}@Bean@ConditionalOnMissingBeanpublic KieBase kieBase() throws IOException {return kieContainer().getKieBase();}@Bean@ConditionalOnMissingBeanpublic KModuleBeanFactoryPostProcessor kiePostProcessor() {return new KModuleBeanFactoryPostProcessor();}
}
9.1.3.4 编写各种实体类
  • 编写实体类CalculationDto
package com.guohaowei.drools.domain.dto;import com.guohaowei.drools.domain.vo.CalculationVo;
import lombok.Data;import java.math.BigDecimal;
import java.util.List;/*** @author guohaowei*/
@Data
public class CalculationDto {/*** 12个月的工资缴税额等信息*/private List<CalculationVo> calculationVoList;
//--------------------------------重复字段用于计算start--------------------------------------------------------/*** 税前工资*/private BigDecimal wageBeforeTax;/*** 预扣预缴应纳税所得额*/private BigDecimal taxableIncome;/*** 累计预扣预缴应纳税所得额*/private BigDecimal taxableIncomeCount;/*** 月税后工资*/private BigDecimal wageDeductedTax;
//--------------------------------重复字段用于计算end----------------------------------------------------------/*** 个人所得税起征点*/private BigDecimal threshold;/*** 养老保险个人缴纳比例,默认8%*/private BigDecimal oldAgeInsuranceRatio;/*** 失业保险个人缴纳比例,默认0.5%*/private BigDecimal unemploymentInsuranceRatio;/*** 医疗保险个人缴纳比例,默认2% + 3元*/private BigDecimal medicalInsuranceRatio = BigDecimal.valueOf(0.02);/*** 医疗保险个人缴纳比例,默认2% + 3元*/private BigDecimal medicalInsuranceAdd = BigDecimal.valueOf(3);/*** 住房公积金个人缴纳比例,默认12%*/private BigDecimal housingFundRatio;/*** 住房租金专项附加扣除,默认1500元* 其他专项附加扣除同理,此处省略*/private BigDecimal specialAdditionalDeductionRent;}
  • 编写实体类CalculationVo
package com.guohaowei.drools.domain.vo;import lombok.Builder;
import lombok.Data;import java.math.BigDecimal;/*** @author guohaowei*/
@Data
@Builder
public class CalculationVo {/*** 月税前工资*/private BigDecimal wageBeforeTax;/*** 月预扣预缴应纳税所得额*/private BigDecimal taxableIncome;/*** 月税率*/private BigDecimal taxRate;/*** 月速算扣除数*/private BigDecimal quickDeduction;/*** 月扣税额*/private BigDecimal taxRebate;/*** 月税后工资*/private BigDecimal wageDeductedTax;/*** 月份*/private Integer month;
}
  • 编写枚举类WithholdingTaxRateEnum
package com.guohaowei.drools.enums;import lombok.AllArgsConstructor;
import lombok.Getter;import java.math.BigDecimal;/*** @Author: 郭浩伟 qq:912161367* @Date: 2022/11/17 0017 7:18* @Description: 个人所得税预扣率表(居民个人工资、薪金所得预扣预缴适用)*/
@AllArgsConstructor
@Getter
public enum WithholdingTaxRateEnum {/*** 级数*/one(36_000, "0.03", 0),two(144_000, "0.10", 2520),three(300_000, "0.20", 16920),four(420_000, "0.25", 31920),five(660_000, "0.30", 52920),six(960_000, "0.35", 85920),seven(960_000, "0.45", 181920),;/*** 全年累计预扣预缴应纳税所得额*/private Integer taxableIncome;/*** 税率,如0.03*/private String taxRate;/*** 速算扣除数*/private Integer quickDeduction;/*** 根据【应纳税所得额】得到含对应的【税率】和【速算扣除数】的枚举值** @param actualTaxableIncome* @return*/public static WithholdingTaxRateEnum getInfoByIncome(BigDecimal actualTaxableIncome) {if(null == actualTaxableIncome){return null;}for (WithholdingTaxRateEnum value : WithholdingTaxRateEnum.values()) {if(actualTaxableIncome.compareTo(BigDecimal.valueOf(value.getTaxableIncome())) <= 0){return value;}}return seven;}
}
  • 编写实体类CalculationReq
package com.guohaowei.drools.domain.req;import lombok.Data;import java.math.BigDecimal;/*** @Author: 郭浩伟 qq:912161367* @Date: 2022/11/16 0016 23:31* @Description:*/
@Data
public class CalculationReq {/*** 税前工资*/private BigDecimal wageBeforeTax;/*** 个人所得税起征点,默认5000元*/private BigDecimal threshold = BigDecimal.valueOf(5000);/*** 养老保险个人缴纳比例,默认8%*/private BigDecimal oldAgeInsuranceRatio = BigDecimal.valueOf(0.08);/*** 失业保险个人缴纳比例,默认0.5%*/private BigDecimal unemploymentInsuranceRatio = BigDecimal.valueOf(0.005);/*** 医疗保险个人缴纳比例,默认2% + 3元*/private BigDecimal medicalInsuranceRatio = BigDecimal.valueOf(0.02);/*** 医疗保险个人缴纳比例,默认2% + 3元*/private BigDecimal medicalInsuranceAdd = BigDecimal.valueOf(3);/*** 住房公积金个人缴纳比例,默认12%*/private BigDecimal housingFundRatio = BigDecimal.valueOf(0.12);/*** 住房租金专项附加扣除,默认1500元* 其他专项附加扣除同理,此处省略*/private BigDecimal specialAdditionalDeductionRent = BigDecimal.valueOf(1500);}
9.1.3.5 在resources/rules下创建规则文件calculation.drl文件
//当前规则文件用于计算个人所得税
package calculation
import java.math.BigDecimal
import com.guohaowei.drools.domain.dto.CalculationDto
import com.guohaowei.drools.enums.WithholdingTaxRateEnum
import java.util.ArrayList
import com.guohaowei.drools.domain.vo.CalculationVo
import java.util.List
global com.guohaowei.drools.domain.dto.CalculationDto calculationGlobalDto
/**当前规则文件中的规则主要分为三类1、计算应纳税所得额有1个规则2、设置税率、速算扣除数有7个规则3、计算税后工资有1个规则
**///计算应纳税所得额
rule "计算应纳税所得额-减去个税起征点"salience 100date-effective "2019-01-01"no-loop truewhenthen//税前工资BigDecimal wageBeforeTax = calculationGlobalDto.getWageBeforeTax();BigDecimal taxableIncome = wageBeforeTax.subtract(calculationGlobalDto.getThreshold());calculationGlobalDto.setWageBeforeTax(wageBeforeTax);calculationGlobalDto.setWageDeductedTax(wageBeforeTax);calculationGlobalDto.setTaxableIncome(BigDecimal.ZERO);if(taxableIncome.compareTo(BigDecimal.valueOf(0)) > 0){calculationGlobalDto.setTaxableIncome(taxableIncome);}endrule "计算应纳税所得额-减去养老保险个人缴纳金额"salience 99date-effective "2019-01-01"no-loop truewhenthen//养老保险个人缴纳额BigDecimal oldAgeInsurance = calculationGlobalDto.getWageBeforeTax().multiply(calculationGlobalDto.getOldAgeInsuranceRatio());//预扣预缴应纳税所得额BigDecimal taxableIncome = calculationGlobalDto.getTaxableIncome();//应纳税所得额如果小于0即不需要缴税也就不需要计算了if(taxableIncome.compareTo(BigDecimal.valueOf(0)) > 0){taxableIncome = taxableIncome.subtract(oldAgeInsurance);calculationGlobalDto.setTaxableIncome(taxableIncome);}//税后工资calculationGlobalDto.setWageDeductedTax(calculationGlobalDto.getWageBeforeTax().subtract(oldAgeInsurance));
endrule "计算应纳税所得额-减去失业保险个人缴纳金额"salience 98date-effective "2019-01-01"no-loop truewhenthen//失业保险个人缴纳额BigDecimal unemploymentInsurance = calculationGlobalDto.getWageBeforeTax().multiply(calculationGlobalDto.getUnemploymentInsuranceRatio());//应纳税所得额如果小于0即不需要缴税也就不需要计算了if(calculationGlobalDto.getTaxableIncome().compareTo(BigDecimal.valueOf(0)) > 0){BigDecimal taxableIncome = calculationGlobalDto.getTaxableIncome().subtract(unemploymentInsurance);calculationGlobalDto.setTaxableIncome(taxableIncome);}//税后工资calculationGlobalDto.setWageDeductedTax(calculationGlobalDto.getWageDeductedTax().subtract(unemploymentInsurance));
endrule "计算应纳税所得额-减去医疗保险个人缴纳金额"salience 97date-effective "2019-01-01"no-loop truewhenthen//医疗保险个人缴纳额BigDecimal medicalInsurance = calculationGlobalDto.getWageBeforeTax().multiply(calculationGlobalDto.getMedicalInsuranceRatio()).add(calculationGlobalDto.getMedicalInsuranceAdd());//应纳税所得额如果小于0即不需要缴税也就不需要计算了if(calculationGlobalDto.getTaxableIncome().compareTo(BigDecimal.valueOf(0)) > 0){BigDecimal taxableIncome = calculationGlobalDto.getTaxableIncome().subtract(medicalInsurance);calculationGlobalDto.setTaxableIncome(taxableIncome);}//税后工资calculationGlobalDto.setWageDeductedTax(calculationGlobalDto.getWageDeductedTax().subtract(medicalInsurance));
endrule "计算应纳税所得额-减去住房公积金个人缴纳金额"salience 96date-effective "2019-01-01"no-loop truewhenthen//住房公积金个人缴纳额BigDecimal housingFund = calculationGlobalDto.getWageBeforeTax().multiply(calculationGlobalDto.getHousingFundRatio());//应纳税所得额如果小于0即不需要缴税也就不需要计算了if(calculationGlobalDto.getTaxableIncome().compareTo(BigDecimal.valueOf(0)) > 0){BigDecimal taxableIncome = calculationGlobalDto.getTaxableIncome().subtract(housingFund);calculationGlobalDto.setTaxableIncome(taxableIncome);}//税后工资calculationGlobalDto.setWageDeductedTax(calculationGlobalDto.getWageDeductedTax().subtract(housingFund));
endrule "计算应纳税所得额-减去住房租金专项附加扣除金额"salience 95date-effective "2019-01-01"no-loop truewhenthen//应纳税所得额如果小于0即不需要缴税也就不需要计算了if(calculationGlobalDto.getTaxableIncome().compareTo(BigDecimal.valueOf(0)) > 0){BigDecimal taxableIncome = calculationGlobalDto.getTaxableIncome().subtract(calculationGlobalDto.getSpecialAdditionalDeductionRent());calculationGlobalDto.setTaxableIncome(taxableIncome);}
endrule "计算出最终的统计结果"salience 94date-effective "2019-01-01"no-loop truewhenthenList<CalculationVo> calculationVoList = new ArrayList<>();BigDecimal taxRebateCount = BigDecimal.ZERO;//当月扣税额=应纳税所得额x月份数x税率-速算扣除数-累计扣税额for(int i = 1; i <= 12 ; i++) {//累计应纳税所得额BigDecimal taxableIncomeCount = calculationGlobalDto.getTaxableIncome().multiply(BigDecimal.valueOf(i));WithholdingTaxRateEnum withholdingTaxRateEnum = WithholdingTaxRateEnum.getInfoByIncome(taxableIncomeCount);//月税率BigDecimal taxRate = new BigDecimal(withholdingTaxRateEnum.getTaxRate());//月速算扣除数BigDecimal quickDeduction = BigDecimal.valueOf(withholdingTaxRateEnum.getQuickDeduction());//月扣税额BigDecimal taxRebate =taxableIncomeCount.multiply(taxRate).subtract(quickDeduction).subtract(taxRebateCount);//累计扣税额taxRebateCount = taxRebateCount.add(taxRebate);//组装voCalculationVo calculationVo = CalculationVo.builder().wageBeforeTax(calculationGlobalDto.getWageBeforeTax()).taxableIncome(calculationGlobalDto.getTaxableIncome()).taxRate(taxRate).quickDeduction(quickDeduction).taxRebate(taxRebate).wageDeductedTax(calculationGlobalDto.getWageDeductedTax().subtract(taxRebate)).month(i).build();calculationVoList.add(calculationVo);}calculationGlobalDto.setCalculationVoList(calculationVoList);
end
9.1.3.6 创建RuleServiceImpl
package com.guohaowei.drools.service.impl;import cn.hutool.core.bean.BeanUtil;
import com.guohaowei.drools.domain.dto.CalculationDto;
import com.guohaowei.drools.domain.req.CalculationReq;
import com.guohaowei.drools.domain.vo.CalculationVo;
import com.guohaowei.drools.service.RuleService;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;
/*** @Author: 郭浩伟 qq:912161367* @Date: 2022/11/18 0018 19:41* @Description:*/
@Service
public class RuleServiceImpl implements RuleService {@Autowiredprivate KieBase kieBase;/*** 调用Drools规则引擎实现个人所得税计算** @param req* @return*/@Overridepublic List<CalculationVo> calculate(CalculationReq req) {KieSession session = kieBase.newKieSession();CalculationDto calculationDto = BeanUtil.copyProperties(req, CalculationDto.class);//初始化税后工资=税前工资calculationDto.setWageDeductedTax(calculationDto.getWageBeforeTax());//设置全局变量,名称和类型必须和规则文件中定义的全局变量名称对应session.setGlobal("calculationGlobalDto",calculationDto);session.fireAllRules();session.dispose();return calculationDto.getCalculationVoList();}
}
9.1.3.7 创建RuleController
package com.guohaowei.drools.controller;import com.guohaowei.drools.domain.req.CalculationReq;
import com.guohaowei.drools.domain.vo.CalculationVo;
import com.guohaowei.drools.service.impl.RuleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@RequestMapping("/rule")
public class RuleController {@Autowiredprivate RuleServiceImpl ruleService;@RequestMapping("/calculate")public List<CalculationVo> calculate(@RequestBody CalculationReq req){return ruleService.calculate(req);}
}
9.1.3.8 创建启动类DroolsApplication
package com.guohaowei.drools;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DroolsApplication {public static void main(String[] args) {SpringApplication.run(DroolsApplication.class);}
}

全套代码及资料全部完整提供,点此处下载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_38579.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode - 76 最小覆盖子串

目录 题目来源 题目描述 示例 提示 题目解析 算法源码 题目来源 76. 最小覆盖子串 - 力扣&#xff08;LeetCode&#xff09; 题目描述 给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串&#xff0c;则返回…

iClient for Leaflet设置地图掩膜

作者&#xff1a;lly 文章目录背景一、实现思路二、步骤代码三、完整代码背景 最近很多小伙伴需要只展示地图的某个行政区域&#xff0c;由于地图存在多个图层&#xff0c;所以图层过滤的方式并不能很好的适用&#xff0c;这个时候&#xff0c;我们可以考虑给地图覆盖一层掩膜…

界面控件DevExpress WPF的主题设计器,可轻松完成应用主题研发

DevExpress WPF拥有120个控件和库&#xff0c;将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序&#xff0c;这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 DevExpress WPF的The…

双十二薅羊毛!这几款数码好物不可错过

双十二即将开始&#xff0c;在这段时间里有的人已经将自己心仪的塞满了整个购物车了吧&#xff0c;而有的人还没想好到底要入手什么&#xff0c;如果你也是还在纠结的话&#xff0c;不知道该买什么又或是想知道哪些产品更适合你入手&#xff0c;不妨来看看小编今天为你带来的这…

MySQL第一弹

目录 一、数据库的基本概念 1、数据 (Data) 2、表 3、数据库 4、数据库管理系统(DBMS) 5、数据库系统 6、DBMS的工作模式如下 二、数据库的发展史 1.第一代数据库&#xff08;淘汰&#xff09; 2.第二代数据库&#xff08;现在用的基本上都是二代&#xff09; 3.第…

亲戚小孩月薪17k,而我只有4k+,好慌......

我们总是在悲观与乐观中反复折磨自己&#xff0c;感觉自己一事无成。总是眼高手低&#xff0c;总以为大运会砸到自己&#xff0c;遇到挫折就会感到很沮丧。 大学四年没考到英语六级证书&#xff0c;小学教资考了两次。现在想要考研&#xff0c;但总是觉得来不及&#xff0c;或…

磁盘划分和磁盘格式化

文章目录列出装置的 UUID 等参数parted 列出磁盘的分区表类型与分区信息磁盘分区&#xff1a;gdisk、fdisk用 gdisk 新增分区槽用 gdisk 删除一个分区槽磁盘格式化&#xff08;建立文件系统&#xff09;XFS 文件系统 mkfs.xfsXFS 文件系统 for RAID 效能优化&#xff08;Option…

java中csv导出-追加-列转行

1、问题描述 业务数据量比较大&#xff0c;业务上查询条件写入数据库&#xff0c;java定时去读&#xff0c;然后导出csv&#xff0c;供用户下载&#xff0c;因为有模板要求&#xff0c;前一部分是统计信息&#xff0c;后一部分是明细信息&#xff1b;首先csv中写入统计信息&am…

IDEA的日常快捷键大全

更多内容在&#xff1a;https://javaxiaobear.gitee.io/ ​​​​​​第1组&#xff1a;通用型 说明 快捷键 复制代码-copy ctrl c 粘贴-paste ctrl v 剪切-cut ctrl x 撤销-undo ctrl z 反撤销-redo ctrl shift z 保存-save all ctrl s 全选-select all …

Python连接Clickhouse遇坑篇,耗时一天成功连接!

首先&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;不要看网上那些乱七八糟的使用clickhouse-driver连接了&#xff0c;真tm难用&#xff0c;端口能搞死你那种&#xff0c;超级烦&#xff01; 推荐直接看官方…

数商云SRM系统招标流程分享,助力建筑材料企业降低采购成本,提高采购效率

近年来&#xff0c;随着主管部门对房地产市场的监管非常严格&#xff0c;房地产业的发展已进入瓶颈期&#xff0c;这对与房地产业密切相关的建材行业产生了很大的影响。同时&#xff0c;我国城市化进入成熟期&#xff0c;行业规模发展动力减弱&#xff0c;建材行业增长压力明显…

Kotlin高仿微信-第8篇-单聊

Kotlin高仿微信-项目实践58篇详细讲解了各个功能点&#xff0c;包括&#xff1a;注册、登录、主页、单聊(文本、表情、语音、图片、小视频、视频通话、语音通话、红包、转账)、群聊、个人信息、朋友圈、支付服务、扫一扫、搜索好友、添加好友、开通VIP等众多功能。 Kotlin高仿…

VauditDemo靶场代码审计

靶场搭建 将下载好的VAuditDemo_Debug目录复制到phpstudy的www目录下&#xff0c;然后将其文件名字修改成VAuditDemo&#xff0c;当然你也可以修改成其他的 运行phpstudy并且访问install目录下的install.php&#xff0c;这里我访问的是http://127.0.0.1/VAuditDemo/install/in…

竞赛——【蓝桥杯】2022年12月第十四届蓝桥杯模拟赛第二期C/C++

1、最小的2022 问题描述 请找到一个大于 2022 的最小数&#xff0c;这个数转换成二进制之后&#xff0c;最低的 6 个二进制为全为 0 。 请将这个数的十进制形式作为答案提交。 答案提交 这是一道结果填空的题&#xff0c;你只需要算出结果后提交即可。本题的结果为一个整数…

Java学习之继承练习题

目录 第一题 代码 输出流程分析 运行结果 考察知识点 第二题 代码 流程分析 运行结果 第三题 题目要求 我的代码 代码改进 第一题 代码 package com.hspedu.extends_.exercise;public class ExtendsExercise01 {public static void main(String[] args) {B b new …

Sentinel-2(哨兵2数据介绍)

哥白尼 Sentinel-2&#xff08;哨兵 2&#xff09;计划是一个由两颗相同的 Sentinel-2 极轨卫星组成的星座&#xff0c;两颗卫星相位差 180&#xff0c;运行在平均高度 786 km 的太阳同步轨道上。每颗卫星在其轨道上的位置由双频全球导航卫星系统&#xff08;GNSS&#xff09;接…

ggrcs 包2.4绘制RCS(限制立方样条图)实际操作演示(1)

ggrcs 包2.4版本已经发布一段时间了&#xff0c;大概几个月了吧&#xff0c;收到不少好评&#xff0c; 没听说太大的问题&#xff0c;最主要的问题有两个&#xff1a; 1.是说变量不是数字变量。 2.是说数据超过10万&#xff0c;无法处理 第一个问题非常好处理&#xff0c;这…

R语言MCMC:Metropolis-Hastings采样用于回归的贝叶斯估计

全文链接&#xff1a;http://tecdat.cn/?p19664 MCMC是从复杂概率模型中采样的通用技术。蒙特卡洛马尔可夫链Metropolis-Hastings算法&#xff08;点击文末“阅读原文”获取完整代码数据&#xff09;。问题如果需要计算有复杂后验pdf p&#xff08;θ| y&#xff09;的随机变量…

简单聊聊什么是react-redux,它能解决哪些问题

或许 在大多数人眼中 redux是一个相对复查很多的知识点 但确实如果你熟悉了流程 其实也比较简单的 redux是一个数据管理方案 我们先来举个例子 目前我们知道 react中有两种组件数据通信的方式 分别是 props 父传子 定义事件 子传父 通过事件将自己的数据传给父级 那如果是兄弟…

领悟《信号与系统》之 周期信号的傅里叶变换计算

周期信号的傅里叶变换计算一、周期信号的傅里叶变换存在的条件二、周期信号的傅里叶变换例题&#xff1a;一、周期信号的傅里叶变换存在的条件 典型非周期信号&#xff08;如指数信号&#xff0c;矩形信号等&#xff09;都是满足绝对可积&#xff08;或绝对可和&#xff09;条…