package com.fdkankan.manage_jp.service.impl;
import com.alibaba.fastjson.JSONObject;
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.common.util.DateUtil;
import com.fdkankan.common.util.SecurityUtil;
import com.fdkankan.manage_jp.common.PageInfo;
import com.fdkankan.manage_jp.common.ResultCode;
import com.fdkankan.manage_jp.entity.*;
import com.fdkankan.manage_jp.exception.BusinessException;
import com.fdkankan.manage_jp.mapper.ICompanyMapper;
import com.fdkankan.manage_jp.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.manage_jp.vo.request.RequestCompany;
import com.fdkankan.manage_jp.vo.response.ResponseCompany;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* 客户企业logo信息 服务实现类
*
*
* @author
* @since 2022-12-23
*/
@Service
@Slf4j
public class CompanyServiceImpl extends ServiceImpl implements ICompanyService {
@Autowired
IUserService userService;
@Autowired
IUserRoleService userRoleService;
@Autowired
ICameraService cameraService;
@Autowired
ICameraDetailService cameraDetailService;
@Autowired
ISceneProService sceneProService;
@Autowired
IMailTemplateService mailTemplateService;
@Override
public void insertOrUpdate(RequestCompany param) {
Company entity = new Company();
if(param.getId() != null){
entity = this.getById(param.getId());
BeanUtils.copyProperties(param, entity);
entity.setUpdateTime(null);
this.updateById(entity);
return;
}
// 判断邮箱是否注册过
User userEntity = userService.getByUserName(param.getManagerPhone());
if(!ObjectUtils.isEmpty(userEntity)){
throw new BusinessException(ResultCode.USER_EXIST);
}
BeanUtils.copyProperties(param, entity);
entity.setState(1);
entity.setAuditTime(new Date());
this.save(entity);
userEntity = new User();
userEntity.setPassword(SecurityUtil.MD5("Geosign123"));
userEntity.setUserName(param.getManagerPhone());
userEntity.setCompanyId(entity.getId());
userEntity.setNickName(param.getManagerName());
userEntity.setCreateTime(DateUtil.date2String(new Date(),"yyyy-MM-dd HH:mm:ss"));
userEntity.setUpdateTime(new Date());
userEntity.setRegisterTime(DateUtil.date2String(new Date(),"yyyy-MM-dd HH:mm:ss"));
userEntity.setRecStatus("A");
userService.save(userEntity);
UserRole userRoleEntity = new UserRole();
userRoleEntity.setUserId(userEntity.getId());
userRoleEntity.setRoleId(6L);
userRoleService.save(userRoleEntity);
entity.setManagerId(userEntity.getId());
entity.setUpdateTime(null);
this.updateById(entity);
}
@Override
public void auditCompany(Company bo) {
if(bo == null || bo.getId() == null){
throw new BusinessException(ResultCode.PARAM_ERROR);
}
Company companyEntity = this.getById(bo.getId());
if(ObjectUtils.isEmpty(companyEntity)){
throw new BusinessException(ResultCode.PARAM_ERROR);
}
bo.setAuditTime(new Date());
String expirationDate = DateUtil.date2String(new Date(), DateUtil.YYYY_MM_DD_DATE_FORMAT);
bo.setExpirationTime(DateUtil.string2Date(expirationDate + " 23:59:59", null));
bo.setUpdateTime(null);
bo.setCreateTime(null);
this.updateById(bo);
// 等于审核通过
if(bo.getState() == 1){
// 发送企业认证通过邮件
User userEntity = userService.getById(companyEntity.getManagerId());
if(!ObjectUtils.isEmpty(userEntity)){
log.info("发送审核通过邮件:{}",userEntity.getUserName());
MailTemplate mailTemplate = mailTemplateService.getById(1);
Boolean mail = mailTemplateService.sendMail(userEntity.getUserName(), mailTemplate,null);
if(!mail){
throw new BusinessException(ResultCode.MAIL_SEND_ERROR);
}
}
}
}
@Override
public void addPoint(Long id, Integer point) {
Company company = getById(id);
if(company == null){
throw new BusinessException(ResultCode.NOT_RECORD);
}
point = company.getPoint() + point;
if (point < 0) {
throw new BusinessException(ResultCode.POINT_GT_ZERO);
}
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(Company::getId,id);
wrapper.set(Company::getPoint,point);
this.update(wrapper);
}
@Override
public void checkDevice(String childName) {
Camera instance = cameraService.findByChildName(childName);
if(ObjectUtils.isEmpty(instance)){
throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
}
}
@Override
public PageInfo companyList(RequestCompany param) {
MPJLambdaWrapper wrapper = new MPJLambdaWrapper()
.selectAll(Company.class)
.selectAll(User.class)
.leftJoin(User.class,User::getId,Company::getManagerId);
if(!StringUtils.isEmpty(param.getManagerName())){
wrapper.like(User::getUserName,param.getManagerName());
}
wrapper.eq(Company :: getState ,1);
wrapper.orderByDesc(Company::getCreateTime);
Page page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
List list = page.getRecords().stream().map(entity->{
ResponseCompany company = new ResponseCompany();
BeanUtils.copyProperties(entity,company);
if(entity.getManagerId() != null){
User userEntity = userService.getById(entity.getManagerId());
if(userEntity != null){
company.setManagerName(userEntity.getUserName());
company.setManagerPhone(userEntity.getUserName());
}
}
company.setSceneNum(0);
List users = userService.findByCompanyId(company.getId());
company.setTotalSubNum(company.getSubNum());
// 去除企业账号
company.setSubNum(users.size());
if (users.size() > 0) {
company.setSubNum(users.size() - 1);
}
List userIds = users.stream().map(User::getId).collect(Collectors.toList());
if(!CollectionUtils.isEmpty(userIds)){
Long sceneNum = sceneProService.getCountByUserIds(userIds);
company.setSceneNum(Math.toIntExact(sceneNum));
}
int cameraNum = cameraDetailService.findCountByCompanyId(company.getId());
company.setCameraNum(cameraNum);
return company;
}).collect(Collectors.toList());
Page pageVo = new Page<>(param.getPageNum(),param.getPageSize());
pageVo.setRecords(list);
pageVo.setTotal(page.getTotal());
return PageInfo.PageInfo(pageVo);
}
@Override
public PageInfo selectCompanyByType(RequestCompany param) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
if(!StringUtils.isEmpty(param.getCompanyName())){
wrapper.eq(Company::getCompanyName,param.getCompanyName());
}
if(!StringUtils.isEmpty(param.getState())){
wrapper.eq(Company::getState,param.getState());
}
if(!ObjectUtils.isEmpty(param.getStartTime())){
String startTime = param.getStartTime().split(" ")[0]+" 00:00:00";
wrapper.ge(Company::getCreateTime,startTime);
}
if(!ObjectUtils.isEmpty(param.getUserName())){
List nameList = userService.findByUserNameList(param.getUserName());
if(CollectionUtils.isEmpty(nameList)){
return PageInfo.PageInfoEmpty(param.getPageNum(),param.getPageSize());
}
List companyIds = nameList.stream().map(User::getCompanyId).collect(Collectors.toList());
wrapper.in(Company::getId,companyIds);
}
if(!ObjectUtils.isEmpty(param.getEndTime())){
String endTime = param.getEndTime().split(" ")[0]+" 23:59:59";
wrapper.le(Company::getCreateTime,endTime);
}
wrapper.orderByDesc(Company::getId);
Page page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
List list = page.getRecords().stream().map(entity->{
ResponseCompany company = new ResponseCompany();
BeanUtils.copyProperties(entity,company);
if(entity.getManagerId() != null){
User userEntity = userService.getById(entity.getManagerId());
if(userEntity != null){
company.setManagerName(userEntity.getUserName());
company.setManagerPhone(userEntity.getUserName());
}
}
company.setSceneNum(0);
return company;
}).collect(Collectors.toList());
Page pageVo = new Page<>(param.getPageNum(),param.getPageSize());
pageVo.setRecords(list);
pageVo.setTotal(page.getTotal());
return PageInfo.PageInfo(pageVo);
}
@Override
public void saveSubUsers(RequestCompany bo) {
Company tbCompany = getById(bo.getId());
if(ObjectUtils.isEmpty(tbCompany)){
throw new BusinessException("企业数据查询失败");
}
// 更新子账号数量
tbCompany.setSubNum(bo.getSubNum());
tbCompany.setUpdateTime(null);
this.updateById(tbCompany);
Date currentDate = new Date();
List oldUsers = userService.findByCompanyId(tbCompany.getId());
oldUsers = oldUsers.stream().filter(param -> param.getId() - tbCompany.getManagerId() != 0).collect(Collectors.toList());
// 获取编辑的子账号
oldUsers.stream().forEach(user -> {
bo.getSubUsers().stream().filter(param -> user.getId().equals(param.getId()))
.filter(param -> !org.apache.commons.lang3.StringUtils.equals(param.getNickName(), user.getNickName()))
.forEach(upUser -> {
user.setNickName(upUser.getNickName());
user.setUpdateTime(currentDate);
userService.updateById(user);
}
);
});
// 获取解绑的子账号
oldUsers.stream().forEach(user -> {
if (bo.getSubUsers().stream().noneMatch(param -> user.getId().equals(param.getId()))) {
// 将场景重新绑定到企业账号下面
sceneProService.rebindUser(user.getId(),tbCompany.getManagerId());
cameraDetailService.unbindUser(user.getId(),null);
userService.removeById(user.getId());
}
});
// 获取新增的子账号
bo.getSubUsers().stream().filter(user -> ObjectUtils.isEmpty(user.getId())).forEach(user -> {
// 获取用户是否存在
User userEntity = userService.findByUserName(user.getUserName());
if (!ObjectUtils.isEmpty(userEntity)) {
if (!ObjectUtils.isEmpty(userEntity.getCompanyId())) {
throw new BusinessException(user.getUserName() + "已经被绑定,请确认!");
}
}else{
userEntity = new User();
userEntity.setRegisterTime(DateUtil.date2String(new Date(),"yyyy-MM-dd HH:mm:ss"));
}
userEntity.setPassword(SecurityUtil.MD5("Geosign123"));
userEntity.setUserName(user.getUserName());
userEntity.setCompanyId(tbCompany.getId());
userEntity.setNickName(user.getNickName());
userEntity.setCreateTime(DateUtil.date2String(currentDate,"yyyy-MM-dd HH:mm:ss"));
userEntity.setUpdateTime(currentDate);
userEntity.setRecStatus("A");
if (ObjectUtils.isEmpty(userEntity.getId())) {
userService.save(userEntity);
} else {
userService.updateById(userEntity);
}
// 保存用户员工角色
UserRole userRole = new UserRole();
userRole.setUserId(userEntity.getId());
userRole.setRoleId(8L);
userRoleService.save(userRole);
});
}
@Override
public Company getByUserName(String userName) {
User user = userService.getByUserName(userName);
if(user != null && user.getCompanyId() != null){
return this.getById(user.getCompanyId());
}
return null;
}
@Override
public Company getByCameraId(Long cameraId) {
CameraDetail cameraDetail = cameraDetailService.getByCameraId(cameraId);
if(cameraDetail == null || cameraDetail.getCameraId() == null){
return null;
}
return this.getById(cameraDetail.getCompanyId());
}
@Override
public Company getByChildName(String childName) {
CameraDetail cameraDetail = cameraDetailService.getByChildName(childName);
if(cameraDetail == null || cameraDetail.getCameraId() == null){
return null;
}
return this.getById(cameraDetail.getCompanyId());
}
@Override
public List getLikeCompanyName(String companyName) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.like(Company::getCompanyName,companyName);
return this.list(wrapper);
}
}