//package com.engine.salary.service.impl; // //import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; //import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; //import com.engine.salary.entity.taxapiflow.po.TaxDeclarationApiFlowWarnConfigPO; //import com.engine.salary.mapper.taxdeclaration.TaxDeclarationApiFlowWarnConfigMapper; //import com.engine.salary.service.SalaryEmployeeService; //import com.engine.salary.service.TaxDeclarationApiFlowWarnService; //import com.weaver.common.base.entity.result.WeaResult; //import com.weaver.common.distribution.genid.IdGenerator; //import com.weaver.framework.spring.annotation.AopClass; //import com.weaver.hrm.salary.common.OptionDTO; //import com.weaver.hrm.salary.dao.TaxDeclarationApiFlowWarnReceiverMapper; //import com.weaver.hrm.salary.entity.taxagent.po.TaxAgentEmployeePO; //import com.weaver.hrm.salary.entity.taxapiflow.bo.TaxApiFlowBO; //import com.weaver.hrm.salary.entity.taxapiflow.dto.TaxDeclarationApiFlowWarnReceiverFormDTO; //import com.weaver.hrm.salary.entity.taxapiflow.dto.TaxDeclarationApiFlowWarnReceiverListDTO; //import com.weaver.hrm.salary.entity.taxapiflow.param.CreateMessageRuleParam; //import com.weaver.hrm.salary.entity.taxapiflow.param.TaxDeclarationApiFlowWarnConfigSaveParam; //import com.weaver.hrm.salary.entity.taxapiflow.param.TaxDeclarationApiFlowWarnReceiverSaveParam; //import com.weaver.hrm.salary.entity.taxapiflow.po.TaxDeclarationApiFlowWarnReceiverPO; //import com.weaver.hrm.salary.entity.taxapiflow.response.QueryAccountBalanceResponse; //import com.weaver.hrm.salary.enums.salaryitem.SalaryOnOffEnum; //import com.weaver.hrm.salary.enums.sicategory.DeleteTypeEnum; //import com.weaver.hrm.salary.service.TaxAgentService; //import com.weaver.hrm.salary.service.TaxDeclarationApiFlowStatisticService; //import com.weaver.hrm.salary.util.SalaryAssert; //import com.weaver.hrm.salary.util.SalaryEntityUtil; //import com.weaver.hrm.salary.util.SalaryI18nUtil; //import com.weaver.mc.api.async.AsyncSystemMessageRest; //import com.weaver.mc.api.entity.*; //import com.weaver.mc.api.rest.RuleRest; //import com.weaver.teams.domain.user.SimpleEmployee; //import lombok.extern.slf4j.Slf4j; //import org.apache.commons.lang3.StringUtils; //import org.springframework.stereotype.Service; //import org.springframework.transaction.annotation.Transactional; // //import java.time.LocalDateTime; //import java.util.Collections; //import java.util.List; //import java.util.Map; //import java.util.Objects; //import java.util.stream.Collectors; // ///** // * @author chengliming // * @date 2022-11-11 2:57 PM // **/ //@Service //@Slf4j //@AopClass //public class TaxDeclarationApiFlowWarnServiceImpl implements TaxDeclarationApiFlowWarnService { // private TaxDeclarationApiFlowWarnConfigMapper taxDeclarationApiFlowWarnConfigMapper; // private TaxDeclarationApiFlowWarnReceiverMapper taxDeclarationApiFlowWarnReceiverMapper; // private SalaryEmployeeService salaryEmployeeService; // private RuleRest ruleRest; // private AsyncSystemMessageRest asyncSystemMessageRest; // private TaxAgentService taxAgentService; // private TaxDeclarationApiFlowStatisticService taxDeclarationApiFlowStatisticService; // // @Override // public TaxDeclarationApiFlowWarnConfigPO getWarnConfig(String currentTenantKey) { // return new LambdaQueryChainWrapper<>(taxDeclarationApiFlowWarnConfigMapper) // .eq(TaxDeclarationApiFlowWarnConfigPO::getDeleteType, DeleteTypeEnum.NOT_DELETED.getValue()) // .eq(TaxDeclarationApiFlowWarnConfigPO::getTenantKey, currentTenantKey) // .one(); // } // // @Override // public List getWarnReceiverList(Long warnConfigId, String currentTenantKey) { // List receiverPOList = new LambdaQueryChainWrapper<>(taxDeclarationApiFlowWarnReceiverMapper) // .eq(TaxDeclarationApiFlowWarnReceiverPO::getDeleteType, DeleteTypeEnum.NOT_DELETED.getValue()) // .eq(TaxDeclarationApiFlowWarnReceiverPO::getTenantKey, currentTenantKey) // .eq(TaxDeclarationApiFlowWarnReceiverPO::getWarnConfigId, warnConfigId) // .list(); // // List salaryEmployees = taxAgentService.listEmployees(currentTenantKey); // Map 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, String currentTenantKey) { // TaxDeclarationApiFlowWarnReceiverPO receiverPO = getWarnReceiverPOById(id, currentTenantKey); // SalaryAssert.notNull(receiverPO, "该提醒对象不存在,请刷新页面后重试"); // new LambdaUpdateChainWrapper<>(taxDeclarationApiFlowWarnReceiverMapper) // .set(TaxDeclarationApiFlowWarnReceiverPO::getDeleteType, DeleteTypeEnum.DELETED.getValue()) // .eq(TaxDeclarationApiFlowWarnReceiverPO::getId, id) // .eq(TaxDeclarationApiFlowWarnReceiverPO::getTenantKey, currentTenantKey) // .eq(TaxDeclarationApiFlowWarnReceiverPO::getDeleteType, DeleteTypeEnum.NOT_DELETED.getValue()) // .update(); // } // // @Override // public TaxDeclarationApiFlowWarnReceiverFormDTO getWarnReceiverFormById(Long id, Long currentEmployeeId, String currentTenantKey) { // TaxDeclarationApiFlowWarnReceiverPO receiverPO = getWarnReceiverPOById(id, currentTenantKey); // SalaryAssert.notNull(receiverPO, "该提醒对象不存在,请刷新页面后重试"); // // 查找人员信息 // List simpleEmployees = salaryEmployeeService.listAll(currentTenantKey); // Map empMap = SalaryEntityUtil.convert2Map(simpleEmployees, SimpleEmployee::getEmployeeId); // SimpleEmployee simpleEmployee = empMap.get(receiverPO.getEmployeeId()); // SalaryAssert.notNull(simpleEmployee, SalaryI18nUtil.getI18nLabel(currentTenantKey, currentEmployeeId, 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, String currentTenantKey) { // return new LambdaQueryChainWrapper<>(taxDeclarationApiFlowWarnReceiverMapper) // .eq(TaxDeclarationApiFlowWarnReceiverPO::getDeleteType, DeleteTypeEnum.NOT_DELETED.getValue()) // .eq(TaxDeclarationApiFlowWarnReceiverPO::getId, id) // .eq(TaxDeclarationApiFlowWarnReceiverPO::getTenantKey, currentTenantKey) // .one(); // } // // @Override // @Transactional(rollbackFor = Exception.class) // public void saveWarnReceiver(TaxDeclarationApiFlowWarnReceiverSaveParam param, Long currentEmployeeId, String currentTenantKey) { // if (param.getId() != null) { // TaxDeclarationApiFlowWarnReceiverPO receiverPO = getWarnReceiverPOById(param.getId(), currentTenantKey); // SalaryAssert.notNull(receiverPO, "该提醒对象不存在,请刷新页面后重试"); // if (param.getEmployeeId() != null && !receiverPO.getEmployeeId().equals(param.getEmployeeId())) { // filterByEmpId(param, currentTenantKey); // receiverPO.setEmployeeId(param.getEmployeeId()); // } // receiverPO.setUpdateTime(LocalDateTime.now()); // receiverPO.setEmail(param.getEmail()); // receiverPO.setMobile(param.getMobile()); // taxDeclarationApiFlowWarnReceiverMapper.updateById(receiverPO); // } else { // filterByEmpId(param, currentTenantKey); // TaxDeclarationApiFlowWarnReceiverPO receiverPO = TaxDeclarationApiFlowWarnReceiverPO.builder() // .id(IdGenerator.generate()) // .email(param.getEmail()) // .mobile(param.getMobile()) // .employeeId(param.getEmployeeId()) // .warnConfigId(param.getWarnConfigId()) // .createTime(LocalDateTime.now()) // .updateTime(LocalDateTime.now()) // .deleteType(DeleteTypeEnum.NOT_DELETED.getValue()) // .tenantKey(currentTenantKey) // .creator(currentEmployeeId) // .build(); // taxDeclarationApiFlowWarnReceiverMapper.insert(receiverPO); // } // } // // private void filterByEmpId(TaxDeclarationApiFlowWarnReceiverSaveParam param, String currentTenantKey) { // List another = new LambdaQueryChainWrapper<>(taxDeclarationApiFlowWarnReceiverMapper) // .eq(TaxDeclarationApiFlowWarnReceiverPO::getDeleteType, DeleteTypeEnum.NOT_DELETED.getValue()) // .eq(TaxDeclarationApiFlowWarnReceiverPO::getWarnConfigId, param.getWarnConfigId()) // .eq(TaxDeclarationApiFlowWarnReceiverPO::getEmployeeId, param.getEmployeeId()) // .eq(TaxDeclarationApiFlowWarnReceiverPO::getTenantKey, currentTenantKey) // .list(); // SalaryAssert.isEmpty(another, SalaryI18nUtil.getI18nLabel(184007, "当前人员对应的提醒对象已存在,请更换人员后尝试")); // } // // @Override // public void createMessageRule(CreateMessageRuleParam param, Long currentEmployeeId, String currentTenantKey) { // 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 // @Transactional(rollbackFor = Exception.class) // public String saveWarnConfig(TaxDeclarationApiFlowWarnConfigSaveParam param, Long currentEmployeeId, String currentTenantKey) { // TaxDeclarationApiFlowWarnConfigPO warnConfigPO; // if (param.getId() != null) { // warnConfigPO = taxDeclarationApiFlowWarnConfigMapper.selectById(param.getId()); // SalaryAssert.notNull(warnConfigPO, "提醒规则不存在,请先保存"); // warnConfigPO.setThreshold(param.getThreshold()); // warnConfigPO.setEnableWarn(param.getEnable() ? 1 : 0); // warnConfigPO.setUpdateTime(LocalDateTime.now()); // taxDeclarationApiFlowWarnConfigMapper.updateById(warnConfigPO); // } else { // warnConfigPO = TaxDeclarationApiFlowWarnConfigPO.builder() // .id(IdGenerator.generate()) // .businessId(param.getBusinessId()) // .threshold(param.getThreshold()) // .enableWarn(param.getEnable() ? 1 : 0) // .createTime(LocalDateTime.now()) // .updateTime(LocalDateTime.now()) // .creator(currentEmployeeId) // .deleteType(DeleteTypeEnum.NOT_DELETED.getValue()) // .tenantKey(currentTenantKey) // .build(); // taxDeclarationApiFlowWarnConfigMapper.insert(warnConfigPO); // } // if (StringUtils.isNotEmpty(param.getConfig())) { // CreateRuleEntity createRule = new CreateRuleEntity() // .setName(SalaryI18nUtil.getI18nLabel(currentTenantKey, currentEmployeeId, 159146, "流量不足提醒")) // .setUser(new UserEntity(currentEmployeeId, currentTenantKey)) // .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(String currentTenantKey) { // TaxDeclarationApiFlowWarnConfigPO warnConfig = this.getWarnConfig(currentTenantKey); // return warnConfig == null ? IdGenerator.generate() : warnConfig.getBusinessId(); // } // // @Override // public void sendFlowWarnMessage(TaxDeclarationApiBillingServiceImpl.ApiFlowUpdateWrapper updateWrapper) { // TaxDeclarationApiFlowWarnConfigPO warnConfig = getWarnConfig(updateWrapper.getTenantKey()); // // 未配置或开关关闭 // if (Objects.isNull(warnConfig) || SalaryOnOffEnum.OFF.getValue().equals(warnConfig.getEnableWarn())) { // return; // } // QueryAccountBalanceResponse response = taxDeclarationApiFlowStatisticService.getQueryAccountBalanceResponse(updateWrapper.getApiConfig()); // // 剩余流量大于阈值无需提醒 // if (StringUtils.isNotEmpty(response.getBody().getSurplus()) && Long.parseLong(response.getBody().getSurplus()) > warnConfig.getThreshold()) { // return; // } // List warnReceiverList = getWarnReceiverList(warnConfig.getId(), updateWrapper.getTenantKey()); // if (warnReceiverList.isEmpty()) { // log.info("warnReceiverList is empty, send msg fail"); // return; // } // List 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 weaResult = asyncSystemMessageRest.sendMsg(smg); // SalaryAssert.isTrue(weaResult.isStatus(), SalaryI18nUtil.getI18nLabel(184009, "流量不足提醒发送失败")); // } //}