package com.fdkankan.manage.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.manage.common.PageInfo;
import com.fdkankan.manage.common.ResultCode;
import com.fdkankan.manage.entity.JyPlatform;
import com.fdkankan.manage.entity.JyUser;
import com.fdkankan.manage.exception.BusinessException;
import com.fdkankan.manage.mapper.IJyPlatformMapper;
import com.fdkankan.manage.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.manage.vo.request.JyPlatformParam;
import com.fdkankan.manage.vo.request.JyPlatformVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author
* @since 2024-11-14
*/
@Service
public class JyPlatformServiceImpl extends ServiceImpl implements IJyPlatformService {
@Autowired
IJyUserPlatformService jyUserPlatformService;
@Autowired
ISysUserService sysUserService;
@Autowired
IJyUserService jyUserService;
@Override
public Object pageList(JyPlatformParam param) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
if(StringUtils.isNotBlank(param.getPlatformName())){
wrapper.like(JyPlatform::getPlatformName,param.getPlatformName());
}
if(StringUtils.isNotBlank(param.getName())){
wrapper.like(JyPlatform::getName,param.getName());
}
if(StringUtils.isNotBlank(param.getPhone())){
wrapper.like(JyPlatform::getPhone,param.getPhone());
}
wrapper.orderByDesc(JyPlatform::getId);
Page page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
return PageInfo.PageInfo(page);
}
@Override
public JyPlatform addOrUpdate(JyPlatformVo param) {
if(StringUtils.isBlank(param.getPlatformName()) || StringUtils.isBlank(param.getName())
|| StringUtils.isBlank(param.getIdCard())){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
JyPlatform jyPlatform = null;
JyUser jyUser = null;
if(param.getId() == null){ //创建平台自动生成平台访问地址
String uuid = UUID.randomUUID().toString().substring(0, 18).replace("-", "");
param.setPlatformAddress(uuid);
jyPlatform = this.getByIdCard(param.getIdCard());
if(jyPlatform != null){
throw new BusinessException(ResultCode.ID_CARD_EXIT);
}
jyPlatform = new JyPlatform();
jyUser = jyUserService.getByIdCard(param.getIdCard());
if(jyUser != null && (jyUser.getPlatformId() != null || jyUser.getIsJm() == 0)){
throw new BusinessException(ResultCode.ID_CARD_EXIT);
}
}else {
jyPlatform = this.getById(param.getId());
if(jyPlatform == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
param.setPlatformAddress(jyPlatform.getPlatformAddress());
JyPlatform jyPlatform2 = this.getByIdCard(param.getIdCard());
if(jyPlatform2 != null && !jyPlatform2.getId().equals(jyPlatform.getId())){
throw new BusinessException(ResultCode.ID_CARD_EXIT);
}
}
BeanUtils.copyProperties(param,jyPlatform);
this.saveOrUpdate(jyPlatform);
if (jyUser !=null){
jyUserService.updatePlatformId(jyUser.getId(),jyPlatform.getId());
}
return jyPlatform;
}
@Override
public JyPlatform getByIdCard(String idCard) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(JyPlatform::getIdCard,idCard);
return this.getOne(wrapper);
}
@Override
public void del(JyPlatform param) {
if(param.getId() == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
this.removeById(param.getId());
}
@Override
public Object getByAddress(String address) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(JyPlatform::getPlatformAddress,address);
JyPlatform one = this.getOne(wrapper);
return one;
}
@Override
public void enable(JyPlatformVo param) {
if(param.getId() == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
JyPlatform jyPlatform = this.getById(param.getId());
if(jyPlatform == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
//update userRole
JyUser jyUser = jyUserService.getById(param.getJyUserId());
if(jyUser != null){
sysUserService.updateRoleId(jyUser.getSysUserId(),48);
}
this.updateStatus(jyPlatform.getId(),0);
}
private void updateStatus(Integer id, Integer status) {
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(JyPlatform::getId,id);
wrapper.set(JyPlatform::getStatus,status);
this.update(wrapper);
}
@Override
public void disable(JyPlatformVo param) {
if(param.getId() == null|| param.getToPlatformId() == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
if(param.getId() == 1){
throw new BusinessException(ResultCode.ADMIN_NOT_DISABLE);
}
JyPlatform jyPlatform = this.getById(param.getId());
if(jyPlatform == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
//update userRole
JyUser jyUser = jyUserService.getById(param.getJyUserId());
if(jyUser != null){
sysUserService.updateRoleId(jyUser.getSysUserId(),47);
}
jyUserService.updatePlatformId(param.getId(),param.getToPlatformId());
this.updateStatus(jyPlatform.getId(),1);
}
@Override
public List getIds() {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(JyPlatform::getStatus,0);
List list = this.list();
if(list.isEmpty()){
return new ArrayList<>();
}
return list.stream().map(JyPlatform::getIdCard).collect(Collectors.toList());
}
}