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.common.MessageUtil; import weaver.email.EmailWorkRunnable; import weaver.general.BaseBean; 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 getWarnReceiverList(Long warnConfigId) { List receiverPOList = getTaxDeclarationApiFlowWarnReceiverMapper().listSome(TaxDeclarationApiFlowWarnReceiverPO.builder().warnConfigId(warnConfigId).build()); List empIds = SalaryEntityUtil.properties(receiverPOList, TaxDeclarationApiFlowWarnReceiverPO::getEmployeeId, Collectors.toList()); List empInfos = getSalaryEmployeeService(user).getEmployeeByIdsAll(empIds); Map 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 getWarnReceiverPageList(Long warnConfigId) { List 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 simpleEmployees = getSalaryEmployeeService(user).listAll(UseEmployeeTypeEnum.ALL); Map 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 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 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 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 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(); sendMsg(receiver, title, context, warnConfig.getBusinessId()); sendEmail(e.getEmail(), title, context); sendSMS(e.getMobile(), context); } ); } //提醒的消息类型 BaseBean bb = new BaseBean(); private final String flowNoticeMessageType = bb.getPropValue("hrmSalaryCustom", "flowNoticeMessageType"); /** * 发送Em消息 * * @param receiver * @param title * @param context * @param businessId */ public void sendMsg(Long receiver, String title, String context, Long businessId) { if (StringUtils.isBlank(flowNoticeMessageType)) { return; } MessageType messageType = MessageType.newInstance(Integer.parseInt(flowNoticeMessageType)); Set userIdList = new HashSet<>(); userIdList.add(receiver.toString()); try { MessageBean messageBean = Util_Message.createMessage(messageType, userIdList, title, context, "", ""); messageBean.setCreater(1);// 创建人id messageBean.setBizState("0");// 需要修改消息为已处理等状态时传入,表示消息最初状态为待处理 messageBean.setTargetId(flowNoticeMessageType + "|" + businessId); //消息来源code +“|”+业务id需要修改消息为已处理等状态时传入 Util_Message.store(messageBean); } catch (IOException e) { e.printStackTrace(); } } /** * 发送邮件 * * @param email * @param title * @param context */ private void sendEmail(String email, String title, String context) { if (MessageUtil.checkSendEmail()) { EmailWorkRunnable.threadModeReminder(email, title, context); } } /** * 发送短信 * * @param mobile * @param context */ private void sendSMS(String mobile, String context) { if (MessageUtil.checkSendSMS()) { MessageUtil.sendSMS(mobile, context); } } }