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

291 lines
15 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.engine.salary.service.impl;
import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
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.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.SalaryEmployeeService;
import com.engine.salary.service.TaxAgentService;
import com.engine.salary.service.TaxDeclarationApiFlowStatisticService;
import com.engine.salary.service.TaxDeclarationApiFlowWarnService;
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 com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.SalaryPageUtil;
import dm.jdbc.util.IdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import weaver.email.EmailWorkRunnable;
import weaver.hrm.User;
import java.io.IOException;
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
public TaxDeclarationApiFlowWarnConfigPO getWarnConfig() {
return getTaxDeclarationApiFlowWarnConfigMapper().getOne();
}
@Override
public List<TaxDeclarationApiFlowWarnReceiverListDTO> getWarnReceiverList(Long warnConfigId) {
List<TaxDeclarationApiFlowWarnReceiverPO> receiverPOList = getTaxDeclarationApiFlowWarnReceiverMapper().listSome(TaxDeclarationApiFlowWarnReceiverPO.builder().warnConfigId(warnConfigId).build());
List<Long> empIds = SalaryEntityUtil.properties(receiverPOList, TaxDeclarationApiFlowWarnReceiverPO::getEmployeeId, Collectors.toList());
List<DataCollectionEmployee> empInfos = getSalaryEmployeeService(user).getEmployeeByIdsAll(empIds);
Map<Long, DataCollectionEmployee> salaryEmployeeMap = SalaryEntityUtil.convert2Map(empInfos, DataCollectionEmployee::getEmployeeId);
return receiverPOList.stream().map(e -> {
DataCollectionEmployee employee = salaryEmployeeMap.get(e.getEmployeeId());
return TaxDeclarationApiFlowWarnReceiverListDTO.builder()
.employeeName(employee.getUsername())
.employeeId(e.getEmployeeId())
.mobile(employee.getMobile())
.email(employee.getEmail())
.id(e.getId())
.build();
}).collect(Collectors.toList());
}
@Override
public PageInfo<TaxDeclarationApiFlowWarnReceiverListDTO> getWarnReceiverPageList(Long warnConfigId) {
List<TaxDeclarationApiFlowWarnReceiverListDTO> dtos = getWarnReceiverList(warnConfigId);
return SalaryPageUtil.buildPage(1, 10000, dtos, TaxDeclarationApiFlowWarnReceiverListDTO.class);
}
@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) {
// 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接口创建规则失败"));
}
@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 消息
// CreateRuleEntity createRule = new CreateRuleEntity()
// .setName(SalaryI18nUtil.getI18nLabel(159146, "流量不足提醒"))
// .setUser(new UserEntity(currentEmployeeId))
// .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接口创建规则失败"));
}
return warnConfigPO.getId().toString();
}
@Override
public Long getRuleBusinessId() {
TaxDeclarationApiFlowWarnConfigPO warnConfig = this.getWarnConfig();
return warnConfig == null ? IdGenerator.generate() : warnConfig.getBusinessId();
}
@Override
public void sendFlowWarnMessage(TaxDeclarationApiBillingServiceImpl.ApiFlowUpdateWrapper updateWrapper) {
TaxDeclarationApiFlowWarnConfigPO warnConfig = getWarnConfig();
// 未配置或开关关闭
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;
}
String title = "流量不足提醒";
String context = String.format(SalaryI18nUtil.getI18nLabel(111, "智能算薪流量已不足%s请及时采购续费避免次月无法使用。"), warnConfig.getThreshold());
warnReceiverList.forEach(e -> {
Long receiver = e.getEmployeeId();
sendEmail(receiver, title, context);
sendMsg(receiver, title, context, warnConfig.getBusinessId());
}
);
}
/**
* 发送邮件
*
* @param receiver
* @param title
* @param context
*/
private void sendEmail(Long receiver, String title, String context) {
EmailWorkRunnable.threadModeReminder(receiver.toString(), title, context);
}
/**
* 发送Em消息
*
* @param receiver
* @param title
* @param context
* @param businessId
*/
public static void sendMsg(Long receiver, String title, String context, Long businessId) {
MessageType messageType = MessageType.newInstance(499); // 消息来源(见文档第四点补充 必填)
Set<String> userIdList = new HashSet<>(); // 接收人id 必填
userIdList.add(receiver.toString());
try {
MessageBean messageBean = Util_Message.createMessage(messageType, userIdList, title, context, "", "");
messageBean.setCreater(1);// 创建人id
messageBean.setBizState("0");// 需要修改消息为已处理等状态时传入,表示消息最初状态为待处理
messageBean.setTargetId("499|" + businessId); //消息来源code +“|”+业务id需要修改消息为已处理等状态时传入
Util_Message.store(messageBean);
} catch (IOException e) {
e.printStackTrace();
}
}
}