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.DistrictUtil;
import com.fdkankan.manage.common.PageInfo;
import com.fdkankan.manage.common.ResultCode;
import com.fdkankan.manage.entity.DistrictCode;
import com.fdkankan.manage.entity.JyPlatform;
import com.fdkankan.manage.entity.JyUser;
import com.fdkankan.manage.entity.SysUser;
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;
@Autowired
IDistrictCodeService districtCodeService;
@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());
}
if(param.getStatus() !=null){
wrapper.eq(JyPlatform::getStatus,param.getStatus());
}
wrapper.orderByDesc(JyPlatform::getId);
Page page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
return PageInfo.PageInfo(page);
}
@Override
public synchronized 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);
}
jyUser = jyUserService.getByIdCard(param.getIdCard());
if(jyUser != null && jyUser.getPlatformId() != null ){
throw new BusinessException(ResultCode.ID_CARD_EXIT2);
}
if(jyUser != null && jyUser.getStatus() !=1){
throw new BusinessException(ResultCode.USER_BAN);
}
jyPlatform = new JyPlatform();
BeanUtils.copyProperties(param,jyPlatform);
this.save(jyPlatform);
}else {
jyPlatform = this.getById(param.getId());
if(jyPlatform == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
JyPlatform jyPlatform2 = this.getByIdCard(param.getIdCard());
if(jyPlatform2 != null && !jyPlatform2.getId().equals(jyPlatform.getId())){
throw new BusinessException(ResultCode.ID_CARD_EXIT);
}
jyUser = jyUserService.getByIdCard(param.getIdCard());
if(jyUser != null && jyUser.getPlatformId() != null && !jyUser.getPlatformId().equals(param.getId())){
throw new BusinessException(ResultCode.ID_CARD_EXIT2);
}
if(jyUser != null && jyUser.getStatus() !=1){
throw new BusinessException(ResultCode.USER_BAN);
}
if(!param.getIdCard().equals(jyPlatform.getIdCard())){
JyUser jyUser1 = jyUserService.getByIdCard(jyPlatform.getIdCard());
if(jyUser1 != null && jyUser1.getStatus() !=1){
throw new BusinessException(ResultCode.USER_BAN);
}
if(jyUser1 != null){
sysUserService.updateRoleId(jyUser1.getSysUserId(),47L);
}
}
param.setPlatformAddress(jyPlatform.getPlatformAddress());
BeanUtils.copyProperties(param,jyPlatform);
this.updateById(jyPlatform);
}
if (jyUser !=null){
jyUserService.updatePlatformId(jyUser.getId(),jyPlatform.getId());
sysUserService.updateRoleId(jyUser.getSysUserId(),48L);
}
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.getByIdCard(jyPlatform.getIdCard());
if(jyUser != null){
sysUserService.updateRoleId(jyUser.getSysUserId(),48L);
}
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);
}
JyUser jyUser = jyUserService.getByIdCard(jyPlatform.getIdCard());
//update userRole
if(jyUser != null){
sysUserService.updateRoleId(jyUser.getSysUserId(),47L);
jyUserService.updatePlatformIdByPlatformId(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());
}
@Override
public List getNotBanList() {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(JyPlatform::getStatus,0);
return this.list(wrapper);
}
@Override
public JyPlatform getByDistrictCode(String districtCode,String phone ,String idCard) {
DistrictCode district = getPlatFormDistrictCode(districtCode);
if(district != null){
JyPlatform jyPlatform = getPlatformByCode(district.getCode());
if(jyPlatform != null){
return jyPlatform;
}
String platflormName = DistrictUtil.getPureCityName(district.getName()) +"公安";
jyPlatform = this.getPlatformName(platflormName);
if(jyPlatform!=null){
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(JyPlatform::getId,jyPlatform.getId());
wrapper.set(JyPlatform::getDistrictCode,district.getCode());
wrapper.set(JyPlatform::getDistrictName,district.getName());
this.update(wrapper);
return jyPlatform;
}
String uuid = UUID.randomUUID().toString().substring(0, 18).replace("-", "");
jyPlatform = new JyPlatform();
jyPlatform.setPlatformName(platflormName);
jyPlatform.setPlatformAddress(uuid);
jyPlatform.setName(platflormName);
jyPlatform.setIdCard(idCard);
jyPlatform.setDistrictCode(district.getCode());
jyPlatform.setDistrictName(district.getName());
jyPlatform.setIsNew(true);
this.save(jyPlatform);
return jyPlatform;
}
return null;
}
private JyPlatform getPlatformName(String platflormName) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(JyPlatform::getPlatformName,platflormName);
List list = this.list(wrapper);
if(list == null || list.isEmpty()){
return null;
}
return list.get(0);
}
private JyPlatform getPlatformByCode(String code){
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(JyPlatform::getDistrictCode,code);
List list = this.list(wrapper);
if(list == null || list.isEmpty()){
return null;
}
return list.get(0);
}
private DistrictCode getPlatFormDistrictCode(String districtCode){
DistrictCode byCode = districtCodeService.getByCode(districtCode);
if(byCode == null || StringUtils.isBlank(byCode.getParentCode())){
return null;
}
if(DistrictUtil.isCityLevel(districtCode)){
return byCode;
}
return getPlatFormDistrictCode(byCode.getParentCode());
}
}