package com.ycl.service.message.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.common.util.CommonUtils;
import com.ycl.dto.message.MessageSmsParam;
import com.ycl.entity.message.Message;
import com.ycl.entity.message.SmsAudit;
import com.ycl.entity.user.UmsDepartManage;
import com.ycl.exception.ApiException;
import com.ycl.mapper.message.MessageMapper;
import com.ycl.mapper.message.SmsAuditMapper;
import com.ycl.mapper.user.UmsDepartManageMapper;
import com.ycl.service.message.ISmsAuditService;
import com.ycl.service.user.UmsAdminService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 短信审核 服务实现类
*
*
* @author mg
* @since 2022-10-17
*/
@Service
public class SmsAuditServiceImpl extends ServiceImpl implements ISmsAuditService {
@Resource
private UmsDepartManageMapper umsDepartManageMapper;
@Resource
private MessageMapper messageMapper;
@Resource
private UmsAdminService adminService;
@Override
public IPage list(SmsAudit smsAudit) {
Page page = new Page<>(smsAudit.getCurrent(), smsAudit.getPageSize());
/*LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
if (smsAudit.getAuditId() != null) {
queryWrapper.eq(SmsAudit::getAuditId, smsAudit.getAuditId());
}
if (smsAudit.getAuditStatus() != null) {
queryWrapper.eq(SmsAudit::getAuditStatus, smsAudit.getAuditStatus());
}
queryWrapper.orderByDesc(SmsAudit::getCreateTime);
Page result = baseMapper.selectPage(page, queryWrapper);*/
Page result = baseMapper.selectCondPage(page, smsAudit);
return result;
}
@Override
public void startAuditSms(Message message) {
String[] arr = message.getTargetTo().split(",");
List ids = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
ids.add(Long.parseLong(arr[i]));
}
//查询部门
LambdaQueryWrapper queryDeptWrapper = new LambdaQueryWrapper<>();
queryDeptWrapper.in(UmsDepartManage::getUserId, ids);
List depts = umsDepartManageMapper.selectList(queryDeptWrapper);
//部门去重
List uniques = depts.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getDepartId()))), ArrayList::new));
List deptLeadIds = uniques.stream().map(UmsDepartManage::getDepartId).collect(Collectors.toList());
LambdaQueryWrapper queryDeptLeadWrapper = new LambdaQueryWrapper<>();
queryDeptLeadWrapper.in(UmsDepartManage::getDepartId, deptLeadIds).eq(UmsDepartManage::getIsLeader, 1);
List deptLeads = umsDepartManageMapper.selectList(queryDeptLeadWrapper);
//审核去重
List uniqueLeads = deptLeads.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getUserId()))), ArrayList::new));
//添加短信审核
List audits = new ArrayList<>();
for (UmsDepartManage depart : uniqueLeads) {
SmsAudit audit = new SmsAudit();
audit.setMessageId(message.getId());
audit.setAuditId(depart.getUserId());
//待审核状态
audit.setAuditStatus(0);
Long userId = Long.parseLong(message.getTargetFrom());
audit.setCreateUser(userId);
audit.setCreateTime(new Date());
audit.setUpdateUser(userId);
audit.setUpdateTime(new Date());
audits.add(audit);
}
saveBatch(audits);
}
@Override
public Boolean checkSms(MessageSmsParam messageSmsParam) {
switch (messageSmsParam.getStatus()) {
//未通过
case 0:
noPassMethod(messageSmsParam);
break;
//通过
case 1:
passMethod(messageSmsParam);
break;
default:
throw new ApiException("未匹配到该类型");
}
return true;
}
@Transactional
public void passMethod(MessageSmsParam messageSmsParam) {
Message message = messageMapper.selectById(messageSmsParam.getMessageId());
if (CommonUtils.isEmpty(message)) {
throw new ApiException("未找到该审核短信消息");
}
//id换成手机号
String targetToPhone = adminService.getTargetTo(message.getTargetTo(), message.getChannelCode());
message.setTargetTo(targetToPhone);
Message updateMessage = new Message();
updateMessage.setSendTime(new Date());
updateMessage.setIsScan(1);
updateMessage.setUpdateTime(new Date());
updateMessage.setUpdateUser(messageSmsParam.getUpdateUser());
LambdaQueryWrapper updateWrapper = new LambdaQueryWrapper<>();
updateWrapper.eq(Message::getMessageNumber, message.getMessageNumber());
messageMapper.update(updateMessage, updateWrapper);
//审核短信更新
updateSmsAudit(messageSmsParam);
}
@Transactional
public void noPassMethod(MessageSmsParam messageSmsParam) {
//审核短信更新
updateSmsAudit(messageSmsParam);
}
public void updateSmsAudit(MessageSmsParam messageSmsParam) {
SmsAudit smsAudit = packageData(messageSmsParam);
LambdaQueryWrapper smsAuditUpdate = new LambdaQueryWrapper<>();
smsAuditUpdate.eq(SmsAudit::getMessageId, messageSmsParam.getMessageId());
baseMapper.update(smsAudit, smsAuditUpdate);
}
public SmsAudit packageData(MessageSmsParam messageSmsParam) {
SmsAudit smsAudit = new SmsAudit();
smsAudit.setMessageId(messageSmsParam.getMessageId());
smsAudit.setAuditStatus(messageSmsParam.getStatus());
smsAudit.setAuditOpinion(messageSmsParam.getAuditOpinion());
smsAudit.setAuditResult(messageSmsParam.getResult());
smsAudit.setRealAuditId(messageSmsParam.getUpdateUser());
smsAudit.setUpdateTime(new Date());
smsAudit.setUpdateUser(messageSmsParam.getUpdateUser());
return smsAudit;
}
}