weaver-hrm-salary/src/com/engine/salary/service/impl/TaxDeclarationApiFlowWarnSe...

243 lines
12 KiB
Java
Raw Normal View History

2023-08-28 13:31:11 +08:00
package com.engine.salary.service.impl;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.common.OptionDTO;
import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.taxagent.po.TaxAgentEmployeePO;
import com.engine.salary.entity.taxapiflow.dto.TaxDeclarationApiFlowWarnReceiverFormDTO;
import com.engine.salary.entity.taxapiflow.dto.TaxDeclarationApiFlowWarnReceiverListDTO;
import com.engine.salary.entity.taxapiflow.param.CreateMessageRuleParam;
import com.engine.salary.entity.taxapiflow.param.TaxDeclarationApiFlowWarnConfigSaveParam;
import com.engine.salary.entity.taxapiflow.param.TaxDeclarationApiFlowWarnReceiverSaveParam;
import com.engine.salary.entity.taxapiflow.po.TaxDeclarationApiFlowWarnConfigPO;
import com.engine.salary.entity.taxapiflow.po.TaxDeclarationApiFlowWarnReceiverPO;
import com.engine.salary.entity.taxapiflow.response.QueryAccountBalanceResponse;
import com.engine.salary.enums.SalaryOnOffEnum;
import com.engine.salary.enums.datacollection.UseEmployeeTypeEnum;
import com.engine.salary.enums.sicategory.DeleteTypeEnum;
import com.engine.salary.mapper.taxapiflow.TaxDeclarationApiFlowWarnConfigMapper;
import com.engine.salary.mapper.taxapiflow.TaxDeclarationApiFlowWarnReceiverMapper;
import com.engine.salary.service.*;
import com.engine.salary.util.SalaryAssert;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.db.MapperProxyFactory;
import dm.jdbc.util.IdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import weaver.hrm.User;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author chengliming
* @date 2022-11-11 2:57 PM
**/
@Slf4j
public class TaxDeclarationApiFlowWarnServiceImpl extends Service implements TaxDeclarationApiFlowWarnService {
private TaxDeclarationApiFlowWarnConfigMapper getTaxDeclarationApiFlowWarnConfigMapper() {
return MapperProxyFactory.getProxy(TaxDeclarationApiFlowWarnConfigMapper.class);
}
private TaxDeclarationApiFlowWarnReceiverMapper getTaxDeclarationApiFlowWarnReceiverMapper() {
return MapperProxyFactory.getProxy(TaxDeclarationApiFlowWarnReceiverMapper.class);
}
private SalaryEmployeeService getSalaryEmployeeService(User user) {
return ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user);
}
private TaxAgentService getTaxAgentService(User user) {
return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
}
private TaxDeclarationApiFlowStatisticService getTaxDeclarationApiFlowStatisticService(User user) {
return ServiceUtil.getService(TaxDeclarationApiFlowStatisticServiceImpl.class, user);
}
@Override
2023-08-28 15:19:57 +08:00
public TaxDeclarationApiFlowWarnConfigPO getWarnConfig() {
2023-08-28 13:31:11 +08:00
return getTaxDeclarationApiFlowWarnConfigMapper().getOne();
}
@Override
public List<TaxDeclarationApiFlowWarnReceiverListDTO> getWarnReceiverList(Long warnConfigId) {
List<TaxDeclarationApiFlowWarnReceiverPO> receiverPOList = getTaxDeclarationApiFlowWarnReceiverMapper().listSome(TaxDeclarationApiFlowWarnReceiverPO
.builder()
.warnConfigId(warnConfigId)
.build());
List<TaxAgentEmployeePO> salaryEmployees = getTaxAgentService(user).listEmployees();
Map<Long, String> salaryEmployeeMap = SalaryEntityUtil.convert2Map(salaryEmployees, TaxAgentEmployeePO::getEmployeeId, TaxAgentEmployeePO::getUsername);
return receiverPOList.stream().map(e -> TaxDeclarationApiFlowWarnReceiverListDTO.builder()
.employeeName(salaryEmployeeMap.get(e.getEmployeeId()))
.employeeId(e.getEmployeeId())
.mobile(e.getMobile())
.email(e.getEmail())
.id(e.getId())
.build()).collect(Collectors.toList());
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteReceiver(Long id) {
TaxDeclarationApiFlowWarnReceiverPO receiverPO = getWarnReceiverPOById(id);
SalaryAssert.notNull(receiverPO, "该提醒对象不存在,请刷新页面后重试");
getTaxDeclarationApiFlowWarnReceiverMapper().delete(TaxDeclarationApiFlowWarnReceiverPO.builder().id(id).build());
}
@Override
public TaxDeclarationApiFlowWarnReceiverFormDTO getWarnReceiverFormById(Long id) {
TaxDeclarationApiFlowWarnReceiverPO receiverPO = getWarnReceiverPOById(id);
SalaryAssert.notNull(receiverPO, "该提醒对象不存在,请刷新页面后重试");
// 查找人员信息
List<DataCollectionEmployee> simpleEmployees = getSalaryEmployeeService(user).listAll(UseEmployeeTypeEnum.ALL);
Map<Long, DataCollectionEmployee> empMap = SalaryEntityUtil.convert2Map(simpleEmployees, DataCollectionEmployee::getEmployeeId);
DataCollectionEmployee simpleEmployee = empMap.get(receiverPO.getEmployeeId());
SalaryAssert.notNull(simpleEmployee, SalaryI18nUtil.getI18nLabel(139811, "人员不存在"));
return TaxDeclarationApiFlowWarnReceiverFormDTO.builder()
.id(receiverPO.getId())
.employee(Collections.singletonList(new OptionDTO(receiverPO.getEmployeeId().toString(), simpleEmployee.getUsername())))
.email(receiverPO.getEmail())
.mobile(receiverPO.getMobile())
.emailOptions(StringUtils.isNotEmpty(simpleEmployee.getEmail()) ? Collections.singletonList(simpleEmployee.getEmail()) : null)
.build();
}
private TaxDeclarationApiFlowWarnReceiverPO getWarnReceiverPOById(Long id) {
return getTaxDeclarationApiFlowWarnReceiverMapper().getById(id);
}
@Override
public void saveWarnReceiver(TaxDeclarationApiFlowWarnReceiverSaveParam param) {
Date now = new Date();
if (param.getId() != null) {
TaxDeclarationApiFlowWarnReceiverPO receiverPO = getWarnReceiverPOById(param.getId());
SalaryAssert.notNull(receiverPO, "该提醒对象不存在,请刷新页面后重试");
if (param.getEmployeeId() != null && !receiverPO.getEmployeeId().equals(param.getEmployeeId())) {
filterByEmpId(param);
receiverPO.setEmployeeId(param.getEmployeeId());
}
receiverPO.setUpdateTime(now);
receiverPO.setEmail(param.getEmail());
receiverPO.setMobile(param.getMobile());
getTaxDeclarationApiFlowWarnReceiverMapper().updateIgnoreNull(receiverPO);
} else {
filterByEmpId(param);
TaxDeclarationApiFlowWarnReceiverPO receiverPO = TaxDeclarationApiFlowWarnReceiverPO.builder()
.id(IdGenerator.generate())
.email(param.getEmail())
.mobile(param.getMobile())
.employeeId(param.getEmployeeId())
.warnConfigId(param.getWarnConfigId())
.createTime(now)
.updateTime(now)
.deleteType(DeleteTypeEnum.NOT_DELETED.getValue())
.tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
.creator((long) user.getUID())
.build();
getTaxDeclarationApiFlowWarnReceiverMapper().insertIgnoreNull(receiverPO);
}
}
private void filterByEmpId(TaxDeclarationApiFlowWarnReceiverSaveParam param) {
List<TaxDeclarationApiFlowWarnReceiverPO> another = getTaxDeclarationApiFlowWarnReceiverMapper().filterByEmpId(param);
SalaryAssert.isEmpty(another, SalaryI18nUtil.getI18nLabel(184007, "当前人员对应的提醒对象已存在,请更换人员后尝试"));
}
@Override
public void createMessageRule(CreateMessageRuleParam param) {
2023-07-20 09:58:27 +08:00
// CreateRuleEntity createRule = new CreateRuleEntity()
// .setBusinessId(param.getBusinessId())
// .setEvent(MessageEvent.PAYROLL)
// .setModule(MessageModule.HRSA)
// .setConfig(param.getConfig());
// log.info("RuleRest.createRuleV2 -> config ====== {}", param.getConfig());
// WeaResult<Long> weaResult = ruleRest.createRuleV2(createRule);
// SalaryAssert.isTrue(weaResult.isStatus(), SalaryI18nUtil.getI18nLabel(184008, "RPC接口创建规则失败"));
2023-08-28 13:31:11 +08:00
}
@Override
public String saveWarnConfig(TaxDeclarationApiFlowWarnConfigSaveParam param) {
Date now = new Date();
TaxDeclarationApiFlowWarnConfigPO warnConfigPO;
if (param.getId() != null) {
warnConfigPO = getTaxDeclarationApiFlowWarnConfigMapper().getById(param.getId());
SalaryAssert.notNull(warnConfigPO, "提醒规则不存在,请先保存");
warnConfigPO.setThreshold(param.getThreshold());
warnConfigPO.setEnableWarn(param.getEnable() ? 1 : 0);
warnConfigPO.setUpdateTime(now);
getTaxDeclarationApiFlowWarnConfigMapper().updateIgnoreNull(warnConfigPO);
} else {
warnConfigPO = TaxDeclarationApiFlowWarnConfigPO.builder()
.id(IdGenerator.generate())
.businessId(param.getBusinessId())
.threshold(param.getThreshold())
.enableWarn(param.getEnable() ? 1 : 0)
.createTime(now)
.updateTime(now)
.creator((long) user.getUID())
.deleteType(DeleteTypeEnum.NOT_DELETED.getValue())
.tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
.build();
getTaxDeclarationApiFlowWarnConfigMapper().insertIgnoreNull(warnConfigPO);
}
if (StringUtils.isNotEmpty(param.getConfig())) {
//todo 消息
2023-07-20 09:58:27 +08:00
// CreateRuleEntity createRule = new CreateRuleEntity()
2023-08-28 13:31:11 +08:00
// .setName(SalaryI18nUtil.getI18nLabel(159146, "流量不足提醒"))
// .setUser(new UserEntity(currentEmployeeId))
2023-07-20 09:58:27 +08:00
// .setModule(MessageModule.HRSA)
// .setEvent(MessageEvent.PAYROLL)
// .setBusinessId(param.getBusinessId().toString())
// .setConfig(param.getConfig());
// log.info("RuleRest.createRuleV2 -> config ====== {}", param.getConfig());
// WeaResult<Long> weaResult = ruleRest.createRuleV2(createRule);
// SalaryAssert.isTrue(weaResult.isStatus(), SalaryI18nUtil.getI18nLabel(184008, "RPC接口创建规则失败"));
2023-08-28 13:31:11 +08:00
}
return warnConfigPO.getId().toString();
}
@Override
2023-08-28 15:19:57 +08:00
public Long getRuleBusinessId() {
TaxDeclarationApiFlowWarnConfigPO warnConfig = this.getWarnConfig();
2023-08-28 13:31:11 +08:00
return warnConfig == null ? IdGenerator.generate() : warnConfig.getBusinessId();
}
@Override
public void sendFlowWarnMessage(TaxDeclarationApiBillingServiceImpl.ApiFlowUpdateWrapper updateWrapper) {
2023-08-28 15:19:57 +08:00
TaxDeclarationApiFlowWarnConfigPO warnConfig = getWarnConfig();
2023-08-28 13:31:11 +08:00
// 未配置或开关关闭
if (Objects.isNull(warnConfig) || SalaryOnOffEnum.OFF.getValue().equals(warnConfig.getEnableWarn())) {
return;
}
QueryAccountBalanceResponse response = getTaxDeclarationApiFlowStatisticService(user).getQueryAccountBalanceResponse(updateWrapper.getApiConfig());
// 剩余流量大于阈值无需提醒
if (StringUtils.isNotEmpty(response.getBody().getSurplus()) && Long.parseLong(response.getBody().getSurplus()) > warnConfig.getThreshold()) {
return;
}
List<TaxDeclarationApiFlowWarnReceiverListDTO> warnReceiverList = getWarnReceiverList(warnConfig.getId());
if (warnReceiverList.isEmpty()) {
log.info("warnReceiverList is empty, send msg fail");
return;
}
//todo 消息
2023-07-20 09:58:27 +08:00
// List<UserEntity> receivers = warnReceiverList.stream().map(e ->
// new UserEntity(e.getEmployeeId(), updateWrapper.getTenantKey())
// .setEmail(e.getEmail()).setPhone(e.getMobile()).setLanguage(6)
// ).collect(Collectors.toList());
// // 组装消息对象
// SendMessageEntity smg = TaxApiFlowBO.buildSendMessageEntity(warnConfig, receivers, updateWrapper);
// // 发送消息
// WeaResult<Long> weaResult = asyncSystemMessageRest.sendMsg(smg);
// SalaryAssert.isTrue(weaResult.isStatus(), SalaryI18nUtil.getI18nLabel(184009, "流量不足提醒发送失败"));
2023-08-28 13:31:11 +08:00
}
}